Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
SpanKind,
trace,
diag,
TraceFlags,
SpanContext,
} from '@opentelemetry/api';
import {
ATTR_DB_NAMESPACE,
Expand Down Expand Up @@ -63,6 +65,14 @@ function getTraceHandlerBaseClass(
}
}

export function buildTraceparent(
spanContext: SpanContext
): string | undefined {
return `00-${spanContext.traceId}-${spanContext.spanId}-0${Number(
spanContext.traceFlags || TraceFlags.NONE
).toString(16)}`;
}

export function getOracleTelemetryTraceHandlerClass(
obj: typeof oracleDBTypes
): any {
Expand Down Expand Up @@ -354,6 +364,27 @@ export function getOracleTelemetryTraceHandlerClass(
};

if (traceContext.fn) {
if (
this._instrumentConfig.traceContextPropagation &&
(traceContext.operation === SpanNames.EXECUTE ||
traceContext.operation === SpanNames.EXECUTE_MANY)
) {
const connection = traceContext.additionalConfig?.self;
const traceparent = buildTraceparent(
traceContext.userContext.span.spanContext()
);
if (connection && 'action' in connection && traceparent) {
try {
connection.action = traceparent;
} catch (err) {
diag.debug(
'Failed to set connection.action for trace propagation',
err
);
}
}
}

// wrap the active span context to the exported function.
traceContext.fn = context.bind(
trace.setSpan(context.active(), traceContext.userContext.span),
Expand Down
7 changes: 7 additions & 0 deletions packages/instrumentation-oracledb/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,11 @@ export interface OracleInstrumentationConfig extends InstrumentationConfig {
* @default false
*/
requireParentSpan?: boolean;

/**
* Automatic propagation of trace context using V$SESSION.ACTION.
*
* @default false
*/
traceContextPropagation?: boolean;
}
24 changes: 24 additions & 0 deletions packages/instrumentation-oracledb/test/oracle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
import * as assert from 'assert';
import { OracleInstrumentation } from '../src';
import { SpanNames } from '../src/constants';
import { buildTraceparent} from '../src/OracleTelemetryTraceHandler';

import {
ATTR_DB_NAMESPACE,
Expand Down Expand Up @@ -524,6 +525,7 @@ describe('oracledb', () => {
instrumentation.setConfig({
enhancedDatabaseReporting: false,
dbStatementDump: false,
traceContextPropagation: false,
});
});

Expand Down Expand Up @@ -975,6 +977,28 @@ describe('oracledb', () => {
});
});

it('should propagate trace context via connection.action when enabled', async () => {
instrumentation.setConfig({ traceContextPropagation: true });
const result = await connection.execute(
"select sys_context('USERENV', 'ACTION') as action from dual",
[],
{ outFormat: oracledb.OUT_FORMAT_OBJECT }
);
const row = result.rows?.[0] as Record<string, string> | undefined;
const actionValue = row?.ACTION;
const spans = memoryExporter.getFinishedSpans();
const executeSpan = spans[spans.length - 1];
assert.ok(executeSpan, 'expected span to verify trace propagation');
assert.ok(
executeSpan.name.startsWith(SpanNames.EXECUTE),
`expected execute span, got ${executeSpan.name}`
);
const expectedTraceparent = buildTraceparent(
executeSpan.spanContext()
);
assert.strictEqual(actionValue, expectedTraceparent);
});

it('should intercept connection.execute(sql, values) bind-by-name', async () => {
const span = tracer.startSpan('test span');
await context.with(trace.setSpan(context.active(), span), async () => {
Expand Down
Loading