Skip to content

Commit

Permalink
feat(exporters): update proto version and use otlp-transformer (#2929)
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc authored May 4, 2022
1 parent 65aeff0 commit 1343d16
Show file tree
Hide file tree
Showing 76 changed files with 800 additions and 2,234 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,13 @@ These instrumentations are hosted at <https://github.com/open-telemetry/opentele

## Upgrade guidelines

### 0.28.x to 0.29.x

- `@opentelemetry/exporter-trace-otlp-http` is now exporting `scopeSpans` instead of `instrumentationLibrarySpans`
- this exporter now requires collector version `0.48` and up.
- `@opentelemetry/exporter-metrics-otlp-http` is now exporting `scopeMetrics` instead of `instrumentationLibraryMetrics`
- this exporter now requires collector version `0.48` and up.

### 0.27.x to 0.28.x

- In `@opentelemetry/exporter-trace-otlp-http`, `OTLPExporterBase._isShutdown` is replaced with `_shutdownOnce`.
Expand Down
9 changes: 4 additions & 5 deletions examples/otlp-exporter-node/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Overview

<!-- TODO: This example needs an update. -->

This example shows how to use [@opentelemetry/exporter-otlp-http](https://github.com/open-telemetry/opentelemetry-js/tree/v0.26.0/experimental/packages/opentelemetry-exporter-otlp-http) to instrument a simple Node.js application.

This example will export spans data simultaneously using [Exporter Collector](https://github.com/open-telemetry/opentelemetry-js/tree/v0.26.0/experimental/packages/opentelemetry-exporter-otlp-http) and grpc. It will use [proto format](https://github.com/open-telemetry/opentelemetry-proto).
This example shows how to use
[@opentelemetry/exporter-trace-otlp-http](https://github.com/open-telemetry/opentelemetry-js/tree/v0.28.0/experimental/packages/exporter-trace-otlp-http)
and [@opentelemetry/exporter-metrics-otlp-http](https://github.com/open-telemetry/opentelemetry-js/tree/v0.28.0/experimental/packages/opentelemetry-exporter-metrics-otlp-http)
to instrument a simple Node.js application.

## Installation

Expand Down
2 changes: 1 addition & 1 deletion examples/otlp-exporter-node/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.42.0
image: otel/opentelemetry-collector-contrib:0.50.0
# image: otel/opentelemetry-collector-contrib:latest
command: ["--config=/conf/collector-config.yaml"]
volumes:
Expand Down
31 changes: 17 additions & 14 deletions examples/otlp-exporter-node/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@

const { DiagConsoleLogger, DiagLogLevel, diag } = require('@opentelemetry/api');
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-http');
// const { OTLPMetricExporter } = require('@opentelemetry/exporter-otlp-grpc');
// const { OTLPMetricExporter } = require('@opentelemetry/exporter-otlp-proto');
const { MeterProvider } = require('@opentelemetry/sdk-metrics-base');
// const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-grpc');
// const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-proto');
const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics-base');
const { Resource } = require('@opentelemetry/resources');
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');

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

const metricExporter = new OTLPMetricExporter({
url: 'http://localhost:4318/v1/metrics',
});
const metricExporter = new OTLPMetricExporter({});

const meter = new MeterProvider({
exporter: metricExporter,
interval: 1000,
const meterProvider = new MeterProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'basic-metric-service',
}),
}).getMeter('example-exporter-collector');
});

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

const meter = meterProvider.getMeter('example-exporter-collector');

const requestCounter = meter.createCounter('requests', {
description: 'Example of a Counter',
Expand All @@ -35,10 +38,10 @@ const histogram = meter.createHistogram('test_histogram', {
description: 'Example of a Histogram',
});

const labels = { pid: process.pid, environment: 'staging' };
const attributes = { pid: process.pid, environment: 'staging' };

setInterval(() => {
requestCounter.add(1, labels);
upDownCounter.add(Math.random() > 0.5 ? 1 : -1, labels);
histogram.record(Math.random(), labels);
requestCounter.add(1, attributes);
upDownCounter.add(Math.random() > 0.5 ? 1 : -1, attributes);
histogram.record(Math.random(), attributes);
}, 1000);
23 changes: 12 additions & 11 deletions examples/otlp-exporter-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@
},
"dependencies": {
"@opentelemetry/api": "^1.0.2",
"@opentelemetry/core": "1.0.1",
"@opentelemetry/exporter-trace-otlp-http": "0.27.0",
"@opentelemetry/exporter-metrics-otlp-http": "0.27.0",
"@opentelemetry/exporter-trace-otlp-grpc": "0.27.0",
"@opentelemetry/exporter-metrics-otlp-grpc": "0.27.0",
"@opentelemetry/exporter-trace-otlp-proto": "0.27.0",
"@opentelemetry/exporter-metrics-otlp-proto": "0.27.0",
"@opentelemetry/resources": "1.0.1",
"@opentelemetry/semantic-conventions": "1.0.1",
"@opentelemetry/sdk-metrics-base": "0.27.0",
"@opentelemetry/sdk-trace-base": "1.0.1"
"@opentelemetry/api-metrics": "0.28.0",
"@opentelemetry/core": "1.1.1",
"@opentelemetry/exporter-trace-otlp-http": "0.28.0",
"@opentelemetry/exporter-metrics-otlp-http": "0.28.0",
"@opentelemetry/exporter-trace-otlp-grpc": "0.28.0",
"@opentelemetry/exporter-metrics-otlp-grpc": "0.28.0",
"@opentelemetry/exporter-trace-otlp-proto": "0.28.0",
"@opentelemetry/exporter-metrics-otlp-proto": "0.28.0",
"@opentelemetry/resources": "1.1.1",
"@opentelemetry/semantic-conventions": "1.1.1",
"@opentelemetry/sdk-metrics-base": "0.28.0",
"@opentelemetry/sdk-trace-base": "1.1.1"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js#readme"
}
23 changes: 13 additions & 10 deletions examples/otlp-exporter-node/tracing.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
'use strict';

const opentelemetry = require('@opentelemetry/api');
const { BasicTracerProvider, ConsoleSpanExporter, SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http');
const { Resource } = require('@opentelemetry/resources');
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
// const { OTLPTraceExporter } = require('@opentelemetry/exporter-otlp-grpc');
// const { OTLPTraceExporter } = require('@opentelemetry/exporter-otlp-proto');
const {
diag,
trace,
context,
DiagConsoleLogger,
DiagLogLevel,
} = require('@opentelemetry/api');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http');
// const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc');
// const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-proto');

// opentelemetry.diag.setLogger(
// new opentelemetry.DiagConsoleLogger(),
// opentelemetry.DiagLogLevel.DEBUG,
// );
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);

const exporter = new OTLPTraceExporter({
// headers: {
Expand All @@ -28,7 +31,7 @@ provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.register();

const tracer = opentelemetry.trace.getTracer('example-otlp-exporter-node');
const tracer = trace.getTracer('example-otlp-exporter-node');

// Create a span. A span must be closed.
const parentSpan = tracer.startSpan('main');
Expand All @@ -47,7 +50,7 @@ setTimeout(() => {
function doWork(parent) {
// Start another span. In this example, the main method already started a
// span, so that'll be the parent span, and this will be a child span.
const ctx = opentelemetry.trace.setSpan(opentelemetry.context.active(), parent);
const ctx = trace.setSpan(context.active(), parent);
const span = tracer.startSpan('doWork', undefined, ctx);

// simulate some random work.
Expand Down
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ All notable changes to experimental packages in this project will be documented

### :rocket: (Enhancement)

* feat(exporters): update proto version and use otlp-transformer #2929 @pichlermarc

### :bug: (Bug Fix)

### :books: (Refine Doc)
Expand Down
8 changes: 4 additions & 4 deletions experimental/packages/exporter-trace-otlp-grpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
[![NPM Published Version][npm-img]][npm-url]
[![Apache License][license-image]][license-image]

This module provides exporter for web and node to be used with [opentelemetry-collector][opentelemetry-collector-url] - last tested with version **0.25.0**.
This module provides exporter for web and node to be used with [opentelemetry-collector][opentelemetry-collector-url].
Compatible with [opentelemetry-collector][opentelemetry-collector-url] versions `>=0.16 <=0.50`.

## Installation

Expand Down Expand Up @@ -135,9 +136,8 @@ OTEL_EXPORTER_OTLP_TRACES_COMPRESSION=gzip
## Running opentelemetry-collector locally to see the traces

1. Go to examples/otlp-exporter-node
2. run `npm run docker:start`
3. Open page at `http://localhost:9411/zipkin/` to observe the traces
1. Go to `examples/otlp-exporter-node`
2. Follow the instructions there to inspect traces.

## Useful links

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"@opentelemetry/core": "1.2.0",
"@grpc/grpc-js": "^1.5.9",
"@grpc/proto-loader": "^0.6.9",
"@opentelemetry/exporter-trace-otlp-http": "0.28.0",
"@opentelemetry/otlp-transformer": "0.28.0",
"@opentelemetry/otlp-grpc-exporter-base": "0.28.0",
"@opentelemetry/resources": "1.2.0",
"@opentelemetry/sdk-trace-base": "1.2.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
*/

import { ReadableSpan, SpanExporter } from '@opentelemetry/sdk-trace-base';
import {
otlpTypes,
toOTLPExportTraceServiceRequest,
} from '@opentelemetry/exporter-trace-otlp-http';
import { baggageUtils, getEnv } from '@opentelemetry/core';
import { Metadata } from '@grpc/grpc-js';
import {
Expand All @@ -27,6 +23,7 @@ import {
ServiceClientType,
validateAndNormalizeUrl
} from '@opentelemetry/otlp-grpc-exporter-base';
import { createExportTraceServiceRequest, IExportTraceServiceRequest } from '@opentelemetry/otlp-transformer';

const DEFAULT_COLLECTOR_URL = 'localhost:4317';

Expand All @@ -35,7 +32,7 @@ const DEFAULT_COLLECTOR_URL = 'localhost:4317';
*/
export class OTLPTraceExporter
extends OTLPGRPCExporterNodeBase<ReadableSpan,
otlpTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest>
IExportTraceServiceRequest>
implements SpanExporter {

constructor(config: OTLPGRPCExporterConfigNode = {}) {
Expand All @@ -47,10 +44,8 @@ export class OTLPTraceExporter
}
}

convert(
spans: ReadableSpan[]
): otlpTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest {
return toOTLPExportTraceServiceRequest(spans, this);
convert(spans: ReadableSpan[]): IExportTraceServiceRequest {
return createExportTraceServiceRequest(spans);
}

getDefaultUrl(config: OTLPGRPCExporterConfigNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

import * as protoLoader from '@grpc/proto-loader';
import { otlpTypes } from '@opentelemetry/exporter-trace-otlp-http';
import { diag } from '@opentelemetry/api';
import {
BasicTracerProvider,
Expand All @@ -37,6 +36,7 @@ import {
} from './traceHelper';
import { CompressionAlgorithm } from '@opentelemetry/otlp-exporter-base';
import { GrpcCompressionAlgorithm } from '@opentelemetry/otlp-grpc-exporter-base';
import { IExportTraceServiceRequest, IResourceSpans } from '@opentelemetry/otlp-transformer';

const traceServiceProtoPath =
'opentelemetry/proto/collector/trace/v1/trace_service.proto';
Expand All @@ -59,7 +59,7 @@ const testCollectorExporter = (params: TestParams) =>
let collectorExporter: OTLPTraceExporter;
let server: grpc.Server;
let exportedData:
| otlpTypes.opentelemetryProto.trace.v1.ResourceSpans
| IResourceSpans
| undefined;
let reqMetadata: grpc.Metadata | undefined;

Expand All @@ -83,15 +83,13 @@ const testCollectorExporter = (params: TestParams) =>
.service,
{
Export: (data: {
request: otlpTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest;
request: IExportTraceServiceRequest;
metadata: grpc.Metadata;
}) => {
try {
if (data.request.resourceSpans != null) {
exportedData = data.request.resourceSpans[0];
reqMetadata = data.metadata;
} catch (e) {
exportedData = undefined;
}
reqMetadata = data.metadata;
},
}
);
Expand Down Expand Up @@ -178,24 +176,26 @@ const testCollectorExporter = (params: TestParams) =>
typeof exportedData !== 'undefined',
'resource' + " doesn't exist"
);
let spans;
let resource;
if (exportedData) {
spans = exportedData.instrumentationLibrarySpans[0].spans;
resource = exportedData.resource;
ensureExportedSpanIsCorrect(spans[0]);

assert.ok(
typeof resource !== 'undefined',
"resource doesn't exist"
);
if (resource) {
ensureResourceIsCorrect(resource);
}
}
if (params.metadata && reqMetadata) {
ensureMetadataIsCorrect(reqMetadata, params.metadata);
}
const spans = exportedData.scopeSpans[0].spans;
const resource = exportedData.resource;

assert.ok(
typeof spans !== 'undefined',
'spans do not exist'
);

ensureExportedSpanIsCorrect(spans[0]);

assert.ok(
typeof resource !== 'undefined',
"resource doesn't exist"
);

ensureResourceIsCorrect(resource);

ensureMetadataIsCorrect(reqMetadata, params?.metadata);

done();
}, 200);
});
Expand Down Expand Up @@ -228,24 +228,23 @@ const testCollectorExporter = (params: TestParams) =>
typeof exportedData !== 'undefined',
'resource' + " doesn't exist"
);
let spans;
let resource;
if (exportedData) {
spans = exportedData.instrumentationLibrarySpans[0].spans;
resource = exportedData.resource;
ensureExportedSpanIsCorrect(spans[0]);
const spans = exportedData.scopeSpans[0].spans;
const resource = exportedData.resource;

assert.ok(
typeof spans !== 'undefined',
'spans do not exist'
);
ensureExportedSpanIsCorrect(spans[0]);

assert.ok(
typeof resource !== 'undefined',
"resource doesn't exist"
);
ensureResourceIsCorrect(resource);

ensureMetadataIsCorrect(reqMetadata, params.metadata);

assert.ok(
typeof resource !== 'undefined',
"resource doesn't exist"
);
if (resource) {
ensureResourceIsCorrect(resource);
}
}
if (params.metadata && reqMetadata) {
ensureMetadataIsCorrect(reqMetadata, params.metadata);
}
done();
}, 500);
});
Expand All @@ -261,7 +260,7 @@ const testCollectorExporter = (params: TestParams) =>
)
: undefined;

envSource.OTEL_EXPORTER_OTLP_COMPRESSION='gzip';
envSource.OTEL_EXPORTER_OTLP_COMPRESSION = 'gzip';
collectorExporter = new OTLPTraceExporter({
url: 'grpcs://' + address,
credentials,
Expand Down Expand Up @@ -337,5 +336,3 @@ describe('when configuring via environment', () => {
testCollectorExporter({ useTLS: true });
testCollectorExporter({ useTLS: false });
testCollectorExporter({ metadata });


Loading

0 comments on commit 1343d16

Please sign in to comment.