Skip to content

Commit

Permalink
[APM] Add telemetry for top traces (#166263)
Browse files Browse the repository at this point in the history
Closes #161985
  • Loading branch information
MiriamAparicio authored Sep 14, 2023
1 parent de8e1cc commit dcce011
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 1 deletion.
30 changes: 30 additions & 0 deletions x-pack/plugins/apm/common/__snapshots__/apm_telemetry.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -590,4 +590,56 @@ describe('data telemetry collection tasks', () => {
});
});
});

describe('top_traces', () => {
const task = tasks.find((t) => t.name === 'top_traces');

it('returns max and median number of documents in top traces', async () => {
const search = jest.fn().mockResolvedValueOnce({
aggregations: {
top_traces: {
buckets: [
{
key: '521485',
doc_count: 1026,
},
{
key: '594439',
doc_count: 1025,
},
{
key: '070251',
doc_count: 1023,
},
{
key: '108079',
doc_count: 1023,
},
{
key: '118887',
doc_count: 1023,
},
],
},
max: {
value: 1026,
},
median: {
values: {
'50.0': 1023,
},
},
},
});

expect(
await task?.executor({ indices, telemetryClient: { search } } as any)
).toEqual({
top_traces: {
max: 1026,
median: 1023,
},
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
SERVICE_RUNTIME_NAME,
SERVICE_RUNTIME_VERSION,
SERVICE_VERSION,
TRACE_ID,
TRANSACTION_NAME,
TRANSACTION_RESULT,
TRANSACTION_TYPE,
Expand Down Expand Up @@ -1484,4 +1485,48 @@ export const tasks: TelemetryTask[] = [
};
},
},
{
name: 'top_traces',
executor: async ({ indices, telemetryClient }) => {
const response = await telemetryClient.search({
index: [indices.transaction, indices.span, indices.error],
body: {
size: 0,
track_total_hits: false,
timeout,
query: {
bool: {
filter: [range1d],
},
},
aggs: {
top_traces: {
terms: {
field: TRACE_ID,
size: 100,
},
},
max: {
max_bucket: {
buckets_path: 'top_traces>_count',
},
},
median: {
percentiles_bucket: {
buckets_path: 'top_traces>_count',
percents: [50],
},
},
},
},
});

return {
top_traces: {
max: response.aggregations?.max.value ?? 0,
median: response.aggregations?.median.values['50.0'] ?? 0,
},
};
},
},
];
27 changes: 27 additions & 0 deletions x-pack/plugins/apm/server/lib/apm_telemetry/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,22 @@ export const apmSchema: MakeSchemaFrom<APMUsage, true> = {
},
},
per_service: { type: 'array', items: { ...apmPerServiceSchema } },
top_traces: {
max: {
type: 'long',
_meta: {
description:
'Max number of documents in top 100 traces withing the last day',
},
},
median: {
type: 'long',
_meta: {
description:
'Median number of documents in top 100 traces within the last day',
},
},
},
tasks: {
aggregated_transactions: {
took: {
Expand Down Expand Up @@ -1169,5 +1185,16 @@ export const apmSchema: MakeSchemaFrom<APMUsage, true> = {
},
},
},
top_traces: {
took: {
ms: {
type: 'long',
_meta: {
description:
'Execution time in milliseconds for the "top_traces" task',
},
},
},
},
},
};
7 changes: 6 additions & 1 deletion x-pack/plugins/apm/server/lib/apm_telemetry/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ export interface APMUsage {
total: number;
};
per_service: APMPerService[];
top_traces: {
max: number;
median: number;
};
tasks: Record<
| 'aggregated_transactions'
| 'cloud'
Expand All @@ -226,7 +230,8 @@ export interface APMUsage {
| 'cardinality'
| 'environments'
| 'service_groups'
| 'per_service',
| 'per_service'
| 'top_traces',
{ took: { ms: number } }
>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5067,6 +5067,22 @@
}
}
},
"top_traces": {
"properties": {
"max": {
"type": "long",
"_meta": {
"description": "Max number of documents in top 100 traces withing the last day"
}
},
"median": {
"type": "long",
"_meta": {
"description": "Median number of documents in top 100 traces within the last day"
}
}
}
},
"tasks": {
"properties": {
"aggregated_transactions": {
Expand Down Expand Up @@ -5278,6 +5294,20 @@
}
}
}
},
"top_traces": {
"properties": {
"took": {
"properties": {
"ms": {
"type": "long",
"_meta": {
"description": "Execution time in milliseconds for the \"top_traces\" task"
}
}
}
}
}
}
}
}
Expand Down

0 comments on commit dcce011

Please sign in to comment.