|
16 | 16 |
|
17 | 17 | import px |
18 | 18 |
|
19 | | -def export_flame_graph(start_time: str, namespace: str, pod: str, baseline_pod: str): |
| 19 | +negate = False |
| 20 | +# TODO(ddelnano): negation requires switching the type of join from right to left |
| 21 | +# or returning a different DataFrame. This might not be possible with pxl's current |
| 22 | +# functionality, but this should be implemented once it's possible. |
| 23 | + |
| 24 | +def merge_and_compute_delta(pod1, pod2, negate: bool): |
| 25 | + |
| 26 | + diff = pod1.merge( |
| 27 | + pod2, |
| 28 | + how='right', |
| 29 | + left_on='stack_trace', |
| 30 | + right_on='stack_trace' |
| 31 | + suffixes=['_1', '_2'], |
| 32 | + ) |
| 33 | + # TODO(ddelnano): This needs to be switched with pod1 if the flamegraph should |
| 34 | + # be negated. |
| 35 | + percentage_agg = pod2.groupby(['pod']).agg( |
| 36 | + count=('count', px.sum), |
| 37 | + ) |
| 38 | + diff.pod = px.select(negate, diff.pod_1, diff.pod_2) |
| 39 | + diff.stack_trace = px.select(negate, diff.stack_trace_1, diff.stack_trace_2) |
| 40 | + diff.stack_trace = px.replace(' ', diff.stack_trace, '') |
| 41 | + diff.count = px.select(negate, diff.count_1, diff.count_2) |
| 42 | + diff.delta = diff.count_2 - diff.count_1 |
| 43 | + diff.delta = px.select(negate, px.negate(diff.delta), diff.delta) |
| 44 | + |
| 45 | + merged = diff.merge( |
| 46 | + percentage_agg, |
| 47 | + how='inner', |
| 48 | + left_on='pod', |
| 49 | + right_on='pod', |
| 50 | + suffixes=['', '_x'] |
| 51 | + ) |
| 52 | + merged.percent = 100 * merged.count / merged.count_x |
| 53 | + return merged |
| 54 | + |
| 55 | +def differential_flamegraph(start_time: str, namespace: str, pod: str, baseline_pod: str): |
20 | 56 | stack_traces = px.DataFrame(table='stack_traces.beta', start_time=start_time) |
21 | 57 | stack_traces.namespace = stack_traces.ctx['namespace'] |
22 | | - stack_traces = stack_traces[px.contains(stack_traces.namespace, namespace)] |
| 58 | + stack_traces = stack_traces[stack_traces.namespace == namespace] |
23 | 59 | stack_traces.node = px.Node(px._exec_hostname()) |
24 | 60 | stack_traces.pod = stack_traces.ctx['pod'] |
25 | | - stack_traces.keep_row = px.select(px.contains(stack_traces.pod, baseline_pod), True, False) |
26 | | - stack_traces.keep_row = px.select(stack_traces.keep_row or px.contains(stack_traces.pod, pod), True, False) |
| 61 | + stack_traces.keep_row = stack_traces.pod == baseline_pod |
| 62 | + stack_traces.keep_row = px.select(stack_traces.keep_row or stack_traces.pod == pod, True, False) |
27 | 63 | stack_traces = stack_traces[stack_traces.keep_row] |
28 | 64 |
|
29 | 65 | stack_traces = stack_traces.groupby(['node', 'namespace', 'pod', 'stack_trace_id']).agg( |
30 | 66 | stack_trace=('stack_trace', px.any), |
31 | 67 | count=('count', px.sum) |
32 | 68 | ) |
33 | 69 |
|
34 | | - pod1 = stack_traces[px.contains(stack_traces.pod, baseline_pod)] |
35 | | - pod1 = pod1.drop(['node', 'namespace', 'pod', 'stack_trace_id']) |
| 70 | + pod1 = stack_traces[stack_traces.pod == baseline_pod] |
| 71 | + pod1 = pod1.drop(['node', 'namespace', 'stack_trace_id']) |
36 | 72 |
|
37 | | - pod2 = stack_traces[px.contains(stack_traces.pod, pod)] |
38 | | - pod2 = pod2.drop(['node', 'namespace', 'pod', 'stack_trace_id']) |
| 73 | + pod2 = stack_traces[stack_traces.pod == pod] |
| 74 | + pod2 = pod2.drop(['node', 'namespace', 'stack_trace_id']) |
39 | 75 |
|
40 | | - merged = pod1.merge( |
41 | | - pod2, |
42 | | - how='right', |
43 | | - left_on='stack_trace', |
44 | | - right_on='stack_trace' |
45 | | - suffixes=['_1', '_2'], |
46 | | - ) |
47 | | - # Pixie's stack trace format permits spaces, but Brendan Gregg's scripts do not |
48 | | - merged.stack_trace_2 = px.replace(' ', merged.stack_trace_2, '') |
49 | | - merged.delta = merged.count_2 - merged.count_1 |
50 | | - return merged[['stack_trace_2', 'count_1', 'count_2', 'delta']] |
| 76 | + merged = merge_and_compute_delta(pod1, pod2, negate) |
| 77 | + return merged[['stack_trace', 'count', 'delta', 'percent']] |
0 commit comments