Skip to content

Commit 9db6352

Browse files
seemkpichlermarc
andauthored
fix: otlp json encoding (#4220)
Co-authored-by: Marc Pichler <[email protected]>
1 parent 586def4 commit 9db6352

File tree

27 files changed

+396
-362
lines changed

27 files changed

+396
-362
lines changed

experimental/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ All notable changes to experimental packages in this project will be documented
1313
* fix(sdk-node): remove the explicit dependency on @opentelemetry/exporter-jaeger that was kept on the previous release
1414
* '@opentelemetry/exporter-jaeger' is no longer be a dependency of this package. To continue using '@opentelemetry/exporter-jaeger', please install it manually.
1515
* NOTE: `@opentelemetry/exporter-jaeger` is deprecated, consider switching to one of the alternatives described [here](https://www.npmjs.com/package/@opentelemetry/exporter-jaeger)
16+
* fix(otlp-transformer): OTLP json encoding [#4220](https://github.com/open-telemetry/opentelemetry-js/pull/4220) @seemk
17+
* Fixes a bug in the OTLP (http/json) exporters where timestamps were not encoded as strings, causing the export to
18+
be rejected by OTLP endpoints
1619

1720
### :books: (Refine Doc)
1821

experimental/packages/exporter-logs-otlp-http/src/platform/browser/OTLPLogExporter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ export class OTLPLogExporter
4848
}
4949

5050
convert(logRecords: ReadableLogRecord[]): IExportLogsServiceRequest {
51-
return createExportLogsServiceRequest(logRecords, true);
51+
return createExportLogsServiceRequest(logRecords, {
52+
useHex: true,
53+
useLongBits: false,
54+
});
5255
}
5356

5457
getDefaultUrl(config: OTLPExporterConfigBase): string {

experimental/packages/exporter-logs-otlp-http/src/platform/node/OTLPLogExporter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ export class OTLPLogExporter
4848
}
4949

5050
convert(logRecords: ReadableLogRecord[]): IExportLogsServiceRequest {
51-
return createExportLogsServiceRequest(logRecords, true);
51+
return createExportLogsServiceRequest(logRecords, {
52+
useHex: true,
53+
useLongBits: false,
54+
});
5255
}
5356

5457
getDefaultUrl(config: OTLPExporterNodeConfigBase): string {

experimental/packages/exporter-logs-otlp-http/test/logHelper.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { Resource } from '@opentelemetry/resources';
2020
import * as assert from 'assert';
2121
import { VERSION } from '@opentelemetry/core';
2222
import {
23-
hrTimeToFixed64Nanos,
2423
IAnyValue,
2524
IExportLogsServiceRequest,
2625
IKeyValue,
@@ -77,22 +76,17 @@ export function ensureExportedBodyIsCorrect(body?: IAnyValue) {
7776
);
7877
}
7978

80-
function hrTimeToFixed64(hrTime: HrTime) {
81-
const { low, high } = hrTimeToFixed64Nanos(hrTime);
82-
return { low, high };
83-
}
84-
8579
export function ensureExportedLogRecordIsCorrect(logRecord: ILogRecord) {
8680
ensureExportedBodyIsCorrect(logRecord.body);
8781
ensureExportedAttributesAreCorrect(logRecord.attributes);
8882
assert.deepStrictEqual(
8983
logRecord.timeUnixNano,
90-
hrTimeToFixed64(mockedReadableLogRecord.hrTime),
84+
'1680253513123241635',
9185
'timeUnixNano is wrong'
9286
);
9387
assert.deepStrictEqual(
9488
logRecord.observedTimeUnixNano,
95-
hrTimeToFixed64(mockedReadableLogRecord.hrTimeObserved),
89+
'1680253513123241635',
9690
'observedTimeUnixNano is wrong'
9791
);
9892
assert.strictEqual(

experimental/packages/exporter-trace-otlp-http/src/platform/browser/OTLPTraceExporter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ export class OTLPTraceExporter
4747
);
4848
}
4949
convert(spans: ReadableSpan[]): IExportTraceServiceRequest {
50-
return createExportTraceServiceRequest(spans, true);
50+
return createExportTraceServiceRequest(spans, {
51+
useHex: true,
52+
useLongBits: false,
53+
});
5154
}
5255

5356
getDefaultUrl(config: OTLPExporterConfigBase): string {

experimental/packages/exporter-trace-otlp-http/src/platform/node/OTLPTraceExporter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ export class OTLPTraceExporter
5353
}
5454

5555
convert(spans: ReadableSpan[]): IExportTraceServiceRequest {
56-
return createExportTraceServiceRequest(spans, true);
56+
return createExportTraceServiceRequest(spans, {
57+
useHex: true,
58+
useLongBits: false,
59+
});
5760
}
5861

5962
getDefaultUrl(config: OTLPExporterNodeConfigBase): string {

experimental/packages/exporter-trace-otlp-http/test/traceHelper.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import {
3131
ILink,
3232
IResource,
3333
ISpan,
34-
UnsignedLong,
3534
} from '@opentelemetry/otlp-transformer';
3635

3736
if (typeof Buffer === 'undefined') {
@@ -244,59 +243,54 @@ export const multiInstrumentationLibraryTrace: ReadableSpan[] = [
244243
},
245244
];
246245

247-
function fixed64FromString(str: string) {
248-
const { low, high } = UnsignedLong.fromString(str);
249-
return { low, high };
250-
}
251-
252246
export function ensureEventsAreCorrect(events: IEvent[]) {
253247
assert.deepStrictEqual(
254248
events,
255249
[
256250
{
257-
timeUnixNano: fixed64FromString('1574120165429803070'),
251+
timeUnixNano: '1574120165429803070',
258252
name: 'fetchStart',
259253
attributes: [],
260254
droppedAttributesCount: 0,
261255
},
262256
{
263-
timeUnixNano: fixed64FromString('1574120165429803070'),
257+
timeUnixNano: '1574120165429803070',
264258
name: 'domainLookupStart',
265259
attributes: [],
266260
droppedAttributesCount: 0,
267261
},
268262
{
269-
timeUnixNano: fixed64FromString('1574120165429803070'),
263+
timeUnixNano: '1574120165429803070',
270264
name: 'domainLookupEnd',
271265
attributes: [],
272266
droppedAttributesCount: 0,
273267
},
274268
{
275-
timeUnixNano: fixed64FromString('1574120165429803070'),
269+
timeUnixNano: '1574120165429803070',
276270
name: 'connectStart',
277271
attributes: [],
278272
droppedAttributesCount: 0,
279273
},
280274
{
281-
timeUnixNano: fixed64FromString('1574120165429803070'),
275+
timeUnixNano: '1574120165429803070',
282276
name: 'connectEnd',
283277
attributes: [],
284278
droppedAttributesCount: 0,
285279
},
286280
{
287-
timeUnixNano: fixed64FromString('1574120165435513070'),
281+
timeUnixNano: '1574120165435513070',
288282
name: 'requestStart',
289283
attributes: [],
290284
droppedAttributesCount: 0,
291285
},
292286
{
293-
timeUnixNano: fixed64FromString('1574120165436923070'),
287+
timeUnixNano: '1574120165436923070',
294288
name: 'responseStart',
295289
attributes: [],
296290
droppedAttributesCount: 0,
297291
},
298292
{
299-
timeUnixNano: fixed64FromString('1574120165438688070'),
293+
timeUnixNano: '1574120165438688070',
300294
name: 'responseEnd',
301295
attributes: [],
302296
droppedAttributesCount: 0,
@@ -372,12 +366,12 @@ export function ensureSpanIsCorrect(span: ISpan, useHex = true) {
372366
assert.strictEqual(span.kind, ESpanKind.SPAN_KIND_INTERNAL, 'kind is wrong');
373367
assert.deepStrictEqual(
374368
span.startTimeUnixNano,
375-
fixed64FromString('1574120165429803070'),
369+
'1574120165429803070',
376370
'startTimeUnixNano is wrong'
377371
);
378372
assert.deepStrictEqual(
379373
span.endTimeUnixNano,
380-
fixed64FromString('1574120165438688070'),
374+
'1574120165438688070',
381375
'endTimeUnixNano is wrong'
382376
);
383377
assert.strictEqual(

experimental/packages/opentelemetry-exporter-metrics-otlp-grpc/test/metricsHelper.ts

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ import {
3333
View,
3434
} from '@opentelemetry/sdk-metrics';
3535
import {
36-
hrTimeToFixed64Nanos,
36+
encodeAsString,
3737
IKeyValue,
3838
IMetric,
3939
IResource,
40-
UnsignedLong,
4140
} from '@opentelemetry/otlp-transformer';
4241

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

155-
assert.deepStrictEqual(
156-
UnsignedLong.fromString(dp.startTimeUnixNano as string),
157-
hrTimeToFixed64Nanos(startTime)
158-
);
159-
assert.deepStrictEqual(
160-
UnsignedLong.fromString(dp.timeUnixNano as string),
161-
hrTimeToFixed64Nanos(time)
162-
);
154+
assert.deepStrictEqual(dp.startTimeUnixNano, encodeAsString(startTime));
155+
assert.deepStrictEqual(dp.timeUnixNano as string, encodeAsString(time));
163156
}
164157

165158
export function ensureExportedObservableGaugeIsCorrect(
@@ -179,14 +172,8 @@ export function ensureExportedObservableGaugeIsCorrect(
179172
assert.strictEqual(dp.asDouble, 6);
180173
assert.strictEqual(dp.flags, 0);
181174

182-
assert.deepStrictEqual(
183-
UnsignedLong.fromString(dp.startTimeUnixNano as string),
184-
hrTimeToFixed64Nanos(startTime)
185-
);
186-
assert.deepStrictEqual(
187-
UnsignedLong.fromString(dp.timeUnixNano as string),
188-
hrTimeToFixed64Nanos(time)
189-
);
175+
assert.deepStrictEqual(dp.startTimeUnixNano, encodeAsString(startTime));
176+
assert.deepStrictEqual(dp.timeUnixNano, encodeAsString(time));
190177
}
191178

192179
export function ensureExportedHistogramIsCorrect(
@@ -215,14 +202,8 @@ export function ensureExportedHistogramIsCorrect(
215202
assert.strictEqual(dp.min, 7);
216203
assert.strictEqual(dp.max, 14);
217204

218-
assert.deepStrictEqual(
219-
UnsignedLong.fromString(dp.startTimeUnixNano as string),
220-
hrTimeToFixed64Nanos(startTime)
221-
);
222-
assert.deepStrictEqual(
223-
UnsignedLong.fromString(dp.timeUnixNano as string),
224-
hrTimeToFixed64Nanos(time)
225-
);
205+
assert.deepStrictEqual(dp.startTimeUnixNano, encodeAsString(startTime));
206+
assert.deepStrictEqual(dp.timeUnixNano, encodeAsString(time));
226207
assert.deepStrictEqual(dp.bucketCounts, bucketCounts);
227208
assert.deepStrictEqual(dp.explicitBounds, explicitBounds);
228209
}

experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/browser/OTLPMetricExporter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class OTLPExporterBrowserProxy extends OTLPExporterBrowserBase<
6262
}
6363

6464
convert(metrics: ResourceMetrics[]): IExportMetricsServiceRequest {
65-
return createExportMetricsServiceRequest(metrics);
65+
return createExportMetricsServiceRequest(metrics, { useLongBits: false });
6666
}
6767
}
6868

experimental/packages/opentelemetry-exporter-metrics-otlp-http/src/platform/node/OTLPMetricExporter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class OTLPExporterNodeProxy extends OTLPExporterNodeBase<
5252
}
5353

5454
convert(metrics: ResourceMetrics[]): IExportMetricsServiceRequest {
55-
return createExportMetricsServiceRequest(metrics);
55+
return createExportMetricsServiceRequest(metrics, { useLongBits: false });
5656
}
5757

5858
getDefaultUrl(config: OTLPExporterNodeConfigBase): string {

0 commit comments

Comments
 (0)