What is @opentelemetry/exporter-trace-otlp-grpc?
The @opentelemetry/exporter-trace-otlp-grpc npm package is used to export telemetry data, specifically traces, from an application to a backend that supports the OpenTelemetry Protocol (OTLP) over gRPC. This is part of the OpenTelemetry project, which provides a set of APIs, libraries, agents, and instrumentation to create and manage telemetry data (metrics, logs, and traces) for cloud-native software.
What are @opentelemetry/exporter-trace-otlp-grpc's main functionalities?
Exporting Traces
This code sample demonstrates how to set up the OpenTelemetry Node.js tracer provider with the OTLP gRPC Trace Exporter. It initializes the exporter and adds it to the provider as a span processor, which will batch and send the collected traces to the configured backend.
const { TraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc');
const { NodeTracerProvider } = require('@opentelemetry/node');
const { BatchSpanProcessor } = require('@opentelemetry/tracing');
// Initialize a tracer provider
const provider = new NodeTracerProvider();
// Initialize the OTLP gRPC Trace Exporter
const otlpExporter = new TraceExporter();
// Add the exporter to the provider
provider.addSpanProcessor(new BatchSpanProcessor(otlpExporter));
// Register the provider
provider.register();
Other packages similar to @opentelemetry/exporter-trace-otlp-grpc
@opentelemetry/exporter-jaeger
This package is similar to @opentelemetry/exporter-trace-otlp-grpc as it is also used for exporting trace data. However, it exports the data to Jaeger, which is a different backend that specializes in monitoring and troubleshooting microservices-based distributed systems.
@opentelemetry/exporter-zipkin
Like the OTLP gRPC exporter, the Zipkin exporter is used to send trace data, but it sends the data to a Zipkin backend. Zipkin is another distributed tracing system that helps gather timing data needed to troubleshoot latency problems in service architectures.
@opentelemetry/exporter-prometheus
This package is part of the OpenTelemetry exporters, but instead of exporting trace data, it exports metrics to a Prometheus backend, which is a monitoring system and time series database.
OpenTelemetry Collector Exporter for node with grpc
This module provides exporter for web and node to be used with opentelemetry-collector - last tested with version 0.25.0.
Installation
npm install --save @opentelemetry/exporter-trace-otlp-grpc
Service Name
The OpenTelemetry Collector Exporter does not have a service name configuration.
In order to set the service name, use the service.name
resource attribute as prescribed in the OpenTelemetry Resource Semantic Conventions.
To see documentation and sample code for the metric exporter, see the exporter-metrics-otlp-grpc package
Traces in Node - GRPC
The OTLPTraceExporter in Node expects the URL to only be the hostname. It will not work with /v1/traces
.
const { BasicTracerProvider, SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc');
const collectorOptions = {
url: 'grpc://<collector-hostname>:<port>',
};
const provider = new BasicTracerProvider();
const exporter = new OTLPTraceExporter(collectorOptions);
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
provider.register();
['SIGINT', 'SIGTERM'].forEach(signal => {
process.on(signal, () => provider.shutdown().catch(console.error));
});
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/grpc-js');
const { BasicTracerProvider, SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc');
const collectorOptions = {
url: 'grpc://<collector-hostname>:<port>',
credentials: grpc.credentials.createSsl(),
};
const provider = new BasicTracerProvider();
const exporter = new OTLPTraceExporter(collectorOptions);
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
provider.register();
['SIGINT', 'SIGTERM'].forEach(signal => {
process.on(signal, () => provider.shutdown().catch(console.error));
});
To use mutual authentication, pass to the createSsl()
constructor:
credentials: grpc.credentials.createSsl(
fs.readFileSync('./ca.crt'),
fs.readFileSync('./client.key'),
fs.readFileSync('./client.crt')
),
To generate credentials for mutual authentication, 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/grpc-js');
const { BasicTracerProvider, SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc');
const metadata = new grpc.Metadata();
metadata.set('k', 'v');
const collectorOptions = {
url: 'grpc://<collector-hostname>:<port>',
metadata,
};
const provider = new BasicTracerProvider();
const exporter = new OTLPTraceExporter(collectorOptions);
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
provider.register();
['SIGINT', 'SIGTERM'].forEach(signal => {
process.on(signal, () => provider.shutdown().catch(console.error));
});
Note, that this will only work if TLS is also configured on the server.
Running opentelemetry-collector locally to see the traces
- Go to examples/otlp-exporter-node
- run
npm run docker:start
- Open page at
http://localhost:9411/zipkin/
to observe the traces
Useful links
License
Apache 2.0 - See LICENSE for more information.