From bed4685a1be7f860a21a6e4afbe45c4509c1d312 Mon Sep 17 00:00:00 2001 From: Simon Stone Date: Mon, 5 Jul 2021 16:25:56 +0100 Subject: [PATCH] fix(mysql): bind get connection callback to active context Signed-off-by: Simon Stone --- .../src/instrumentation.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/plugins/node/opentelemetry-instrumentation-mysql/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-mysql/src/instrumentation.ts index 301b85e51d..f6efb99a7c 100644 --- a/plugins/node/opentelemetry-instrumentation-mysql/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-mysql/src/instrumentation.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { diag, Span, SpanKind, SpanStatusCode } from '@opentelemetry/api'; +import { context, diag, Span, SpanKind, SpanStatusCode, trace } from '@opentelemetry/api'; import { InstrumentationBase, InstrumentationNodeModuleDefinition, @@ -203,6 +203,13 @@ export class MySQLInstrumentation extends InstrumentationBase< private _getConnectionCallbackPatchFn(cb: Function, format: formatType) { const thisPlugin = this; + // If a parent span exists, bind the callback so that the parent span + // is still visible after we've gone through the MySQL pool code. + const activeContext = context.active(); + const parentSpan = trace.getSpan(activeContext); + if (parentSpan && typeof cb === 'function') { + cb = context.bind(activeContext, cb); + } return function () { if (arguments[1]) { // this is the callback passed into a query