From dcce011f91894d24e09ba650d28f8d5bbd77aa69 Mon Sep 17 00:00:00 2001 From: Miriam <31922082+MiriamAparicio@users.noreply.github.com> Date: Thu, 14 Sep 2023 08:29:54 +0100 Subject: [PATCH] [APM] Add telemetry for top traces (#166263) Closes https://github.com/elastic/kibana/issues/161985 --- .../__snapshots__/apm_telemetry.test.ts.snap | 30 +++++++++++ .../collect_data_telemetry/tasks.test.ts | 52 +++++++++++++++++++ .../collect_data_telemetry/tasks.ts | 45 ++++++++++++++++ .../apm/server/lib/apm_telemetry/schema.ts | 27 ++++++++++ .../apm/server/lib/apm_telemetry/types.ts | 7 ++- .../schema/xpack_plugins.json | 30 +++++++++++ 6 files changed, 190 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/apm/common/__snapshots__/apm_telemetry.test.ts.snap b/x-pack/plugins/apm/common/__snapshots__/apm_telemetry.test.ts.snap index c389d411462d7..d84aa51a0e365 100644 --- a/x-pack/plugins/apm/common/__snapshots__/apm_telemetry.test.ts.snap +++ b/x-pack/plugins/apm/common/__snapshots__/apm_telemetry.test.ts.snap @@ -1957,6 +1957,22 @@ exports[`APM telemetry helpers getApmTelemetry generates a JSON object with the } } }, + "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": { @@ -2168,6 +2184,20 @@ exports[`APM telemetry helpers getApmTelemetry generates a JSON object with the } } } + }, + "top_traces": { + "properties": { + "took": { + "properties": { + "ms": { + "type": "long", + "_meta": { + "description": "Execution time in milliseconds for the \\"top_traces\\" task" + } + } + } + } + } } } } diff --git a/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.test.ts b/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.test.ts index fb061eec49baf..ba9d06b78bfae 100644 --- a/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.test.ts +++ b/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.test.ts @@ -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, + }, + }); + }); + }); }); diff --git a/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.ts b/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.ts index beb84481e84b7..aad51e5ed1c5d 100644 --- a/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.ts +++ b/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.ts @@ -39,6 +39,7 @@ import { SERVICE_RUNTIME_NAME, SERVICE_RUNTIME_VERSION, SERVICE_VERSION, + TRACE_ID, TRANSACTION_NAME, TRANSACTION_RESULT, TRANSACTION_TYPE, @@ -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, + }, + }; + }, + }, ]; diff --git a/x-pack/plugins/apm/server/lib/apm_telemetry/schema.ts b/x-pack/plugins/apm/server/lib/apm_telemetry/schema.ts index e43fda17a951c..1c41229b47bca 100644 --- a/x-pack/plugins/apm/server/lib/apm_telemetry/schema.ts +++ b/x-pack/plugins/apm/server/lib/apm_telemetry/schema.ts @@ -1006,6 +1006,22 @@ export const apmSchema: MakeSchemaFrom = { }, }, 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: { @@ -1169,5 +1185,16 @@ export const apmSchema: MakeSchemaFrom = { }, }, }, + top_traces: { + took: { + ms: { + type: 'long', + _meta: { + description: + 'Execution time in milliseconds for the "top_traces" task', + }, + }, + }, + }, }, }; diff --git a/x-pack/plugins/apm/server/lib/apm_telemetry/types.ts b/x-pack/plugins/apm/server/lib/apm_telemetry/types.ts index 9c6c312c284f4..d5c16a6b3692a 100644 --- a/x-pack/plugins/apm/server/lib/apm_telemetry/types.ts +++ b/x-pack/plugins/apm/server/lib/apm_telemetry/types.ts @@ -211,6 +211,10 @@ export interface APMUsage { total: number; }; per_service: APMPerService[]; + top_traces: { + max: number; + median: number; + }; tasks: Record< | 'aggregated_transactions' | 'cloud' @@ -226,7 +230,8 @@ export interface APMUsage { | 'cardinality' | 'environments' | 'service_groups' - | 'per_service', + | 'per_service' + | 'top_traces', { took: { ms: number } } >; } diff --git a/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json b/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json index c643f616e5eb3..000d54ade631c 100644 --- a/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json +++ b/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json @@ -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": { @@ -5278,6 +5294,20 @@ } } } + }, + "top_traces": { + "properties": { + "took": { + "properties": { + "ms": { + "type": "long", + "_meta": { + "description": "Execution time in milliseconds for the \"top_traces\" task" + } + } + } + } + } } } }