Skip to content
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

refactor(otlp-transformer): json schema based validation #5265

Closed
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
2 changes: 1 addition & 1 deletion api/test/common/diag/logLevel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('LogLevelFilter DiagLogger', () => {

const levelMap: Array<{
message: string;
level: DiagLogLevel;
level: number | DiagLogLevel;
Copy link
Author

Choose a reason for hiding this comment

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

This change is because this test is purposely using invalid enum values to check that the function handles them correctly. Typescript v4 didn't complain about invalid values, but Typescript v5 did so I added number to explicitly tell it we're using invalid numbers

ignoreFuncs: Array<keyof DiagLogger>;
}> = [
{ message: 'ALL', level: DiagLogLevel.ALL, ignoreFuncs: [] },
Expand Down
4 changes: 4 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ All notable changes to experimental packages in this project will be documented

### :boom: Breaking Change

* feat(otlp-transformer)!: add new entrypoints for non-core features [#5259](https://github.com/open-telemetry/opentelemetry-js/pull/5259/)
Copy link
Author

Choose a reason for hiding this comment

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

Note: this PR is based on #5259 to avoid ugly merge work once their PR is merged. It makes the diff unfortunately ugly until their PR is merged though

* (user-facing): OTLP (binary protobuf) utilities now located at `@opentelemetry/otlp-transformer/proto`
* (user-facing): OTLP (json) utilities now located at `@opentelemetry/otlp-transformer/json`
* (internal): features to remain experimental post-stabilization now located at `@opentelemetry/otlp-transformer/experimental` (empty for now)
* feat(otlp-exporter-base)!: collapse base classes into one [#5031](https://github.com/open-telemetry/opentelemetry-js/pull/5031) @pichlermarc
* `OTLPExporterNodeBase` has been removed in favor of a platform-agnostic implementation (`OTLPExporterBase`)
* `OTLPExporterBrowserBase` has been removed in favor of a platform-agnostic implementation (`OTLPExporterBase`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
createOtlpGrpcExportDelegate,
OTLPGRPCExporterConfigNode,
} from '@opentelemetry/otlp-grpc-exporter-base';
import { ProtobufLogsSerializer } from '@opentelemetry/otlp-transformer';
import { ProtobufLogsSerializer } from '@opentelemetry/otlp-transformer/protobuf';
import { OTLPExporterBase } from '@opentelemetry/otlp-exporter-base';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type {
} from '@opentelemetry/sdk-logs';
import type { OTLPExporterConfigBase } from '@opentelemetry/otlp-exporter-base';
import { OTLPExporterBase } from '@opentelemetry/otlp-exporter-base';
import { JsonLogsSerializer } from '@opentelemetry/otlp-transformer';
import { JsonLogsSerializer } from '@opentelemetry/otlp-transformer/json';
import { createLegacyOtlpBrowserExportDelegate } from '@opentelemetry/otlp-exporter-base/browser-http';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type {
} from '@opentelemetry/sdk-logs';
import type { OTLPExporterNodeConfigBase } from '@opentelemetry/otlp-exporter-base';
import { OTLPExporterBase } from '@opentelemetry/otlp-exporter-base';
import { JsonLogsSerializer } from '@opentelemetry/otlp-transformer';
import { JsonLogsSerializer } from '@opentelemetry/otlp-transformer/json';
import { VERSION } from '../../version';
import {
convertLegacyHttpOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
OTLPExporterConfigBase,
OTLPExporterBase,
} from '@opentelemetry/otlp-exporter-base';
import { ProtobufLogsSerializer } from '@opentelemetry/otlp-transformer';
import { ProtobufLogsSerializer } from '@opentelemetry/otlp-transformer/protobuf';

import { ReadableLogRecord, LogRecordExporter } from '@opentelemetry/sdk-logs';
import { createLegacyOtlpBrowserExportDelegate } from '@opentelemetry/otlp-exporter-base/browser-http';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
OTLPExporterBase,
OTLPExporterNodeConfigBase,
} from '@opentelemetry/otlp-exporter-base';
import { ProtobufLogsSerializer } from '@opentelemetry/otlp-transformer';
import { ProtobufLogsSerializer } from '@opentelemetry/otlp-transformer/protobuf';
import {
convertLegacyHttpOptions,
createOtlpHttpExportDelegate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
createOtlpGrpcExportDelegate,
OTLPGRPCExporterConfigNode,
} from '@opentelemetry/otlp-grpc-exporter-base';
import { ProtobufTraceSerializer } from '@opentelemetry/otlp-transformer';
import { ProtobufTraceSerializer } from '@opentelemetry/otlp-transformer/protobuf';
import { OTLPExporterBase } from '@opentelemetry/otlp-exporter-base';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
OTLPExporterConfigBase,
OTLPExporterBase,
} from '@opentelemetry/otlp-exporter-base';
import { JsonTraceSerializer } from '@opentelemetry/otlp-transformer';
import { JsonTraceSerializer } from '@opentelemetry/otlp-transformer/json';
import { createLegacyOtlpBrowserExportDelegate } from '@opentelemetry/otlp-exporter-base/browser-http';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
OTLPExporterBase,
} from '@opentelemetry/otlp-exporter-base';
import { VERSION } from '../../version';
import { JsonTraceSerializer } from '@opentelemetry/otlp-transformer';
import { JsonTraceSerializer } from '@opentelemetry/otlp-transformer/json';
import {
convertLegacyHttpOptions,
createOtlpHttpExportDelegate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
OTLPExporterConfigBase,
OTLPExporterBase,
} from '@opentelemetry/otlp-exporter-base';
import { ProtobufTraceSerializer } from '@opentelemetry/otlp-transformer';
import { ProtobufTraceSerializer } from '@opentelemetry/otlp-transformer/protobuf';
import { createLegacyOtlpBrowserExportDelegate } from '@opentelemetry/otlp-exporter-base/browser-http';

const DEFAULT_COLLECTOR_RESOURCE_PATH = 'v1/traces';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
OTLPExporterNodeConfigBase,
OTLPExporterBase,
} from '@opentelemetry/otlp-exporter-base';
import { ProtobufTraceSerializer } from '@opentelemetry/otlp-transformer';
import { ProtobufTraceSerializer } from '@opentelemetry/otlp-transformer/protobuf';
import { VERSION } from '../../version';
import {
createOtlpHttpExportDelegate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
createOtlpGrpcExportDelegate,
OTLPGRPCExporterConfigNode,
} from '@opentelemetry/otlp-grpc-exporter-base';
import { ProtobufMetricsSerializer } from '@opentelemetry/otlp-transformer';
import { ProtobufMetricsSerializer } from '@opentelemetry/otlp-transformer/protobuf';

/**
* OTLP-gRPC metric exporter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { OTLPMetricExporterOptions } from '../../OTLPMetricExporterOptions';
import { OTLPMetricExporterBase } from '../../OTLPMetricExporterBase';
import { OTLPExporterConfigBase } from '@opentelemetry/otlp-exporter-base';
import { JsonMetricsSerializer } from '@opentelemetry/otlp-transformer';
import { JsonMetricsSerializer } from '@opentelemetry/otlp-transformer/json';
import { createLegacyOtlpBrowserExportDelegate } from '@opentelemetry/otlp-exporter-base/browser-http';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { OTLPMetricExporterOptions } from '../../OTLPMetricExporterOptions';
import { OTLPMetricExporterBase } from '../../OTLPMetricExporterBase';
import { OTLPExporterNodeConfigBase } from '@opentelemetry/otlp-exporter-base';
import { JsonMetricsSerializer } from '@opentelemetry/otlp-transformer';
import { JsonMetricsSerializer } from '@opentelemetry/otlp-transformer/json';
import { VERSION } from '../../version';
import {
convertLegacyHttpOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { OTLPMetricExporterOptions } from '@opentelemetry/exporter-metrics-otlp-http';
import { OTLPMetricExporterBase } from '@opentelemetry/exporter-metrics-otlp-http';
import { OTLPExporterNodeConfigBase } from '@opentelemetry/otlp-exporter-base';
import { ProtobufMetricsSerializer } from '@opentelemetry/otlp-transformer';
import { ProtobufMetricsSerializer } from '@opentelemetry/otlp-transformer/protobuf';
import { VERSION } from './version';
import {
convertLegacyHttpOptions,
Expand Down
83 changes: 79 additions & 4 deletions experimental/packages/otlp-transformer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ npm install @opentelemetry/api

### Serialize Traces/Metrics/Logs

This module exports functions to serialize traces, metrics and logs from the OpenTelemetry SDK into protocol buffers which can be sent over HTTP to the OpenTelemetry collector or a compatible receiver.
This package exports functions to serialize traces, metrics and logs from the OpenTelemetry SDK into OTLP JSON format, which can be sent over HTTP to the OpenTelemetry collector or a compatible receiver.

```typescript
import {
Expand All @@ -30,11 +30,86 @@ import {
createExportLogsServiceRequest,
} from '@opentelemetry/otlp-transformer';

const serializedSpans = createExportTraceServiceRequest(readableSpans);
const serializedMetrics = createExportMetricsServiceRequest(readableMetrics);
const serializedLogs = createExportLogsServiceRequest(readableLogRecords);
const serializableSpans = createExportTraceServiceRequest(readableSpans);
const serializableMetrics = createExportMetricsServiceRequest(readableMetrics);
const serializableLogs = createExportLogsServiceRequest(readableLogRecords);
```

Note that these functions return JSON-serializable objects (i.e. JavaScript objects that can be passed into `JSON.stringify(...)`) adhering to the OTLP JSON format, not the serialized JSON-text payload themselves.

### Protobuf (Binary) Serializers

Additionally, this package also exports serializers to serialize traces, metrics and logs into the export requests using the OTLP binary protobuf format, as well as deserializing the corresponding binary response payloads.

```typescript
import {
ProtobufTraceSerializer,
ProtobufMetricsSerializer,
ProtobufLogsSerializer,
} from '@opentelemetry/otlp-transformer/protobuf';

const serializedSpans: Uint8Array =
ProtobufTraceSerializer.serializeRequest(readableSpans);
const serializedMetrics: Uint8Array =
ProtobufMetricsSerializer.serializeRequest(readableMetrics);
const serializedLogs: Uint8Array =
ProtobufLogsSerializer.serializeRequest(readableLogRecords);

// ...

const deserializedTraceResponse = ProtobufTraceSerializer.deserializeResponse(
serializedTraceResponse /* Uint8Array */
);
const deserializedMetricsResponse =
ProtobufMetricsSerializer.deserializeResponse(
serializedTraceResponse /* Uint8Array */
);
const deserializedLogsResponse = ProtobufLogsSerializer.deserializeResponse(
serializedTraceResponse /* Uint8Array */
);
```

### JSON Serializers

For feature parity, this package also exports JSON serializers with the same interfaces:

```typescript
import {
JsonTraceSerializer,
JsonMetricsSerializer,
JsonLogsSerializer,
} from '@opentelemetry/otlp-transformer/json';

