Skip to content
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
10 changes: 1 addition & 9 deletions bundler-tests/browser/webpack-4/webpack.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,9 @@ export default {
},
resolve: {
alias: {
// Webpack 4 doesn't support package.json exports field, so we need to manually map subpath exports
// Webpack 4 doesn't support package.json exports field, so we need to manually map the browser-http subpath
'@opentelemetry/otlp-exporter-base/browser-http':
'@opentelemetry/otlp-exporter-base/build/esm/index-browser-http.js',
// @bufbuild/protobuf subpath exports (used by @opentelemetry/otlp-transformer)
// These paths are compatible with @bufbuild/protobuf v2.x
'@bufbuild/protobuf/codegenv1':
'@bufbuild/protobuf/dist/esm/codegenv1/index.js',
'@bufbuild/protobuf/wkt': '@bufbuild/protobuf/dist/esm/wkt/index.js',
'@bufbuild/protobuf/wire': '@bufbuild/protobuf/dist/esm/wire/index.js',
'@bufbuild/protobuf/reflect':
'@bufbuild/protobuf/dist/esm/reflect/index.js',
},
},
module: {
Expand Down
2 changes: 0 additions & 2 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2
* test(instrumentation-http): replace uses of deprecated abort() [#6149](https://github.com/open-telemetry/opentelemetry-js/pull/6149) @cjihrig
* refactor(configuration): simplify boolean check [#6158](https://github.com/open-telemetry/opentelemetry-js/pull/6158) @cjihrig
* chore: disallow constructor parameter property syntax [#6187](https://github.com/open-telemetry/opentelemetry-js/pull/6187) @legendecas
* refactor(otlp-transformer): migrate from protobufjs to protobuf-es [#6179](https://github.com/open-telemetry/opentelemetry-js/pull/6179) @overbalance
* chore(otlp-transformer, sampler-composite): clean up tsconfig after protobuf-es migration [#6192](https://github.com/open-telemetry/opentelemetry-js/pull/6192) @overbalance
* chore(instrumentation-grpc): use local protobuf-ts plugin instead of buf.build remote [#6202](https://github.com/open-telemetry/opentelemetry-js/pull/6202) @overbalance
* test(otlp-transformer): add benchmark for ProtobufTraceSerializer [#6226](https://github.com/open-telemetry/opentelemetry-js/pull/6226) @overbalance

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@types/mocha": "10.0.10",
"@types/node": "18.6.5",
"@types/sinon": "17.0.4",
"@typescript/vfs": "1.6.2",
"mocha": "11.7.5",
"nyc": "17.1.0",
"sinon": "18.0.1",
Expand Down
8 changes: 0 additions & 8 deletions experimental/packages/otlp-transformer/buf.gen.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions experimental/packages/otlp-transformer/buf.yaml

This file was deleted.

11 changes: 6 additions & 5 deletions experimental/packages/otlp-transformer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"compile": "tsc --build tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
"clean": "tsc --build --clean tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
"protos": "npm run submodule && npm run protos:generate",
"protos:generate": "buf generate",
"protos:generate:js": "pbjs -t static-module -p ./protos -w commonjs --null-defaults -o ./src/generated/root.js ./protos/opentelemetry/proto/common/v1/common.proto ./protos/opentelemetry/proto/resource/v1/resource.proto ./protos/opentelemetry/proto/trace/v1/trace.proto ./protos/opentelemetry/proto/collector/trace/v1/trace_service.proto ./protos/opentelemetry/proto/metrics/v1/metrics.proto ./protos/opentelemetry/proto/collector/metrics/v1/metrics_service.proto ./protos/opentelemetry/proto/logs/v1/logs.proto ./protos/opentelemetry/proto/collector/logs/v1/logs_service.proto",
"protos:generate:ts": "pbts -o ./src/generated/root.d.ts ./src/generated/root.js",
"protos:generate": "npm run protos:generate:js && npm run protos:generate:ts",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
"tdd": "npm run test -- --watch-extensions ts --watch",
Expand Down Expand Up @@ -61,8 +63,6 @@
"@opentelemetry/api": "^1.3.0"
},
"devDependencies": {
"@bufbuild/buf": "1.47.2",
"@bufbuild/protoc-gen-es": "2.2.3",
"@opentelemetry/api": "1.9.0",
"@types/mocha": "10.0.10",
"@types/webpack-env": "1.16.3",
Expand All @@ -75,18 +75,19 @@
"karma-webpack": "5.0.1",
"mocha": "11.7.5",
"nyc": "17.1.0",
"protobufjs-cli": "1.1.3",
"ts-loader": "9.5.4",
"typescript": "5.0.4",
"webpack": "5.101.3"
},
"dependencies": {
"@bufbuild/protobuf": "2.2.3",
"@opentelemetry/api-logs": "0.208.0",
"@opentelemetry/core": "2.2.0",
"@opentelemetry/resources": "2.2.0",
"@opentelemetry/sdk-logs": "0.208.0",
"@opentelemetry/sdk-metrics": "2.2.0",
"@opentelemetry/sdk-trace-base": "2.2.0"
"@opentelemetry/sdk-trace-base": "2.2.0",
"protobufjs": "7.3.0"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/otlp-transformer",
"sideEffects": false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as protobuf from 'protobufjs';

export interface ExportType<T, R = T & { toJSON: () => unknown }> {
encode(message: T, writer?: protobuf.Writer): protobuf.Writer;
decode(reader: protobuf.Reader | Uint8Array, length?: number): R;
}
41 changes: 5 additions & 36 deletions experimental/packages/otlp-transformer/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import type { OtlpEncodingOptions, Fixed64, LongBits } from './internal-types';
import { HrTime } from '@opentelemetry/api';
import { hrTimeToNanoseconds } from '@opentelemetry/core';
import { hexToBinary } from './hex-to-binary';

export function hrTimeToNanos(hrTime: HrTime): bigint {
Expand All @@ -41,6 +42,9 @@ export function encodeAsString(hrTime: HrTime): string {
return nanos.toString();
}

const encodeTimestamp =
typeof BigInt !== 'undefined' ? encodeAsString : hrTimeToNanoseconds;

export type HrTimeEncodeFunction = (hrTime: HrTime) => Fixed64;
export type SpanContextEncodeFunction = (
spanContext: string
Expand All @@ -64,47 +68,12 @@ function optionalHexToBinary(str: string | undefined): Uint8Array | undefined {
return hexToBinary(str);
}

/**
* Convert hex string to base64 (for protobuf JSON format).
*/
export function hexToBase64(hex: string): string {
const bytes = hexToBinary(hex);
// Works in both Node.js and browser
if (typeof Buffer !== 'undefined') {
return Buffer.from(bytes).toString('base64');
}
// Browser fallback using spread to avoid string concatenation in loop
return btoa(String.fromCharCode(...bytes));
}

function optionalHexToBase64(str: string | undefined): string | undefined {
if (str === undefined) return undefined;
return hexToBase64(str);
}

const DEFAULT_ENCODER: Encoder = {
encodeHrTime: encodeAsLongBits,
encodeSpanContext: hexToBinary,
encodeOptionalSpanContext: optionalHexToBinary,
};

/**
* Encoder for protobuf JSON format (used with fromJson).
* Uses string timestamps and base64 for bytes.
*/
export const PROTOBUF_JSON_ENCODER: Encoder = {
encodeHrTime: encodeAsString,
encodeSpanContext: hexToBase64,
encodeOptionalSpanContext: optionalHexToBase64,
};

/** @internal */
export function isOtlpEncoder(
obj: OtlpEncodingOptions | Encoder | undefined
): obj is Encoder {
return obj !== undefined && 'encodeHrTime' in obj;
}

export function getOtlpEncoder(options?: OtlpEncodingOptions): Encoder {
if (options === undefined) {
return DEFAULT_ENCODER;
Expand All @@ -113,7 +82,7 @@ export function getOtlpEncoder(options?: OtlpEncodingOptions): Encoder {
const useLongBits = options.useLongBits ?? true;
const useHex = options.useHex ?? false;
return {
encodeHrTime: useLongBits ? encodeAsLongBits : encodeAsString,
encodeHrTime: useLongBits ? encodeAsLongBits : encodeTimestamp,
encodeSpanContext: useHex ? identity : hexToBinary,
encodeOptionalSpanContext: useHex ? identity : optionalHexToBinary,
};
Expand Down
6 changes: 3 additions & 3 deletions experimental/packages/otlp-transformer/src/logs/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
IResourceLogs,
} from './internal-types';
import { Resource } from '@opentelemetry/resources';
import { Encoder, getOtlpEncoder, isOtlpEncoder } from '../common/utils';
import { Encoder, getOtlpEncoder } from '../common/utils';
import {
createInstrumentationScope,
createResource,
Expand All @@ -35,9 +35,9 @@ import { LogAttributes } from '@opentelemetry/api-logs';

export function createExportLogsServiceRequest(
logRecords: ReadableLogRecord[],
options?: OtlpEncodingOptions | Encoder
options?: OtlpEncodingOptions
): IExportLogsServiceRequest {
const encoder = isOtlpEncoder(options) ? options : getOtlpEncoder(options);
const encoder = getOtlpEncoder(options);
return {
resourceLogs: logRecordsToResourceLogs(logRecords, encoder),
};
Expand Down
37 changes: 14 additions & 23 deletions experimental/packages/otlp-transformer/src/logs/protobuf/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { toBinary, fromBinary, fromJsonString } from '@bufbuild/protobuf';
import * as root from '../../generated/root';

import { IExportLogsServiceRequest } from '../internal-types';
import { IExportLogsServiceResponse } from '../export-response';

import { createExportLogsServiceRequest } from '../internal';
import { ReadableLogRecord } from '@opentelemetry/sdk-logs';
import { ExportType } from '../../common/protobuf/protobuf-export-type';
import { ISerializer } from '../../i-serializer';
import {
ExportLogsServiceRequestSchema,
ExportLogsServiceResponseSchema,
} from '../../generated/opentelemetry/proto/collector/logs/v1/logs_service_pb';
import { PROTOBUF_JSON_ENCODER } from '../../common/utils';

const logsResponseType = root.opentelemetry.proto.collector.logs.v1
.ExportLogsServiceResponse as ExportType<IExportLogsServiceResponse>;

const logsRequestType = root.opentelemetry.proto.collector.logs.v1
.ExportLogsServiceRequest as ExportType<IExportLogsServiceRequest>;

/*
* @experimental this serializer may receive breaking changes in minor versions, pin this package's version when using this constant
Expand All @@ -32,24 +37,10 @@ export const ProtobufLogsSerializer: ISerializer<
IExportLogsServiceResponse
> = {
serializeRequest: (arg: ReadableLogRecord[]) => {
const request = createExportLogsServiceRequest(arg, PROTOBUF_JSON_ENCODER);
const message = fromJsonString(
ExportLogsServiceRequestSchema,
JSON.stringify(request)
);
return toBinary(ExportLogsServiceRequestSchema, message);
const request = createExportLogsServiceRequest(arg);
return logsRequestType.encode(request).finish();
},
deserializeResponse: (arg: Uint8Array) => {
const response = fromBinary(ExportLogsServiceResponseSchema, arg);
return {
partialSuccess: response.partialSuccess
? {
rejectedLogRecords: Number(
response.partialSuccess.rejectedLogRecords
),
errorMessage: response.partialSuccess.errorMessage,
}
: undefined,
};
return logsResponseType.decode(arg);
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
IResourceMetrics,
IScopeMetrics,
} from './internal-types';
import { Encoder, getOtlpEncoder, isOtlpEncoder } from '../common/utils';
import { Encoder, getOtlpEncoder } from '../common/utils';
import {
createInstrumentationScope,
createResource,
Expand All @@ -44,9 +44,9 @@ import {

export function toResourceMetrics(
resourceMetrics: ResourceMetrics,
options?: OtlpEncodingOptions | Encoder
options?: OtlpEncodingOptions
): IResourceMetrics {
const encoder = isOtlpEncoder(options) ? options : getOtlpEncoder(options);
const encoder = getOtlpEncoder(options);
const processedResource = createResource(resourceMetrics.resource);
return {
resource: processedResource,
Expand Down Expand Up @@ -209,7 +209,7 @@ function toAggregationTemporality(

export function createExportMetricsServiceRequest(
resourceMetrics: ResourceMetrics[],
options?: OtlpEncodingOptions | Encoder
options?: OtlpEncodingOptions
): IExportMetricsServiceRequest {
return {
resourceMetrics: resourceMetrics.map(metrics =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,29 @@
* limitations under the License.
*/

import { toBinary, fromBinary, fromJsonString } from '@bufbuild/protobuf';
import * as root from '../../generated/root';
import { ISerializer } from '../../i-serializer';
import { IExportMetricsServiceRequest } from '../internal-types';
import { ExportType } from '../../common/protobuf/protobuf-export-type';
import { createExportMetricsServiceRequest } from '../internal';
import { ResourceMetrics } from '@opentelemetry/sdk-metrics';
import { IExportMetricsServiceResponse } from '../export-response';
import {
ExportMetricsServiceRequestSchema,
ExportMetricsServiceResponseSchema,
} from '../../generated/opentelemetry/proto/collector/metrics/v1/metrics_service_pb';
import { PROTOBUF_JSON_ENCODER } from '../../common/utils';

const metricsResponseType = root.opentelemetry.proto.collector.metrics.v1
.ExportMetricsServiceResponse as ExportType<IExportMetricsServiceResponse>;

const metricsRequestType = root.opentelemetry.proto.collector.metrics.v1
.ExportMetricsServiceRequest as ExportType<IExportMetricsServiceRequest>;

export const ProtobufMetricsSerializer: ISerializer<
ResourceMetrics,
IExportMetricsServiceResponse
> = {
serializeRequest: (arg: ResourceMetrics) => {
const request = createExportMetricsServiceRequest(
[arg],
PROTOBUF_JSON_ENCODER
);
const message = fromJsonString(
ExportMetricsServiceRequestSchema,
JSON.stringify(request)
);
return toBinary(ExportMetricsServiceRequestSchema, message);
const request = createExportMetricsServiceRequest([arg]);
return metricsRequestType.encode(request).finish();
},
deserializeResponse: (arg: Uint8Array) => {
const response = fromBinary(ExportMetricsServiceResponseSchema, arg);
return {
partialSuccess: response.partialSuccess
? {
rejectedDataPoints: Number(
response.partialSuccess.rejectedDataPoints
),
errorMessage: response.partialSuccess.errorMessage,
}
: undefined,
};
return metricsResponseType.decode(arg);
},
};
22 changes: 19 additions & 3 deletions experimental/packages/otlp-transformer/src/trace/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
ISpan,
} from './internal-types';
import { OtlpEncodingOptions } from '../common/internal-types';
import { getOtlpEncoder, isOtlpEncoder } from '../common/utils';
import { getOtlpEncoder } from '../common/utils';

// Span flags constants matching the OTLP specification
const SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK = 0x100;
Expand Down Expand Up @@ -107,11 +107,27 @@ export function toOtlpSpanEvent(
};
}

/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is odd, but it was indeed in the original state of the file.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rolled back the commits verbatim. This quirk was restored along with a few unused tsconfig variables.

export function createExportTraceServiceRequest(
spans: ReadableSpan[],
options?: OtlpEncodingOptions | Encoder
options?: OtlpEncodingOptions
): IExportTraceServiceRequest {
const encoder = isOtlpEncoder(options) ? options : getOtlpEncoder(options);
const encoder = getOtlpEncoder(options);
return {
resourceSpans: spanRecordsToResourceSpans(spans, encoder),
};
Expand Down
Loading