What is @opentelemetry/instrumentation-grpc?
@opentelemetry/instrumentation-grpc is an npm package that provides automatic instrumentation for gRPC services and clients using OpenTelemetry. This allows you to collect and export telemetry data such as traces and metrics from your gRPC-based applications, which can be used for monitoring and observability purposes.
What are @opentelemetry/instrumentation-grpc's main functionalities?
Automatic gRPC Server Instrumentation
This code sets up automatic instrumentation for a gRPC server. It initializes the OpenTelemetry tracer provider, adds a span processor to export spans to the console, and registers the gRPC instrumentation.
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base');
const { ConsoleSpanExporter } = require('@opentelemetry/sdk-trace-base');
const { GrpcInstrumentation } = require('@opentelemetry/instrumentation-grpc');
const provider = new NodeTracerProvider();
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.register();
const grpcInstrumentation = new GrpcInstrumentation();
grpcInstrumentation.setTracerProvider(provider);
Automatic gRPC Client Instrumentation
This code sets up automatic instrumentation for a gRPC client. Similar to the server instrumentation, it initializes the OpenTelemetry tracer provider, adds a span processor to export spans to the console, and registers the gRPC instrumentation.
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base');
const { ConsoleSpanExporter } = require('@opentelemetry/sdk-trace-base');
const { GrpcInstrumentation } = require('@opentelemetry/instrumentation-grpc');
const provider = new NodeTracerProvider();
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.register();
const grpcInstrumentation = new GrpcInstrumentation();
grpcInstrumentation.setTracerProvider(provider);
Other packages similar to @opentelemetry/instrumentation-grpc
@opentelemetry/instrumentation-http
@opentelemetry/instrumentation-http provides automatic instrumentation for HTTP and HTTPS modules in Node.js. It is similar to @opentelemetry/instrumentation-grpc but focuses on HTTP-based communication instead of gRPC.
@opentelemetry/instrumentation-express
@opentelemetry/instrumentation-express provides automatic instrumentation for Express.js applications. It is similar to @opentelemetry/instrumentation-grpc but is specifically designed for the Express.js framework.
@opentelemetry/instrumentation-mongodb
@opentelemetry/instrumentation-mongodb provides automatic instrumentation for MongoDB operations. It is similar to @opentelemetry/instrumentation-grpc but focuses on MongoDB database interactions.
OpenTelemetry gRPC Instrumentation for Node.js
This module provides automatic instrumentation for grpc
. Currently, version 1.x
of the Node.js gRPC library is supported.
For automatic instrumentation see the
@opentelemetry/node package.
Installation
npm install --save @opentelemetry/instrumentation-grpc
Usage
OpenTelemetry gRPC Instrumentation allows the user to automatically collect trace data and export them to the backend of choice, to give observability to distributed systems when working with gRPC.
To load a specific instrumentation (gRPC in this case), specify it in the Node Tracer's configuration.
const { NodeTracerProvider } = require('@opentelemetry/node');
const { GrpcInstrumentation } = require('@opentelemetry/instrumentation-grpc');
const provider = new NodeTracerProvider({
plugins: {
grpc: { enabled: false, path: '@opentelemetry/plugin-groc' }
},
});
const grpcInstrumentation = new GrpcInstrumentation({
});
grpcInstrumentation.enable();
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.register();
See examples/grpc for a short example.
gRPC Instrumentation Options
gRPC instrumentation accepts the following configuration:
Options | Type | Description |
---|
ignoreGrpcMethods | IgnoreMatcher[] | gRPC instrumentation will not trace any methods that match anything in this list. You may pass a string (case-insensitive match), a RegExp object, or a filter function. |
Useful links
License
Apache 2.0 - See LICENSE for more information.