diff --git a/detectors/node/opentelemetry-resource-detector-github/package.json b/detectors/node/opentelemetry-resource-detector-github/package.json index 4b80ebe7c4..e0f1004a99 100644 --- a/detectors/node/opentelemetry-resource-detector-github/package.json +++ b/detectors/node/opentelemetry-resource-detector-github/package.json @@ -43,8 +43,8 @@ "access": "public" }, "devDependencies": { - "@opentelemetry/core": "^0.14.0", - "@opentelemetry/tracing": "^0.14.0", + "@opentelemetry/core": "^0.15.0", + "@opentelemetry/tracing": "^0.15.0", "@types/mocha": "8.0.1", "@types/node": "14.0.27", "@types/sinon": "9.0.4", @@ -60,8 +60,8 @@ "typescript": "3.9.7" }, "dependencies": { - "@opentelemetry/api": "^0.14.0", - "@opentelemetry/core": "^0.14.0", - "@opentelemetry/resources": "^0.14.0" + "@opentelemetry/api": "^0.15.0", + "@opentelemetry/core": "^0.15.0", + "@opentelemetry/resources": "^0.15.0" } } diff --git a/metapackages/plugins-node-core-and-contrib/package.json b/metapackages/plugins-node-core-and-contrib/package.json index 0baf75d6f3..49e785b424 100644 --- a/metapackages/plugins-node-core-and-contrib/package.json +++ b/metapackages/plugins-node-core-and-contrib/package.json @@ -23,6 +23,6 @@ "@opentelemetry/plugin-pg": "^0.12.1", "@opentelemetry/plugin-pg-pool": "^0.12.1", "@opentelemetry/plugin-redis": "^0.12.1", - "@opentelemetry/plugins-node-core": "^0.14.0" + "@opentelemetry/plugins-node-core": "^0.15.0" } } diff --git a/packages/opentelemetry-host-metrics/package.json b/packages/opentelemetry-host-metrics/package.json index fab6285677..0d9dbc8d40 100644 --- a/packages/opentelemetry-host-metrics/package.json +++ b/packages/opentelemetry-host-metrics/package.json @@ -42,7 +42,7 @@ "access": "public" }, "devDependencies": { - "@opentelemetry/exporter-prometheus": "^0.14.0", + "@opentelemetry/exporter-prometheus": "^0.15.0", "@types/mocha": "8.0.2", "@types/node": "14.0.27", "@types/sinon": "9.0.4", @@ -61,9 +61,10 @@ "typescript": "3.9.7" }, "dependencies": { - "@opentelemetry/api": "^0.14.0", - "@opentelemetry/core": "^0.14.0", - "@opentelemetry/metrics": "^0.14.0", + "@opentelemetry/api": "^0.15.0", + "@opentelemetry/api-metrics": "^0.15.0", + "@opentelemetry/core": "^0.15.0", + "@opentelemetry/metrics": "^0.15.0", "systeminformation": "^4.31.0" } } diff --git a/packages/opentelemetry-host-metrics/src/BaseMetrics.ts b/packages/opentelemetry-host-metrics/src/BaseMetrics.ts index 0ed0b88cb7..ff0a746a77 100644 --- a/packages/opentelemetry-host-metrics/src/BaseMetrics.ts +++ b/packages/opentelemetry-host-metrics/src/BaseMetrics.ts @@ -14,7 +14,8 @@ * limitations under the License. */ -import * as api from '@opentelemetry/api'; +import { Logger, NoopLogger } from '@opentelemetry/api'; +import * as api from '@opentelemetry/api-metrics'; import * as metrics from '@opentelemetry/metrics'; import { VERSION } from './version'; @@ -23,7 +24,7 @@ import { VERSION } from './version'; * Metrics Collector Configuration */ export interface MetricsCollectorConfig { - logger?: api.Logger; + logger?: Logger; // maximum timeout to wait for stats collection default is 500ms maxTimeoutUpdateMS?: number; // Meter Provider @@ -47,7 +48,7 @@ const DEFAULT_KEY = 'name'; * Base Class for metrics */ export abstract class BaseMetrics { - protected _logger: api.Logger | undefined; + protected _logger: Logger | undefined; protected _maxTimeoutUpdateMS: number; protected _meter: metrics.Meter; private _name: string; @@ -55,7 +56,7 @@ export abstract class BaseMetrics { private _metricNameSeparator: string; constructor(config: MetricsCollectorConfig) { - this._logger = config.logger || new api.NoopLogger(); + this._logger = config.logger || new NoopLogger(); this._name = config.name || DEFAULT_NAME; this._maxTimeoutUpdateMS = config.maxTimeoutUpdateMS || DEFAULT_MAX_TIMEOUT_UPDATE_MS; diff --git a/packages/opentelemetry-host-metrics/src/metric.ts b/packages/opentelemetry-host-metrics/src/metric.ts index 374e9996a9..e71c9a0b9c 100644 --- a/packages/opentelemetry-host-metrics/src/metric.ts +++ b/packages/opentelemetry-host-metrics/src/metric.ts @@ -15,7 +15,7 @@ */ import { BaseMetrics } from './BaseMetrics'; -import * as api from '@opentelemetry/api'; +import * as api from '@opentelemetry/api-metrics'; import * as enums from './enum'; import { getCpuUsageData, getMemoryData } from './stats/common'; diff --git a/packages/opentelemetry-test-utils/package.json b/packages/opentelemetry-test-utils/package.json index e23b157817..20d2d33d06 100644 --- a/packages/opentelemetry-test-utils/package.json +++ b/packages/opentelemetry-test-utils/package.json @@ -32,8 +32,8 @@ "typescript": "3.9.6" }, "dependencies": { - "@opentelemetry/api": "^0.14.0", - "@opentelemetry/core": "^0.14.0", - "@opentelemetry/tracing": "^0.14.0" + "@opentelemetry/api": "^0.15.0", + "@opentelemetry/core": "^0.15.0", + "@opentelemetry/tracing": "^0.15.0" } } diff --git a/plugins/node/opentelemetry-hapi-instrumentation/package.json b/plugins/node/opentelemetry-hapi-instrumentation/package.json index 39535f34d6..c186a22042 100644 --- a/plugins/node/opentelemetry-hapi-instrumentation/package.json +++ b/plugins/node/opentelemetry-hapi-instrumentation/package.json @@ -42,9 +42,9 @@ }, "devDependencies": { "@hapi/hapi": "20.0.1", - "@opentelemetry/context-async-hooks": "^0.14.0", - "@opentelemetry/node": "^0.14.0", - "@opentelemetry/tracing": "^0.14.0", + "@opentelemetry/context-async-hooks": "^0.15.0", + "@opentelemetry/node": "^0.15.0", + "@opentelemetry/tracing": "^0.15.0", "@types/hapi__hapi": "20.0.1", "@types/mocha": "7.0.2", "@types/node": "12.12.47", @@ -64,9 +64,9 @@ "typescript": "3.9.6" }, "dependencies": { - "@opentelemetry/api": "^0.14.0", - "@opentelemetry/core": "^0.14.0", - "@opentelemetry/semantic-conventions": "^0.14.0", + "@opentelemetry/api": "^0.15.0", + "@opentelemetry/core": "^0.15.0", + "@opentelemetry/semantic-conventions": "^0.15.0", "shimmer": "^1.2.1" } } diff --git a/plugins/node/opentelemetry-hapi-instrumentation/src/hapi.ts b/plugins/node/opentelemetry-hapi-instrumentation/src/hapi.ts index 8c6c44b3da..d02c4df80a 100644 --- a/plugins/node/opentelemetry-hapi-instrumentation/src/hapi.ts +++ b/plugins/node/opentelemetry-hapi-instrumentation/src/hapi.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import { context, getSpan, setSpan } from '@opentelemetry/api'; import { BasePlugin } from '@opentelemetry/core'; import type * as Hapi from '@hapi/hapi'; import { VERSION } from './version'; @@ -328,7 +329,7 @@ export class HapiInstrumentation extends BasePlugin { const newHandler: PatchableExtMethod = async function ( ...params: Parameters ) { - if (instrumentation._tracer.getCurrentSpan() === undefined) { + if (getSpan(context.active()) === undefined) { return await method(...params); } const metadata = getExtMetadata(extPoint, pluginName); @@ -336,7 +337,7 @@ export class HapiInstrumentation extends BasePlugin { attributes: metadata.attributes, }); let res; - await instrumentation._tracer.withSpan(span, async () => { + await context.with(setSpan(context.active(), span), async () => { res = await method(...params); }); span.end(); @@ -368,7 +369,7 @@ export class HapiInstrumentation extends BasePlugin { h: Hapi.ResponseToolkit, err?: Error ) { - if (instrumentation._tracer.getCurrentSpan() === undefined) { + if (getSpan(context.active()) === undefined) { return await oldHandler(request, h, err); } const metadata = getRouteMetadata(route, pluginName); diff --git a/plugins/node/opentelemetry-hapi-instrumentation/test/hapi-plugin.test.ts b/plugins/node/opentelemetry-hapi-instrumentation/test/hapi-plugin.test.ts index aad232028e..fe76b25bad 100644 --- a/plugins/node/opentelemetry-hapi-instrumentation/test/hapi-plugin.test.ts +++ b/plugins/node/opentelemetry-hapi-instrumentation/test/hapi-plugin.test.ts @@ -14,8 +14,7 @@ * limitations under the License. */ -import { context } from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; +import { context, NoopLogger, setSpan } from '@opentelemetry/api'; import { NodeTracerProvider } from '@opentelemetry/node'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; import { @@ -122,7 +121,7 @@ describe('Hapi Instrumentation - Hapi.Plugin Tests', () => { await server.start(); assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { const res = await server.inject({ method: 'GET', url: '/test', @@ -173,7 +172,7 @@ describe('Hapi Instrumentation - Hapi.Plugin Tests', () => { await server.start(); assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { const res1 = await server.inject({ method: 'GET', url: '/test', @@ -243,7 +242,7 @@ describe('Hapi Instrumentation - Hapi.Plugin Tests', () => { await server.start(); assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { const res1 = await server.inject({ method: 'GET', url: '/test', @@ -301,7 +300,7 @@ describe('Hapi Instrumentation - Hapi.Plugin Tests', () => { await server.start(); assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { const res = await server.inject({ method: 'GET', url: '/package', @@ -342,7 +341,7 @@ describe('Hapi Instrumentation - Hapi.Plugin Tests', () => { await server.start(); assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { const res = await server.inject({ method: 'GET', url: '/package', diff --git a/plugins/node/opentelemetry-hapi-instrumentation/test/hapi-server-ext.test.ts b/plugins/node/opentelemetry-hapi-instrumentation/test/hapi-server-ext.test.ts index a984f13b08..f5fa60ce04 100644 --- a/plugins/node/opentelemetry-hapi-instrumentation/test/hapi-server-ext.test.ts +++ b/plugins/node/opentelemetry-hapi-instrumentation/test/hapi-server-ext.test.ts @@ -14,8 +14,7 @@ * limitations under the License. */ -import { context } from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; +import { context, NoopLogger, setSpan } from '@opentelemetry/api'; import { NodeTracerProvider } from '@opentelemetry/node'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; import { @@ -72,7 +71,7 @@ describe('Hapi Instrumentation - Server.Ext Tests', () => { await server.start(); assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { const res = await server.inject({ method: 'GET', url: '/test', @@ -113,7 +112,7 @@ describe('Hapi Instrumentation - Server.Ext Tests', () => { await server.start(); assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { const res = await server.inject({ method: 'GET', url: '/test', @@ -159,7 +158,7 @@ describe('Hapi Instrumentation - Server.Ext Tests', () => { await server.start(); assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { const res = await server.inject({ method: 'GET', url: '/test', @@ -219,7 +218,7 @@ describe('Hapi Instrumentation - Server.Ext Tests', () => { await server.start(); assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { const res = await server.inject({ method: 'GET', url: '/test', @@ -282,7 +281,7 @@ describe('Hapi Instrumentation - Server.Ext Tests', () => { await server.start(); assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { const res = await server.inject({ method: 'GET', url: '/test', @@ -313,7 +312,7 @@ describe('Hapi Instrumentation - Server.Ext Tests', () => { it('does not instrument Hapi.ServerExtPointFunction handlers', async () => { const rootSpan = tracer.startSpan('rootSpan'); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { server.ext('onPreStart', async (server: hapi.Server) => { return; }); diff --git a/plugins/node/opentelemetry-hapi-instrumentation/test/hapi.test.ts b/plugins/node/opentelemetry-hapi-instrumentation/test/hapi.test.ts index 189b9776ec..410404cab3 100644 --- a/plugins/node/opentelemetry-hapi-instrumentation/test/hapi.test.ts +++ b/plugins/node/opentelemetry-hapi-instrumentation/test/hapi.test.ts @@ -14,8 +14,7 @@ * limitations under the License. */ -import { context } from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; +import { context, NoopLogger, setSpan } from '@opentelemetry/api'; import { NodeTracerProvider } from '@opentelemetry/node'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; import { @@ -75,7 +74,7 @@ describe('Hapi Instrumentation - Core Tests', () => { await server.start(); assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { const res = await server.inject({ method: 'GET', url: '/', @@ -117,7 +116,7 @@ describe('Hapi Instrumentation - Core Tests', () => { await server.start(); assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { const res = await server.inject({ method: 'GET', url: '/', @@ -162,7 +161,7 @@ describe('Hapi Instrumentation - Core Tests', () => { await server.start(); assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { const res = await server.inject({ method: 'GET', url: '/', @@ -210,7 +209,7 @@ describe('Hapi Instrumentation - Core Tests', () => { await server.start(); assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { const resFirst = await server.inject({ method: 'GET', url: '/first', @@ -264,7 +263,7 @@ describe('Hapi Instrumentation - Core Tests', () => { await server.start(); assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { const res = await server.inject({ method: 'GET', url: '/users/1', @@ -334,7 +333,7 @@ describe('Hapi Instrumentation - Core Tests', () => { assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { const res = await server.inject({ method: 'GET', url: '/', diff --git a/plugins/node/opentelemetry-instrumentation-graphql/package.json b/plugins/node/opentelemetry-instrumentation-graphql/package.json index 83b26371ee..44c1c3f9cd 100644 --- a/plugins/node/opentelemetry-instrumentation-graphql/package.json +++ b/plugins/node/opentelemetry-instrumentation-graphql/package.json @@ -43,7 +43,7 @@ "access": "public" }, "devDependencies": { - "@opentelemetry/tracing": "^0.14.0", + "@opentelemetry/tracing": "^0.15.0", "@types/graphql": "14.5.0", "@types/mocha": "8.0.1", "@types/node": "14.0.27", @@ -61,7 +61,7 @@ "typescript": "3.9.7" }, "dependencies": { - "@opentelemetry/api": "^0.14.0", - "@opentelemetry/instrumentation": "^0.14.0" + "@opentelemetry/api": "^0.15.0", + "@opentelemetry/instrumentation": "^0.15.0" } } diff --git a/plugins/node/opentelemetry-instrumentation-graphql/src/graphql.ts b/plugins/node/opentelemetry-instrumentation-graphql/src/graphql.ts index 0dee195030..4c90795f4b 100644 --- a/plugins/node/opentelemetry-instrumentation-graphql/src/graphql.ts +++ b/plugins/node/opentelemetry-instrumentation-graphql/src/graphql.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import { context, setSpan } from '@opentelemetry/api'; import { isWrapped, InstrumentationBase, @@ -58,6 +59,8 @@ const DEFAULT_CONFIG: GraphQLInstrumentationConfig = { allowValues: false, }; +const supportedVersions = ['15.*']; + export class GraphQLInstrumentation extends InstrumentationBase { constructor( config: GraphQLInstrumentationConfig & InstrumentationConfig = {} @@ -76,7 +79,7 @@ export class GraphQLInstrumentation extends InstrumentationBase { protected init() { const module = new InstrumentationNodeModuleDefinition( 'graphql', - ['15.*'] + supportedVersions ); module.files.push(this._addPatchingExecute()); module.files.push(this._addPatchingParser()); @@ -90,6 +93,7 @@ export class GraphQLInstrumentation extends InstrumentationBase { > { return new InstrumentationNodeModuleFile( 'graphql/execution/execute.js', + supportedVersions, // cannot make it work with appropriate type as execute function has 2 //types and/cannot import function but only types (moduleExports: any) => { @@ -116,6 +120,7 @@ export class GraphQLInstrumentation extends InstrumentationBase { > { return new InstrumentationNodeModuleFile( 'graphql/language/parser.js', + supportedVersions, moduleExports => { if (isWrapped(moduleExports.execute)) { this._unwrap(moduleExports, 'parse'); @@ -136,6 +141,7 @@ export class GraphQLInstrumentation extends InstrumentationBase { > { return new InstrumentationNodeModuleFile( 'graphql/validation/validate.js', + supportedVersions, moduleExports => { if (isWrapped(moduleExports.execute)) { this._unwrap(moduleExports, 'validate'); @@ -209,7 +215,7 @@ export class GraphQLInstrumentation extends InstrumentationBase { fields: {}, }; - return instrumentation.tracer.withSpan(span, () => { + return context.with(setSpan(context.active(), span), () => { return safeExecuteInTheMiddle< PromiseOrValue >( @@ -273,7 +279,7 @@ export class GraphQLInstrumentation extends InstrumentationBase { const config = this._getConfig(); const span = this.tracer.startSpan(SpanNames.PARSE); - return this.tracer.withSpan(span, () => { + return context.with(setSpan(context.active(), span), () => { return safeExecuteInTheMiddle< graphqlTypes.DocumentNode & ObjectWithGraphQLData >( @@ -306,7 +312,7 @@ export class GraphQLInstrumentation extends InstrumentationBase { ): ReadonlyArray { const span = this.tracer.startSpan(SpanNames.VALIDATE, {}); - return this.tracer.withSpan(span, () => { + return context.with(setSpan(context.active(), span), () => { return safeExecuteInTheMiddle>( () => { return original.call( diff --git a/plugins/node/opentelemetry-instrumentation-graphql/src/utils.ts b/plugins/node/opentelemetry-instrumentation-graphql/src/utils.ts index de7e6c80ba..c6ea59eacd 100644 --- a/plugins/node/opentelemetry-instrumentation-graphql/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-graphql/src/utils.ts @@ -103,7 +103,7 @@ function createResolverSpan( { attributes, }, - parentSpan ? api.setActiveSpan(api.context.active(), parentSpan) : undefined + parentSpan ? api.setSpan(api.context.active(), parentSpan) : undefined ); const document = contextValue[OTEL_GRAPHQL_DATA_SYMBOL].source; @@ -356,23 +356,26 @@ export function wrapFieldResolver( shouldEndSpan = newField.spanAdded; } - return tracer.withSpan(field.span, () => { - return safeExecuteInTheMiddleAsync< - | Maybe> - | Promise< - Maybe> - > - >( - () => { - return fieldResolver.call(this, source, args, contextValue, info); - }, - err => { - if (shouldEndSpan) { - endSpan(field.span, err); + return api.context.with( + api.setSpan(api.context.active(), field.span), + () => { + return safeExecuteInTheMiddleAsync< + | Maybe> + | Promise< + Maybe> + > + >( + () => { + return fieldResolver.call(this, source, args, contextValue, info); + }, + err => { + if (shouldEndSpan) { + endSpan(field.span, err); + } } - } - ); - }); + ); + } + ); } (wrappedFieldResolver as OtelPatched)[OTEL_PATCHED_SYMBOL] = true; diff --git a/plugins/node/opentelemetry-koa-instrumentation/package.json b/plugins/node/opentelemetry-koa-instrumentation/package.json index 6ed5f4c222..7c7e3b2a18 100644 --- a/plugins/node/opentelemetry-koa-instrumentation/package.json +++ b/plugins/node/opentelemetry-koa-instrumentation/package.json @@ -44,9 +44,9 @@ }, "devDependencies": { "@koa/router": "9.4.0", - "@opentelemetry/context-async-hooks": "^0.14.0", - "@opentelemetry/node": "^0.14.0", - "@opentelemetry/tracing": "^0.14.0", + "@opentelemetry/context-async-hooks": "^0.15.0", + "@opentelemetry/node": "^0.15.0", + "@opentelemetry/tracing": "^0.15.0", "@types/koa": "2.11.4", "@types/koa__router": "8.0.2", "@types/mocha": "7.0.2", @@ -67,9 +67,9 @@ "typescript": "3.9.6" }, "dependencies": { - "@opentelemetry/api": "^0.14.0", - "@opentelemetry/core": "^0.14.0", - "@opentelemetry/semantic-conventions": "^0.14.0", + "@opentelemetry/api": "^0.15.0", + "@opentelemetry/core": "^0.15.0", + "@opentelemetry/semantic-conventions": "^0.15.0", "shimmer": "^1.2.1" } } diff --git a/plugins/node/opentelemetry-koa-instrumentation/src/koa.ts b/plugins/node/opentelemetry-koa-instrumentation/src/koa.ts index 175f1e8712..a90c3dfa84 100644 --- a/plugins/node/opentelemetry-koa-instrumentation/src/koa.ts +++ b/plugins/node/opentelemetry-koa-instrumentation/src/koa.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import * as api from '@opentelemetry/api'; import { BasePlugin } from '@opentelemetry/core'; import type * as koa from 'koa'; import * as shimmer from 'shimmer'; @@ -117,7 +118,7 @@ export class KoaInstrumentation extends BasePlugin { middlewareLayer[kLayerPatched] = true; this._logger.debug('patching Koa middleware layer'); return async (context: KoaContext, next: koa.Next) => { - if (this._tracer.getCurrentSpan() === undefined) { + if (api.getSpan(api.context.active()) === undefined) { return middlewareLayer(context, next); } const metadata = getMiddlewareMetadata( @@ -130,16 +131,19 @@ export class KoaInstrumentation extends BasePlugin { attributes: metadata.attributes, }); - return this._tracer.withSpan(span, async () => { - try { - return await middlewareLayer(context, next); - } catch (err) { - span.recordException(err); - throw err; - } finally { - span.end(); + return api.context.with( + api.setSpan(api.context.active(), span), + async () => { + try { + return await middlewareLayer(context, next); + } catch (err) { + span.recordException(err); + throw err; + } finally { + span.end(); + } } - }); + ); }; } } diff --git a/plugins/node/opentelemetry-koa-instrumentation/test/koa-router.test.ts b/plugins/node/opentelemetry-koa-instrumentation/test/koa-router.test.ts index 0a93852cf8..3fa43aeef3 100644 --- a/plugins/node/opentelemetry-koa-instrumentation/test/koa-router.test.ts +++ b/plugins/node/opentelemetry-koa-instrumentation/test/koa-router.test.ts @@ -14,8 +14,7 @@ * limitations under the License. */ -import { context } from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; +import { context, setSpan, NoopLogger } from '@opentelemetry/api'; import { NodeTracerProvider } from '@opentelemetry/node'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; import { @@ -86,7 +85,9 @@ describe('Koa Instrumentation - Router Tests', () => { describe('Instrumenting @koa/router calls', () => { it('should create a child span for middlewares', async () => { const rootSpan = tracer.startSpan('rootSpan'); - app.use((ctx, next) => tracer.withSpan(rootSpan, next)); + app.use((ctx, next) => + context.with(setSpan(context.active(), rootSpan), next) + ); const router = new KoaRouter(); router.get('/post/:id', ctx => { @@ -95,7 +96,7 @@ describe('Koa Instrumentation - Router Tests', () => { app.use(router.routes()); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { await httpRequest.get(`http://localhost:${port}/post/0`); rootSpan.end(); @@ -124,7 +125,9 @@ describe('Koa Instrumentation - Router Tests', () => { it('should correctly instrument nested routers', async () => { const rootSpan = tracer.startSpan('rootSpan'); - app.use((ctx, next) => tracer.withSpan(rootSpan, next)); + app.use((ctx, next) => + context.with(setSpan(context.active(), rootSpan), next) + ); const router = new KoaRouter(); const nestedRouter = new KoaRouter(); @@ -135,7 +138,7 @@ describe('Koa Instrumentation - Router Tests', () => { router.use('/:first', nestedRouter.routes()); app.use(router.routes()); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { await httpRequest.get(`http://localhost:${port}/test/post/0`); rootSpan.end(); @@ -164,7 +167,9 @@ describe('Koa Instrumentation - Router Tests', () => { it('should correctly instrument prefixed routers', async () => { const rootSpan = tracer.startSpan('rootSpan'); - app.use((ctx, next) => tracer.withSpan(rootSpan, next)); + app.use((ctx, next) => + context.with(setSpan(context.active(), rootSpan), next) + ); const router = new KoaRouter(); router.get('/post/:id', ctx => { @@ -173,7 +178,7 @@ describe('Koa Instrumentation - Router Tests', () => { router.prefix('/:first'); app.use(router.routes()); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { await httpRequest.get(`http://localhost:${port}/test/post/0`); rootSpan.end(); diff --git a/plugins/node/opentelemetry-koa-instrumentation/test/koa.test.ts b/plugins/node/opentelemetry-koa-instrumentation/test/koa.test.ts index ce541ffa2e..087f9c6ef0 100644 --- a/plugins/node/opentelemetry-koa-instrumentation/test/koa.test.ts +++ b/plugins/node/opentelemetry-koa-instrumentation/test/koa.test.ts @@ -14,8 +14,7 @@ * limitations under the License. */ -import { context } from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; +import { context, setSpan, NoopLogger } from '@opentelemetry/api'; import { NodeTracerProvider } from '@opentelemetry/node'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; import { @@ -117,12 +116,14 @@ describe('Koa Instrumentation - Core Tests', () => { describe('Instrumenting core middleware calls', () => { it('should create a child span for middlewares', async () => { const rootSpan = tracer.startSpan('rootSpan'); - app.use((ctx, next) => tracer.withSpan(rootSpan, next)); + app.use((ctx, next) => + context.with(setSpan(context.active(), rootSpan), next) + ); app.use(customMiddleware); app.use(simpleResponse); app.use(spanCreateMiddleware); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { await httpRequest.get(`http://localhost:${port}`); rootSpan.end(); assert.deepStrictEqual(memoryExporter.getFinishedSpans().length, 8); @@ -178,10 +179,12 @@ describe('Koa Instrumentation - Core Tests', () => { it('should handle async middleware functions', async () => { const rootSpan = tracer.startSpan('rootSpan'); - app.use((ctx, next) => tracer.withSpan(rootSpan, next)); + app.use((ctx, next) => + context.with(setSpan(context.active(), rootSpan), next) + ); app.use(asyncMiddleware); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { await httpRequest.get(`http://localhost:${port}`); rootSpan.end(); assert.deepStrictEqual(memoryExporter.getFinishedSpans().length, 3); @@ -204,7 +207,9 @@ describe('Koa Instrumentation - Core Tests', () => { it('should propagate exceptions in the middleware while marking the span with an exception', async () => { const rootSpan = tracer.startSpan('rootSpan'); - app.use((_ctx, next) => tracer.withSpan(rootSpan, next)); + app.use((_ctx, next) => + context.with(setSpan(context.active(), rootSpan), next) + ); app.use(failingMiddleware); const res = await httpRequest.get(`http://localhost:${port}`); assert.deepStrictEqual(res, 'Internal Server Error'); @@ -243,7 +248,7 @@ describe('Koa Instrumentation - Core Tests', () => { const rootSpan = tracer.startSpan('rootSpan'); app.use(customMiddleware); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { await httpRequest.get(`http://localhost:${port}`); rootSpan.end(); assert.deepStrictEqual(memoryExporter.getFinishedSpans().length, 1); diff --git a/plugins/node/opentelemetry-plugin-dns/package.json b/plugins/node/opentelemetry-plugin-dns/package.json index 6c525f9607..d3bc4731c2 100644 --- a/plugins/node/opentelemetry-plugin-dns/package.json +++ b/plugins/node/opentelemetry-plugin-dns/package.json @@ -41,8 +41,8 @@ "access": "public" }, "devDependencies": { - "@opentelemetry/node": "^0.14.0", - "@opentelemetry/tracing": "^0.14.0", + "@opentelemetry/node": "^0.15.0", + "@opentelemetry/tracing": "^0.15.0", "@types/mocha": "7.0.2", "@types/node": "14.0.27", "@types/semver": "7.3.1", @@ -61,8 +61,8 @@ "typescript": "3.9.7" }, "dependencies": { - "@opentelemetry/api": "^0.14.0", - "@opentelemetry/core": "^0.14.0", + "@opentelemetry/api": "^0.15.0", + "@opentelemetry/core": "^0.15.0", "semver": "^7.3.2", "shimmer": "^1.2.1" } diff --git a/plugins/node/opentelemetry-plugin-dns/test/functionals/dns-disable.test.ts b/plugins/node/opentelemetry-plugin-dns/test/functionals/dns-disable.test.ts index cf318bce4d..59f817ef80 100644 --- a/plugins/node/opentelemetry-plugin-dns/test/functionals/dns-disable.test.ts +++ b/plugins/node/opentelemetry-plugin-dns/test/functionals/dns-disable.test.ts @@ -19,7 +19,7 @@ import { SimpleSpanProcessor, } from '@opentelemetry/tracing'; import * as assert from 'assert'; -import { NoopLogger } from '@opentelemetry/core'; +import { NoopLogger, context } from '@opentelemetry/api'; import { NodeTracerProvider } from '@opentelemetry/node'; import { plugin } from '../../src/dns'; import * as sinon from 'sinon'; @@ -38,8 +38,8 @@ describe('DnsPlugin', () => { }); beforeEach(() => { - tracer.startSpan = sinon.spy(); - tracer.withSpan = sinon.spy(); + sinon.spy(tracer, 'startSpan'); + sinon.spy(context, 'with'); }); afterEach(() => { @@ -59,7 +59,7 @@ describe('DnsPlugin', () => { assert.strictEqual(spans.length, 0); assert.strictEqual(dns.lookup.__wrapped, undefined); - assert.strictEqual((tracer.withSpan as sinon.SinonSpy).called, false); + assert.strictEqual((context.with as sinon.SinonSpy).called, false); done(); }); }); diff --git a/plugins/node/opentelemetry-plugin-dns/test/functionals/dns-enable.test.ts b/plugins/node/opentelemetry-plugin-dns/test/functionals/dns-enable.test.ts index 356b1dc726..13f520845f 100644 --- a/plugins/node/opentelemetry-plugin-dns/test/functionals/dns-enable.test.ts +++ b/plugins/node/opentelemetry-plugin-dns/test/functionals/dns-enable.test.ts @@ -19,7 +19,7 @@ import { SimpleSpanProcessor, } from '@opentelemetry/tracing'; import * as assert from 'assert'; -import { NoopLogger } from '@opentelemetry/core'; +import { NoopLogger } from '@opentelemetry/api'; import { NodeTracerProvider } from '@opentelemetry/node'; import { plugin, DnsPlugin } from '../../src/dns'; import * as dns from 'dns'; diff --git a/plugins/node/opentelemetry-plugin-dns/test/functionals/utils.test.ts b/plugins/node/opentelemetry-plugin-dns/test/functionals/utils.test.ts index f07347368b..135c8f7395 100644 --- a/plugins/node/opentelemetry-plugin-dns/test/functionals/utils.test.ts +++ b/plugins/node/opentelemetry-plugin-dns/test/functionals/utils.test.ts @@ -14,9 +14,13 @@ * limitations under the License. */ -import { NoopLogger } from '@opentelemetry/core'; import { BasicTracerProvider, Span } from '@opentelemetry/tracing'; -import { ROOT_CONTEXT, SpanKind, TraceFlags } from '@opentelemetry/api'; +import { + ROOT_CONTEXT, + SpanKind, + TraceFlags, + NoopLogger, +} from '@opentelemetry/api'; import * as assert from 'assert'; import * as sinon from 'sinon'; import { AttributeNames } from '../../src/enums/AttributeNames'; diff --git a/plugins/node/opentelemetry-plugin-dns/test/integrations/dns-lookup.test.ts b/plugins/node/opentelemetry-plugin-dns/test/integrations/dns-lookup.test.ts index 4897b298fa..56d3c7534c 100644 --- a/plugins/node/opentelemetry-plugin-dns/test/integrations/dns-lookup.test.ts +++ b/plugins/node/opentelemetry-plugin-dns/test/integrations/dns-lookup.test.ts @@ -19,7 +19,7 @@ import { SimpleSpanProcessor, } from '@opentelemetry/tracing'; import * as assert from 'assert'; -import { NoopLogger } from '@opentelemetry/core'; +import { NoopLogger } from '@opentelemetry/api'; import { NodeTracerProvider } from '@opentelemetry/node'; import { plugin } from '../../src/dns'; import * as dns from 'dns'; diff --git a/plugins/node/opentelemetry-plugin-dns/test/integrations/dnspromise-lookup.test.ts b/plugins/node/opentelemetry-plugin-dns/test/integrations/dnspromise-lookup.test.ts index 19f6b484bc..8e7040756c 100644 --- a/plugins/node/opentelemetry-plugin-dns/test/integrations/dnspromise-lookup.test.ts +++ b/plugins/node/opentelemetry-plugin-dns/test/integrations/dnspromise-lookup.test.ts @@ -19,7 +19,7 @@ import { SimpleSpanProcessor, } from '@opentelemetry/tracing'; import * as assert from 'assert'; -import { NoopLogger } from '@opentelemetry/core'; +import { NoopLogger } from '@opentelemetry/api'; import { NodeTracerProvider } from '@opentelemetry/node'; import { plugin } from '../../src/dns'; import * as dns from 'dns'; diff --git a/plugins/node/opentelemetry-plugin-express/package.json b/plugins/node/opentelemetry-plugin-express/package.json index 897bdc28e7..af3ec0b434 100644 --- a/plugins/node/opentelemetry-plugin-express/package.json +++ b/plugins/node/opentelemetry-plugin-express/package.json @@ -42,9 +42,9 @@ "access": "public" }, "devDependencies": { - "@opentelemetry/context-async-hooks": "^0.14.0", - "@opentelemetry/node": "^0.14.0", - "@opentelemetry/tracing": "^0.14.0", + "@opentelemetry/context-async-hooks": "^0.15.0", + "@opentelemetry/node": "^0.15.0", + "@opentelemetry/tracing": "^0.15.0", "@types/express": "4.17.7", "@types/mocha": "7.0.2", "@types/node": "14.0.27", @@ -63,8 +63,8 @@ "typescript": "3.9.7" }, "dependencies": { - "@opentelemetry/api": "^0.14.0", - "@opentelemetry/core": "^0.14.0", + "@opentelemetry/api": "^0.15.0", + "@opentelemetry/core": "^0.15.0", "shimmer": "^1.2.1" } } diff --git a/plugins/node/opentelemetry-plugin-express/src/express.ts b/plugins/node/opentelemetry-plugin-express/src/express.ts index a43adbc7c6..e8ad8a5e27 100644 --- a/plugins/node/opentelemetry-plugin-express/src/express.ts +++ b/plugins/node/opentelemetry-plugin-express/src/express.ts @@ -15,7 +15,7 @@ */ import { BasePlugin, hrTime } from '@opentelemetry/core'; -import { Attributes } from '@opentelemetry/api'; +import { Attributes, getSpan, context } from '@opentelemetry/api'; import * as express from 'express'; import * as core from 'express-serve-static-core'; import * as shimmer from 'shimmer'; @@ -188,7 +188,7 @@ export class ExpressPlugin extends BasePlugin { metadata.attributes[AttributeNames.EXPRESS_TYPE] === ExpressLayerType.REQUEST_HANDLER ) { - const parent = plugin._tracer.getCurrentSpan() as ExpressPluginSpan; + const parent = getSpan(context.active()) as ExpressPluginSpan; if (parent?.name) { const parentRoute = parent.name.split(' ')[1]; if (!route.includes(parentRoute)) { @@ -201,7 +201,7 @@ export class ExpressPlugin extends BasePlugin { if (isLayerIgnored(metadata.name, type, plugin._config)) { return original.apply(this, arguments); } - if (plugin._tracer.getCurrentSpan() === undefined) { + if (getSpan(context.active()) === undefined) { return original.apply(this, arguments); } @@ -240,7 +240,7 @@ export class ExpressPlugin extends BasePlugin { (req[_LAYERS_STORE_PROPERTY] as string[]).pop(); } const callback = args[callbackIdx] as Function; - return plugin._tracer.bind(callback).apply(this, arguments); + return context.bind(callback).apply(this, arguments); }; } const result = original.apply(this, arguments); diff --git a/plugins/node/opentelemetry-plugin-express/test/express.test.ts b/plugins/node/opentelemetry-plugin-express/test/express.test.ts index 33bdd50915..03065e566a 100644 --- a/plugins/node/opentelemetry-plugin-express/test/express.test.ts +++ b/plugins/node/opentelemetry-plugin-express/test/express.test.ts @@ -14,8 +14,7 @@ * limitations under the License. */ -import { context, Span, Tracer } from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; +import { context, setSpan, NoopLogger, Span, Tracer } from '@opentelemetry/api'; import { NodeTracerProvider } from '@opentelemetry/node'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; import { @@ -60,7 +59,9 @@ const serverWithMiddleware = async ( ): Promise => { const app = express(); if (tracer) { - app.use((req, res, next) => tracer.withSpan(rootSpan, next)); + app.use((req, res, next) => + context.with(setSpan(context.active(), rootSpan), next) + ); } app.use(express.json()); @@ -111,7 +112,9 @@ describe('Express Plugin', () => { it('should create a child span for middlewares', async () => { const rootSpan = tracer.startSpan('rootSpan') as ExpressPluginSpan; const app = express(); - app.use((req, res, next) => tracer.withSpan(rootSpan, next)); + app.use((req, res, next) => + context.with(setSpan(context.active(), rootSpan), next) + ); app.use(express.json()); const customMiddleware: express.RequestHandler = (req, res, next) => { for (let i = 0; i < 1000000; i++) { @@ -136,7 +139,7 @@ describe('Express Plugin', () => { }); const port = (server.address() as AddressInfo).port; assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { const response = await httpRequest.get( `http://localhost:${port}/toto/tata` ); @@ -202,7 +205,7 @@ describe('Express Plugin', () => { }); const port = (server.address() as AddressInfo).port; assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { const response = await httpRequest.get( `http://localhost:${port}/toto/tata` ); @@ -241,7 +244,7 @@ describe('Express Plugin', () => { }); const port = (server.address() as AddressInfo).port; assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { const response = await httpRequest.get( `http://localhost:${port}/toto/tata` ); @@ -280,7 +283,7 @@ describe('Express Plugin', () => { }); const port = (server.address() as AddressInfo).port; assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { const response = await httpRequest.get( `http://localhost:${port}/toto/tata` ); @@ -342,7 +345,7 @@ describe('Express Plugin', () => { const port = (server.address() as AddressInfo).port; assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { await httpRequest.get(`http://localhost:${port}/toto/tata`); rootSpan.end(); assert.deepEqual( @@ -380,7 +383,9 @@ describe('Express Plugin', () => { plugin.enable(express, provider, logger, config); rootSpan = tracer.startSpan('rootSpan') as ExpressPluginSpan; const app = express(); - app.use((req, res, next) => tracer.withSpan(rootSpan, next)); + app.use((req, res, next) => + context.with(setSpan(context.active(), rootSpan), next) + ); app.use(express.json()); app.use((req, res, next) => { for (let i = 0; i < 1000; i++) {} @@ -405,7 +410,7 @@ describe('Express Plugin', () => { it('should ignore all ExpressLayerType based on config', async () => { const port = (server.address() as AddressInfo).port; assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { await httpRequest.get(`http://localhost:${port}/toto/tata`); rootSpan.end(); assert.deepStrictEqual( @@ -428,7 +433,7 @@ describe('Express Plugin', () => { it('root span name should be modified to GET /todo/:id', async () => { const port = (server.address() as AddressInfo).port; assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { await httpRequest.get(`http://localhost:${port}/toto/tata`); rootSpan.end(); assert.strictEqual(rootSpan.name, 'GET /toto/:id'); @@ -456,7 +461,7 @@ describe('Express Plugin', () => { await new Promise(resolve => server.listen(0, resolve)); const port = (server.address() as AddressInfo).port; assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); - await tracer.withSpan(rootSpan, async () => { + await context.with(setSpan(context.active(), rootSpan), async () => { await httpRequest.get(`http://localhost:${port}/toto/tata`); rootSpan.end(); assert.deepEqual(memoryExporter.getFinishedSpans().length, 1); diff --git a/plugins/node/opentelemetry-plugin-ioredis/package.json b/plugins/node/opentelemetry-plugin-ioredis/package.json index a0532c587e..ac315f76e2 100644 --- a/plugins/node/opentelemetry-plugin-ioredis/package.json +++ b/plugins/node/opentelemetry-plugin-ioredis/package.json @@ -44,10 +44,10 @@ "access": "public" }, "devDependencies": { - "@opentelemetry/context-async-hooks": "^0.14.0", - "@opentelemetry/node": "^0.14.0", + "@opentelemetry/context-async-hooks": "^0.15.0", + "@opentelemetry/node": "^0.15.0", "@opentelemetry/test-utils": "^0.12.1", - "@opentelemetry/tracing": "^0.14.0", + "@opentelemetry/tracing": "^0.15.0", "@types/ioredis": "4.17.3", "@types/mocha": "7.0.2", "@types/node": "14.0.27", @@ -66,9 +66,9 @@ "typescript": "3.9.7" }, "dependencies": { - "@opentelemetry/api": "^0.14.0", - "@opentelemetry/core": "^0.14.0", - "@opentelemetry/semantic-conventions": "^0.14.0", + "@opentelemetry/api": "^0.15.0", + "@opentelemetry/core": "^0.15.0", + "@opentelemetry/semantic-conventions": "^0.15.0", "shimmer": "^1.2.1" } } diff --git a/plugins/node/opentelemetry-plugin-ioredis/src/utils.ts b/plugins/node/opentelemetry-plugin-ioredis/src/utils.ts index 7c38e669e0..58201d5009 100644 --- a/plugins/node/opentelemetry-plugin-ioredis/src/utils.ts +++ b/plugins/node/opentelemetry-plugin-ioredis/src/utils.ts @@ -15,7 +15,14 @@ */ import type * as ioredisTypes from 'ioredis'; -import { Tracer, SpanKind, Span, StatusCode } from '@opentelemetry/api'; +import { + Tracer, + SpanKind, + Span, + StatusCode, + getSpan, + context, +} from '@opentelemetry/api'; import { IoredisCommand, IoredisPluginConfig, @@ -84,7 +91,7 @@ export const traceSendCommand = ( return original.apply(this, arguments); } // Do not trace if there is not parent span - if (tracer.getCurrentSpan() === undefined) { + if (getSpan(context.active()) === undefined) { return original.apply(this, arguments); } diff --git a/plugins/node/opentelemetry-plugin-ioredis/test/ioredis.test.ts b/plugins/node/opentelemetry-plugin-ioredis/test/ioredis.test.ts index f10729a933..5027d992d3 100644 --- a/plugins/node/opentelemetry-plugin-ioredis/test/ioredis.test.ts +++ b/plugins/node/opentelemetry-plugin-ioredis/test/ioredis.test.ts @@ -14,8 +14,15 @@ * limitations under the License. */ -import { StatusCode, context, SpanKind, Status } from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; +import { + StatusCode, + context, + SpanKind, + Status, + NoopLogger, + getSpan, + setSpan, +} from '@opentelemetry/api'; import { NodeTracerProvider } from '@opentelemetry/node'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; import * as testUtils from '@opentelemetry/test-utils'; @@ -107,10 +114,7 @@ describe('ioredis', () => { const readyHandler = () => { const endedSpans = memoryExporter.getFinishedSpans(); - assert.strictEqual( - provider.getTracer('ioredis-test').getCurrentSpan(), - span - ); + assert.strictEqual(getSpan(context.active()), span); assert.strictEqual(endedSpans.length, 2); assert.strictEqual(endedSpans[0].name, 'connect'); assert.strictEqual(endedSpans[1].name, 'info'); @@ -136,7 +140,7 @@ describe('ioredis', () => { client.quit(done); }; - provider.getTracer('ioredis-test').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { client = new ioredis(URL); client.on('ready', readyHandler); client.on('error', errorHandler); @@ -209,7 +213,7 @@ describe('ioredis', () => { const span = provider .getTracer('ioredis-test') .startSpan('test span'); - provider.getTracer('ioredis-test').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { command.method((err, _result) => { assert.ifError(err); assert.strictEqual(memoryExporter.getFinishedSpans().length, 1); @@ -237,7 +241,7 @@ describe('ioredis', () => { [DatabaseAttribute.DB_STATEMENT]: `hset ${hashKeyName} random random`, }; const span = provider.getTracer('ioredis-test').startSpan('test span'); - await provider.getTracer('ioredis-test').withSpan(span, async () => { + await context.with(setSpan(context.active(), span), async () => { try { await client.hset(hashKeyName, 'random', 'random'); assert.strictEqual(memoryExporter.getFinishedSpans().length, 1); @@ -265,7 +269,7 @@ describe('ioredis', () => { [DatabaseAttribute.DB_STATEMENT]: 'scan 0', }; const span = provider.getTracer('ioredis-test').startSpan('test span'); - provider.getTracer('ioredis-test').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const stream = client.scanStream(); stream .on('data', resultKeys => { @@ -301,7 +305,7 @@ describe('ioredis', () => { it('should create a child span for pubsub', async () => { const span = provider.getTracer('ioredis-test').startSpan('test span'); - await provider.getTracer('ioredis-test').withSpan(span, async () => { + await context.with(setSpan(context.active(), span), async () => { try { const pub = new ioredis(URL); const sub = new ioredis(URL); @@ -360,7 +364,7 @@ describe('ioredis', () => { }; const span = provider.getTracer('ioredis-test').startSpan('test span'); - provider.getTracer('ioredis-test').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { // This will define a command echo: client.defineCommand('echo', { numberOfKeys: 1, @@ -403,7 +407,7 @@ describe('ioredis', () => { }; const span = provider.getTracer('ioredis-test').startSpan('test span'); - provider.getTracer('ioredis-test').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { client .multi() .set('foo', 'bar') @@ -439,7 +443,7 @@ describe('ioredis', () => { }; const span = provider.getTracer('ioredis-test').startSpan('test span'); - provider.getTracer('ioredis-test').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const pipeline = client.pipeline(); pipeline.set('foo', 'bar'); pipeline.del('cc'); @@ -472,7 +476,7 @@ describe('ioredis', () => { [DatabaseAttribute.DB_STATEMENT]: `get ${testKeyName}`, }; const span = provider.getTracer('ioredis-test').startSpan('test span'); - await provider.getTracer('ioredis-test').withSpan(span, async () => { + await context.with(setSpan(context.active(), span), async () => { try { const value = await client.get(testKeyName); assert.strictEqual(value, 'data'); @@ -501,7 +505,7 @@ describe('ioredis', () => { [DatabaseAttribute.DB_STATEMENT]: `del ${testKeyName}`, }; const span = provider.getTracer('ioredis-test').startSpan('test span'); - await provider.getTracer('ioredis-test').withSpan(span, async () => { + await context.with(setSpan(context.active(), span), async () => { try { const result = await client.del(testKeyName); assert.strictEqual(result, 1); @@ -561,7 +565,7 @@ describe('ioredis', () => { const span = provider .getTracer('ioredis-test') .startSpan('test span'); - provider.getTracer('ioredis-test').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { command.method((err, _result) => { assert.ifError(err); assert.strictEqual(memoryExporter.getFinishedSpans().length, 1); @@ -594,7 +598,7 @@ describe('ioredis', () => { const span = provider .getTracer('ioredis-test') .startSpan('test span'); - provider.getTracer('ioredis-test').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { operation.method((err, _) => { assert.ifError(err); assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); @@ -610,7 +614,7 @@ describe('ioredis', () => { it('should not create a child span for hset promise upon error', async () => { const span = provider.getTracer('ioredis-test').startSpan('test span'); - await provider.getTracer('ioredis-test').withSpan(span, async () => { + await context.with(setSpan(context.active(), span), async () => { try { await client.hset(hashKeyName, 'random', 'random'); assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); diff --git a/plugins/node/opentelemetry-plugin-mongodb/package.json b/plugins/node/opentelemetry-plugin-mongodb/package.json index 0cc5f907dd..62dd7a95e1 100644 --- a/plugins/node/opentelemetry-plugin-mongodb/package.json +++ b/plugins/node/opentelemetry-plugin-mongodb/package.json @@ -41,9 +41,9 @@ "access": "public" }, "devDependencies": { - "@opentelemetry/context-async-hooks": "^0.14.0", - "@opentelemetry/node": "^0.14.0", - "@opentelemetry/tracing": "^0.14.0", + "@opentelemetry/context-async-hooks": "^0.15.0", + "@opentelemetry/node": "^0.15.0", + "@opentelemetry/tracing": "^0.15.0", "@types/mocha": "7.0.2", "@types/mongodb": "3.5.25", "@types/node": "14.0.27", @@ -61,9 +61,9 @@ "typescript": "3.9.7" }, "dependencies": { - "@opentelemetry/api": "^0.14.0", - "@opentelemetry/core": "^0.14.0", - "@opentelemetry/semantic-conventions": "^0.14.0", + "@opentelemetry/api": "^0.15.0", + "@opentelemetry/core": "^0.15.0", + "@opentelemetry/semantic-conventions": "^0.15.0", "shimmer": "^1.2.1" } } diff --git a/plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts b/plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts index 18b4fa16fc..83e5521e1b 100644 --- a/plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts +++ b/plugins/node/opentelemetry-plugin-mongodb/src/mongodb.ts @@ -15,7 +15,13 @@ */ import { BasePlugin } from '@opentelemetry/core'; -import { StatusCode, Span, SpanKind } from '@opentelemetry/api'; +import { + getSpan, + StatusCode, + Span, + SpanKind, + context, +} from '@opentelemetry/api'; import type * as mongodb from 'mongodb'; import * as shimmer from 'shimmer'; import { @@ -106,7 +112,7 @@ export class MongoDBPlugin extends BasePlugin { options: {} | Function, callback: Function ): mongodb.Server { - const currentSpan = plugin._tracer.getCurrentSpan(); + const currentSpan = getSpan(context.active()); const resultHandler = typeof options === 'function' ? options : callback; if ( @@ -224,7 +230,7 @@ export class MongoDBPlugin extends BasePlugin { }, ...args: unknown[] ): mongodb.Cursor { - const currentSpan = plugin._tracer.getCurrentSpan(); + const currentSpan = getSpan(context.active()); const resultHandler = args[0]; if (!currentSpan || typeof resultHandler !== 'function') { return original.apply(this, args); diff --git a/plugins/node/opentelemetry-plugin-mongodb/test/mongodb.test.ts b/plugins/node/opentelemetry-plugin-mongodb/test/mongodb.test.ts index 4157bb074a..bd520d8b12 100644 --- a/plugins/node/opentelemetry-plugin-mongodb/test/mongodb.test.ts +++ b/plugins/node/opentelemetry-plugin-mongodb/test/mongodb.test.ts @@ -17,8 +17,8 @@ // for testing locally use this command to run docker // docker run -e MONGODB_DB=opentelemetry-tests -e MONGODB_PORT=27017 -e MONGODB_HOST=localhost -p 27017:27017 --name otmongo mongo -import { context, SpanKind } from '@opentelemetry/api'; -import { NoopLogger, PluginConfig } from '@opentelemetry/core'; +import { context, setSpan, SpanKind, NoopLogger } from '@opentelemetry/api'; +import { PluginConfig } from '@opentelemetry/core'; import { BasicTracerProvider } from '@opentelemetry/tracing'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; import { @@ -107,7 +107,7 @@ describe('MongoDBPlugin', () => { it('should create a child span for insert', done => { const insertData = [{ a: 1 }, { a: 2 }, { a: 3 }]; const span = provider.getTracer('default').startSpan('insertRootSpan'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { collection.insertMany(insertData, (err, result) => { span.end(); assert.ifError(err); @@ -123,7 +123,7 @@ describe('MongoDBPlugin', () => { it('should create a child span for update', done => { const span = provider.getTracer('default').startSpan('updateRootSpan'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { collection.updateOne({ a: 2 }, { $set: { b: 1 } }, (err, result) => { span.end(); assert.ifError(err); @@ -139,7 +139,7 @@ describe('MongoDBPlugin', () => { it('should create a child span for remove', done => { const span = provider.getTracer('default').startSpan('removeRootSpan'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { collection.deleteOne({ a: 3 }, (err, result) => { span.end(); assert.ifError(err); @@ -159,7 +159,7 @@ describe('MongoDBPlugin', () => { plugin.enable(mongodb, provider, logger, enhancedDbConfig); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { collection.insertMany(insertData, (err, result) => { span.end(); assert.ifError(err); @@ -180,7 +180,7 @@ describe('MongoDBPlugin', () => { describe('Instrumenting cursor operations', () => { it('should create a child span for find', done => { const span = provider.getTracer('default').startSpan('findRootSpan'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { collection.find({}).toArray((err, result) => { span.end(); assert.ifError(err); @@ -199,7 +199,7 @@ describe('MongoDBPlugin', () => { describe('Instrumenting command operations', () => { it('should create a child span for create index', done => { const span = provider.getTracer('default').startSpan('indexRootSpan'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { collection.createIndex({ a: 1 }, (err, result) => { span.end(); assert.ifError(err); diff --git a/plugins/node/opentelemetry-plugin-mongodb/test/multiple_versions.test.ts b/plugins/node/opentelemetry-plugin-mongodb/test/multiple_versions.test.ts index 5635e6f4ad..86dc3b5086 100644 --- a/plugins/node/opentelemetry-plugin-mongodb/test/multiple_versions.test.ts +++ b/plugins/node/opentelemetry-plugin-mongodb/test/multiple_versions.test.ts @@ -14,8 +14,7 @@ * limitations under the License. */ -import { context, SpanKind } from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; +import { context, setSpan, NoopLogger, SpanKind } from '@opentelemetry/api'; import { BasicTracerProvider } from '@opentelemetry/tracing'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; import { @@ -99,7 +98,7 @@ describe('Multiple enable on the plugin', () => { const insertData = [{ a: 1 }, { a: 2 }, { a: 3 }]; const span = provider.getTracer('default').startSpan('insertRootSpan'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { collection.insertMany(insertData, (err, result) => { span.end(); assert.ifError(err); diff --git a/plugins/node/opentelemetry-plugin-mysql/package.json b/plugins/node/opentelemetry-plugin-mysql/package.json index 934b63a5a0..60a53d121f 100644 --- a/plugins/node/opentelemetry-plugin-mysql/package.json +++ b/plugins/node/opentelemetry-plugin-mysql/package.json @@ -41,11 +41,11 @@ "access": "public" }, "devDependencies": { - "@opentelemetry/context-async-hooks": "^0.14.0", - "@opentelemetry/node": "^0.14.0", - "@opentelemetry/semantic-conventions": "^0.14.0", + "@opentelemetry/context-async-hooks": "^0.15.0", + "@opentelemetry/node": "^0.15.0", + "@opentelemetry/semantic-conventions": "^0.15.0", "@opentelemetry/test-utils": "^0.12.1", - "@opentelemetry/tracing": "^0.14.0", + "@opentelemetry/tracing": "^0.15.0", "@types/mocha": "7.0.2", "@types/mysql": "2.15.15", "@types/node": "14.0.27", @@ -63,8 +63,8 @@ "typescript": "3.9.7" }, "dependencies": { - "@opentelemetry/api": "^0.14.0", - "@opentelemetry/core": "^0.14.0", + "@opentelemetry/api": "^0.15.0", + "@opentelemetry/core": "^0.15.0", "shimmer": "^1.2.1" } } diff --git a/plugins/node/opentelemetry-plugin-mysql/test/mysql.test.ts b/plugins/node/opentelemetry-plugin-mysql/test/mysql.test.ts index 4653922123..6d8d0092ee 100644 --- a/plugins/node/opentelemetry-plugin-mysql/test/mysql.test.ts +++ b/plugins/node/opentelemetry-plugin-mysql/test/mysql.test.ts @@ -14,8 +14,7 @@ * limitations under the License. */ -import { StatusCode, context } from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; +import { NoopLogger, StatusCode, context, setSpan } from '@opentelemetry/api'; import { NodeTracerProvider } from '@opentelemetry/node'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; import * as testUtils from '@opentelemetry/test-utils'; @@ -127,7 +126,7 @@ describe('mysql@2.x', () => { describe('when the query is a string', () => { it('should name the span accordingly ', done => { const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT 1+1 as solution'; const query = connection.query(sql); @@ -143,7 +142,7 @@ describe('mysql@2.x', () => { describe('when the query is an object', () => { it('should name the span accordingly ', done => { const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT 1+? as solution'; const query = connection.query({ sql, values: [1] }); @@ -159,7 +158,7 @@ describe('mysql@2.x', () => { describe('#Connection', () => { it('should intercept connection.query(text: string)', done => { const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT 1+1 as solution'; const query = connection.query(sql); let rows = 0; @@ -181,7 +180,7 @@ describe('mysql@2.x', () => { it('should intercept connection.query(text: string, callback)', done => { const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT 1+1 as solution'; connection.query(sql, (err, res) => { assert.ifError(err); @@ -197,7 +196,7 @@ describe('mysql@2.x', () => { it('should intercept connection.query(text: options, callback)', done => { const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT 1+? as solution'; connection.query({ sql, values: [1] }, (err, res) => { assert.ifError(err); @@ -213,7 +212,7 @@ describe('mysql@2.x', () => { it('should intercept connection.query(text: options, values: [], callback)', done => { const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT 1+? as solution'; connection.query({ sql }, [1], (err, res) => { assert.ifError(err); @@ -229,7 +228,7 @@ describe('mysql@2.x', () => { it('should intercept connection.query(text: string, values: [], callback)', done => { const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT ? as solution'; connection.query(sql, [1], (err, res) => { assert.ifError(err); @@ -245,7 +244,7 @@ describe('mysql@2.x', () => { it('should intercept connection.query(text: string, value: any, callback)', done => { const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT ? as solution'; connection.query(sql, 1, (err, res) => { assert.ifError(err); @@ -261,7 +260,7 @@ describe('mysql@2.x', () => { it('should attach error messages to spans', done => { const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT ? as solution'; connection.query(sql, (err, res) => { assert.ok(err); @@ -277,7 +276,7 @@ describe('mysql@2.x', () => { describe('#Pool', () => { it('should intercept pool.query(text: string)', done => { const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT 1+1 as solution'; const query = pool.query(sql); let rows = 0; @@ -299,7 +298,7 @@ describe('mysql@2.x', () => { it('should intercept pool.getConnection().query(text: string)', done => { const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT 1+1 as solution'; pool.getConnection((err, conn) => { const query = conn.query(sql); @@ -323,7 +322,7 @@ describe('mysql@2.x', () => { it('should intercept pool.query(text: string, callback)', done => { const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT 1+1 as solution'; pool.query(sql, (err, res) => { assert.ifError(err); @@ -339,7 +338,7 @@ describe('mysql@2.x', () => { it('should intercept pool.getConnection().query(text: string, callback)', done => { const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT 1+1 as solution'; pool.getConnection((err, conn) => { conn.query(sql, (err, res) => { @@ -357,7 +356,7 @@ describe('mysql@2.x', () => { it('should intercept pool.query(text: options, callback)', done => { const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT 1+? as solution'; pool.query({ sql, values: [1] }, (err, res) => { assert.ifError(err); @@ -373,7 +372,7 @@ describe('mysql@2.x', () => { it('should intercept pool.query(text: options, values: [], callback)', done => { const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT 1+? as solution'; pool.query({ sql }, [1], (err, res) => { assert.ifError(err); @@ -389,7 +388,7 @@ describe('mysql@2.x', () => { it('should intercept pool.query(text: string, values: [], callback)', done => { const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT ? as solution'; pool.query(sql, [1], (err, res) => { assert.ifError(err); @@ -405,7 +404,7 @@ describe('mysql@2.x', () => { it('should intercept pool.query(text: string, value: any, callback)', done => { const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT ? as solution'; pool.query(sql, 1, (err, res) => { assert.ifError(err); @@ -421,7 +420,7 @@ describe('mysql@2.x', () => { it('should attach error messages to spans', done => { const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT ? as solution'; pool.query(sql, (err, res) => { assert.ok(err); @@ -439,7 +438,7 @@ describe('mysql@2.x', () => { poolCluster.getConnection((err, poolClusterConnection) => { assert.ifError(err); const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT 1+1 as solution'; const query = poolClusterConnection.query(sql); let rows = 0; @@ -464,7 +463,7 @@ describe('mysql@2.x', () => { poolCluster.getConnection((err, poolClusterConnection) => { assert.ifError(err); const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT 1+1 as solution'; poolClusterConnection.query(sql, (err, res) => { assert.ifError(err); @@ -483,7 +482,7 @@ describe('mysql@2.x', () => { poolCluster.getConnection((err, poolClusterConnection) => { assert.ifError(err); const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT 1+? as solution'; poolClusterConnection.query({ sql, values: [1] }, (err, res) => { assert.ifError(err); @@ -502,7 +501,7 @@ describe('mysql@2.x', () => { poolCluster.getConnection((err, poolClusterConnection) => { assert.ifError(err); const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT 1+? as solution'; // @ts-ignore this is documented https://github.com/mysqljs/mysql#performing-queries // but does not match the typings @@ -523,7 +522,7 @@ describe('mysql@2.x', () => { poolCluster.getConnection((err, poolClusterConnection) => { assert.ifError(err); const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT ? as solution'; poolClusterConnection.query(sql, [1], (err, res) => { assert.ifError(err); @@ -542,7 +541,7 @@ describe('mysql@2.x', () => { poolCluster.getConnection((err, poolClusterConnection) => { assert.ifError(err); const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT ? as solution'; poolClusterConnection.query(sql, 1, (err, res) => { assert.ifError(err); @@ -561,7 +560,7 @@ describe('mysql@2.x', () => { poolCluster.getConnection((err, poolClusterConnection) => { assert.ifError(err); const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT ? as solution'; poolClusterConnection.query(sql, (err, res) => { assert.ok(err); @@ -578,7 +577,7 @@ describe('mysql@2.x', () => { poolCluster.getConnection('name', (err, poolClusterConnection) => { assert.ifError(err); const span = provider.getTracer('default').startSpan('test span'); - provider.getTracer('default').withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const sql = 'SELECT 1 as solution'; poolClusterConnection.query(sql, (err, res) => { assert.ifError(err); diff --git a/plugins/node/opentelemetry-plugin-pg-pool/package.json b/plugins/node/opentelemetry-plugin-pg-pool/package.json index 8e615b521d..3621bf3426 100644 --- a/plugins/node/opentelemetry-plugin-pg-pool/package.json +++ b/plugins/node/opentelemetry-plugin-pg-pool/package.json @@ -47,10 +47,10 @@ "access": "public" }, "devDependencies": { - "@opentelemetry/context-async-hooks": "^0.14.0", + "@opentelemetry/context-async-hooks": "^0.15.0", "@opentelemetry/plugin-pg": "^0.12.1", "@opentelemetry/test-utils": "^0.12.1", - "@opentelemetry/tracing": "^0.14.0", + "@opentelemetry/tracing": "^0.15.0", "@types/mocha": "7.0.2", "@types/node": "14.0.27", "@types/pg": "7.14.4", @@ -71,8 +71,8 @@ "typescript": "3.9.7" }, "dependencies": { - "@opentelemetry/api": "^0.14.0", - "@opentelemetry/core": "^0.14.0", + "@opentelemetry/api": "^0.15.0", + "@opentelemetry/core": "^0.15.0", "shimmer": "^1.2.1" } } diff --git a/plugins/node/opentelemetry-plugin-pg-pool/src/pg-pool.ts b/plugins/node/opentelemetry-plugin-pg-pool/src/pg-pool.ts index de33d534c9..c8b4ba9f77 100644 --- a/plugins/node/opentelemetry-plugin-pg-pool/src/pg-pool.ts +++ b/plugins/node/opentelemetry-plugin-pg-pool/src/pg-pool.ts @@ -15,7 +15,7 @@ */ import { BasePlugin } from '@opentelemetry/core'; -import { StatusCode, SpanKind } from '@opentelemetry/api'; +import { context, StatusCode, SpanKind, getSpan } from '@opentelemetry/api'; import { AttributeNames } from './enums'; import * as shimmer from 'shimmer'; import * as pgPoolTypes from 'pg-pool'; @@ -79,11 +79,11 @@ export class PostgresPoolPlugin extends BasePlugin { ); if (callback) { - const parentSpan = plugin._tracer.getCurrentSpan(); + const parentSpan = getSpan(context.active()); callback = utils.patchCallback(span, callback) as PgPoolCallback; // If a parent span exists, bind the callback if (parentSpan) { - callback = plugin._tracer.bind(callback); + callback = context.bind(callback); } } @@ -95,11 +95,11 @@ export class PostgresPoolPlugin extends BasePlugin { // No callback was provided, return a promise instead if (connectResult instanceof Promise) { const connectResultPromise = connectResult as Promise; - return plugin._tracer.bind( + return context.bind( connectResultPromise - .then((result: any) => { + .then(result => { // Return a pass-along promise which ends the span and then goes to user's orig resolvers - return new Promise((resolve, _) => { + return new Promise(resolve => { span.end(); resolve(result); }); diff --git a/plugins/node/opentelemetry-plugin-pg-pool/test/pg-pool.test.ts b/plugins/node/opentelemetry-plugin-pg-pool/test/pg-pool.test.ts index bd338f65ec..5501db1196 100644 --- a/plugins/node/opentelemetry-plugin-pg-pool/test/pg-pool.test.ts +++ b/plugins/node/opentelemetry-plugin-pg-pool/test/pg-pool.test.ts @@ -18,12 +18,13 @@ import { Attributes, StatusCode, context, + NoopLogger, Span, SpanKind, Status, TimedEvent, + setSpan, } from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; import { BasicTracerProvider } from '@opentelemetry/tracing'; import { plugin as pgPlugin, PostgresPlugin } from '@opentelemetry/plugin-pg'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; @@ -159,7 +160,7 @@ describe('pg-pool@2.x', () => { }; const events: TimedEvent[] = []; const span = provider.getTracer('test-pg-pool').startSpan('test span'); - await provider.getTracer('test-pg-pool').withSpan(span, async () => { + await context.with(setSpan(context.active(), span), async () => { const client = await pool.connect(); runCallbackTest(span, pgPoolattributes, events, unsetStatus, 1, 0); assert.ok(client, 'pool.connect() returns a promise'); @@ -185,7 +186,7 @@ describe('pg-pool@2.x', () => { const parentSpan = provider .getTracer('test-pg-pool') .startSpan('test span'); - provider.getTracer('test-pg-pool').withSpan(parentSpan, () => { + context.with(setSpan(context.active(), parentSpan), () => { const resNoPromise = pool.connect((err, client, release) => { if (err) { return done(err); @@ -239,7 +240,7 @@ describe('pg-pool@2.x', () => { }; const events: TimedEvent[] = []; const span = provider.getTracer('test-pg-pool').startSpan('test span'); - await provider.getTracer('test-pg-pool').withSpan(span, async () => { + await context.with(setSpan(context.active(), span), async () => { const result = await pool.query('SELECT NOW()'); runCallbackTest(span, pgPoolattributes, events, unsetStatus, 2, 0); runCallbackTest(span, pgAttributes, events, unsetStatus, 2, 1); @@ -260,7 +261,7 @@ describe('pg-pool@2.x', () => { const parentSpan = provider .getTracer('test-pg-pool') .startSpan('test span'); - provider.getTracer('test-pg-pool').withSpan(parentSpan, () => { + context.with(setSpan(context.active(), parentSpan), () => { const resNoPromise = pool.query('SELECT NOW()', (err, result) => { if (err) { return done(err); diff --git a/plugins/node/opentelemetry-plugin-pg/package.json b/plugins/node/opentelemetry-plugin-pg/package.json index fefc09341f..299ea55581 100644 --- a/plugins/node/opentelemetry-plugin-pg/package.json +++ b/plugins/node/opentelemetry-plugin-pg/package.json @@ -46,10 +46,10 @@ "access": "public" }, "devDependencies": { - "@opentelemetry/context-async-hooks": "^0.14.0", - "@opentelemetry/node": "^0.14.0", + "@opentelemetry/context-async-hooks": "^0.15.0", + "@opentelemetry/node": "^0.15.0", "@opentelemetry/test-utils": "^0.12.1", - "@opentelemetry/tracing": "^0.14.0", + "@opentelemetry/tracing": "^0.15.0", "@types/mocha": "7.0.2", "@types/node": "14.0.27", "@types/pg": "7.14.4", @@ -68,8 +68,8 @@ "typescript": "3.9.7" }, "dependencies": { - "@opentelemetry/api": "^0.14.0", - "@opentelemetry/core": "^0.14.0", + "@opentelemetry/api": "^0.15.0", + "@opentelemetry/core": "^0.15.0", "shimmer": "^1.2.1" } } diff --git a/plugins/node/opentelemetry-plugin-pg/src/pg.ts b/plugins/node/opentelemetry-plugin-pg/src/pg.ts index 19e0a55516..2a23391550 100644 --- a/plugins/node/opentelemetry-plugin-pg/src/pg.ts +++ b/plugins/node/opentelemetry-plugin-pg/src/pg.ts @@ -15,7 +15,7 @@ */ import { BasePlugin, isWrapped } from '@opentelemetry/core'; -import { StatusCode, Span } from '@opentelemetry/api'; +import { context, StatusCode, Span, getSpan } from '@opentelemetry/api'; import * as pgTypes from 'pg'; import * as shimmer from 'shimmer'; import { @@ -97,7 +97,7 @@ export class PostgresPlugin extends BasePlugin { // Bind callback to parent span if (args.length > 0) { - const parentSpan = plugin._tracer.getCurrentSpan(); + const parentSpan = getSpan(context.active()); if (typeof args[args.length - 1] === 'function') { // Patch ParameterQuery callback args[args.length - 1] = utils.patchCallback( @@ -106,9 +106,7 @@ export class PostgresPlugin extends BasePlugin { ); // If a parent span exists, bind the callback if (parentSpan) { - args[args.length - 1] = plugin._tracer.bind( - args[args.length - 1] - ); + args[args.length - 1] = context.bind(args[args.length - 1]); } } else if ( typeof (args[0] as NormalizedQueryConfig).callback === 'function' @@ -120,7 +118,7 @@ export class PostgresPlugin extends BasePlugin { ); // If a parent span existed, bind the callback if (parentSpan) { - callback = plugin._tracer.bind(callback); + callback = context.bind(callback); } // Copy the callback instead of writing to args.callback so that we don't modify user's diff --git a/plugins/node/opentelemetry-plugin-pg/test/pg.test.ts b/plugins/node/opentelemetry-plugin-pg/test/pg.test.ts index 4447dc9e8a..6098a57cf6 100644 --- a/plugins/node/opentelemetry-plugin-pg/test/pg.test.ts +++ b/plugins/node/opentelemetry-plugin-pg/test/pg.test.ts @@ -18,13 +18,15 @@ import { Attributes, StatusCode, context, + NoopLogger, Span, SpanKind, Status, TimedEvent, + setSpan, + getSpan, } from '@opentelemetry/api'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; -import { NoopLogger } from '@opentelemetry/core'; import * as testUtils from '@opentelemetry/test-utils'; import { BasicTracerProvider, @@ -194,7 +196,7 @@ describe('pg@7.x', () => { }; const events: TimedEvent[] = []; const span = tracer.startSpan('test span'); - tracer.withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const res = client.query('SELECT NOW()', (err, res) => { assert.strictEqual(err, null); assert.ok(res); @@ -214,7 +216,7 @@ describe('pg@7.x', () => { }; const events: TimedEvent[] = []; const span = tracer.startSpan('test span'); - tracer.withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const resNoPromise = client.query(query, values, (err, res) => { assert.strictEqual(err, null); assert.ok(res); @@ -233,7 +235,7 @@ describe('pg@7.x', () => { }; const events: TimedEvent[] = []; const span = tracer.startSpan('test span'); - tracer.withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const resNoPromise = client.query({ text: query, callback: (err: Error, res: pg.QueryResult) => { @@ -255,7 +257,7 @@ describe('pg@7.x', () => { }; const events: TimedEvent[] = []; const span = tracer.startSpan('test span'); - tracer.withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const resNoPromise = client.query({ text: query }, (err, res) => { assert.strictEqual(err, null); assert.ok(res); @@ -275,7 +277,7 @@ describe('pg@7.x', () => { }; const events: TimedEvent[] = []; const span = tracer.startSpan('test span'); - await tracer.withSpan(span, async () => { + await context.with(setSpan(context.active(), span), async () => { const resPromise = await client.query(query, values); try { assert.ok(resPromise); @@ -295,7 +297,7 @@ describe('pg@7.x', () => { }; const events: TimedEvent[] = []; const span = tracer.startSpan('test span'); - await tracer.withSpan(span, async () => { + await context.with(setSpan(context.active(), span), async () => { const resPromise = await client.query({ text: query, values: values, @@ -321,7 +323,7 @@ describe('pg@7.x', () => { const events: TimedEvent[] = []; const span = tracer.startSpan('test span'); - await tracer.withSpan(span, async () => { + await context.with(setSpan(context.active(), span), async () => { try { const resPromise = await client.query({ name: name, @@ -344,7 +346,7 @@ describe('pg@7.x', () => { }; const events: TimedEvent[] = []; const span = tracer.startSpan('test span'); - await tracer.withSpan(span, async () => { + await context.with(setSpan(context.active(), span), async () => { try { const resPromise = await client.query(query); assert.ok(resPromise); @@ -360,7 +362,7 @@ describe('pg@7.x', () => { const parent = tracer.startSpan('parent'); const queryHandler = (err?: Error, res?: pg.QueryResult) => { - const span = tracer.getCurrentSpan(); + const span = getSpan(context.active()); assert.deepStrictEqual(span!.context(), parent.context()); if (err) { throw err; @@ -376,7 +378,7 @@ describe('pg@7.x', () => { callback: queryHandler, }; - tracer.withSpan(parent, () => { + context.with(setSpan(context.active(), parent), () => { client.query(config.text, config.callback); // 1 client.query(config); // 2 client.query(config.text, queryHandler); // 3 @@ -394,17 +396,17 @@ describe('pg@7.x', () => { const spans = [tracer.startSpan('span 1'), tracer.startSpan('span 2')]; const currentSpans: (Span | undefined)[] = []; const queryHandler = () => { - currentSpans.push(tracer.getCurrentSpan()); + currentSpans.push(getSpan(context.active())); if (currentSpans.length === 2) { assert.deepStrictEqual(currentSpans, spans); done(); } }; - tracer.withSpan(spans[0], () => { + context.with(setSpan(context.active(), spans[0]), () => { client.query('SELECT NOW()', queryHandler); }); - tracer.withSpan(spans[1], () => { + context.with(setSpan(context.active(), spans[1]), () => { client.query('SELECT NOW()', queryHandler); }); }); @@ -413,17 +415,17 @@ describe('pg@7.x', () => { const spans = [tracer.startSpan('span 1'), tracer.startSpan('span 2')]; const currentSpans: (Span | undefined)[] = []; const queryHandler = () => { - currentSpans.push(tracer.getCurrentSpan()); + currentSpans.push(getSpan(context.active())); if (currentSpans.length === 2) { assert.deepStrictEqual(currentSpans, spans); done(); } }; - tracer.withSpan(spans[0], () => { + context.with(setSpan(context.active(), spans[0]), () => { client.query('SELECT NOW()').then(queryHandler); }); - tracer.withSpan(spans[1], () => { + context.with(setSpan(context.active(), spans[1]), () => { client.query('SELECT NOW()').then(queryHandler); }); }); diff --git a/plugins/node/opentelemetry-plugin-redis/package.json b/plugins/node/opentelemetry-plugin-redis/package.json index d6fe8faa70..a866526c5c 100644 --- a/plugins/node/opentelemetry-plugin-redis/package.json +++ b/plugins/node/opentelemetry-plugin-redis/package.json @@ -43,11 +43,11 @@ "access": "public" }, "devDependencies": { - "@opentelemetry/context-async-hooks": "^0.14.0", - "@opentelemetry/node": "^0.14.0", - "@opentelemetry/semantic-conventions": "^0.14.0", + "@opentelemetry/context-async-hooks": "^0.15.0", + "@opentelemetry/node": "^0.15.0", + "@opentelemetry/semantic-conventions": "^0.15.0", "@opentelemetry/test-utils": "^0.12.1", - "@opentelemetry/tracing": "^0.14.0", + "@opentelemetry/tracing": "^0.15.0", "@types/mocha": "7.0.2", "@types/node": "14.0.27", "@types/redis": "2.8.25", @@ -66,8 +66,8 @@ "typescript": "3.9.7" }, "dependencies": { - "@opentelemetry/api": "^0.14.0", - "@opentelemetry/core": "^0.14.0", + "@opentelemetry/api": "^0.15.0", + "@opentelemetry/core": "^0.15.0", "shimmer": "^1.2.1" } } diff --git a/plugins/node/opentelemetry-plugin-redis/src/utils.ts b/plugins/node/opentelemetry-plugin-redis/src/utils.ts index d6b3f44897..7ab0ed4513 100644 --- a/plugins/node/opentelemetry-plugin-redis/src/utils.ts +++ b/plugins/node/opentelemetry-plugin-redis/src/utils.ts @@ -15,7 +15,13 @@ */ import type * as redisTypes from 'redis'; -import { Tracer, SpanKind, Span, StatusCode } from '@opentelemetry/api'; +import { + context, + Tracer, + SpanKind, + Span, + StatusCode, +} from '@opentelemetry/api'; import { RedisCommand, RedisPluginClientTypes } from './types'; import { EventEmitter } from 'events'; import { RedisPlugin } from './redis'; @@ -37,7 +43,7 @@ const endSpan = (span: Span, err?: Error | null) => { export const getTracedCreateClient = (tracer: Tracer, original: Function) => { return function createClientTrace(this: redisTypes.RedisClient) { const client: redisTypes.RedisClient = original.apply(this, arguments); - return tracer.bind(client); + return context.bind(client); }; }; @@ -52,7 +58,7 @@ export const getTracedCreateStreamTrace = ( return this._patched_redis_stream; }, set(val: EventEmitter) { - tracer.bind(val); + context.bind(val); this._patched_redis_stream = val; }, }); diff --git a/plugins/node/opentelemetry-plugin-redis/test/redis.test.ts b/plugins/node/opentelemetry-plugin-redis/test/redis.test.ts index 5d28408fab..97aa50bccc 100644 --- a/plugins/node/opentelemetry-plugin-redis/test/redis.test.ts +++ b/plugins/node/opentelemetry-plugin-redis/test/redis.test.ts @@ -14,8 +14,15 @@ * limitations under the License. */ -import { StatusCode, context, SpanKind, Status } from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; +import { + StatusCode, + context, + SpanKind, + Status, + NoopLogger, + getSpan, + setSpan, +} from '@opentelemetry/api'; import { NodeTracerProvider } from '@opentelemetry/node'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; import * as testUtils from '@opentelemetry/test-utils'; @@ -101,7 +108,7 @@ describe('redis@2.x', () => { const span = tracer.startSpan('test span'); let client: redisTypes.RedisClient; const readyHandler = () => { - assert.strictEqual(tracer.getCurrentSpan(), span); + assert.strictEqual(getSpan(context.active()), span); client.quit(done); }; const errorHandler = (err: Error) => { @@ -109,7 +116,7 @@ describe('redis@2.x', () => { client.quit(done); }; - tracer.withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { client = redis.createClient(URL); client.on('ready', readyHandler); client.on('error', errorHandler); @@ -178,7 +185,7 @@ describe('redis@2.x', () => { [DatabaseAttribute.DB_STATEMENT]: operation.command, }; const span = tracer.startSpan('test span'); - tracer.withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { operation.method((err, _result) => { assert.ifError(err); assert.strictEqual(memoryExporter.getFinishedSpans().length, 1); @@ -212,7 +219,7 @@ describe('redis@2.x', () => { REDIS_OPERATIONS.forEach(operation => { it(`should not create a child span for ${operation.description}`, done => { const span = tracer.startSpan('test span'); - tracer.withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { operation.method((err, _) => { assert.ifError(err); assert.strictEqual(memoryExporter.getFinishedSpans().length, 0); diff --git a/plugins/web/opentelemetry-plugin-document-load/package.json b/plugins/web/opentelemetry-plugin-document-load/package.json index 053a0355be..8e347a2710 100644 --- a/plugins/web/opentelemetry-plugin-document-load/package.json +++ b/plugins/web/opentelemetry-plugin-document-load/package.json @@ -72,10 +72,10 @@ "webpack-merge": "5.0.9" }, "dependencies": { - "@opentelemetry/api": "^0.14.0", - "@opentelemetry/core": "^0.14.0", - "@opentelemetry/semantic-conventions": "^0.14.0", - "@opentelemetry/tracing": "^0.14.0", - "@opentelemetry/web": "^0.14.0" + "@opentelemetry/api": "^0.15.0", + "@opentelemetry/core": "^0.15.0", + "@opentelemetry/semantic-conventions": "^0.15.0", + "@opentelemetry/tracing": "^0.15.0", + "@opentelemetry/web": "^0.15.0" } } diff --git a/plugins/web/opentelemetry-plugin-document-load/src/documentLoad.ts b/plugins/web/opentelemetry-plugin-document-load/src/documentLoad.ts index ee4ab26a5e..d1f9fa356c 100644 --- a/plugins/web/opentelemetry-plugin-document-load/src/documentLoad.ts +++ b/plugins/web/opentelemetry-plugin-document-load/src/documentLoad.ts @@ -17,7 +17,7 @@ import { context, propagation, - setActiveSpan, + setSpan, Span, ROOT_CONTEXT, } from '@opentelemetry/api'; @@ -103,14 +103,14 @@ export class DocumentLoad extends BasePlugin { if (!rootSpan) { return; } - this._tracer.withSpan(rootSpan, () => { + context.with(setSpan(context.active(), rootSpan), () => { const fetchSpan = this._startSpan( AttributeNames.DOCUMENT_FETCH, PTN.FETCH_START, entries ); if (fetchSpan) { - this._tracer.withSpan(fetchSpan, () => { + context.with(setSpan(context.active(), fetchSpan), () => { addSpanNetworkEvents(fetchSpan, entries); this._endSpan(fetchSpan, PTN.RESPONSE_END, entries); }); @@ -241,7 +241,7 @@ export class DocumentLoad extends BasePlugin { { startTime: entries[performanceName], }, - parentSpan ? setActiveSpan(context.active(), parentSpan) : undefined + parentSpan ? setSpan(context.active(), parentSpan) : undefined ); span.setAttribute(AttributeNames.COMPONENT, this.component); return span; diff --git a/plugins/web/opentelemetry-plugin-document-load/test/documentLoad.test.ts b/plugins/web/opentelemetry-plugin-document-load/test/documentLoad.test.ts index efa8f892f2..2a1752db54 100644 --- a/plugins/web/opentelemetry-plugin-document-load/test/documentLoad.test.ts +++ b/plugins/web/opentelemetry-plugin-document-load/test/documentLoad.test.ts @@ -465,10 +465,11 @@ describe('DocumentLoad Plugin', () => { assert.strictEqual(srEvents1[1].name, PTN.DOMAIN_LOOKUP_START); assert.strictEqual(srEvents1[2].name, PTN.DOMAIN_LOOKUP_END); assert.strictEqual(srEvents1[3].name, PTN.CONNECT_START); - assert.strictEqual(srEvents1[4].name, PTN.CONNECT_END); - assert.strictEqual(srEvents1[5].name, PTN.REQUEST_START); - assert.strictEqual(srEvents1[6].name, PTN.RESPONSE_START); - assert.strictEqual(srEvents1[7].name, PTN.RESPONSE_END); + assert.strictEqual(srEvents1[4].name, PTN.SECURE_CONNECTION_START); + assert.strictEqual(srEvents1[5].name, PTN.CONNECT_END); + assert.strictEqual(srEvents1[6].name, PTN.REQUEST_START); + assert.strictEqual(srEvents1[7].name, PTN.RESPONSE_START); + assert.strictEqual(srEvents1[8].name, PTN.RESPONSE_END); assert.strictEqual(spyOnEnd.callCount, 3); done(); @@ -522,18 +523,20 @@ describe('DocumentLoad Plugin', () => { ensureNetworkEventsExists(rsEvents); assert.strictEqual(fsEvents[0].name, PTN.FETCH_START); - assert.strictEqual(fsEvents[1].name, PTN.DOM_INTERACTIVE); + assert.strictEqual(fsEvents[1].name, PTN.UNLOAD_EVENT_START); + assert.strictEqual(fsEvents[2].name, PTN.UNLOAD_EVENT_END); + assert.strictEqual(fsEvents[3].name, PTN.DOM_INTERACTIVE); assert.strictEqual( - fsEvents[2].name, + fsEvents[4].name, PTN.DOM_CONTENT_LOADED_EVENT_START ); - assert.strictEqual(fsEvents[3].name, PTN.DOM_CONTENT_LOADED_EVENT_END); - assert.strictEqual(fsEvents[4].name, PTN.DOM_COMPLETE); - assert.strictEqual(fsEvents[5].name, PTN.LOAD_EVENT_START); - assert.strictEqual(fsEvents[6].name, PTN.LOAD_EVENT_END); + assert.strictEqual(fsEvents[5].name, PTN.DOM_CONTENT_LOADED_EVENT_END); + assert.strictEqual(fsEvents[6].name, PTN.DOM_COMPLETE); + assert.strictEqual(fsEvents[7].name, PTN.LOAD_EVENT_START); + assert.strictEqual(fsEvents[8].name, PTN.LOAD_EVENT_END); assert.strictEqual(rsEvents.length, 9); - assert.strictEqual(fsEvents.length, 7); + assert.strictEqual(fsEvents.length, 9); assert.strictEqual(spyOnEnd.callCount, 2); done(); }); diff --git a/plugins/web/opentelemetry-plugin-react-load/package.json b/plugins/web/opentelemetry-plugin-react-load/package.json index 3e353ab16d..c330146962 100644 --- a/plugins/web/opentelemetry-plugin-react-load/package.json +++ b/plugins/web/opentelemetry-plugin-react-load/package.json @@ -43,7 +43,7 @@ }, "devDependencies": { "@babel/core": "7.10.2", - "@opentelemetry/propagator-b3": "^0.14.0", + "@opentelemetry/propagator-b3": "^0.15.0", "@types/mocha": "7.0.2", "@types/node": "12.12.47", "@types/react": "16.9.46", @@ -82,12 +82,12 @@ "react": "^16.13.1 || ^17.0.0" }, "dependencies": { - "@opentelemetry/api": "^0.14.0", - "@opentelemetry/context-zone": "^0.14.0", - "@opentelemetry/core": "^0.14.0", - "@opentelemetry/semantic-conventions": "^0.14.0", - "@opentelemetry/tracing": "^0.14.0", - "@opentelemetry/web": "^0.14.0", + "@opentelemetry/api": "^0.15.0", + "@opentelemetry/context-zone": "^0.15.0", + "@opentelemetry/core": "^0.15.0", + "@opentelemetry/semantic-conventions": "^0.15.0", + "@opentelemetry/tracing": "^0.15.0", + "@opentelemetry/web": "^0.15.0", "shimmer": "^1.2.1" } } diff --git a/plugins/web/opentelemetry-plugin-react-load/src/BaseOpenTelemetryComponent.ts b/plugins/web/opentelemetry-plugin-react-load/src/BaseOpenTelemetryComponent.ts index 0ddc40a13d..1086ca1868 100644 --- a/plugins/web/opentelemetry-plugin-react-load/src/BaseOpenTelemetryComponent.ts +++ b/plugins/web/opentelemetry-plugin-react-load/src/BaseOpenTelemetryComponent.ts @@ -87,9 +87,7 @@ export class BaseOpenTelemetryComponent extends React.Component { { attributes: this._getAttributes(react), }, - parentSpan - ? api.setActiveSpan(api.context.active(), parentSpan) - : undefined + parentSpan ? api.setSpan(api.context.active(), parentSpan) : undefined ); } @@ -120,7 +118,7 @@ export class BaseOpenTelemetryComponent extends React.Component { const span = this._createSpanWithParent(react, spanName, parent); let wasError = false; try { - return BaseOpenTelemetryComponent._tracer.withSpan(span, () => { + return api.context.with(api.setSpan(api.context.active(), span), () => { return original(); }); } catch (err) { diff --git a/plugins/web/opentelemetry-plugin-user-interaction/package.json b/plugins/web/opentelemetry-plugin-user-interaction/package.json index 0bb8098eb6..558b2a654d 100644 --- a/plugins/web/opentelemetry-plugin-user-interaction/package.json +++ b/plugins/web/opentelemetry-plugin-user-interaction/package.json @@ -43,10 +43,10 @@ }, "devDependencies": { "@babel/core": "7.11.1", - "@opentelemetry/context-base": "^0.14.0", - "@opentelemetry/context-zone-peer-dep": "^0.14.0", - "@opentelemetry/instrumentation-xml-http-request": "^0.14.0", - "@opentelemetry/tracing": "^0.14.0", + "@opentelemetry/context-base": "^0.15.0", + "@opentelemetry/context-zone-peer-dep": "^0.15.0", + "@opentelemetry/instrumentation-xml-http-request": "^0.15.0", + "@opentelemetry/tracing": "^0.15.0", "@types/jquery": "3.5.1", "@types/mocha": "7.0.2", "@types/node": "14.0.27", @@ -80,9 +80,9 @@ "zone.js": "0.10.3" }, "dependencies": { - "@opentelemetry/api": "^0.14.0", - "@opentelemetry/core": "^0.14.0", - "@opentelemetry/web": "^0.14.0", + "@opentelemetry/api": "^0.15.0", + "@opentelemetry/core": "^0.15.0", + "@opentelemetry/web": "^0.15.0", "shimmer": "^1.2.1" }, "peerDependencies": { diff --git a/plugins/web/opentelemetry-plugin-user-interaction/src/userInteraction.ts b/plugins/web/opentelemetry-plugin-user-interaction/src/userInteraction.ts index 748bb34e10..24517f23ef 100644 --- a/plugins/web/opentelemetry-plugin-user-interaction/src/userInteraction.ts +++ b/plugins/web/opentelemetry-plugin-user-interaction/src/userInteraction.ts @@ -15,7 +15,7 @@ */ import * as api from '@opentelemetry/api'; -import { getActiveSpan, Span } from '@opentelemetry/api'; +import { getSpan, Span } from '@opentelemetry/api'; import { BasePlugin, hrTime, isWrapped } from '@opentelemetry/core'; import { getElementXPath } from '@opentelemetry/web'; import * as shimmer from 'shimmer'; @@ -116,9 +116,7 @@ export class UserInteractionPlugin extends BasePlugin { [AttributeNames.HTTP_USER_AGENT]: navigator.userAgent, }, }, - parentSpan - ? api.setActiveSpan(api.context.active(), parentSpan) - : undefined + parentSpan ? api.setSpan(api.context.active(), parentSpan) : undefined ); this._spansData.set(span, { @@ -155,7 +153,7 @@ export class UserInteractionPlugin extends BasePlugin { private _getCurrentSpan(zone: Zone): api.Span | undefined { const context: api.Context | undefined = zone.get(ZONE_CONTEXT_KEY); if (context) { - return getActiveSpan(context); + return getSpan(context); } return context; } @@ -270,12 +268,15 @@ export class UserInteractionPlugin extends BasePlugin { if (event) { plugin._eventsSpanMap.set(event, span); } - return plugin._tracer.withSpan(span, () => { - const result = plugin._invokeListener(listener, target, args); - // no zone so end span immediately - span.end(); - return result; - }); + return api.context.with( + api.setSpan(api.context.active(), span), + () => { + const result = plugin._invokeListener(listener, target, args); + // no zone so end span immediately + span.end(); + return result; + } + ); } else { return plugin._invokeListener(listener, target, args); } @@ -363,7 +364,7 @@ export class UserInteractionPlugin extends BasePlugin { * @param url */ _updateInteractionName(url: string) { - const span: api.Span | undefined = this._tracer.getCurrentSpan(); + const span: api.Span | undefined = api.getSpan(api.context.active()); if (span && typeof span.updateName === 'function') { span.updateName(`${EVENT_NAVIGATION_NAME} ${url}`); } @@ -437,11 +438,19 @@ export class UserInteractionPlugin extends BasePlugin { plugin._incrementTask(span); return activeZone.run(() => { try { - return plugin._tracer.withSpan(span as api.Span, () => { - const currentZone = Zone.current; - task._zone = currentZone; - return original.call(currentZone, task, applyThis, applyArgs); - }); + return api.context.with( + api.setSpan(api.context.active(), span!), + () => { + const currentZone = Zone.current; + task._zone = currentZone; + return original.call( + currentZone, + task, + applyThis, + applyArgs + ); + } + ); } finally { plugin._decrementTask(span as api.Span); } diff --git a/propagators/opentelemetry-propagator-grpc-census-binary/package.json b/propagators/opentelemetry-propagator-grpc-census-binary/package.json index f68c342b32..5a5e162004 100644 --- a/propagators/opentelemetry-propagator-grpc-census-binary/package.json +++ b/propagators/opentelemetry-propagator-grpc-census-binary/package.json @@ -58,7 +58,7 @@ "typescript": "3.9.7" }, "dependencies": { - "@opentelemetry/api": "^0.14.0", - "@opentelemetry/core": "^0.14.0" + "@opentelemetry/api": "^0.15.0", + "@opentelemetry/core": "^0.15.0" } } diff --git a/propagators/opentelemetry-propagator-grpc-census-binary/src/GrpcCensusPropagator.ts b/propagators/opentelemetry-propagator-grpc-census-binary/src/GrpcCensusPropagator.ts index 2af7a96ac7..db94423c16 100644 --- a/propagators/opentelemetry-propagator-grpc-census-binary/src/GrpcCensusPropagator.ts +++ b/propagators/opentelemetry-propagator-grpc-census-binary/src/GrpcCensusPropagator.ts @@ -16,8 +16,8 @@ import { Context, - getParentSpanContext, - setExtractedSpanContext, + getSpanContext, + setSpanContext, TextMapGetter, TextMapSetter, TextMapPropagator, @@ -67,7 +67,7 @@ export class GrpcCensusPropagator implements TextMapPropagator { * the carrier */ inject(context: Context, carrier: unknown, setter: TextMapSetter) { - const spanContext = getParentSpanContext(context); + const spanContext = getSpanContext(context); if (!spanContext) return; if ( @@ -128,7 +128,7 @@ export class GrpcCensusPropagator implements TextMapPropagator { const spanId = decodedContext.spanId; if (isValidTraceId(traceId) && isValidSpanId(spanId)) { - return setExtractedSpanContext(context, { + return setSpanContext(context, { traceId, spanId, isRemote: true, diff --git a/propagators/opentelemetry-propagator-grpc-census-binary/test/GrpcCensusPropagator.test.ts b/propagators/opentelemetry-propagator-grpc-census-binary/test/GrpcCensusPropagator.test.ts index a5b44d9aa0..e482304d28 100644 --- a/propagators/opentelemetry-propagator-grpc-census-binary/test/GrpcCensusPropagator.test.ts +++ b/propagators/opentelemetry-propagator-grpc-census-binary/test/GrpcCensusPropagator.test.ts @@ -15,9 +15,9 @@ */ import { - getParentSpanContext, + getSpanContext, ROOT_CONTEXT, - setExtractedSpanContext, + setSpanContext, SpanContext, TextMapGetter, TextMapSetter, @@ -46,7 +46,7 @@ describe('GrpcCensusPropagator', () => { traceFlags: TraceFlags.SAMPLED, }; censusPropagator.inject( - setExtractedSpanContext(ROOT_CONTEXT, spanContext), + setSpanContext(ROOT_CONTEXT, spanContext), metadata, makeSetter((metadata: any, k: any, v: any) => metadata.set(k, v as MetadataValue) @@ -69,7 +69,7 @@ describe('GrpcCensusPropagator', () => { traceFlags: TraceFlags.NONE, }; censusPropagator.inject( - setExtractedSpanContext(ROOT_CONTEXT, spanContext), + setSpanContext(ROOT_CONTEXT, spanContext), metadata, makeSetter((metadata: any, k: any, v: any) => metadata.set(k, v as MetadataValue) @@ -92,7 +92,7 @@ describe('GrpcCensusPropagator', () => { traceFlags: TraceFlags.NONE, }; censusPropagator.inject( - setExtractedSpanContext(ROOT_CONTEXT, emptySpanContext), + setSpanContext(ROOT_CONTEXT, emptySpanContext), metadata, makeSetter((metadata: any, k: any, v: any) => metadata.set(k, v as MetadataValue) @@ -124,7 +124,7 @@ describe('GrpcCensusPropagator', () => { }; censusPropagator.inject( // cast to any so that undefined traceFlags can be used for coverage - setExtractedSpanContext(ROOT_CONTEXT, spanContext as any), + setSpanContext(ROOT_CONTEXT, spanContext as any), metadata, makeSetter((metadata: any, k: any, v: any) => metadata.set(k, v as MetadataValue) @@ -142,7 +142,7 @@ describe('GrpcCensusPropagator', () => { traceFlags: TraceFlags.SAMPLED, }; censusPropagator.inject( - setExtractedSpanContext(ROOT_CONTEXT, emptySpanContext), + setSpanContext(ROOT_CONTEXT, emptySpanContext), null, makeSetter((metadata: any, k: any, v: any) => metadata.set(k, v as MetadataValue) @@ -160,7 +160,7 @@ describe('GrpcCensusPropagator', () => { const encoded = Buffer.from(encodedArray.buffer); metadata.set(GRPC_TRACE_KEY, encoded); - const extractedSpanContext = getParentSpanContext( + const extractedSpanContext = getSpanContext( censusPropagator.extract(ROOT_CONTEXT, metadata, makeGetter()) ); @@ -180,7 +180,7 @@ describe('GrpcCensusPropagator', () => { const encoded = Buffer.from(encodedArray.buffer); metadata.set(GRPC_TRACE_KEY, encoded); - const extractedSpanContext = getParentSpanContext( + const extractedSpanContext = getSpanContext( censusPropagator.extract(ROOT_CONTEXT, metadata, makeGetter()) ); @@ -193,7 +193,7 @@ describe('GrpcCensusPropagator', () => { }); it('should return undefined when header is not set', () => { - const extractedSpanContext = getParentSpanContext( + const extractedSpanContext = getSpanContext( censusPropagator.extract(ROOT_CONTEXT, metadata, makeGetter()) ); assert.deepStrictEqual(extractedSpanContext, undefined); @@ -207,7 +207,7 @@ describe('GrpcCensusPropagator', () => { const encoded = Buffer.from(encodedArray.buffer); metadata.set(GRPC_TRACE_KEY, encoded); - const extractedSpanContext = getParentSpanContext( + const extractedSpanContext = getSpanContext( censusPropagator.extract(ROOT_CONTEXT, metadata, makeGetter()) ); @@ -227,7 +227,7 @@ describe('GrpcCensusPropagator', () => { const encoded = Buffer.from(encodedArray.buffer); metadata.set(GRPC_TRACE_KEY, encoded); - const extractedSpanContext = getParentSpanContext( + const extractedSpanContext = getSpanContext( censusPropagator.extract(ROOT_CONTEXT, metadata, makeGetter()) ); @@ -235,7 +235,7 @@ describe('GrpcCensusPropagator', () => { }); it('should return undefined when carrier is null', () => { - const extractedSpanContext = getParentSpanContext( + const extractedSpanContext = getSpanContext( censusPropagator.extract(ROOT_CONTEXT, metadata, makeGetter()) ); assert.deepStrictEqual(extractedSpanContext, undefined); diff --git a/propagators/opentelemetry-propagator-jaeger/package.json b/propagators/opentelemetry-propagator-jaeger/package.json index a80a52a1ea..8818b859ee 100644 --- a/propagators/opentelemetry-propagator-jaeger/package.json +++ b/propagators/opentelemetry-propagator-jaeger/package.json @@ -70,7 +70,7 @@ "webpack": "4.43.0" }, "dependencies": { - "@opentelemetry/api": "^0.14.0", - "@opentelemetry/core": "^0.14.0" + "@opentelemetry/api": "^0.15.0", + "@opentelemetry/core": "^0.15.0" } } diff --git a/propagators/opentelemetry-propagator-jaeger/src/JaegerHttpTracePropagator.ts b/propagators/opentelemetry-propagator-jaeger/src/JaegerHttpTracePropagator.ts index 1e69dfefff..84ac964a59 100644 --- a/propagators/opentelemetry-propagator-jaeger/src/JaegerHttpTracePropagator.ts +++ b/propagators/opentelemetry-propagator-jaeger/src/JaegerHttpTracePropagator.ts @@ -16,8 +16,8 @@ import { Context, - getParentSpanContext, - setExtractedSpanContext, + getSpanContext, + setSpanContext, SpanContext, TraceFlags, TextMapGetter, @@ -53,7 +53,7 @@ export class JaegerHttpTracePropagator implements TextMapPropagator { } inject(context: Context, carrier: unknown, setter: TextMapSetter) { - const spanContext = getParentSpanContext(context); + const spanContext = getSpanContext(context); if (!spanContext) return; const traceFlags = `0${(spanContext.traceFlags || TraceFlags.NONE).toString( @@ -78,7 +78,7 @@ export class JaegerHttpTracePropagator implements TextMapPropagator { const spanContext = deserializeSpanContext(uberTraceId); if (!spanContext) return context; - return setExtractedSpanContext(context, spanContext); + return setSpanContext(context, spanContext); } fields(): string[] { diff --git a/propagators/opentelemetry-propagator-jaeger/test/JaegerHttpTracePropagator.test.ts b/propagators/opentelemetry-propagator-jaeger/test/JaegerHttpTracePropagator.test.ts index d7853a9d80..4c27c281d8 100644 --- a/propagators/opentelemetry-propagator-jaeger/test/JaegerHttpTracePropagator.test.ts +++ b/propagators/opentelemetry-propagator-jaeger/test/JaegerHttpTracePropagator.test.ts @@ -17,9 +17,9 @@ import { defaultTextMapGetter, defaultTextMapSetter, - getParentSpanContext, + getSpanContext, ROOT_CONTEXT, - setExtractedSpanContext, + setSpanContext, SpanContext, TextMapGetter, TraceFlags, @@ -51,7 +51,7 @@ describe('JaegerHttpTracePropagator', () => { }; jaegerHttpTracePropagator.inject( - setExtractedSpanContext(ROOT_CONTEXT, spanContext), + setSpanContext(ROOT_CONTEXT, spanContext), carrier, defaultTextMapSetter ); @@ -69,7 +69,7 @@ describe('JaegerHttpTracePropagator', () => { }; customJaegerHttpTracePropagator.inject( - setExtractedSpanContext(ROOT_CONTEXT, spanContext), + setSpanContext(ROOT_CONTEXT, spanContext), carrier, defaultTextMapSetter ); @@ -84,7 +84,7 @@ describe('JaegerHttpTracePropagator', () => { it('should extract context of a sampled span from carrier', () => { carrier[UBER_TRACE_ID_HEADER] = 'd4cda95b652f4a1592b449d5929fda1b:6e0c63257de34c92:0:01'; - const extractedSpanContext = getParentSpanContext( + const extractedSpanContext = getSpanContext( jaegerHttpTracePropagator.extract( ROOT_CONTEXT, carrier, @@ -103,7 +103,7 @@ describe('JaegerHttpTracePropagator', () => { it('should extract context of a sampled span from carrier with 1 bit flag', () => { carrier[UBER_TRACE_ID_HEADER] = '9c41e35aeb6d1272:45fd2a9709dadcf1:a13699e3fb724f40:1'; - const extractedSpanContext = getParentSpanContext( + const extractedSpanContext = getSpanContext( jaegerHttpTracePropagator.extract( ROOT_CONTEXT, carrier, @@ -122,7 +122,7 @@ describe('JaegerHttpTracePropagator', () => { it('should extract context of a sampled span from UTF-8 encoded carrier', () => { carrier[UBER_TRACE_ID_HEADER] = 'ac1f3dc3c2c0b06e%3A5ac292c4a11a163e%3Ac086aaa825821068%3A1'; - const extractedSpanContext = getParentSpanContext( + const extractedSpanContext = getSpanContext( jaegerHttpTracePropagator.extract( ROOT_CONTEXT, carrier, @@ -141,7 +141,7 @@ describe('JaegerHttpTracePropagator', () => { it('should use custom header if provided', () => { carrier[customHeader] = 'd4cda95b652f4a1592b449d5929fda1b:6e0c63257de34c92:0:01'; - const extractedSpanContext = getParentSpanContext( + const extractedSpanContext = getSpanContext( customJaegerHttpTracePropagator.extract( ROOT_CONTEXT, carrier, @@ -159,7 +159,7 @@ describe('JaegerHttpTracePropagator', () => { it('returns undefined if UBER_TRACE_ID_HEADER header is missing', () => { assert.deepStrictEqual( - getParentSpanContext( + getSpanContext( jaegerHttpTracePropagator.extract( ROOT_CONTEXT, carrier, @@ -173,7 +173,7 @@ describe('JaegerHttpTracePropagator', () => { it('returns undefined if UBER_TRACE_ID_HEADER header is invalid', () => { carrier[UBER_TRACE_ID_HEADER] = 'invalid!'; assert.deepStrictEqual( - getParentSpanContext( + getSpanContext( jaegerHttpTracePropagator.extract( ROOT_CONTEXT, carrier,