-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(otlp-transformer): add custom logs response deserializer #6530
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
Merged
pichlermarc
merged 4 commits into
open-telemetry:main
from
dynatrace-oss-contrib:feat/custom-proto-logs-response-deserializer
Apr 7, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ebce3d7
feat(otlp-transformer): add custom logs response deserializer
pichlermarc f1186b6
fix(otlp-transformer): skip unexpected wiretypes
pichlermarc 8d1d792
test(otlp-transformer): test valid proto but invalid otlp
pichlermarc b80fc89
Merge branch 'main' into feat/custom-proto-logs-response-deserializer
pichlermarc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
experimental/packages/otlp-transformer/src/common/protobuf/protobuf-reader.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| /** | ||
| * Minimal binary protobuf reader. | ||
| * Only implements the wire-types that we currently need; this is not intended | ||
| * to be a general-purpose protobuf reader. | ||
| * | ||
| * Since the values we parse are generally small and not very nested, it's public | ||
| * interface does not enforce the same low-allocation philosophy that ProtobufWriter does. | ||
| * If this is needed in the future, we should refactor this to fit the use-case. | ||
| */ | ||
| export class ProtobufReader { | ||
| pos: number = 0; | ||
| private readonly _buf: Uint8Array; | ||
| private readonly _textDecoder: { | ||
| decode: (input?: Uint8Array | null) => string; | ||
| }; | ||
|
|
||
| constructor(buf: Uint8Array) { | ||
| this._buf = buf; | ||
| this._textDecoder = new TextDecoder(); | ||
| } | ||
|
|
||
| isAtEnd(): boolean { | ||
| return this.pos >= this._buf.length; | ||
| } | ||
|
|
||
| /** Read a varint and decode it as a tag, returning field number and wire type. */ | ||
| readTag(): { fieldNumber: number; wireType: number } { | ||
| const raw = this.readVarint(); | ||
| return { fieldNumber: raw >>> 3, wireType: raw & 0x7 }; | ||
| } | ||
|
|
||
| /** | ||
| * Read a base-128 varint. | ||
| * Returns a JS `number`; precision above 2^53 is silently lost. | ||
| */ | ||
| readVarint(): number { | ||
| let result = 0; | ||
| let shift = 0; | ||
| while (this.pos < this._buf.length) { | ||
| const b = this._buf[this.pos++]; | ||
| result += (b & 0x7f) * Math.pow(2, shift); | ||
| shift += 7; | ||
| if ((b & 0x80) === 0) break; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| /** Read a length-delimited byte sequence (bytes field or embedded message). */ | ||
| readBytes(): Uint8Array { | ||
| const len = this.readVarint(); | ||
| const slice = this._buf.subarray(this.pos, this.pos + len); | ||
| this.pos += len; | ||
| return slice; | ||
| } | ||
|
|
||
| /** Read a length-delimited UTF-8 string. */ | ||
| readString(): string { | ||
| return this._textDecoder.decode(this.readBytes()); | ||
| } | ||
|
|
||
| /** | ||
| * Skip an unknown field. | ||
| * Handles wire types 0 (varint), 1 (64-bit), 2 (length-delimited), | ||
| * 3 (start-group), 4 (end-group), and 5 (32-bit). | ||
| */ | ||
| skip(wireType: number): void { | ||
| switch (wireType) { | ||
| case 0: // varint | ||
| this.readVarint(); | ||
| break; | ||
| case 1: // 64-bit fixed | ||
| this.pos += 8; | ||
| break; | ||
| case 2: // length-delimited | ||
| this.readBytes(); | ||
| break; | ||
| case 3: // start group (deprecated) | ||
| // We should never encounter this, but let's handle it gracefully in case we do: | ||
| // Read nested tags until matching end-group (wire type 4) is found. | ||
| // Groups can be nested, so continue until the end-group for this | ||
| // start-group is encountered. | ||
| while (!this.isAtEnd()) { | ||
| const { wireType: nestedWireType } = this.readTag(); | ||
| if (nestedWireType === 4) { | ||
| // matched end-group for this start-group | ||
| break; | ||
| } | ||
| // recursive skip also handles nested groups | ||
| this.skip(nestedWireType); | ||
| } | ||
| break; | ||
| case 4: // end group | ||
| // End-group should be handled by the start-group logic above. | ||
| // When encountered directly in skip, treat it as a no-op (it signals | ||
| // termination of the enclosing group). | ||
| break; | ||
| case 5: // 32-bit fixed | ||
| this.pos += 4; | ||
| break; | ||
| default: | ||
| throw new Error(`Unknown wire type ${wireType}, cannot safely skip`); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
experimental/packages/otlp-transformer/src/logs/protobuf/response-deserializer.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import type { | ||
| IExportLogsServiceResponse, | ||
| IExportLogsPartialSuccess, | ||
| } from '../export-response'; | ||
| import { ProtobufReader } from '../../common/protobuf/protobuf-reader'; | ||
|
|
||
| /** | ||
| * Parse an ExportLogsPartialSuccess embedded message from raw bytes. | ||
| * | ||
| * Field map (opentelemetry/proto/collector/logs/v1/logs_service.proto): | ||
| * 1 rejected_log_records int64 (varint) | ||
| * 2 error_message string (length-delimited) | ||
| */ | ||
| function deserializePartialSuccess( | ||
| data: Uint8Array | ||
| ): IExportLogsPartialSuccess { | ||
| const reader = new ProtobufReader(data); | ||
| const result: IExportLogsPartialSuccess = {}; | ||
|
|
||
| while (!reader.isAtEnd()) { | ||
| const { fieldNumber, wireType } = reader.readTag(); | ||
| switch (fieldNumber) { | ||
| case 1: // rejected_log_records (int64, varint) | ||
| // expected wire type 0 (varint) | ||
| if (wireType === 0) { | ||
| result.rejectedLogRecords = reader.readVarint(); | ||
| } else { | ||
| // unexpected wire type for this field; skip it safely | ||
| reader.skip(wireType); | ||
| } | ||
| break; | ||
| case 2: // error_message (string, length-delimited) | ||
| // expected wire type 2 (length-delimited) | ||
| if (wireType === 2) { | ||
| result.errorMessage = reader.readString(); | ||
| } else { | ||
| // unexpected wire type for this field; skip it safely | ||
| reader.skip(wireType); | ||
| } | ||
| break; | ||
| default: | ||
| reader.skip(wireType); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Parse an ExportLogsServiceResponse protobuf message from raw bytes. | ||
| * | ||
| * Field map (opentelemetry/proto/collector/logs/v1/logs_service.proto): | ||
| * 1 partial_success ExportLogsPartialSuccess (length-delimited) | ||
| */ | ||
| export function deserializeExportLogsServiceResponse( | ||
| data: Uint8Array | ||
| ): IExportLogsServiceResponse { | ||
| const reader = new ProtobufReader(data); | ||
| const result: IExportLogsServiceResponse = {}; | ||
|
|
||
| while (!reader.isAtEnd()) { | ||
| const { fieldNumber, wireType } = reader.readTag(); | ||
| switch (fieldNumber) { | ||
| case 1: // partial_success (ExportLogsPartialSuccess, length-delimited) | ||
| // expected wire type 2 (length-delimited / embedded message) | ||
| if (wireType === 2) { | ||
| result.partialSuccess = deserializePartialSuccess(reader.readBytes()); | ||
| } else { | ||
| // unexpected wire type for this field; skip it safely | ||
| reader.skip(wireType); | ||
| } | ||
| break; | ||
| default: | ||
| reader.skip(wireType); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And almost 1MiB of JS just left the process.
... or almost (need trace and metrics as well).