diff --git a/src/impl/index.ts b/src/impl/index.ts index bb9690d..2a50bdc 100644 --- a/src/impl/index.ts +++ b/src/impl/index.ts @@ -79,9 +79,11 @@ export default class OpenTelemetryTracingImpl { path: "", }, }, - includeRequestParameter: { - enabled: false, - excludeKeys: [] + global_instrumentation: { + requestParameter: { + enabled: false, + excludeKeysFromBeacons: [] + } }, exporter: { maxQueueSize: 100, @@ -305,43 +307,46 @@ export default class OpenTelemetryTracingImpl { }; private getInstrumentationPlugins = () => { - const { plugins, corsUrls, plugins_config, includeRequestParameter } = this.props; + const { + plugins, + corsUrls, + plugins_config, + global_instrumentation + } = this.props; const instrumentations: any = []; // Instrumentation for the document on load (initial request) if (plugins_config?.instrument_document_load?.enabled !== false) { - instrumentations.push(new CustomDocumentLoadInstrumentation(plugins_config.instrument_document_load, includeRequestParameter)); + instrumentations.push(new CustomDocumentLoadInstrumentation(plugins_config.instrument_document_load, global_instrumentation)); } else if (plugins?.instrument_document_load !== false) { - instrumentations.push(new CustomDocumentLoadInstrumentation({}, includeRequestParameter)); + instrumentations.push(new CustomDocumentLoadInstrumentation({}, global_instrumentation)); } // Instrumentation for user interactions if (plugins_config?.instrument_user_interaction?.enabled !== false) { - instrumentations.push(new CustomUserInteractionInstrumentation(plugins_config.instrument_user_interaction, includeRequestParameter)); + instrumentations.push(new CustomUserInteractionInstrumentation(plugins_config.instrument_user_interaction, global_instrumentation)); } else if (plugins?.instrument_user_interaction !== false) { - instrumentations.push(new CustomUserInteractionInstrumentation({}, includeRequestParameter)); + instrumentations.push(new CustomUserInteractionInstrumentation({}, global_instrumentation)); } // XMLHttpRequest Instrumentation for web plugin if (plugins_config?.instrument_xhr?.enabled !== false) { - instrumentations.push(new CustomXMLHttpRequestInstrumentation(plugins_config.instrument_xhr, includeRequestParameter)); + instrumentations.push(new CustomXMLHttpRequestInstrumentation(plugins_config.instrument_xhr, global_instrumentation)); } else if (plugins?.instrument_xhr !== false) { instrumentations.push( - new CustomXMLHttpRequestInstrumentation({ - propagateTraceHeaderCorsUrls: corsUrls - }, includeRequestParameter) + new CustomXMLHttpRequestInstrumentation({ propagateTraceHeaderCorsUrls: corsUrls }, global_instrumentation) ); } // Instrumentation for the fetch API if available const isFetchAPISupported = 'fetch' in window; if (isFetchAPISupported && plugins_config?.instrument_fetch?.enabled !== false) { - instrumentations.push(new CustomFetchInstrumentation(plugins_config.instrument_fetch, includeRequestParameter)); + instrumentations.push(new CustomFetchInstrumentation(plugins_config.instrument_fetch, global_instrumentation)); } else if (isFetchAPISupported && plugins?.instrument_fetch !== false) { - instrumentations.push(new CustomFetchInstrumentation({}, includeRequestParameter)); + instrumentations.push(new CustomFetchInstrumentation({}, global_instrumentation)); } return instrumentations; diff --git a/src/impl/instrumentation/documentLoadInstrumentation.ts b/src/impl/instrumentation/documentLoadInstrumentation.ts index f28159f..c22c883 100644 --- a/src/impl/instrumentation/documentLoadInstrumentation.ts +++ b/src/impl/instrumentation/documentLoadInstrumentation.ts @@ -9,7 +9,7 @@ import { isTracingSuppressed } from '@opentelemetry/core/build/src/trace/suppres import { sanitizeAttributes } from '@opentelemetry/core/build/src/common/attributes'; import { TransactionSpanManager } from '../transaction/transactionSpanManager'; import { addUrlParams } from './urlParams'; -import { RequestParameterConfig } from '../../types'; +import { GlobalInstrumentationConfig, RequestParameterConfig } from '../../types'; export interface CustomDocumentLoadInstrumentationConfig extends InstrumentationConfig { recordTransaction?: boolean; @@ -130,17 +130,13 @@ export class CustomDocumentLoadInstrumentation extends DocumentLoadInstrumentati // Per default transaction should not be recorded private recordTransaction = false; - private readonly excludeUrlKeys: string[] = []; - - constructor(config: CustomDocumentLoadInstrumentationConfig = {}, requestParameterConfig: RequestParameterConfig) { + constructor(config: CustomDocumentLoadInstrumentationConfig = {}, globalInstrumentationConfig: GlobalInstrumentationConfig) { super(config); + const { requestParameter} = globalInstrumentationConfig; if(config.recordTransaction) this.recordTransaction = config.recordTransaction; - if(requestParameterConfig.enabled) - this.excludeUrlKeys = requestParameterConfig.excludeKeys; - //Store original functions in variables const exposedSuper = this as any as ExposedDocumentLoadSuper; const _superStartSpan: ExposedDocumentLoadSuper['_startSpan'] = exposedSuper._startSpan.bind(this); @@ -163,7 +159,10 @@ export class CustomDocumentLoadInstrumentation extends DocumentLoadInstrumentati const exposedSpan = span as any as Span; if(exposedSpan.name == "documentLoad") TransactionSpanManager.setTransactionSpan(span); - if(span) addUrlParams(span, location.href, this.excludeUrlKeys); + if(span && exposedSpan.name == "documentLoad" && requestParameter?.enabled) { + if(requestParameter.excludeKeysFromBeacons) addUrlParams(span, location.href, requestParameter.excludeKeysFromBeacons); + else addUrlParams(span, location.href); + } return span; } @@ -184,7 +183,11 @@ export class CustomDocumentLoadInstrumentation extends DocumentLoadInstrumentati exposedSuper._startSpan = (spanName, performanceName, entries, parentSpan) => { const span = _superStartSpan(spanName, performanceName, entries, parentSpan); const exposedSpan = span as any as Span; - if(span && exposedSpan.name == "documentLoad") addUrlParams(span, location.href, this.excludeUrlKeys); + + if(span && exposedSpan.name == "documentLoad" && requestParameter?.enabled) { + if(requestParameter.excludeKeysFromBeacons) addUrlParams(span, location.href, requestParameter.excludeKeysFromBeacons); + else addUrlParams(span, location.href); + } return span; } diff --git a/src/impl/instrumentation/fetchInstrumentation.ts b/src/impl/instrumentation/fetchInstrumentation.ts index 71f6875..93c83a3 100644 --- a/src/impl/instrumentation/fetchInstrumentation.ts +++ b/src/impl/instrumentation/fetchInstrumentation.ts @@ -1,7 +1,7 @@ import * as api from '@opentelemetry/api'; import { addUrlParams } from './urlParams'; import { FetchInstrumentation, FetchInstrumentationConfig } from '@opentelemetry/instrumentation-fetch'; -import { RequestParameterConfig } from '../../types'; +import { GlobalInstrumentationConfig, RequestParameterConfig } from '../../types'; type ExposedFetchSuper = { _createSpan(url: string, options: Partial): api.Span | undefined; @@ -9,13 +9,9 @@ type ExposedFetchSuper = { export class CustomFetchInstrumentation extends FetchInstrumentation { - private readonly excludeUrlKeys: string[] = []; - - constructor(config: FetchInstrumentationConfig = {}, requestParameterConfig: RequestParameterConfig) { + constructor(config: FetchInstrumentationConfig = {}, globalInstrumentationConfig: GlobalInstrumentationConfig) { super(config); - - if(requestParameterConfig.enabled) - this.excludeUrlKeys = requestParameterConfig.excludeKeys; + const { requestParameter} = globalInstrumentationConfig; //Store original function in variable const exposedSuper = this as any as ExposedFetchSuper; @@ -24,7 +20,11 @@ export class CustomFetchInstrumentation extends FetchInstrumentation { //Override function exposedSuper._createSpan = (url, options = {}) => { const span = _superCreateSpan(url, options); - if(span) addUrlParams(span, url, this.excludeUrlKeys); + + if(span && requestParameter?.enabled) { + if(requestParameter.excludeKeysFromBeacons) addUrlParams(span, url, requestParameter.excludeKeysFromBeacons); + else addUrlParams(span, url); + } return span; } diff --git a/src/impl/instrumentation/urlParams.ts b/src/impl/instrumentation/urlParams.ts index 97687c5..6287716 100644 --- a/src/impl/instrumentation/urlParams.ts +++ b/src/impl/instrumentation/urlParams.ts @@ -6,7 +6,7 @@ import { Span } from '@opentelemetry/api'; * @param url complete request url * @param excludeKeys list of keys, which should not be written to beacons */ -export function addUrlParams(span: Span, url: string, excludeKeys: string[]){ +export function addUrlParams(span: Span, url: string, excludeKeys: string[] = []){ const urlParams = url.split("?")[1]; if(urlParams) { diff --git a/src/impl/instrumentation/userInteractionInstrumentation.ts b/src/impl/instrumentation/userInteractionInstrumentation.ts index dc24403..39d5bc0 100644 --- a/src/impl/instrumentation/userInteractionInstrumentation.ts +++ b/src/impl/instrumentation/userInteractionInstrumentation.ts @@ -1,7 +1,7 @@ import * as api from '@opentelemetry/api'; import { addUrlParams } from './urlParams'; import { FetchInstrumentation, FetchInstrumentationConfig } from '@opentelemetry/instrumentation-fetch'; -import { RequestParameterConfig } from '../../types'; +import { GlobalInstrumentationConfig, RequestParameterConfig } from '../../types'; import { UserInteractionInstrumentation } from '@opentelemetry/instrumentation-user-interaction'; import { InstrumentationConfig } from '@opentelemetry/instrumentation'; @@ -11,13 +11,9 @@ type ExposedUserInteractionSuper = { export class CustomUserInteractionInstrumentation extends UserInteractionInstrumentation { - private readonly excludeUrlKeys: string[] = []; - - constructor(config: InstrumentationConfig = {}, requestParameterConfig: RequestParameterConfig) { + constructor(config: InstrumentationConfig = {}, globalInstrumentationConfig: GlobalInstrumentationConfig) { super(config); - - if(requestParameterConfig.enabled) - this.excludeUrlKeys = requestParameterConfig.excludeKeys; + const { requestParameter} = globalInstrumentationConfig; //Store original function in variable const exposedSuper = this as any as ExposedUserInteractionSuper; @@ -27,7 +23,10 @@ export class CustomUserInteractionInstrumentation extends UserInteractionInstrum exposedSuper._createSpan = (element, eventName, parentSpan) => { const span = _superCreateSpan(element, eventName, parentSpan); - if(span) addUrlParams(span, location.href, this.excludeUrlKeys); + if(span && requestParameter?.enabled) { + if(requestParameter.excludeKeysFromBeacons) addUrlParams(span, location.href, requestParameter.excludeKeysFromBeacons); + else addUrlParams(span, location.href); + } return span; } diff --git a/src/impl/instrumentation/xmlHttpRequestInstrumentation.ts b/src/impl/instrumentation/xmlHttpRequestInstrumentation.ts index 17ef0e4..558951f 100644 --- a/src/impl/instrumentation/xmlHttpRequestInstrumentation.ts +++ b/src/impl/instrumentation/xmlHttpRequestInstrumentation.ts @@ -4,7 +4,7 @@ import { XMLHttpRequestInstrumentationConfig } from '@opentelemetry/instrumentation-xml-http-request'; import { addUrlParams } from './urlParams'; -import { RequestParameterConfig } from '../../types'; +import { GlobalInstrumentationConfig, RequestParameterConfig } from '../../types'; type ExposedXHRSuper = { _createSpan(xhr: XMLHttpRequest, url: string, method: string): api.Span | undefined; @@ -12,13 +12,9 @@ type ExposedXHRSuper = { export class CustomXMLHttpRequestInstrumentation extends XMLHttpRequestInstrumentation { - private readonly excludeUrlKeys: string[] = []; - - constructor(config: XMLHttpRequestInstrumentationConfig = {}, requestParameterConfig: RequestParameterConfig) { + constructor(config: XMLHttpRequestInstrumentationConfig = {}, globalInstrumentationConfig: GlobalInstrumentationConfig) { super(config); - - if(requestParameterConfig.enabled) - this.excludeUrlKeys = requestParameterConfig.excludeKeys; + const { requestParameter} = globalInstrumentationConfig; //Store original function in variable const exposedSuper = this as any as ExposedXHRSuper; @@ -27,7 +23,11 @@ export class CustomXMLHttpRequestInstrumentation extends XMLHttpRequestInstrumen //Override function exposedSuper._createSpan = (xhr, url, method) => { const span = _superCreateSpan(xhr, url, method); - if(span) addUrlParams(span, url, this.excludeUrlKeys); + + if(span && requestParameter?.enabled) { + if(requestParameter.excludeKeysFromBeacons) addUrlParams(span, url, requestParameter.excludeKeysFromBeacons); + else addUrlParams(span, url); + } return span; } diff --git a/src/types.d.ts b/src/types.d.ts index e70e7d6..e1cb394 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -12,7 +12,7 @@ export interface PluginProperties { consoleOnly: boolean; plugins: OTPluginProperties; plugins_config: OTPluginConfig; - includeRequestParameter: RequestParameterConfig; + global_instrumentation: GlobalInstrumentationConfig; exporter: OTExportProperties; commonAttributes: StringMap; prototypeExporterPatch: boolean; @@ -44,9 +44,13 @@ export interface OTPluginConfig { instrument_user_interaction: InstrumentationConfig; } +export interface GlobalInstrumentationConfig { + requestParameter: RequestParameterConfig; +} + export interface RequestParameterConfig { enabled?: boolean; - excludeKeys: string[]; + excludeKeysFromBeacons: string[]; } export interface OTExportProperties {