UNPKG

@grafana/faro-web-tracing

Version:
40 lines 1.86 kB
import { SpanKind } from '@opentelemetry/api'; import { USER_ACTION_CANCEL_MESSAGE_TYPE, USER_ACTION_END_MESSAGE_TYPE, USER_ACTION_START_MESSAGE_TYPE, } from '@grafana/faro-core'; import { apiMessageBus } from '@grafana/faro-web-sdk'; export class FaroUserActionSpanProcessor { constructor(processor) { this.processor = processor; apiMessageBus.subscribe((msg) => { if (msg.type === USER_ACTION_START_MESSAGE_TYPE) { this.message = msg; return; } if ([USER_ACTION_END_MESSAGE_TYPE, USER_ACTION_CANCEL_MESSAGE_TYPE].includes(msg.type)) { this.message = undefined; } }); } forceFlush() { return this.processor.forceFlush(); } onStart(span, parentContext) { var _a, _b; if (span.kind === SpanKind.CLIENT) { // If the span is created when the message object is available it is created before the user action timeout has been reached so it belongs to the user-action. // In this case we can add the user action name and parentId to the span attributes. // If the span is created after the user action timeout span, the message object will be undefined which means the action has been cancelled or is ended. if (this.message) { span.attributes['faro.action.user.name'] = (_a = this.message) === null || _a === void 0 ? void 0 : _a.name; span.attributes['faro.action.user.parentId'] = (_b = this.message) === null || _b === void 0 ? void 0 : _b.parentId; } } this.processor.onStart(span, parentContext); } onEnd(span) { this.processor.onEnd(span); } shutdown() { return this.processor.shutdown(); } } //# sourceMappingURL=faroUserActionSpanProcessor.js.map