-
Notifications
You must be signed in to change notification settings - Fork 989
feat(proto): add @opentelemetry/otlp-transformer package with hand-rolled transformation #2746
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
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
67aa2f4
feat(proto): add @opentelemetry/proto package with hand-rolled proto …
dyladan b1b02df
chore: review comments
dyladan 512cba1
chore: do not export metrics for now
dyladan ee847ec
fix: use proper iteration
dyladan c647f16
chore: update submodules after compile
dyladan 158401e
Merge branch 'main' into proto-json
dyladan 7401a5c
chore: fix browser test (mismatched version)
dyladan 7f62328
chore: fix tsconfigs for new build types
dyladan 8b525cf
chore: more obvious names for intermediate types
dyladan f537dfc
Merge branch 'main' into proto-json
dyladan 0c03d86
Merge branch 'main' into proto-json
Flarna 5be5d87
Merge branch 'main' into proto-json
Flarna 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
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,2 @@ | ||
| build | ||
| src/generated |
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,8 @@ | ||
| module.exports = { | ||
| "env": { | ||
| "mocha": true, | ||
| "commonjs": true, | ||
| "shared-node-browser": true | ||
| }, | ||
| ...require('../../../eslint.config.js') | ||
| } |
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 @@ | ||
| src/generated |
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,115 @@ | ||
| # OpenTelemetry Protocol | ||
|
|
||
| [![NPM Published Version][npm-img]][npm-url] | ||
| [![Apache License][license-image]][license-image] | ||
|
|
||
| This package provides everything needed to serialize [OpenTelemetry SDK][sdk] traces and metrics into the [OpenTelemetry Protocol][otlp] format using [protocol buffers][protobuf] or JSON. | ||
| It also contains service clients for exporting traces and metrics to the OpenTelemetry Collector or a compatible receiver using using OTLP over [gRPC][grpc]. | ||
| This module uses [`protobufjs`][protobufjs] for serialization and is compatible with [`@grpc/grpc-js`][grpc-js]. | ||
|
|
||
| ## Quick Start | ||
|
|
||
| To get started you will need to install a compatible OpenTelemetry API. | ||
|
|
||
| ### Install Peer Dependencies | ||
|
|
||
| ```sh | ||
| npm install \ | ||
| @opentelemetry/api \ | ||
| @grpc/grpc-js # only required if you are using gRPC | ||
| ``` | ||
|
|
||
| ### Serialize Traces and Metrics | ||
|
|
||
| This module exports functions to serialize traces and metrics from the OpenTelemetry SDK into protocol buffers which can be sent over HTTP to the OpenTelemetry collector or a compatible receiver. | ||
|
|
||
| ```typescript | ||
| import { createExportTraceServiceRequest, createExportMetricsServiceRequest } from "@opentelemetry/otlp-transformer"; | ||
|
|
||
| const serializedSpans = createExportTraceServiceRequest(readableSpans); | ||
| const serializedMetrics = createExportMetricsServiceRequest(readableMetrics); | ||
| ``` | ||
|
|
||
| ### Create gRPC Service Clients | ||
|
|
||
| This module also contains gRPC service clients for exporting traces and metrics to an OpenTelemetry collector or compatible receiver over gRPC. | ||
| In order to avoid bundling a gRPC module with this module, it is required to construct an RPC implementation to pass to the constructor of the service clients. | ||
| Any RPC implementation compatible with `grpc` or `@grpc/grpc-js` may be used, but `@grpc/grpc-js` is recommended. | ||
|
|
||
| ```typescript | ||
| import type { RPCImpl } from 'protobufjs'; | ||
| import { makeGenericClientConstructor, credentials } from '@gprc/grpc-js'; | ||
| import { MetricServiceClient, TraceServiceClient } from "@opentelemetry/otlp-transformer"; | ||
|
|
||
| // Construct a RPC Implementation according to protobufjs docs | ||
| const GrpcClientConstructor = makeGenericClientConstructor({}); | ||
|
|
||
| const metricGRPCClient = new GrpcClientConstructor( | ||
| "http://localhost:4317/v1/metrics", // default collector metrics endpoint | ||
| credentials.createInsecure(), | ||
| ); | ||
|
|
||
| const traceGRPCClient = new GrpcClientConstructor( | ||
| "http://localhost:4317/v1/traces", // default collector traces endpoint | ||
| credentials.createInsecure(), | ||
| ); | ||
|
|
||
| const metricRpc: RPCImpl = function(method, requestData, callback) { | ||
| metricGRPCClient.makeUnaryRequest( | ||
| method.name, | ||
| arg => arg, | ||
| arg => arg, | ||
| requestData, | ||
| callback | ||
| ); | ||
| } | ||
|
|
||
| const traceRpc: RPCImpl = function(method, requestData, callback) { | ||
| traceGRPCClient.makeUnaryRequest( | ||
| method.name, | ||
| arg => arg, | ||
| arg => arg, | ||
| requestData, | ||
| callback | ||
| ); | ||
| } | ||
|
|
||
| // Construct service clients to use RPC Implementations | ||
| const metricServiceClient = new MetricServiceClient({ | ||
| rpcImpl: metricRpc, | ||
| startTime: Date.now(), // exporter start time in milliseconds | ||
| }); | ||
|
|
||
| const traceServiceClient = new TraceServiceClient({ | ||
| rpcImpl: traceRpc, | ||
| }); | ||
|
|
||
| // Export ReadableSpan[] and ReadableMetric[] over gRPC | ||
| await metricServiceClient.export(readableMetrics); | ||
| await traceServiceClient.export(readableSpans); | ||
| ``` | ||
|
|
||
| ## Useful links | ||
|
|
||
| - For more information on OpenTelemetry, visit: <https://opentelemetry.io/> | ||
| - For more about OpenTelemetry JavaScript: <https://github.com/open-telemetry/opentelemetry-js> | ||
| - For help or feedback on this project, join us in [GitHub Discussions][discussions-url] | ||
|
|
||
| ## License | ||
|
|
||
| Apache 2.0 - See [LICENSE][license-url] for more information. | ||
|
|
||
| [discussions-url]: https://github.com/open-telemetry/opentelemetry-js/discussions | ||
| [license-url]: https://github.com/open-telemetry/opentelemetry-js/blob/main/LICENSE | ||
| [license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat | ||
| [npm-url]: https://www.npmjs.com/package/@opentelemetry/otlp-transformer | ||
| [npm-img]: https://badge.fury.io/js/%40opentelemetry%otlp-transformer.svg | ||
|
|
||
| [sdk]: https://github.com/open-telemetry/opentelemetry-js | ||
| [otlp]: https://github.com/open-telemetry/opentelemetry-proto | ||
|
|
||
| [protobuf]: https://developers.google.com/protocol-buffers | ||
| [grpc]: https://grpc.io/ | ||
|
|
||
| [protobufjs]: https://www.npmjs.com/package/protobufjs | ||
| [grpc-js]: https://www.npmjs.com/package/@grpc/grpc-js |
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,24 @@ | ||
| /*! | ||
| * 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 | ||
| * | ||
| * http://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. | ||
| */ | ||
|
|
||
| const karmaWebpackConfig = require('../../../karma.webpack'); | ||
| const karmaBaseConfig = require('../../../karma.base'); | ||
|
|
||
| module.exports = (config) => { | ||
| config.set(Object.assign({}, karmaBaseConfig, { | ||
| webpack: karmaWebpackConfig | ||
| })) | ||
| }; |
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,77 @@ | ||
| { | ||
| "name": "@opentelemetry/otlp-transformer", | ||
| "private": true, | ||
| "publishConfig": { | ||
| "access": "restricted" | ||
| }, | ||
| "version": "0.27.0", | ||
| "description": "Transform OpenTelemetry SDK data into OTLP", | ||
| "module": "build/esm/index.js", | ||
| "esnext": "build/esnext/index.js", | ||
| "types": "build/src/index.d.ts", | ||
| "repository": "open-telemetry/opentelemetry-js", | ||
| "scripts": { | ||
| "compile": "tsc --build tsconfig.all.json", | ||
| "clean": "tsc --build --clean tsconfig.all.json", | ||
| "lint": "eslint . --ext .ts", | ||
| "lint:fix": "eslint . --ext .ts --fix", | ||
| "tdd": "npm run test -- --watch-extensions ts --watch", | ||
| "test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'", | ||
| "test:browser": "nyc karma start --single-run", | ||
| "watch": "tsc --build -w tsconfig.all.json" | ||
| }, | ||
| "keywords": [ | ||
| "opentelemetry", | ||
| "nodejs", | ||
| "grpc", | ||
| "protobuf", | ||
| "otlp", | ||
| "tracing", | ||
| "metrics" | ||
| ], | ||
| "author": "OpenTelemetry Authors", | ||
| "license": "Apache-2.0", | ||
| "engines": { | ||
| "node": ">=8.0.0" | ||
| }, | ||
| "files": [ | ||
| "build/src/**/*.js", | ||
| "build/src/**/*.js.map", | ||
| "build/src/**/*.d.ts", | ||
| "LICENSE", | ||
| "README.md" | ||
| ], | ||
| "peerDependencies": { | ||
| "@opentelemetry/api": ">=1.0.0 <1.1.0", | ||
| "@opentelemetry/api-metrics": "~0.27.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@opentelemetry/api": "1.0.4", | ||
| "@opentelemetry/api-metrics": "0.27.0", | ||
| "@types/mocha": "8.2.3", | ||
| "@types/webpack-env": "1.16.3", | ||
| "codecov": "3.8.3", | ||
| "istanbul-instrumenter-loader": "3.0.1", | ||
| "karma": "6.3.16", | ||
| "karma-chrome-launcher": "3.1.0", | ||
| "karma-coverage-istanbul-reporter": "3.0.3", | ||
| "karma-mocha": "2.0.1", | ||
| "karma-spec-reporter": "0.0.32", | ||
| "karma-webpack": "4.0.2", | ||
| "mkdirp": "1.0.4", | ||
| "mocha": "7.2.0", | ||
| "nyc": "15.1.0", | ||
| "protobufjs": "6.11.2", | ||
| "rimraf": "3.0.2", | ||
| "ts-loader": "8.3.0", | ||
| "ts-mocha": "8.0.0", | ||
| "typescript": "4.4.4", | ||
| "webpack": "4.46.0" | ||
| }, | ||
| "dependencies": { | ||
| "@opentelemetry/core": "1.0.1", | ||
| "@opentelemetry/resources": "1.0.1", | ||
| "@opentelemetry/sdk-metrics-base": "0.27.0", | ||
| "@opentelemetry/sdk-trace-base": "1.0.1" | ||
blumamir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
62 changes: 62 additions & 0 deletions
62
experimental/packages/otlp-transformer/src/common/internal.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,62 @@ | ||
| /* | ||
| * 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 type { SpanAttributes } from '@opentelemetry/api'; | ||
| import { IAnyValue, IKeyValue } from './types'; | ||
|
|
||
| export function toAttributes( | ||
| attributes: SpanAttributes | ||
| ): IKeyValue[] { | ||
| return Object.keys(attributes).map(key => toKeyValue(key, attributes[key])); | ||
| } | ||
|
|
||
| export function toKeyValue( | ||
| key: string, | ||
| value: unknown | ||
| ): IKeyValue { | ||
| return { | ||
| key: key, | ||
| value: toAnyValue(value), | ||
| }; | ||
| } | ||
|
|
||
| export function toAnyValue(value: unknown): IAnyValue { | ||
| const t = typeof value; | ||
| if (t === 'string') return { stringValue: value as string }; | ||
| if (t === 'number') { | ||
| if (!Number.isInteger(value)) return { doubleValue: value as number }; | ||
| return { intValue: value as number }; | ||
| } | ||
| if (t === 'boolean') return { boolValue: value as boolean }; | ||
| if (value instanceof Uint8Array) return { bytesValue: value }; | ||
| if (Array.isArray(value)) return { arrayValue: { values: value.map(toAnyValue) } }; | ||
| if (t === 'object' && value != null) return { kvlistValue: { values: Object.entries(value as object).map(([k, v]) => toKeyValue(k, v)) } }; | ||
|
|
||
| return {}; | ||
| } | ||
|
|
||
| export function hexToBuf(hex: string): Uint8Array | undefined { | ||
| const ints = hex.match(/[\da-f]{2}/gi)?.map(h => parseInt(h, 16)); | ||
| return ints && new Uint8Array(ints); | ||
| } | ||
|
|
||
| function i2hex(i: number): string { | ||
| return ('0' + i.toString(16)).slice(-2); | ||
| } | ||
|
|
||
| export function bufToHex(buf?: Uint8Array | null): string | undefined { | ||
| if (buf == null || buf.length === 0) return undefined; | ||
| return Array.from(buf).map(i2hex).join(''); | ||
| } |
69 changes: 69 additions & 0 deletions
69
experimental/packages/otlp-transformer/src/common/types.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,69 @@ | ||
| /* | ||
blumamir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * 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. | ||
| */ | ||
|
|
||
| /** Properties of an InstrumentationLibrary. */ | ||
| export interface IInstrumentationLibrary { | ||
| /** InstrumentationLibrary name */ | ||
| name: string | ||
|
|
||
| /** InstrumentationLibrary version */ | ||
| version?: string; | ||
| } | ||
|
|
||
| /** Properties of a KeyValue. */ | ||
| export interface IKeyValue { | ||
| /** KeyValue key */ | ||
| key: string; | ||
|
|
||
| /** KeyValue value */ | ||
| value: IAnyValue; | ||
| } | ||
|
|
||
| /** Properties of an AnyValue. */ | ||
| export interface IAnyValue { | ||
| /** AnyValue stringValue */ | ||
| stringValue?: (string | null); | ||
|
|
||
| /** AnyValue boolValue */ | ||
| boolValue?: (boolean | null); | ||
|
|
||
| /** AnyValue intValue */ | ||
| intValue?: (number | Long | null); | ||
|
|
||
| /** AnyValue doubleValue */ | ||
| doubleValue?: (number | null); | ||
|
|
||
| /** AnyValue arrayValue */ | ||
| arrayValue?: IArrayValue; | ||
|
|
||
| /** AnyValue kvlistValue */ | ||
| kvlistValue?: IKeyValueList; | ||
|
|
||
| /** AnyValue bytesValue */ | ||
| bytesValue?: Uint8Array; | ||
| } | ||
|
|
||
| /** Properties of an ArrayValue. */ | ||
| export interface IArrayValue { | ||
| /** ArrayValue values */ | ||
| values: IAnyValue[]; | ||
| } | ||
|
|
||
| /** Properties of a KeyValueList. */ | ||
| export interface IKeyValueList { | ||
| /** KeyValueList values */ | ||
| values: IKeyValue[]; | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.