const serializedSpans: Uint8Array =
JsonTraceSerializer.serializeRequest(readableSpans);
const serializedMetrics: Uint8Array =
JsonMetricsSerializer.serializeRequest(readableMetrics);
const serializedLogs: Uint8Array =
JsonLogsSerializer.serializeRequest(readableLogRecords);

// ...

const deserializedTraceResponse = JsonTraceSerializer.deserializeResponse(
serializedTraceResponse /* Uint8Array */
);
const deserializedMetricsResponse = JsonMetricsSerializer.deserializeResponse(
serializedTraceResponse /* Uint8Array */
);
const deserializedLogsResponse = JsonLogsSerializer.deserializeResponse(
serializedTraceResponse /* Uint8Array */
);
```

Unlike the `createExport*Request` functions (and similar to the binary protobuf serializers), these serializer methods return the _serialized_ bytes directly, skipping the need to further serialize them with `JSON.stringify(...)`.

Among other things, the `Uint8Array` can be used directly as the `body` of a `fetch()` request. Likewise, a `Uint8Array` of the response body can be obtained from `await response.bytes()`.

### Experimental Features

As we iterate towards the stabilization of this package, certain features are expected to remain in experimental status. These features are subject to changes and breakages between minor versions of the package, even after the package itself reaches version `1.0`.

These features are exported from the `@opentelemetry/otlp-transformer/experimental` entrypoint. Currently, this entrypoint is empty, as the entire package is considered experimental at the moment. It is expected to be utilized as part of upcoming stabilization efforts.

## Useful links

- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
Expand Down
49 changes: 44 additions & 5 deletions experimental/packages/otlp-transformer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,48 @@
},
"version": "0.56.0",
"description": "Transform OpenTelemetry SDK data into OTLP",
"module": "build/esm/index.js",
"esnext": "build/esnext/index.js",
"types": "build/src/index.d.ts",
"main": "build/src/index.js",
"exports": {
Copy link
Author

Choose a reason for hiding this comment

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

Another alternative to this PR could be to modify this to expose the generated code: https://github.com/open-telemetry/opentelemetry-js/pull/5259/files#r1884673988

".": {
"module": "./build/esm/index.js",
"esnext": "./build/esnext/index.js",
"types": "./build/src/index.d.ts",
"default": "./build/src/index.js"
},
"./experimental": {
"module": "./build/esm/experimental/index.js",
"esnext": "./build/esnext/experimental/index.js",
"types": "./build/src/experimental/index.d.ts",
"default": "./build/src/experimental/index.js"
},
"./json": {
"module": "./build/esm/json/index.js",
"esnext": "./build/esnext/json/index.js",
"types": "./build/src/json/index.d.ts",
"default": "./build/src/json/index.js"
},
"./protobuf": {
"module": "./build/esm/protobuf/index.js",
"esnext": "./build/esnext/protobuf/index.js",
"types": "./build/src/protobuf/index.d.ts",
"default": "./build/src/protobuf/index.js"
}
},
"typesVersions": {
"*": {
"*": [
"./build/src/index.d.ts"
],
"experimental": [
"./build/src/experimental/index.d.ts"
],
"json": [
"./build/src/json/index.d.ts"
],
"protobuf": [
"./build/src/protobuf/index.d.ts"
]
}
},
"repository": "open-telemetry/opentelemetry-js",
"scripts": {
"prepublishOnly": "npm run compile",
Expand Down Expand Up @@ -77,10 +115,11 @@
"nyc": "15.1.0",
"protobufjs-cli": "1.1.3",
"ts-loader": "9.5.1",
"typescript": "4.4.4",
"typescript": "5.7.2",
Copy link
Author

Choose a reason for hiding this comment

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

I had to upgrade to Typescript v5 for typebox to work properly. Since this PR is targeting the next branch which is meant to allow newer typescript versions, I assume this isn't an issue (although I'm not sure which specific typescript v5 version you want to use)

"webpack": "5.96.1"
},
"dependencies": {
"@sinclair/typebox": "0.34.11",
"@opentelemetry/api-logs": "0.56.0",
"@opentelemetry/core": "1.29.0",
"@opentelemetry/resources": "1.29.0",
Expand Down
Loading