Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(user-interaction): adding traceparent context from meta tag #1850

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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 @@ -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 {
Expand Down Expand Up @@ -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,
{
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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' });
});
});
});
Loading