Skip to content

Commit

Permalink
refactor instrumentations
Browse files Browse the repository at this point in the history
  • Loading branch information
EddeCCC committed Sep 26, 2023
1 parent b45259f commit 6a4da21
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 50 deletions.
33 changes: 19 additions & 14 deletions src/impl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ export default class OpenTelemetryTracingImpl {
path: "",
},
},
includeRequestParameter: {
enabled: false,
excludeKeys: []
global_instrumentation: {
requestParameter: {
enabled: false,
excludeKeysFromBeacons: []
}
},
exporter: {
maxQueueSize: 100,
Expand Down Expand Up @@ -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;
Expand Down
21 changes: 12 additions & 9 deletions src/impl/instrumentation/documentLoadInstrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
16 changes: 8 additions & 8 deletions src/impl/instrumentation/fetchInstrumentation.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
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<Request | RequestInit>): api.Span | undefined;
}

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;
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/impl/instrumentation/urlParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 7 additions & 8 deletions src/impl/instrumentation/userInteractionInstrumentation.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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;
Expand All @@ -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;
}
Expand Down
16 changes: 8 additions & 8 deletions src/impl/instrumentation/xmlHttpRequestInstrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@ 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;
}

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;
Expand All @@ -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;
}
Expand Down
8 changes: 6 additions & 2 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 6a4da21

Please sign in to comment.