Skip to content

Commit

Permalink
Fix input for Elastic flamegraph
Browse files Browse the repository at this point in the history
This is not a complete solution, but at least the flamechart is
rendering.
  • Loading branch information
jbcrail authored and rockdaboot committed Jul 5, 2022
1 parent c796d9e commit df2a2c4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
37 changes: 37 additions & 0 deletions src/plugins/profiling/server/routes/flamegraph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export class FlameGraph {
constructor(events, totalEvents, stackTraces, stackFrames) {
this.events = events;
this.totalEvents = totalEvents;
this.stacktraces = stackTraces;
this.stackframes = stackFrames;
}

toElastic() {
let facts = [];
for (const trace of this.stacktraces) {
if (trace.found) {
const pairs = ['root'].concat(trace._source.FrameID).map((item, i) => [i, item]);
const fact = {
id: trace._source.FrameID[trace._source.FrameID.length - 1],
value: 1,
depth: trace._source.FrameID.length,
layers: Object.fromEntries(pairs),
};
facts.push(fact);
}
}
return { facts };
}

toPixi() {
return {};
}
}
15 changes: 9 additions & 6 deletions src/plugins/profiling/server/routes/search_flameChart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { schema } from '@kbn/config-schema';
import type { DataRequestHandlerContext } from '../../../data/server';
import type { IRouter } from '../../../../core/server';
import { getRemoteRoutePaths } from '../../common';
import { FlameGraph } from './flamegraph';

export function registerFlameChartSearchRoute(router: IRouter<DataRequestHandlerContext>) {
const paths = getRemoteRoutePaths();
Expand Down Expand Up @@ -150,13 +151,15 @@ export function registerFlameChartSearchRoute(router: IRouter<DataRequestHandler
body: { ids: [...stackFrameDocIDs] },
});

const flamegraph = new FlameGraph(
resEvents.body,
resTotalEvents.body,
resStackTraces.body.docs,
resStackFrames.body.docs
);

return response.ok({
body: {
events: resEvents.body,
totalEvents: resTotalEvents.body,
stackTraces: resStackTraces.body.docs,
stackFrames: resStackFrames.body.docs,
},
body: flamegraph.toElastic(),
});
} catch (e) {
return response.customError({
Expand Down

0 comments on commit df2a2c4

Please sign in to comment.