Skip to content

Commit

Permalink
Update express example to include route filtering (open-telemetry#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
hermanbanken authored Sep 2, 2021
1 parent 5fb3313 commit a70b760
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/express/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`, {
Expand Down
22 changes: 21 additions & 1 deletion examples/express/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,6 +25,7 @@ module.exports = (serviceName) => {
resource: new Resource({
[ResourceAttributesSC.SERVICE_NAME]: serviceName,
}),
sampler: filterSampler(ignoreHealthCheck, new AlwaysOnSampler()),
});
registerInstrumentations({
tracerProvider: provider,
Expand All @@ -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";
}

0 comments on commit a70b760

Please sign in to comment.