From a96eaf8eb36af8f60e0ad2dce154f023cd4cef69 Mon Sep 17 00:00:00 2001 From: Oscar Reyes Date: Thu, 7 Dec 2023 09:51:13 -0600 Subject: [PATCH] feat: adding traceparent from meta tag --- .../src/instrumentation.ts | 21 ++++++++++++--- .../test/userInteraction.test.ts | 26 +++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/plugins/web/opentelemetry-instrumentation-user-interaction/src/instrumentation.ts b/plugins/web/opentelemetry-instrumentation-user-interaction/src/instrumentation.ts index e8be5e3fc5..e1bd5a44f1 100644 --- a/plugins/web/opentelemetry-instrumentation-user-interaction/src/instrumentation.ts +++ b/plugins/web/opentelemetry-instrumentation-user-interaction/src/instrumentation.ts @@ -19,7 +19,7 @@ import { isWrapped, InstrumentationBase } from '@opentelemetry/instrumentation'; import * as api from '@opentelemetry/api'; -import { hrTime } from '@opentelemetry/core'; +import { TRACE_PARENT_HEADER, hrTime } from '@opentelemetry/core'; import { getElementXPath } from '@opentelemetry/sdk-trace-web'; import { AttributeNames } from './enums/AttributeNames'; import { @@ -132,6 +132,7 @@ export class UserInteractionInstrumentation extends InstrumentationBase } const xpath = getElementXPath(element, true); try { + const context = this._getContext(); const span = this.tracer.startSpan( eventName, { @@ -143,8 +144,8 @@ export class UserInteractionInstrumentation extends InstrumentationBase }, }, parentSpan - ? api.trace.setSpan(api.context.active(), parentSpan) - : undefined + ? api.trace.setSpan(context, parentSpan) + : context ); if (this._shouldPreventSpanCreation(eventName, element, span) === true) { @@ -177,6 +178,20 @@ export class UserInteractionInstrumentation extends InstrumentationBase } } + /** + * + * Returns the current context from meta tag + */ + private _getContext(): api.Context { + const metaElement = Array.from(document.getElementsByTagName('meta')).find( + e => e.getAttribute('name') === TRACE_PARENT_HEADER + ); + const traceparent = (metaElement && metaElement.content) || ''; + const context = api.propagation.extract(api.context.active(), { traceparent }); + + return context; + } + /** * Return the current span * @param zone diff --git a/plugins/web/opentelemetry-instrumentation-user-interaction/test/userInteraction.test.ts b/plugins/web/opentelemetry-instrumentation-user-interaction/test/userInteraction.test.ts index 792387e8c5..d60712f88b 100644 --- a/plugins/web/opentelemetry-instrumentation-user-interaction/test/userInteraction.test.ts +++ b/plugins/web/opentelemetry-instrumentation-user-interaction/test/userInteraction.test.ts @@ -38,6 +38,7 @@ import { fakeEventInteraction, getData, } from './helper.test'; +import { TRACE_PARENT_HEADER } from '@opentelemetry/core'; const FILE_URL = 'https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/main/package.json'; @@ -621,5 +622,30 @@ describe('UserInteractionInstrumentation', () => { 'go should be unwrapped' ); }); + + it('should use the traceparent trace id as parent id', () => { + registerTestInstrumentations({ + eventNames: ['click'], + }); + + const version = '00'; + const idGenerator = new tracing.RandomIdGenerator(); + const traceId = idGenerator.generateTraceId(); + const spanId = idGenerator.generateSpanId(); + const samplingFlags = '01'; + + const traceparent = `${version}-${traceId}-${spanId}-${samplingFlags}`; + + const metaElement = document.createElement('meta'); + metaElement.setAttribute('name', TRACE_PARENT_HEADER); + metaElement.setAttribute('content', traceparent); + document.head.appendChild(metaElement); + + fakeEventInteraction('click'); + assert.strictEqual(exportSpy.args.length, 1, 'should export one span'); + const span = exportSpy.args[0][0][0]; + assert.strictEqual(span.parentSpanId, spanId); + assertInteractionSpan(span, { name: 'click' }); + }); }); });