Skip to content

Commit

Permalink
refactor(http-plugin): use withAsync instead of withSpan
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed May 23, 2020
1 parent ff9dabf commit 77b67fe
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
1 change: 0 additions & 1 deletion packages/opentelemetry-plugin-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
},
"devDependencies": {
"@opentelemetry/context-async-hooks": "^0.8.2",
"@opentelemetry/context-base": "^0.8.2",
"@opentelemetry/node": "^0.8.2",
"@opentelemetry/tracing": "^0.8.2",
"@types/got": "^9.6.7",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
Span as ISpan,
SpanKind,
} from '@opentelemetry/api';
import { NoopLogger } from '@opentelemetry/core';
import { NoopLogger, setActiveSpan } from '@opentelemetry/core';
import { NodeTracerProvider } from '@opentelemetry/node';
import {
InMemorySpanExporter,
Expand All @@ -37,7 +37,6 @@ import { OT_REQUEST_HEADER } from '../../src/utils';
import { assertSpan } from '../utils/assertSpan';
import { DummyPropagation } from '../utils/DummyPropagation';
import { httpRequest } from '../utils/httpRequest';
import { ContextManager } from '@opentelemetry/context-base';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import { ClientRequest, IncomingMessage, ServerResponse } from 'http';

Expand Down Expand Up @@ -91,7 +90,7 @@ export const responseHookFunction = (
};

describe('HttpPlugin', () => {
let contextManager: ContextManager;
let contextManager: AsyncHooksContextManager;

beforeEach(() => {
contextManager = new AsyncHooksContextManager().enable();
Expand Down Expand Up @@ -461,21 +460,24 @@ describe('HttpPlugin', () => {
doNock(hostname, testPath, 200, 'Ok', num);
const name = 'TestRootSpan';
const span = provider.getTracer('default').startSpan(name);
await provider.getTracer('default').withSpan(span, async () => {
for (let i = 0; i < num; i++) {
await httpRequest.get(`${protocol}://${hostname}${testPath}`);
await contextManager.withAsync(
setActiveSpan(contextManager.active(), span),
async () => {
for (let i = 0; i < num; i++) {
await httpRequest.get(`${protocol}://${hostname}${testPath}`);
const spans = memoryExporter.getFinishedSpans();
assert.ok(spans[i].name.indexOf(testPath) >= 0);
assert.strictEqual(
span.context().traceId,
spans[i].spanContext.traceId
);
}
span.end();
const spans = memoryExporter.getFinishedSpans();
assert.ok(spans[i].name.indexOf(testPath) >= 0);
assert.strictEqual(
span.context().traceId,
spans[i].spanContext.traceId
);
// 5 child spans ended + 1 span (root)
assert.strictEqual(spans.length, 6);
}
span.end();
const spans = memoryExporter.getFinishedSpans();
// 5 child spans ended + 1 span (root)
assert.strictEqual(spans.length, 6);
});
);
});

for (const ignored of ['string', 'function', 'regexp']) {
Expand Down
1 change: 0 additions & 1 deletion packages/opentelemetry-plugin-https/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
},
"devDependencies": {
"@opentelemetry/context-async-hooks": "^0.8.2",
"@opentelemetry/context-base": "^0.8.2",
"@opentelemetry/node": "^0.8.2",
"@opentelemetry/tracing": "^0.8.2",
"@types/got": "^9.6.7",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
Span as ISpan,
SpanKind,
} from '@opentelemetry/api';
import { NoopLogger } from '@opentelemetry/core';
import { NoopLogger, setActiveSpan } from '@opentelemetry/core';
import { NodeTracerProvider } from '@opentelemetry/node';
import {
AttributeNames,
Expand All @@ -30,7 +30,6 @@ import {
OT_REQUEST_HEADER,
} from '@opentelemetry/plugin-http';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import { ContextManager } from '@opentelemetry/context-base';
import {
InMemorySpanExporter,
SimpleSpanProcessor,
Expand Down Expand Up @@ -83,7 +82,7 @@ export const customAttributeFunction = (span: ISpan): void => {
};

describe('HttpsPlugin', () => {
let contextManager: ContextManager;
let contextManager: AsyncHooksContextManager;

beforeEach(() => {
contextManager = new AsyncHooksContextManager().enable();
Expand Down Expand Up @@ -453,21 +452,24 @@ describe('HttpsPlugin', () => {
doNock(hostname, testPath, 200, 'Ok', num);
const name = 'TestRootSpan';
const span = tracer.startSpan(name);
await tracer.withSpan(span, async () => {
for (let i = 0; i < num; i++) {
await httpsRequest.get(`${protocol}://${hostname}${testPath}`);
await contextManager.withAsync(
setActiveSpan(contextManager.active(), span),
async () => {
for (let i = 0; i < num; i++) {
await httpsRequest.get(`${protocol}://${hostname}${testPath}`);
const spans = memoryExporter.getFinishedSpans();
assert.ok(spans[i].name.indexOf(testPath) >= 0);
assert.strictEqual(
span.context().traceId,
spans[i].spanContext.traceId
);
}
span.end();
const spans = memoryExporter.getFinishedSpans();
assert.ok(spans[i].name.indexOf(testPath) >= 0);
assert.strictEqual(
span.context().traceId,
spans[i].spanContext.traceId
);
// 5 child spans ended + 1 span (root)
assert.strictEqual(spans.length, 6);
}
span.end();
const spans = memoryExporter.getFinishedSpans();
// 5 child spans ended + 1 span (root)
assert.strictEqual(spans.length, 6);
});
);
});

for (const ignored of ['string', 'function', 'regexp']) {
Expand Down

0 comments on commit 77b67fe

Please sign in to comment.