Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(otlp-transformer): avoid precision loss when converting HrTime to unix nanoseconds #4062

Merged
merged 16 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ All notable changes to experimental packages in this project will be documented

### :bug: (Bug Fix)

* fix(otlp-transformer): Avoid precision loss when converting from HrTime to unix nanoseconds. [#4062](https://github.com/open-telemetry/opentelemetry-js/pull/4062)
* fix(exporter-logs-otlp-http): add @opentelemetry/api-logs as dependency

## 0.41.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export function ensureExportedLogRecordIsCorrect(logRecord: ILogRecord) {
ensureExportedAttributesAreCorrect(logRecord.attributes);
assert.strictEqual(
logRecord.timeUnixNano,
'1680253513123241728',
'1680253513123241635',
'timeUnixNano is wrong'
);
assert.strictEqual(
Expand Down
14 changes: 10 additions & 4 deletions experimental/packages/exporter-logs-otlp-http/test/logHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Resource } from '@opentelemetry/resources';
import * as assert from 'assert';
import { VERSION } from '@opentelemetry/core';
import {
hrTimeToFixed64Nanos,
IAnyValue,
IExportLogsServiceRequest,
IKeyValue,
Expand Down Expand Up @@ -76,17 +77,22 @@ export function ensureExportedBodyIsCorrect(body?: IAnyValue) {
);
}

function hrTimeToFixed64(hrTime: HrTime) {
const { low, high } = hrTimeToFixed64Nanos(hrTime);
return { low, high };
}

export function ensureExportedLogRecordIsCorrect(logRecord: ILogRecord) {
ensureExportedBodyIsCorrect(logRecord.body);
ensureExportedAttributesAreCorrect(logRecord.attributes);
assert.strictEqual(
assert.deepStrictEqual(
logRecord.timeUnixNano,
1680253513123241700,
hrTimeToFixed64(mockedReadableLogRecord.hrTime),
'timeUnixNano is wrong'
);
assert.strictEqual(
assert.deepStrictEqual(
logRecord.observedTimeUnixNano,
1680253513123241700,
hrTimeToFixed64(mockedReadableLogRecord.hrTimeObserved),
'observedTimeUnixNano is wrong'
);
assert.strictEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ export function ensureExportedLogRecordIsCorrect(logRecord: ILogRecord) {
ensureExportedAttributesAreCorrect(logRecord.attributes);
assert.strictEqual(
logRecord.timeUnixNano,
'1680253513123241728',
'1680253513123241635',
'timeUnixNano is wrong'
);
assert.strictEqual(
logRecord.observedTimeUnixNano,
'1680253513123241728',
'1680253513123241635',
'observedTimeUnixNano is wrong'
);
assert.strictEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,49 +114,49 @@ export function ensureExportedEventsAreCorrect(events: IEvent[]) {
[
{
attributes: [],
timeUnixNano: '1574120165429803008',
timeUnixNano: '1574120165429803070',
name: 'fetchStart',
droppedAttributesCount: 0,
},
{
attributes: [],
timeUnixNano: '1574120165429803008',
timeUnixNano: '1574120165429803070',
name: 'domainLookupStart',
droppedAttributesCount: 0,
},
{
attributes: [],
timeUnixNano: '1574120165429803008',
timeUnixNano: '1574120165429803070',
name: 'domainLookupEnd',
droppedAttributesCount: 0,
},
{
attributes: [],
timeUnixNano: '1574120165429803008',
timeUnixNano: '1574120165429803070',
name: 'connectStart',
droppedAttributesCount: 0,
},
{
attributes: [],
timeUnixNano: '1574120165429803008',
timeUnixNano: '1574120165429803070',
name: 'connectEnd',
droppedAttributesCount: 0,
},
{
attributes: [],
timeUnixNano: '1574120165435513088',
timeUnixNano: '1574120165435513070',
name: 'requestStart',
droppedAttributesCount: 0,
},
{
attributes: [],
timeUnixNano: '1574120165436923136',
timeUnixNano: '1574120165436923070',
name: 'responseStart',
droppedAttributesCount: 0,
},
{
attributes: [],
timeUnixNano: '1574120165438688000',
timeUnixNano: '1574120165438688070',
name: 'responseEnd',
droppedAttributesCount: 0,
},
Expand Down Expand Up @@ -235,12 +235,12 @@ export function ensureExportedSpanIsCorrect(span: ISpan) {
assert.strictEqual(span.kind, 'SPAN_KIND_INTERNAL', 'kind is wrong');
assert.strictEqual(
span.startTimeUnixNano,
'1574120165429803008',
'1574120165429803070',
'startTimeUnixNano is wrong'
);
assert.strictEqual(
span.endTimeUnixNano,
'1574120165438688000',
'1574120165438688070',
'endTimeUnixNano is wrong'
);
assert.strictEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ describe('OTLPTraceExporter - node with json over http', () => {

fakeRequest.on('end', () => {
const responseBody = buff.toString();

const json = JSON.parse(responseBody) as IExportTraceServiceRequest;
const span1 = json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0];
assert.ok(typeof span1 !== 'undefined', "span doesn't exist");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
ILink,
IResource,
ISpan,
UnsignedLong,
} from '@opentelemetry/otlp-transformer';

if (typeof Buffer === 'undefined') {
Expand Down Expand Up @@ -243,54 +244,59 @@ export const multiInstrumentationLibraryTrace: ReadableSpan[] = [
},
];

function fixed64FromString(str: string) {
const { low, high } = UnsignedLong.fromString(str);
return { low, high };
}

export function ensureEventsAreCorrect(events: IEvent[]) {
assert.deepStrictEqual(
events,
[
{
timeUnixNano: 1574120165429803000,
timeUnixNano: fixed64FromString('1574120165429803070'),
name: 'fetchStart',
attributes: [],
droppedAttributesCount: 0,
},
{
timeUnixNano: 1574120165429803000,
timeUnixNano: fixed64FromString('1574120165429803070'),
name: 'domainLookupStart',
attributes: [],
droppedAttributesCount: 0,
},
{
timeUnixNano: 1574120165429803000,
timeUnixNano: fixed64FromString('1574120165429803070'),
name: 'domainLookupEnd',
attributes: [],
droppedAttributesCount: 0,
},
{
timeUnixNano: 1574120165429803000,
timeUnixNano: fixed64FromString('1574120165429803070'),
name: 'connectStart',
attributes: [],
droppedAttributesCount: 0,
},
{
timeUnixNano: 1574120165429803000,
timeUnixNano: fixed64FromString('1574120165429803070'),
name: 'connectEnd',
attributes: [],
droppedAttributesCount: 0,
},
{
timeUnixNano: 1574120165435513000,
timeUnixNano: fixed64FromString('1574120165435513070'),
name: 'requestStart',
attributes: [],
droppedAttributesCount: 0,
},
{
timeUnixNano: 1574120165436923100,
timeUnixNano: fixed64FromString('1574120165436923070'),
name: 'responseStart',
attributes: [],
droppedAttributesCount: 0,
},
{
timeUnixNano: 1574120165438688000,
timeUnixNano: fixed64FromString('1574120165438688070'),
name: 'responseEnd',
attributes: [],
droppedAttributesCount: 0,
Expand Down Expand Up @@ -364,14 +370,14 @@ export function ensureSpanIsCorrect(span: ISpan, useHex = true) {
);
assert.strictEqual(span.name, 'documentFetch', 'name is wrong');
assert.strictEqual(span.kind, ESpanKind.SPAN_KIND_INTERNAL, 'kind is wrong');
assert.strictEqual(
assert.deepStrictEqual(
span.startTimeUnixNano,
1574120165429803008,
fixed64FromString('1574120165429803070'),
'startTimeUnixNano is wrong'
);
assert.strictEqual(
assert.deepStrictEqual(
span.endTimeUnixNano,
1574120165438688000,
fixed64FromString('1574120165438688070'),
'endTimeUnixNano is wrong'
);
assert.strictEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,42 +109,42 @@ export function ensureProtoEventsAreCorrect(events: IEvent[]) {
events,
[
{
timeUnixNano: '1574120165429803008',
timeUnixNano: '1574120165429803070',
name: 'fetchStart',
droppedAttributesCount: 0,
},
{
timeUnixNano: '1574120165429803008',
timeUnixNano: '1574120165429803070',
name: 'domainLookupStart',
droppedAttributesCount: 0,
},
{
timeUnixNano: '1574120165429803008',
timeUnixNano: '1574120165429803070',
name: 'domainLookupEnd',
droppedAttributesCount: 0,
},
{
timeUnixNano: '1574120165429803008',
timeUnixNano: '1574120165429803070',
name: 'connectStart',
droppedAttributesCount: 0,
},
{
timeUnixNano: '1574120165429803008',
timeUnixNano: '1574120165429803070',
name: 'connectEnd',
droppedAttributesCount: 0,
},
{
timeUnixNano: '1574120165435513088',
timeUnixNano: '1574120165435513070',
name: 'requestStart',
droppedAttributesCount: 0,
},
{
timeUnixNano: '1574120165436923136',
timeUnixNano: '1574120165436923070',
name: 'responseStart',
droppedAttributesCount: 0,
},
{
timeUnixNano: '1574120165438688000',
timeUnixNano: '1574120165438688070',
name: 'responseEnd',
droppedAttributesCount: 0,
},
Expand Down Expand Up @@ -219,12 +219,12 @@ export function ensureProtoSpanIsCorrect(span: ISpan) {
assert.strictEqual(span.kind, 'SPAN_KIND_INTERNAL', 'kind is wrong');
assert.strictEqual(
span.startTimeUnixNano,
'1574120165429803008',
'1574120165429803070',
'startTimeUnixNano is wrong'
);
assert.strictEqual(
span.endTimeUnixNano,
'1574120165438688000',
'1574120165438688070',
'endTimeUnixNano is wrong'
);
assert.strictEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,23 @@ const testOTLPMetricExporter = (params: TestParams) => {
exportedData[0].scopeMetrics[0].metrics[histogramIndex];
ensureExportedCounterIsCorrect(
counter,
counter.sum?.dataPoints[0].timeUnixNano,
counter.sum?.dataPoints[0].startTimeUnixNano
metrics.scopeMetrics[0].metrics[counterIndex].dataPoints[0].endTime,
metrics.scopeMetrics[0].metrics[counterIndex].dataPoints[0]
.startTime
);
ensureExportedObservableGaugeIsCorrect(
observableGauge,
observableGauge.gauge?.dataPoints[0].timeUnixNano,
observableGauge.gauge?.dataPoints[0].startTimeUnixNano
metrics.scopeMetrics[0].metrics[observableIndex].dataPoints[0]
.endTime,
metrics.scopeMetrics[0].metrics[observableIndex].dataPoints[0]
.startTime
);
ensureExportedHistogramIsCorrect(
histogram,
histogram.histogram?.dataPoints[0].timeUnixNano,
histogram.histogram?.dataPoints[0].startTimeUnixNano,
metrics.scopeMetrics[0].metrics[histogramIndex].dataPoints[0]
.endTime,
metrics.scopeMetrics[0].metrics[histogramIndex].dataPoints[0]
.startTime,
[0, 100],
['0', '2', '0']
);
Expand Down
Loading