UNPKG

@coozzy/tracing

Version:

Provides tooling for distributed tracing with OpenTelemetry

65 lines (59 loc) 3.2 kB
const JaegerExporter = require('@opentelemetry/exporter-trace-otlp-grpc').OTLPTraceExporter; const Resource = require('@opentelemetry/resources').Resource; const SemanticResourceAttributes = require('@opentelemetry/semantic-conventions').SemanticResourceAttributes; const BatchSpanProcessor = require('@opentelemetry/sdk-trace-base').BatchSpanProcessor; const B3Propagator = require('@opentelemetry/propagator-b3').B3Propagator; const NodeTracerProvider = require('@opentelemetry/sdk-trace-node').NodeTracerProvider; const registerInstrumentations = require('@opentelemetry/instrumentation').registerInstrumentations; const GrpcInstrumentation = require('@opentelemetry/instrumentation-grpc').GrpcInstrumentation; const HttpInstrumentation = require('@opentelemetry/instrumentation-http').HttpInstrumentation; const ExpressInstrumentation = require('@opentelemetry/instrumentation-express').ExpressInstrumentation; const AmqplibInstrumentation = require('@opentelemetry/instrumentation-amqplib').AmqplibInstrumentation; const MySQLInstrumentation = require('@opentelemetry/instrumentation-mysql').MySQLInstrumentation; const TypeormInstrumentation = require('opentelemetry-instrumentation-typeorm').TypeormInstrumentation; const MongoDBInstrumentation = require('@opentelemetry/instrumentation-mongodb').MongoDBInstrumentation; const MongooseInstrumentation = require('@opentelemetry/instrumentation-mongoose').MongooseInstrumentation; const IORedisInstrumentation = require('@opentelemetry/instrumentation-ioredis').IORedisInstrumentation; const WinstonInstrumentation = require('@opentelemetry/instrumentation-winston').WinstonInstrumentation; const api = require('@opentelemetry/api'); let initServiceName = ''; let provider = null; module.exports.init = (exporterUrl, namespace, serviceName) => { initServiceName = serviceName; const exporter = new JaegerExporter({ url: exporterUrl }); provider = new NodeTracerProvider({ resource: new Resource({ [SemanticResourceAttributes.SERVICE_NAME]: serviceName, [SemanticResourceAttributes.SERVICE_NAMESPACE]: namespace }) }); provider.addSpanProcessor(new BatchSpanProcessor(exporter)); provider.register({propagator: new B3Propagator()}); registerInstrumentations({ instrumentations: [ new GrpcInstrumentation({ ignoreGrpcMethods: ['check'] }), new HttpInstrumentation({ ignoreIncomingRequestHook: request => { return request.url === '/metrics'; } }), new ExpressInstrumentation(), new AmqplibInstrumentation(), new MySQLInstrumentation(), new TypeormInstrumentation(), new MongoDBInstrumentation(), new MongooseInstrumentation(), new IORedisInstrumentation(), new WinstonInstrumentation() ], tracerProvider: provider }); }; module.exports.shutdown = async () => { await provider?.shutdown(); } module.exports.tracer = api.trace.getTracer(initServiceName);