Skip to content

Commit

Permalink
Added OpenTelemetry detectors and propagators using composite propaga…
Browse files Browse the repository at this point in the history
…tor from core

This will help us better identify propagation as it moves data between services and processes.

Added OpenTelemetry instrumentation for node:dns

This module enables name resolution. For example, use it to look up IP addresses of host names.
  • Loading branch information
saidsef committed Aug 14, 2023
1 parent 886c68b commit fd123e6
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 147 deletions.
36 changes: 20 additions & 16 deletions libs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* limitations under the License.
*/

const { CompositePropagator, W3CBaggagePropagator, W3CTraceContextPropagator } = require('@opentelemetry/core');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { BatchSpanProcessor } = require('@opentelemetry/sdk-trace-base');
Expand All @@ -27,7 +28,8 @@ const { Resource } = require('@opentelemetry/resources');
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
const { AwsInstrumentation } = require('@opentelemetry/instrumentation-aws-sdk');
const { PinoInstrumentation } = require('@opentelemetry/instrumentation-pino');
const { JaegerPropagator } = require('@opentelemetry/propagator-jaeger');
const { DnsInstrumentation } = require('@opentelemetry/instrumentation-dns');
const { B3Propagator, B3InjectEncoding } = require('@opentelemetry/propagator-b3');

diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO);

Expand All @@ -39,7 +41,7 @@ function setupTracing(serviceName, appName="application", endpoint=null) {
[SemanticResourceAttributes.CONTAINER_NAME]: serviceName,
[SemanticResourceAttributes.HOST_NAME]: serviceName,
instrumentationLibrarySemanticConvention: true,
}),
})
});

// Configure exporter with the Collector endpoint - uses gRPC
Expand All @@ -51,25 +53,23 @@ function setupTracing(serviceName, appName="application", endpoint=null) {
// Register the span processor with the tracer provider
provider.addSpanProcessor(new BatchSpanProcessor(new OTLPTraceExporter(exportOptions)));

// Ignore spans from static assets.
const ignoreIncomingRequestHook = (req) => {
const isStaticAsset = !!req.url.match(/^\/metrics|\/healthz.*$/);
return isStaticAsset;
}

// Register instrumentations
registerInstrumentations({
tracerProvider: provider,
instrumentations: [
new ExpressInstrumentation({
ignoreIncomingRequestHook(req) {
// Ignore spans from static assets.
const isStaticAsset = !!req.url.match(/^\/metrics.*$/);
return isStaticAsset;
}
}),
new HttpInstrumentation({
requireParentforOutgoingSpans: false,
requireParentforIncomingSpans: false,
ignoreIncomingRequestHook(req) {
// Ignore spans from static assets.
const isStaticAsset = !!req.url.match(/^\/metrics.*$/);
return isStaticAsset;
}
ignoreIncomingRequestHook,
}),
new ExpressInstrumentation({
ignoreIncomingRequestHook,
}),
new AwsInstrumentation({
sqsExtractContextPropagationFromPayload: true
Expand All @@ -78,12 +78,16 @@ function setupTracing(serviceName, appName="application", endpoint=null) {
logHook: (span, record) => {
record['resource.service.name'] = provider.resource.attributes['service.name'];
},
})
}),
new DnsInstrumentation(),
],
});

// Initialize the tracer provider
provider.register({propagator: new JaegerPropagator()});
provider.register({
propagator: new CompositePropagator({
propagators: [new W3CBaggagePropagator(), new W3CTraceContextPropagator(), new B3Propagator({ injectEncoding: B3InjectEncoding.MULTI_HEADER })],
})});

// Return the tracer for the service
return provider.getTracer(serviceName);
Expand Down
Loading

0 comments on commit fd123e6

Please sign in to comment.