fix(otlp-transformer): do not throw when deserializing empty JSON response (#5551)

This commit is contained in:
Marc Pichler 2025-04-14 10:13:32 +02:00 committed by GitHub
parent 6e15d69cb8
commit 37fe1e495b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 47 additions and 0 deletions

View File

@ -12,6 +12,8 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2
### :bug: Bug Fixes
fix(otlp-transformer): do not throw when deserializing empty JSON response [#5551](https://github.com/open-telemetry/opentelemetry-js/pull/5551) @pichlermarc
### :books: Documentation
### :house: Internal

View File

@ -34,6 +34,9 @@ export const JsonLogsSerializer: ISerializer<
return encoder.encode(JSON.stringify(request));
},
deserializeResponse: (arg: Uint8Array) => {
if (arg.length === 0) {
return {};
}
const decoder = new TextDecoder();
return JSON.parse(decoder.decode(arg)) as IExportLogsServiceResponse;
},

View File

@ -30,6 +30,9 @@ export const JsonMetricsSerializer: ISerializer<
return encoder.encode(JSON.stringify(request));
},
deserializeResponse: (arg: Uint8Array) => {
if (arg.length === 0) {
return {};
}
const decoder = new TextDecoder();
return JSON.parse(decoder.decode(arg)) as IExportMetricsServiceResponse;
},

View File

@ -31,6 +31,9 @@ export const JsonTraceSerializer: ISerializer<
return encoder.encode(JSON.stringify(request));
},
deserializeResponse: (arg: Uint8Array) => {
if (arg.length === 0) {
return {};
}
const decoder = new TextDecoder();
return JSON.parse(decoder.decode(arg)) as IExportTraceServiceResponse;
},

View File

@ -344,6 +344,12 @@ describe('Logs', () => {
1
);
});
it('does not throw when deserializing an empty response', () => {
assert.doesNotThrow(() =>
ProtobufLogsSerializer.deserializeResponse(new Uint8Array([]))
);
});
});
describe('JsonLogsSerializer', function () {
@ -383,5 +389,11 @@ describe('Logs', () => {
1
);
});
it('does not throw when deserializing an empty response', () => {
assert.doesNotThrow(() =>
JsonLogsSerializer.deserializeResponse(new Uint8Array([]))
);
});
});
});

View File

@ -858,6 +858,12 @@ describe('Metrics', () => {
1
);
});
it('does not throw when deserializing an empty response', () => {
assert.doesNotThrow(() =>
ProtobufMetricsSerializer.deserializeResponse(new Uint8Array([]))
);
});
});
describe('JsonMetricsSerializer', function () {
@ -929,5 +935,11 @@ describe('Metrics', () => {
1
);
});
it('does not throw when deserializing an empty response', () => {
assert.doesNotThrow(() =>
JsonMetricsSerializer.deserializeResponse(new Uint8Array([]))
);
});
});
});

View File

@ -495,6 +495,12 @@ describe('Trace', () => {
1
);
});
it('does not throw when deserializing an empty response', () => {
assert.doesNotThrow(() =>
ProtobufTraceSerializer.deserializeResponse(new Uint8Array([]))
);
});
});
describe('JsonTracesSerializer', function () {
@ -537,5 +543,11 @@ describe('Trace', () => {
1
);
});
it('does not throw when deserializing an empty response', () => {
assert.doesNotThrow(() =>
JsonTraceSerializer.deserializeResponse(new Uint8Array([]))
);
});
});
});