Skip to content

feat(exporter-metrics-otlp-proto)!: rewrite exporter #4415

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

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
8 changes: 7 additions & 1 deletion examples/opentelemetry-web/docker/collector-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ receivers:
allowed_origins:
- http://*
- https://*
allowed_headers: ["*"]

exporters:
zipkin:
endpoint: "http://zipkin-all-in-one:9411/api/v2/spans"
prometheus:
endpoint: "0.0.0.0:9464"
debug:
verbosity: detailed
sampling_initial: 1
sampling_thereafter: 1


processors:
batch:
Expand All @@ -28,5 +34,5 @@ service:
processors: [batch]
metrics:
receivers: [otlp]
exporters: [prometheus]
exporters: [prometheus, debug]
processors: [batch]
2 changes: 1 addition & 1 deletion examples/opentelemetry-web/docker/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: "3"
services:
# Collector
collector:
image: otel/opentelemetry-collector-contrib:0.53.0
image: otel/opentelemetry-collector:0.91.0
# image: otel/opentelemetry-collector-contrib:latest
command: ["--config=/conf/collector-config.yaml"]
volumes:
Expand Down
24 changes: 24 additions & 0 deletions examples/opentelemetry-web/examples/metrics-proto/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>Metrics Example</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
Example of using metrics with Collector Exporter
<script type="text/javascript" src="metrics-proto.js"></script>
<br/>
<button id="startBtn">Start metrics</button>
<button id="stopBtn">Stop metrics</button>
<br/>

If you run the collector from this example, you should see metrics at: <br/>
<a href="http://localhost:9090/graph?g0.range_input=1m&g0.expr=requests&g0.tab=0/" target="_blank">http://localhost:9090/</a>

</body>

</html>
56 changes: 56 additions & 0 deletions examples/opentelemetry-web/examples/metrics-proto/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const { DiagConsoleLogger, DiagLogLevel, diag, metrics } = require('@opentelemetry/api');
const { createMetricsExporter } = require('@opentelemetry/exporter-metrics-otlp-proto');
const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');

// Optional and only needed to see the internal diagnostic logging (during development)
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);

let interval;
let meter;

function stopMetrics() {
console.log('STOPPING METRICS proto');
clearInterval(interval);
metrics.getMeterProvider().shutdown()
.then(() => metrics.disable());
}

function startMetrics() {
console.log('STARTING METRICS');

const meterProvider = new MeterProvider();

meterProvider.addMetricReader(new PeriodicExportingMetricReader({
exporter: createMetricsExporter({}),
exportIntervalMillis: 1000
}));

metrics.setGlobalMeterProvider(meterProvider);

meter = metrics.getMeter('example-exporter-collector')

const requestCounter = meter.createCounter('requests', {
description: 'Example of a Counter',
});

const upDownCounter = meter.createUpDownCounter('test_up_down_counter', {
description: 'Example of a UpDownCounter',
});

const attributes = { environment: 'staging' };

interval = setInterval(() => {
requestCounter.add(1, attributes);
upDownCounter.add(Math.random() > 0.5 ? 1 : -1, attributes);
}, 1000);
}

const addClickEvents = () => {
const startBtn = document.getElementById('startBtn');

const stopBtn = document.getElementById('stopBtn');
startBtn.addEventListener('click', startMetrics);
stopBtn.addEventListener('click', stopMetrics);
};

