Skip to content

Commit

Permalink
fix: otlp json encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
seemk committed Oct 22, 2023
1 parent 1c7d7a3 commit 12f91b8
Show file tree
Hide file tree
Showing 26 changed files with 393 additions and 362 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ export class OTLPLogExporter
}

convert(logRecords: ReadableLogRecord[]): IExportLogsServiceRequest {
return createExportLogsServiceRequest(logRecords, true);
return createExportLogsServiceRequest(logRecords, {
useHex: true,
useLongBits: false,
});
}

getDefaultUrl(config: OTLPExporterConfigBase): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ export class OTLPLogExporter
}

convert(logRecords: ReadableLogRecord[]): IExportLogsServiceRequest {
return createExportLogsServiceRequest(logRecords, true);
return createExportLogsServiceRequest(logRecords, {
useHex: true,
useLongBits: false,
});
}

getDefaultUrl(config: OTLPExporterNodeConfigBase): string {
Expand Down
10 changes: 2 additions & 8 deletions experimental/packages/exporter-logs-otlp-http/test/logHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { Resource } from '@opentelemetry/resources';
import * as assert from 'assert';
import { VERSION } from '@opentelemetry/core';
import {
hrTimeToFixed64Nanos,
IAnyValue,
IExportLogsServiceRequest,
IKeyValue,
Expand Down Expand Up @@ -77,22 +76,17 @@ 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.deepStrictEqual(
logRecord.timeUnixNano,
hrTimeToFixed64(mockedReadableLogRecord.hrTime),
'1680253513123241635',
'timeUnixNano is wrong'
);
assert.deepStrictEqual(
logRecord.observedTimeUnixNano,
hrTimeToFixed64(mockedReadableLogRecord.hrTimeObserved),
'1680253513123241635',
'observedTimeUnixNano is wrong'
);
assert.strictEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ export class OTLPTraceExporter
);
}
convert(spans: ReadableSpan[]): IExportTraceServiceRequest {
return createExportTraceServiceRequest(spans, true);
return createExportTraceServiceRequest(spans, {
useHex: true,
useLongBits: false,
});
}

getDefaultUrl(config: OTLPExporterConfigBase): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ export class OTLPTraceExporter
}

convert(spans: ReadableSpan[]): IExportTraceServiceRequest {
return createExportTraceServiceRequest(spans, true);
return createExportTraceServiceRequest(spans, {
useHex: true,
useLongBits: false,
});
}

getDefaultUrl(config: OTLPExporterNodeConfigBase): string {
Expand Down
26 changes: 10 additions & 16 deletions experimental/packages/exporter-trace-otlp-http/test/traceHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
ILink,
IResource,
ISpan,
UnsignedLong,
} from '@opentelemetry/otlp-transformer';

