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