From 8c64f96d0e92532bae415758f3cc1d21ddc9eff0 Mon Sep 17 00:00:00 2001 From: jjllee Date: Thu, 8 Aug 2024 10:48:35 -0700 Subject: [PATCH] recordLatency enhancement - use duration --- .../src/aws-span-metrics-processor.ts | 5 +++-- .../test/aws-span-metrics-processor.test.ts | 14 ++++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/aws-distro-opentelemetry-node-autoinstrumentation/src/aws-span-metrics-processor.ts b/aws-distro-opentelemetry-node-autoinstrumentation/src/aws-span-metrics-processor.ts index 2709f23..d5c9d29 100644 --- a/aws-distro-opentelemetry-node-autoinstrumentation/src/aws-span-metrics-processor.ts +++ b/aws-distro-opentelemetry-node-autoinstrumentation/src/aws-span-metrics-processor.ts @@ -123,8 +123,9 @@ export class AwsSpanMetricsProcessor implements SpanProcessor { } private recordLatency(span: ReadableSpan, attributes: Attributes): void { - const millisFromSeconds: number = (span.endTime[0] - span.startTime[0]) * this.SECONDS_TO_MILLIS_MULTIPLIER; - const millisFromNanos: number = (span.endTime[1] - span.startTime[1]) / this.NANOS_TO_MILLIS_DIVIDER; + const millisFromSeconds: number = span.duration[0] * this.SECONDS_TO_MILLIS_MULTIPLIER; + const millisFromNanos: number = span.duration[1] / this.NANOS_TO_MILLIS_DIVIDER; + const millis: number = millisFromSeconds + millisFromNanos; this.latencyHistogram.record(millis, attributes); } diff --git a/aws-distro-opentelemetry-node-autoinstrumentation/test/aws-span-metrics-processor.test.ts b/aws-distro-opentelemetry-node-autoinstrumentation/test/aws-span-metrics-processor.test.ts index 4ca7b66..7dfe08a 100644 --- a/aws-distro-opentelemetry-node-autoinstrumentation/test/aws-span-metrics-processor.test.ts +++ b/aws-distro-opentelemetry-node-autoinstrumentation/test/aws-span-metrics-processor.test.ts @@ -5,6 +5,7 @@ import { Attributes, Context, Histogram, + HrTime, Meter, SpanContext, SpanKind, @@ -13,7 +14,7 @@ import { TraceFlags, isSpanContextValid, } from '@opentelemetry/api'; -import { InstrumentationLibrary } from '@opentelemetry/core'; +import { InstrumentationLibrary, hrTimeDuration } from '@opentelemetry/core'; import { Resource } from '@opentelemetry/resources'; import { MeterProvider } from '@opentelemetry/sdk-metrics'; import { ReadableSpan, Span } from '@opentelemetry/sdk-trace-base'; @@ -317,6 +318,7 @@ describe('AwsSpanMetricsProcessorTest', () => { configureMocksForOnEnd(readableSpanMock, metricAttributesMap); readableSpanMock.endTime[1] = 5_500_000; + (readableSpanMock as any).duration = hrTimeDuration(readableSpanMock.startTime, readableSpanMock.endTime); awsSpanMetricsProcessor.onEnd(readableSpanMock); sinon.assert.calledOnceWithExactly(errorHistogramMockRecord, 0, metricAttributesMap[SERVICE_METRIC]); @@ -430,6 +432,10 @@ describe('AwsSpanMetricsProcessorTest', () => { name: '@opentelemetry/instrumentation-aws-sdk', }; + const startTime: HrTime = [0, 0]; + const endTime: HrTime = [0, TEST_LATENCY_NANOS]; + const duration: HrTime = hrTimeDuration(startTime, endTime); + // Configure spanData const mockSpanData: ReadableSpan = { name: 'spanName', @@ -443,16 +449,16 @@ describe('AwsSpanMetricsProcessorTest', () => { }; return spanContext; }, - startTime: [0, 0], + startTime: startTime, // Configure latency - endTime: [0, TEST_LATENCY_NANOS], + endTime: endTime, // Configure Span Status status: statusData, // Configure attributes attributes: spanAttributes, links: [], events: [], - duration: [0, 1], + duration: duration, ended: true, resource: new Resource({}), // Configure Instrumentation Library