0.25.0 • Published 3 years ago

@opentelemetry/exporter-collector v0.25.0

Weekly downloads
15,802
License
Apache-2.0
Repository
github
Last release
3 years ago

OpenTelemetry Collector Exporter for web and node

Gitter chat NPM Published Version dependencies devDependencies Apache License

This module provides exporter for web and node to be used with opentelemetry-collector.

Installation

npm install --save @opentelemetry/exporter-collector

Traces in Web

The CollectorTraceExporter in Web expects the endpoint to end in /v1/trace.

import { SimpleSpanProcessor } from '@opentelemetry/tracing';
import { WebTracerProvider } from '@opentelemetry/web';
import { CollectorTraceExporter } from '@opentelemetry/exporter-collector';

const collectorOptions = {
  url: '<opentelemetry-collector-url>', // url is optional and can be omitted - default is http://localhost:55681/v1/trace
  headers: {}, //an optional object containing custom headers to be sent with each request
};

const provider = new WebTracerProvider();
const exporter = new CollectorTraceExporter(collectorOptions);
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

provider.register();

Metrics in Web

The CollectorMetricExporter in Web expects the endpoint to end in /v1/metrics.

import { MetricProvider } from '@opentelemetry/metrics';
import { CollectorMetricExporter } from '@opentelemetry/exporter-collector';
const collectorOptions = {
  url: '<opentelemetry-collector-url>', // url is optional and can be omitted - default is http://localhost:55681/v1/metrics
  headers: {}, //an optional object containing custom headers to be sent with each request
};
const exporter = new CollectorMetricExporter(collectorOptions);

// Register the exporter
const meter = new MeterProvider({
  exporter,
  interval: 60000,
}).getMeter('example-meter');

// Now, start recording data
const counter = meter.createCounter('metric_name');
counter.add(10, { 'key': 'value' });

Traces in Node - GRPC

The CollectorTraceExporter in Node expects the URL to only be the hostname. It will not work with /v1/trace.

const { BasicTracerProvider, SimpleSpanProcessor } = require('@opentelemetry/tracing');
const { CollectorTraceExporter } =  require('@opentelemetry/exporter-collector');

const collectorOptions = {
  serviceName: 'basic-service',
  url: '<opentelemetry-collector-url>' // url is optional and can be omitted - default is localhost:55680
};

const provider = new BasicTracerProvider();
const exporter = new CollectorTraceExporter(collectorOptions);
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

provider.register();

By default, plaintext connection is used. In order to use TLS in Node.js, provide credentials option like so:

const fs = require('fs');
const grpc = require('grpc');
const { BasicTracerProvider, SimpleSpanProcessor } = require('@opentelemetry/tracing');
const { CollectorTraceExporter } =  require('@opentelemetry/exporter-collector');

const collectorOptions = {
  serviceName: 'basic-service',
  url: '<opentelemetry-collector-url>', // url is optional and can be omitted - default is localhost:55680
  credentials: grpc.credentials.createSsl(
    fs.readFileSync('./ca.crt'),
    fs.readFileSync('./client.key'),
    fs.readFileSync('./client.crt')
  )
};

const provider = new BasicTracerProvider();
const exporter = new CollectorTraceExporter(collectorOptions);
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

provider.register();

To see how to generate credentials, you can refer to the script used to generate certificates for tests here

The exporter can be configured to send custom metadata with each request as in the example below:

const grpc = require('grpc');
const { BasicTracerProvider, SimpleSpanProcessor } = require('@opentelemetry/tracing');
const { CollectorTraceExporter } =  require('@opentelemetry/exporter-collector');

const metadata = new grpc.Metadata();
metadata.set('k', 'v');

const collectorOptions = {
  serviceName: 'basic-service',
  url: '<opentelemetry-collector-url>', // url is optional and can be omitted - default is localhost:55680
  metadata, // // an optional grpc.Metadata object to be sent with each request
};

const provider = new BasicTracerProvider();
const exporter = new CollectorTraceExporter(collectorOptions);
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

provider.register();

Note, that this will only work if TLS is also configured on the server.

Traces in Node - JSON over http

const { BasicTracerProvider, SimpleSpanProcessor } = require('@opentelemetry/tracing');
const { CollectorExporter, CollectorTransportNode } =  require('@opentelemetry/exporter-collector');

