From 6ec4061a1134153198cf81eec94732bdf4fabfe5 Mon Sep 17 00:00:00 2001 From: Katerina Patticha Date: Mon, 10 Oct 2022 11:32:04 +0200 Subject: [PATCH] [APM] Fix service metrics generator (#142790) Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- .../aggregators/service_metrics_aggregator.ts | 58 +++++++++---------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/packages/kbn-apm-synthtrace/src/lib/apm/aggregators/service_metrics_aggregator.ts b/packages/kbn-apm-synthtrace/src/lib/apm/aggregators/service_metrics_aggregator.ts index 618c9e52b9f2c..9ed6f805d1546 100644 --- a/packages/kbn-apm-synthtrace/src/lib/apm/aggregators/service_metrics_aggregator.ts +++ b/packages/kbn-apm-synthtrace/src/lib/apm/aggregators/service_metrics_aggregator.ts @@ -34,21 +34,19 @@ export type ServiceFields = Fields & | 'service.name' | 'service.version' | 'service.environment' - | 'transaction.type' > & Partial<{ _doc_count: number; transaction: { - duration: { - summary: { - min: number; - max: number; - sum: number; - value_count: number; - }; - }; failure_count: number; success_count: number; + type: string; + 'duration.summary': { + min: number; + max: number; + sum: number; + value_count: number; + }; }; }>; @@ -86,10 +84,10 @@ export class ServicMetricsAggregator implements StreamAggregator { }, }, failure_count: { - type: { type: 'long' }, + type: 'long', }, success_count: { - type: { type: 'long' }, + type: 'long', }, }, }, @@ -141,22 +139,24 @@ export class ServicMetricsAggregator implements StreamAggregator { } const state = this.state[key]; - state.count++; - - switch (event['event.outcome']) { - case 'failure': - state.failure_count++; - break; - case 'success': - state.success_count++; - break; - } const duration = Number(event['transaction.duration.us']); + if (duration >= 0) { + state.count++; + state.sum += duration; if (duration > state.max) state.max = duration; if (duration < state.min) state.min = Math.min(0, duration); + + switch (event['event.outcome']) { + case 'failure': + state.failure_count++; + break; + case 'success': + state.success_count++; + break; + } } }; @@ -197,18 +197,16 @@ export class ServicMetricsAggregator implements StreamAggregator { 'processor.event': 'metric', 'service.name': state['service.name'], 'service.environment': state['service.environment'], - 'transaction.type': state['transaction.type'], transaction: { - duration: { - summary: { - min: state.min, - max: state.max, - sum: state.sum, - value_count: state.count, - }, + 'duration.summary': { + min: state.min, + max: state.max, + sum: state.sum, + value_count: state.count, }, - failure_count: state.failure_count, success_count: state.success_count, + failure_count: state.failure_count, + type: state['transaction.type'] ?? 'request', }, }; }