Skip to content

Commit 1a79f82

Browse files
committed
Propagate action with traceparent
1 parent c352766 commit 1a79f82

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

packages/instrumentation-oracledb/src/OracleTelemetryTraceHandler.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
SpanKind,
2525
trace,
2626
diag,
27+
TraceFlags,
2728
} from '@opentelemetry/api';
2829
import {
2930
ATTR_DB_NAMESPACE,
@@ -63,6 +64,12 @@ function getTraceHandlerBaseClass(
6364
}
6465
}
6566

67+
68+
function _buildTraceparent(span: Span): string | undefined {
69+
const sc = span.spanContext();
70+
return `00-${sc.traceId}-${sc.spanId}-0${Number(sc.traceFlags || TraceFlags.NONE).toString(16)}`;
71+
}
72+
6673
export function getOracleTelemetryTraceHandlerClass(
6774
obj: typeof oracleDBTypes
6875
): any {
@@ -354,6 +361,27 @@ export function getOracleTelemetryTraceHandlerClass(
354361
};
355362

356363
if (traceContext.fn) {
364+
if (
365+
this._instrumentConfig.traceContextPropagation &&
366+
(traceContext.operation === SpanNames.EXECUTE ||
367+
traceContext.operation === SpanNames.EXECUTE_MANY)
368+
) {
369+
const connection = traceContext.additionalConfig?.self;
370+
const traceparent = _buildTraceparent(
371+
traceContext.userContext.span
372+
);
373+
if (connection && 'action' in connection && traceparent) {
374+
try {
375+
connection.action = traceparent;
376+
} catch (err) {
377+
diag.debug(
378+
'Failed to set connection.action for trace propagation',
379+
err
380+
);
381+
}
382+
}
383+
}
384+
357385
// wrap the active span context to the exported function.
358386
traceContext.fn = context.bind(
359387
trace.setSpan(context.active(), traceContext.userContext.span),

packages/instrumentation-oracledb/src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,11 @@ export interface OracleInstrumentationConfig extends InstrumentationConfig {
9191
* @default false
9292
*/
9393
requireParentSpan?: boolean;
94+
95+
/**
96+
* Automatic propagation of trace context using V$SESSION.ACTION.
97+
*
98+
* @default false
99+
*/
100+
traceContextPropagation?: boolean;
94101
}

0 commit comments

Comments
 (0)