const collectorOptions = {
  protocolNode: CollectorTransportNode.HTTP_JSON,
  serviceName: 'basic-service',
  url: '<opentelemetry-collector-url>', // url is optional and can be omitted - default is http://localhost:55681/v1/trace
  headers: {
    foo: 'bar'
  }, //an optional object containing custom headers to be sent with each request will only work with http
};

const provider = new BasicTracerProvider();
const exporter = new CollectorExporter(collectorOptions);
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

provider.register();

Traces in Node - PROTO over http

const { BasicTracerProvider, SimpleSpanProcessor } = require('@opentelemetry/tracing');
const { CollectorExporter, CollectorTransportNode } =  require('@opentelemetry/exporter-collector');

const collectorOptions = {
  protocolNode: CollectorTransportNode.HTTP_PROTO,
  serviceName: 'basic-service',
  url: '<opentelemetry-collector-url>', // url is optional and can be omitted - default is http://localhost:55681/v1/trace
  headers: {
    foo: 'bar'
  }, //an optional object containing custom headers to be sent with each request will only work with http
};

const provider = new BasicTracerProvider();
const exporter = new CollectorExporter(collectorOptions);
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

provider.register();

Metrics in Node

The CollectorTraceExporter in Node expects the URL to only be the hostname. It will not work with /v1/metrics. All options that work with trace also work with metrics.

const { MeterProvider } = require('@opentelemetry/metrics');
const { CollectorMetricExporter } =  require('@opentelemetry/exporter-collector');
const collectorOptions = {
  serviceName: 'basic-service',
  url: '<opentelemetry-collector-url>', // url is optional and can be omitted - default is localhost:55681
};
const exporter = new CollectorMetricExporter(collectorOptions);

// Register the exporter
const meter = new MeterProvider({
  exporter,
  interval: 60000,
}).getMeter('example-meter');

// Now, start recording data
const counter = meter.createCounter('metric_name');
counter.add(10, { 'key': 'value' });

Running opentelemetry-collector locally to see the traces

  1. Go to examples/collector-exporter-node
  2. run npm run docker:start
  3. Open page at http://localhost:9411/zipkin/ to observe the traces

Useful links

License

Apache 2.0 - See LICENSE for more information.

0.25.1-alpha.23

3 years ago

0.25.1-alpha.14

3 years ago

0.25.1-alpha.16

3 years ago

0.25.1-alpha.17

3 years ago

0.25.1-alpha.12

3 years ago

0.25.1-alpha.13

3 years ago

0.25.1-alpha.7

3 years ago

0.25.1-alpha.2

3 years ago

0.25.1-alpha.4

3 years ago

0.25.0

3 years ago

0.24.1-alpha.20

3 years ago

0.25.1-alpha.21

3 years ago

0.24.1-alpha.19

3 years ago

0.24.1-alpha.18

3 years ago

0.24.1-alpha.15

3 years ago

0.24.1-alpha.14

3 years ago

0.24.1-alpha.7

3 years ago

0.24.1-alpha.6

3 years ago

0.24.1-alpha.9

3 years ago

0.24.1-alpha.8

3 years ago

0.24.1-alpha.5

3 years ago

0.24.1-alpha.4

3 years ago

0.24.1-alpha.1

3 years ago

0.24.1-alpha.31

3 years ago

0.23.1-alpha.28

3 years ago

0.23.1-alpha.29

3 years ago

0.24.0

3 years ago

0.23.1-alpha.24

3 years ago

0.23.1-alpha.23

3 years ago

0.23.1-alpha.20

3 years ago

0.23.1-alpha.15

3 years ago

0.23.1-alpha.3

3 years ago

0.23.1-alpha.17

3 years ago

0.23.1-alpha.18

3 years ago

0.22.1-alpha.16

3 years ago

0.22.1-alpha.15

3 years ago

0.23.0

3 years ago

0.22.1-alpha.13

3 years ago

0.22.1-alpha.2

3 years ago

0.22.1-alpha.3

3 years ago

0.22.1-alpha.5

3 years ago

0.22.0

3 years ago

0.21.1-alpha.3

3 years ago

0.20.0

3 years ago

0.21.0

3 years ago

0.21.1-alpha.2

3 years ago

0.21.1-alpha.1

3 years ago

0.21.1-alpha.7

3 years ago

0.19.1-alpha.41

3 years ago

0.19.1-alpha.43

3 years ago

0.19.1-alpha.44

3 years ago

0.20.1-alpha.4

3 years ago

0.20.1-alpha.3

3 years ago

