Skip to content

Commit

Permalink
flamegraph: improvements to logging and type safety (#28)
Browse files Browse the repository at this point in the history
* use optional chaining
* do not log execution interval to build Promise
* add log message on response to client

Signed-off-by: inge4pres <[email protected]>
  • Loading branch information
inge4pres authored Mar 3, 2022
1 parent 37edd80 commit 36f4c8e
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions src/plugins/profiling/server/routes/search_flamechart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ async function queryFlameGraph(
}
);

const totalCount: number = resEvents.body.aggregations.total_count.value;
const totalCount: number = resEvents.body.aggregations?.total_count.value;
const stackTraceEvents = new Map<StackTraceID, number>();

let docCount = 0;
let bucketCount = 0;
resEvents.body.aggregations.group_by.buckets.forEach((item: any) => {
resEvents.body.aggregations?.group_by.buckets.forEach((item: any) => {
const traceid: StackTraceID = item.key.traceid;
stackTraceEvents.set(traceid, item.sum_count.value);
docCount += item.doc_count;
Expand Down Expand Up @@ -186,7 +186,7 @@ async function queryFlameGraph(
// Also, ES doesn't know about transactions.

// Create a lookup map StackTraceID -> StackTrace.
let stackTraces = new Map<StackTraceID, StackTrace>();
const stackTraces = new Map<StackTraceID, StackTrace>();
for (const trace of resStackTraces.body.docs) {
if (trace.found) {
stackTraces.set(trace._id, {
Expand Down Expand Up @@ -267,20 +267,18 @@ async function queryFlameGraph(
}
}

return logExecutionLatency<any>(logger, 'building Flamegraph data', () => {
return new Promise((resolve, _) => {
return resolve(
new FlameGraph(
eventsIndex.sampleRate,
totalCount,
stackTraceEvents,
stackTraces,
stackFrames,
executables,
logger
)
);
});
return new Promise<FlameGraph>((resolve, _) => {
return resolve(
new FlameGraph(
eventsIndex.sampleRate,
totalCount,
stackTraceEvents,
stackTraces,
stackFrames,
executables,
logger
)
);
});
}

Expand All @@ -298,7 +296,7 @@ export function registerFlameChartElasticSearchRoute(
projectID: schema.maybe(schema.string()),
timeFrom: schema.maybe(schema.string()),
timeTo: schema.maybe(schema.string()),
n: schema.maybe(schema.number()),
n: schema.maybe(schema.number({ defaultValue: 200 })),
}),
},
},
Expand All @@ -311,6 +309,7 @@ export function registerFlameChartElasticSearchRoute(
const filter = newProjectTimeQuery(projectID!, timeFrom!, timeTo!);

const flamegraph = await queryFlameGraph(logger, esClient, filter, targetSampleSize);
logger.info('returning payload response to client');

return response.ok({
body: flamegraph.toElastic(),
Expand Down

0 comments on commit 36f4c8e

Please sign in to comment.