if (typeof Buffer === 'undefined') {
Expand Down Expand Up @@ -244,59 +243,54 @@ 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: fixed64FromString('1574120165429803070'),
timeUnixNano: '1574120165429803070',
name: 'fetchStart',
attributes: [],
droppedAttributesCount: 0,
},
{
timeUnixNano: fixed64FromString('1574120165429803070'),
timeUnixNano: '1574120165429803070',
name: 'domainLookupStart',
attributes: [],
droppedAttributesCount: 0,
},
{
timeUnixNano: fixed64FromString('1574120165429803070'),
timeUnixNano: '1574120165429803070',
name: 'domainLookupEnd',
attributes: [],
droppedAttributesCount: 0,
},
{
timeUnixNano: fixed64FromString('1574120165429803070'),
timeUnixNano: '1574120165429803070',
name: 'connectStart',
attributes: [],
droppedAttributesCount: 0,
},
{
timeUnixNano: fixed64FromString('1574120165429803070'),
timeUnixNano: '1574120165429803070',
name: 'connectEnd',
attributes: [],
droppedAttributesCount: 0,
},
{
timeUnixNano: fixed64FromString('1574120165435513070'),
timeUnixNano: '1574120165435513070',
name: 'requestStart',
attributes: [],
droppedAttributesCount: 0,
},
{
timeUnixNano: fixed64FromString('1574120165436923070'),
timeUnixNano: '1574120165436923070',
name: 'responseStart',
attributes: [],
droppedAttributesCount: 0,
},
{
timeUnixNano: fixed64FromString('1574120165438688070'),
timeUnixNano: '1574120165438688070',
name: 'responseEnd',
attributes: [],
droppedAttributesCount: 0,
Expand Down Expand Up @@ -372,12 +366,12 @@ export function ensureSpanIsCorrect(span: ISpan, useHex = true) {
assert.strictEqual(span.kind, ESpanKind.SPAN_KIND_INTERNAL, 'kind is wrong');
assert.deepStrictEqual(
span.startTimeUnixNano,
fixed64FromString('1574120165429803070'),
'1574120165429803070',
'startTimeUnixNano is wrong'
);
assert.deepStrictEqual(
span.endTimeUnixNano,
fixed64FromString('1574120165438688070'),
'1574120165438688070',
'endTimeUnixNano is wrong'
);
assert.strictEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ import {
View,
} from '@opentelemetry/sdk-metrics';
import {
hrTimeToFixed64Nanos,
encodeAsString,
IKeyValue,
IMetric,
IResource,
UnsignedLong,
} from '@opentelemetry/otlp-transformer';

class TestMetricReader extends MetricReader {
Expand Down Expand Up @@ -152,14 +151,8 @@ export function ensureExportedCounterIsCorrect(
assert.strictEqual(dp.asInt, '1');
assert.strictEqual(dp.flags, 0);

assert.deepStrictEqual(
UnsignedLong.fromString(dp.startTimeUnixNano as string),
hrTimeToFixed64Nanos(startTime)
);
assert.deepStrictEqual(
UnsignedLong.fromString(dp.timeUnixNano as string),
hrTimeToFixed64Nanos(time)
);
assert.deepStrictEqual(dp.startTimeUnixNano, encodeAsString(startTime));
assert.deepStrictEqual(dp.timeUnixNano as string, encodeAsString(time));
}

export function ensureExportedObservableGaugeIsCorrect(
Expand All @@ -179,14 +172,8 @@ export function ensureExportedObservableGaugeIsCorrect(
assert.strictEqual(dp.asDouble, 6);
assert.strictEqual(dp.flags, 0);

assert.deepStrictEqual(
UnsignedLong.fromString(dp.startTimeUnixNano as string),
hrTimeToFixed64Nanos(startTime)
);
assert.deepStrictEqual(
UnsignedLong.fromString(dp.timeUnixNano as string),
hrTimeToFixed64Nanos(time)
);
assert.deepStrictEqual(dp.startTimeUnixNano, encodeAsString(startTime));
assert.deepStrictEqual(dp.timeUnixNano, encodeAsString(time));
}

export function ensureExportedHistogramIsCorrect(
Expand Down Expand Up @@ -215,14 +202,8 @@ export function ensureExportedHistogramIsCorrect(
assert.strictEqual(dp.min, 7);
assert.strictEqual(dp.max, 14);

assert.deepStrictEqual(
UnsignedLong.fromString(dp.startTimeUnixNano as string),
hrTimeToFixed64Nanos(startTime)
);
assert.deepStrictEqual(
UnsignedLong.fromString(dp.timeUnixNano as string),
hrTimeToFixed64Nanos(time)
);
assert.deepStrictEqual(dp.startTimeUnixNano, encodeAsString(startTime));
assert.deepStrictEqual(dp.timeUnixNano, encodeAsString(time));
assert.deepStrictEqual(dp.bucketCounts, bucketCounts);
assert.deepStrictEqual(dp.explicitBounds, explicitBounds);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class OTLPExporterBrowserProxy extends OTLPExporterBrowserBase<
}

convert(metrics: ResourceMetrics[]): IExportMetricsServiceRequest {
return createExportMetricsServiceRequest(metrics);
return createExportMetricsServiceRequest(metrics, { useLongBits: false });
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class OTLPExporterNodeProxy extends OTLPExporterNodeBase<
}

convert(metrics: ResourceMetrics[]): IExportMetricsServiceRequest {
return createExportMetricsServiceRequest(metrics);
return createExportMetricsServiceRequest(metrics, { useLongBits: false });
}

getDefaultUrl(config: OTLPExporterNodeConfigBase): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
View,
} from '@opentelemetry/sdk-metrics';
import {
hrTimeToFixed64Nanos,
encodeAsString,
IExportMetricsServiceRequest,
IKeyValue,
IMetric,
Expand Down Expand Up @@ -207,11 +207,6 @@ export function ensureWebResourceIsCorrect(resource: IResource) {
assert.strictEqual(resource.droppedAttributesCount, 0);
}

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

export function ensureCounterIsCorrect(
metric: IMetric,
time: HrTime,
Expand All @@ -228,8 +223,8 @@ export function ensureCounterIsCorrect(

assert.deepStrictEqual(dp.attributes, []);
assert.strictEqual(dp.asInt, 1);
assert.deepStrictEqual(dp.startTimeUnixNano, hrTimeToFixed64(startTime));
assert.deepStrictEqual(dp.timeUnixNano, hrTimeToFixed64(time));
assert.deepStrictEqual(dp.startTimeUnixNano, encodeAsString(startTime));
assert.deepStrictEqual(dp.timeUnixNano, encodeAsString(time));
}

export function ensureDoubleCounterIsCorrect(
Expand Down Expand Up @@ -273,8 +268,8 @@ export function ensureObservableGaugeIsCorrect(
assert.deepStrictEqual(dp.attributes, []);
assert.strictEqual(dp.asDouble, value);

assert.deepStrictEqual(dp.startTimeUnixNano, hrTimeToFixed64(startTime));
assert.deepStrictEqual(dp.timeUnixNano, hrTimeToFixed64(time));
assert.deepStrictEqual(dp.startTimeUnixNano, encodeAsString(startTime));
assert.deepStrictEqual(dp.timeUnixNano, encodeAsString(time));
}

export function ensureHistogramIsCorrect(
Expand All @@ -300,8 +295,8 @@ export function ensureHistogramIsCorrect(
assert.deepStrictEqual(dp.bucketCounts, bucketCounts);
assert.deepStrictEqual(dp.explicitBounds, explicitBounds);

assert.deepStrictEqual(dp.startTimeUnixNano, hrTimeToFixed64(startTime));
assert.deepStrictEqual(dp.timeUnixNano, hrTimeToFixed64(time));
assert.deepStrictEqual(dp.startTimeUnixNano, encodeAsString(startTime));
assert.deepStrictEqual(dp.timeUnixNano, encodeAsString(time));
}

export function ensureExportMetricsServiceRequestIsSet(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ import {
View,
} from '@opentelemetry/sdk-metrics';
import {
hrTimeToFixed64Nanos,
encodeAsString,
IExportMetricsServiceRequest,
IKeyValue,
IMetric,
UnsignedLong,
} from '@opentelemetry/otlp-transformer';
import { Stream } from 'stream';

Expand Down Expand Up @@ -147,14 +146,8 @@ export function ensureExportedCounterIsCorrect(

const [dp] = metric.sum.dataPoints;
assert.strictEqual(dp.asInt, '1');
assert.deepStrictEqual(
UnsignedLong.fromString(dp.startTimeUnixNano as string),
hrTimeToFixed64Nanos(startTime)
);
assert.deepStrictEqual(
UnsignedLong.fromString(dp.timeUnixNano as string),
hrTimeToFixed64Nanos(time)
);
assert.deepStrictEqual(dp.startTimeUnixNano, encodeAsString(startTime));
assert.deepStrictEqual(dp.timeUnixNano, encodeAsString(time));
}

export function ensureExportedObservableGaugeIsCorrect(
Expand All @@ -169,14 +162,8 @@ export function ensureExportedObservableGaugeIsCorrect(

const [dp] = metric.gauge.dataPoints;
assert.strictEqual(dp.asDouble, 6);
assert.deepStrictEqual(
UnsignedLong.fromString(dp.startTimeUnixNano as string),
hrTimeToFixed64Nanos(startTime)
);
assert.deepStrictEqual(
UnsignedLong.fromString(dp.timeUnixNano as string),
hrTimeToFixed64Nanos(time)
);
assert.deepStrictEqual(dp.startTimeUnixNano, encodeAsString(startTime));
assert.deepStrictEqual(dp.timeUnixNano, encodeAsString(time));
}

export function ensureExportedHistogramIsCorrect(
Expand Down Expand Up @@ -204,14 +191,8 @@ export function ensureExportedHistogramIsCorrect(
assert.strictEqual(dp.max, 14);
assert.deepStrictEqual(dp.explicitBounds, explicitBounds);
assert.deepStrictEqual(dp.bucketCounts, bucketCounts);
assert.deepStrictEqual(
UnsignedLong.fromString(dp.startTimeUnixNano as string),
hrTimeToFixed64Nanos(startTime)
);
assert.deepStrictEqual(
UnsignedLong.fromString(dp.timeUnixNano as string),
hrTimeToFixed64Nanos(time)
);
assert.deepStrictEqual(dp.startTimeUnixNano, encodeAsString(startTime));
assert.deepStrictEqual(dp.timeUnixNano, encodeAsString(time));
}

export function ensureExportMetricsServiceRequestIsSet(
Expand Down
Loading

0 comments on commit 12f91b8

Please sign in to comment.