Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added OpenTelemetry detectors from resources #25

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 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,9 @@ 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 { containerDetector } = require('@opentelemetry/resource-detector-container');
const { B3Propagator, B3InjectEncoding } = require('@opentelemetry/propagator-b3');

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

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

Expand All @@ -51,25 +55,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 +80,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