From 77b67fe379587ba5cec0c111ba90ba66d1e2da42 Mon Sep 17 00:00:00 2001 From: vmarchaud Date: Sat, 23 May 2020 15:45:28 +0200 Subject: [PATCH] refactor(http-plugin): use withAsync instead of withSpan --- .../opentelemetry-plugin-http/package.json | 1 - .../test/functionals/http-enable.test.ts | 34 ++++++++++--------- .../opentelemetry-plugin-https/package.json | 1 - .../test/functionals/https-enable.test.ts | 34 ++++++++++--------- 4 files changed, 36 insertions(+), 34 deletions(-) diff --git a/packages/opentelemetry-plugin-http/package.json b/packages/opentelemetry-plugin-http/package.json index e59a6f3afe..219c2a7e13 100644 --- a/packages/opentelemetry-plugin-http/package.json +++ b/packages/opentelemetry-plugin-http/package.json @@ -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", diff --git a/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts b/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts index 1c32b3156f..b000542952 100644 --- a/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts +++ b/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts @@ -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, @@ -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'; @@ -91,7 +90,7 @@ export const responseHookFunction = ( }; describe('HttpPlugin', () => { - let contextManager: ContextManager; + let contextManager: AsyncHooksContextManager; beforeEach(() => { contextManager = new AsyncHooksContextManager().enable(); @@ -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']) { diff --git a/packages/opentelemetry-plugin-https/package.json b/packages/opentelemetry-plugin-https/package.json index ada320bc18..b8bf63b0f6 100644 --- a/packages/opentelemetry-plugin-https/package.json +++ b/packages/opentelemetry-plugin-https/package.json @@ -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", diff --git a/packages/opentelemetry-plugin-https/test/functionals/https-enable.test.ts b/packages/opentelemetry-plugin-https/test/functionals/https-enable.test.ts index 21d43c39bd..72c655b54d 100644 --- a/packages/opentelemetry-plugin-https/test/functionals/https-enable.test.ts +++ b/packages/opentelemetry-plugin-https/test/functionals/https-enable.test.ts @@ -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, @@ -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, @@ -83,7 +82,7 @@ export const customAttributeFunction = (span: ISpan): void => { }; describe('HttpsPlugin', () => { - let contextManager: ContextManager; + let contextManager: AsyncHooksContextManager; beforeEach(() => { contextManager = new AsyncHooksContextManager().enable(); @@ -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']) {