0.20.1-alpha.49

3 years ago

0.19.1-alpha.27

3 years ago

0.19.1-alpha.26

3 years ago

0.19.1-alpha.29

3 years ago

0.19.1-alpha.28

3 years ago

0.19.1-alpha.21

3 years ago

0.19.1-alpha.23

3 years ago

0.19.1-alpha.25

3 years ago

0.19.0

3 years ago

0.19.1-alpha.19

3 years ago

0.19.1-alpha.12

3 years ago

0.19.1-alpha.11

3 years ago

0.19.1-alpha.14

3 years ago

0.19.1-alpha.13

3 years ago

0.19.1-alpha.42

3 years ago

0.19.1-alpha.37

3 years ago

0.19.1-alpha.39

3 years ago

0.19.1-alpha.9

3 years ago

0.19.1-alpha.31

3 years ago

0.19.1-alpha.7

3 years ago

0.19.1-alpha.33

3 years ago

0.19.1-alpha.36

3 years ago

0.18.3-alpha.31

3 years ago

0.18.3-alpha.30

3 years ago

0.18.3-alpha.28

3 years ago

0.18.3-alpha.29

3 years ago

0.18.3-alpha.27

3 years ago

0.18.3-alpha.20

3 years ago

0.18.3-alpha.23

3 years ago

0.18.3-alpha.25

3 years ago

0.18.3-alpha.17

3 years ago

0.18.3-alpha.18

3 years ago

0.18.3-alpha.16

3 years ago

0.18.3-alpha.14

3 years ago

0.18.3-alpha.9

3 years ago

0.18.3-alpha.10

3 years ago

0.18.3-alpha.11

3 years ago

0.18.3-alpha.7

3 years ago

0.18.3-alpha.5

3 years ago

0.18.3-alpha.4

3 years ago

0.18.3-alpha.3

3 years ago

0.18.2-alpha.1

3 years ago

0.18.2

3 years ago

0.18.3-alpha.2

3 years ago

0.18.3-alpha.1

3 years ago

0.18.2-alpha.81

3 years ago

0.18.1

3 years ago

0.18.1-alpha.27

3 years ago

0.18.1-alpha.26

3 years ago

0.18.1-alpha.20

3 years ago

0.18.1-alpha.24

3 years ago

0.18.1-alpha.23

3 years ago

0.18.1-alpha.21

3 years ago

0.18.1-alpha.16

3 years ago

0.18.1-alpha.13

3 years ago

0.17.1-alpha.11

3 years ago

0.18.1-alpha.12

3 years ago

0.18.0

3 years ago

0.18.1-alpha.3

3 years ago

0.17.1-alpha.10

3 years ago

0.17.1-alpha.8

3 years ago

0.17.1-alpha.7

3 years ago

0.17.1-alpha.5

3 years ago

0.17.1-alpha.2

3 years ago

0.17.0

3 years ago

0.16.1-alpha.20

3 years ago

0.17.1-alpha.21

3 years ago

0.16.1-alpha.18

3 years ago

0.16.1-alpha.14

3 years ago

0.16.1-alpha.15

3 years ago

0.16.1-alpha.12

3 years ago

0.16.1-alpha.11

3 years ago

0.16.1-alpha.10

3 years ago

0.16.0

3 years ago

0.15.0

3 years ago

0.14.0

3 years ago

0.13.0

3 years ago

0.12.1-alpha.4

4 years ago

0.12.1-alpha.7

4 years ago

0.12.1-alpha.3

4 years ago

0.12.0

4 years ago

0.11.1-alpha.53

4 years ago

0.12.1-alpha.54

4 years ago

0.11.1-alpha.44

4 years ago

0.11.1-alpha.35

4 years ago

0.11.1-alpha.36

4 years ago

0.11.1-alpha.15

4 years ago

0.11.1-alpha.48

4 years ago

0.11.0

4 years ago

0.10.3-alpha.40

4 years ago

0.10.3-alpha.41

4 years ago

0.10.3-alpha.35

4 years ago

0.10.3-alpha.34

4 years ago

0.10.3-alpha.28

4 years ago

0.10.3-canary.0

4 years ago

0.10.2

4 years ago

0.10.1

4 years ago

0.10.0

4 years ago

0.9.0

4 years ago

0.8.3

4 years ago

0.8.2

4 years ago

0.8.1

4 years ago

0.8.0

4 years ago

0.7.0

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.2

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.0

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago