Skip to content

Commit

Permalink
improv(instrumentation-http): supressInstrumentation when we get a re…
Browse files Browse the repository at this point in the history
…quest on ignoredPath [#1831] (#1838)
  • Loading branch information
vmarchaud authored Jan 20, 2021
1 parent f43855d commit 2fcb76d
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 2 deletions.
7 changes: 6 additions & 1 deletion packages/opentelemetry-instrumentation-http/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
TraceFlags,
ROOT_CONTEXT,
getSpan,
suppressInstrumentation,
} from '@opentelemetry/api';
import { NoRecordingSpan } from '@opentelemetry/core';
import type * as http from 'http';
Expand Down Expand Up @@ -392,7 +393,11 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {
)
)
) {
return original.apply(this, [event, ...args]);
return context.with(suppressInstrumentation(context.active()), () => {
context.bind(request);
context.bind(response);
return original.apply(this, [event, ...args]);
});
}

const headers = request.headers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ describe('HttpInstrumentation', () => {
});
instrumentation.enable();
server = http.createServer((request, response) => {
if (request.url?.includes('/ignored')) {
provider.getTracer('test').startSpan('some-span').end();
}
response.end('Test Server Response');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ describe('HttpsInstrumentation', () => {
cert: fs.readFileSync('test/fixtures/server-cert.pem'),
},
(request, response) => {
if (request.url?.includes('/ignored')) {
tracer.startSpan('some-span').end();
}
response.end('Test Server Response');
}
);
Expand Down
7 changes: 6 additions & 1 deletion packages/opentelemetry-plugin-http/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
setSpan,
ROOT_CONTEXT,
getSpan,
suppressInstrumentation,
} from '@opentelemetry/api';
import { BasePlugin, NoRecordingSpan } from '@opentelemetry/core';
import type {
Expand Down Expand Up @@ -295,7 +296,11 @@ export class HttpPlugin extends BasePlugin<Http> {
plugin._logger.error('caught ignoreIncomingPaths error: ', e)
)
) {
return original.apply(this, [event, ...args]);
return context.with(suppressInstrumentation(context.active()), () => {
context.bind(request);
context.bind(response);
return original.apply(this, [event, ...args]);
});
}

const headers = request.headers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ describe('HttpPlugin', () => {
};
plugin.enable(http, provider, provider.logger, config);
server = http.createServer((request, response) => {
if (request.url?.includes('/ignored')) {
provider.getTracer('test').startSpan('some-span').end();
}
response.end('Test Server Response');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ describe('HttpsPlugin', () => {
cert: fs.readFileSync('test/fixtures/server-cert.pem'),
},
(request, response) => {
if (request.url?.includes('/ignored')) {
tracer.startSpan('some-span').end();
}
response.end('Test Server Response');
}
);
Expand Down

0 comments on commit 2fcb76d

Please sign in to comment.