|
| 1 | +# Copyright 2018- The Pixie Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# SPDX-License-Identifier: Apache-2.0 |
| 16 | + |
| 17 | +import px |
| 18 | + |
| 19 | +def export_flame_graph(start_time: str, namespace: str, pod: str, baseline_pod: str): |
| 20 | + stack_traces = px.DataFrame(table='stack_traces.beta', start_time=start_time) |
| 21 | + stack_traces.namespace = stack_traces.ctx['namespace'] |
| 22 | + stack_traces = stack_traces[px.contains(stack_traces.namespace, namespace)] |
| 23 | + stack_traces.node = px.Node(px._exec_hostname()) |
| 24 | + 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) |
| 27 | + stack_traces = stack_traces[stack_traces.keep_row] |
| 28 | + |
| 29 | + stack_traces = stack_traces.groupby(['node', 'namespace', 'pod', 'stack_trace_id']).agg( |
| 30 | + stack_trace=('stack_trace', px.any), |
| 31 | + count=('count', px.sum) |
| 32 | + ) |
| 33 | + |
| 34 | + pod1 = stack_traces[px.contains(stack_traces.pod, baseline_pod)] |
| 35 | + pod1 = pod1.drop(['node', 'namespace', 'pod', 'stack_trace_id']) |
| 36 | + |
| 37 | + pod2 = stack_traces[px.contains(stack_traces.pod, pod)] |
| 38 | + pod2 = pod2.drop(['node', 'namespace', 'pod', 'stack_trace_id']) |
| 39 | + |
| 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']] |
0 commit comments