Skip to content

Commit

Permalink
recordLatency enhancement - use duration
Browse files Browse the repository at this point in the history
  • Loading branch information
jj22ee committed Aug 8, 2024
1 parent 4b57f90 commit 8c64f96
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Attributes,
Context,
Histogram,
HrTime,
Meter,
SpanContext,
SpanKind,
Expand All @@ -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';
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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',
Expand All @@ -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
Expand Down

0 comments on commit 8c64f96

Please sign in to comment.