From a70b7601f304524285b1eaddb9b5cd3af23b1600 Mon Sep 17 00:00:00 2001 From: Herman Date: Thu, 2 Sep 2021 21:41:41 +0200 Subject: [PATCH] Update express example to include route filtering (#652) Fixes #651 --- examples/express/server.js | 1 + examples/express/tracer.js | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/examples/express/server.js b/examples/express/server.js index d8e0e54157d..79921205f74 100644 --- a/examples/express/server.js +++ b/examples/express/server.js @@ -32,6 +32,7 @@ const authMiddleware = (req, res, next) => { }; app.use(express.json()); +app.get('/health', (req, res) => res.status(200).send("HEALTHY")); // endpoint that is called by framework/cluster app.get('/run_test', async (req, res) => { // Calls another endpoint of the same API, somewhat mimicing an external API call const createdCat = await axios.post(`http://localhost:${PORT}/cats`, { diff --git a/examples/express/tracer.js b/examples/express/tracer.js index a22063a67c0..638f377b9d4 100644 --- a/examples/express/tracer.js +++ b/examples/express/tracer.js @@ -6,13 +6,14 @@ const opentelemetry = require('@opentelemetry/api'); const { diag, DiagConsoleLogger, DiagLogLevel } = opentelemetry; diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO); +const { AlwaysOnSampler } = require('@opentelemetry/core'); const { registerInstrumentations } = require('@opentelemetry/instrumentation'); const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node'); const { SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base'); const { JaegerExporter } = require('@opentelemetry/exporter-jaeger'); const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin'); const { Resource } = require('@opentelemetry/resources'); -const { SemanticResourceAttributes: ResourceAttributesSC } = require('@opentelemetry/semantic-conventions'); +const { SemanticAttributes, SemanticResourceAttributes: ResourceAttributesSC } = require('@opentelemetry/semantic-conventions'); const Exporter = (process.env.EXPORTER || '') .toLowerCase().startsWith('z') ? ZipkinExporter : JaegerExporter; @@ -24,6 +25,7 @@ module.exports = (serviceName) => { resource: new Resource({ [ResourceAttributesSC.SERVICE_NAME]: serviceName, }), + sampler: filterSampler(ignoreHealthCheck, new AlwaysOnSampler()), }); registerInstrumentations({ tracerProvider: provider, @@ -45,3 +47,21 @@ module.exports = (serviceName) => { return opentelemetry.trace.getTracer('express-example'); }; + +function filterSampler(filterFn, parent) { + return { + shouldSample(ctx, tid, spanName, spanKind, attr, links) { + if (!filterFn(spanName, spanKind, attr)) { + return { decision: opentelemetry.SamplingDecision.NOT_RECORD }; + } + return parent.shouldSample(ctx, tid, name, kind, attr, links); + }, + toString() { + return `FilterSampler(${parent.toString()})`; + } + } +} + +function ignoreHealthCheck(spanName, spanKind, attributes) { + return spanKind !== opentelemetry.SpanKind.SERVER || attributes[SemanticAttributes.HTTP_ROUTE] !== "/health"; +}