window.addEventListener('load', addClickEvents);
7 changes: 4 additions & 3 deletions examples/opentelemetry-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"start-nc": "webpack serve --progress --color --port 8090 --config webpack.dev.config.js --hot --host 0.0.0.0 --no-compress",
"start-prod": "webpack serve --progress --color --port 8090 --config webpack.prod.config.js --hot --host 0.0.0.0 --compress",
"start-prodnc": "webpack serve --progress --color --port 8090 --config webpack.prod.config.js --hot --host 0.0.0.0 --no-compress",
"docker:start": "cd ./docker && docker-compose down && docker-compose up",
"docker:startd": "cd ./docker && docker-compose down && docker-compose up -d",
"docker:stop": "cd ./docker && docker-compose down"
"docker:start": "cd ./docker && docker compose down && docker compose up",
"docker:startd": "cd ./docker && docker compose down && docker compose up -d",
"docker:stop": "cd ./docker && docker compose down"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -47,6 +47,7 @@
"@opentelemetry/context-zone": "1.21.0",
"@opentelemetry/core": "1.21.0",
"@opentelemetry/exporter-metrics-otlp-http": "0.48.0",
"@opentelemetry/exporter-metrics-otlp-proto": "0.48.0",
"@opentelemetry/exporter-trace-otlp-http": "0.48.0",
"@opentelemetry/exporter-trace-otlp-proto": "0.48.0",
"@opentelemetry/exporter-zipkin": "1.21.0",
Expand Down
1 change: 1 addition & 0 deletions examples/opentelemetry-web/webpack.dev.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const common = {
mode: 'development',
entry: {
metrics: 'examples/metrics/index.js',
'metrics-proto': 'examples/metrics-proto/index.js',
fetch: 'examples/fetch/index.js',
'xml-http-request': 'examples/xml-http-request/index.js',
fetchXhr: 'examples/fetchXhr/index.js',
Expand Down
1 change: 1 addition & 0 deletions examples/opentelemetry-web/webpack.prod.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const common = {
mode: 'production',
entry: {
metrics: 'examples/metrics/index.js',
'metrics-proto': 'examples/metrics-proto/index.js',
fetch: 'examples/fetch/index.js',
'xml-http-request': 'examples/xml-http-request/index.js',
fetchXhr: 'examples/fetchXhr/index.js',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
"esnext": "build/esnext/index.js",
"types": "build/src/index.d.ts",
"repository": "open-telemetry/opentelemetry-js",
"browser": {
"./src/platform/index.ts": "./src/platform/browser/index.ts",
"./build/esm/platform/index.js": "./build/esm/platform/browser/index.js",
"./build/esnext/platform/index.js": "./build/esnext/platform/browser/index.js",
"./build/src/platform/index.js": "./build/src/platform/browser/index.js"
},
"scripts": {
"prepublishOnly": "npm run compile",
"compile": "tsc --build tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
Expand Down Expand Up @@ -55,6 +61,7 @@
"access": "public"
},
"devDependencies": {
"@babel/core": "7.23.3",
"@opentelemetry/api": "1.7.0",
"@types/mocha": "10.0.6",
"@types/node": "18.6.5",
Expand All @@ -77,6 +84,10 @@
"@opentelemetry/core": "1.21.0",
"@opentelemetry/exporter-metrics-otlp-http": "0.48.0",
"@opentelemetry/otlp-exporter-base": "0.48.0",
"@opentelemetry/otlp-metrics-exporter-base": "0.48.0",
"@opentelemetry/otlp-http-exporter-base": "0.48.0",
"@opentelemetry/otlp-http-exporter-node-base": "0.48.0",
"@opentelemetry/otlp-http-exporter-browser-base": "0.48.0",
"@opentelemetry/otlp-proto-exporter-base": "0.48.0",
"@opentelemetry/otlp-transformer": "0.48.0",
"@opentelemetry/resources": "1.21.0",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@
* limitations under the License.
*/

export * from './OTLPMetricExporter';
// Legacy exporter kept for compatibility, scheduled for removal in 2.0
export { OTLPMetricExporter } from './legacy/OTLPMetricExporter';

export {
// New exporter factory function and config.
createMetricsExporter,
OtlpHttpProtoMetricsConfiguration,
// Scheduled for removal in 2.0
LegacyConfig,
} from './platform';
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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 {
ExportMetricsServiceRequest,
ExportMetricsServiceResponse,
IExportMetricsServiceRequest as ProtobufServiceRequest,
} from '@opentelemetry/otlp-proto-exporter-base';
import {
IExportMetricsServiceRequest,
IExportMetricsServiceResponse,
} from '@opentelemetry/otlp-transformer';
import { IMetricsSerializer } from '@opentelemetry/otlp-metrics-exporter-base';

export function createProtobufMetricsSerializer(): IMetricsSerializer {
return {
serializeRequest,
deserializeResponse,
};
}

function serializeRequest(
request: IExportMetricsServiceRequest
): Uint8Array | undefined {
const message = ExportMetricsServiceRequest.create(
request as ProtobufServiceRequest
);
if (message) {
return ExportMetricsServiceRequest.encode(message).finish();
}
return undefined;
}

function deserializeResponse(data: Uint8Array): IExportMetricsServiceResponse {
return ExportMetricsServiceResponse.decode(
data
) as IExportMetricsServiceResponse;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 { ResourceMetrics } from '@opentelemetry/sdk-metrics';
import { ITransformer } from '@opentelemetry/otlp-exporter-base';
import {
createExportMetricsServiceRequest,
IExportMetricsServiceRequest,
} from '@opentelemetry/otlp-transformer';

export function createProtobufMetricsTransformer(): ITransformer<
ResourceMetrics,
IExportMetricsServiceRequest
> {
return {
transform: (metrics: ResourceMetrics) => {
return createExportMetricsServiceRequest([metrics]);
},
};
}
Loading