Skip to content

Commit

Permalink
feat(instrumentation-fetch): add option to apply attributes to all fe…
Browse files Browse the repository at this point in the history
…tch spans
  • Loading branch information
niekert committed Apr 2, 2021
1 parent 853fcb9 commit bb62ad3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
33 changes: 32 additions & 1 deletion packages/opentelemetry-instrumentation-fetch/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
isWrapped,
InstrumentationBase,
InstrumentationConfig,
safeExecuteInTheMiddle,
} from '@opentelemetry/instrumentation';
import * as core from '@opentelemetry/core';
import * as web from '@opentelemetry/web';
Expand All @@ -43,6 +44,14 @@ const getUrlNormalizingAnchor = () => {
return a;
};

export interface FetchCustomAttributeFunction {
(
span: api.Span,
request: Request | RequestInit,
result: Response | FetchError
): void;
}

/**
* FetchPlugin Config
*/
Expand All @@ -61,6 +70,8 @@ export interface FetchInstrumentationConfig extends InstrumentationConfig {
* also not be traced.
*/
ignoreUrls?: Array<string | RegExp>;
/** Function for adding custom attributes on the span */
applyCustomAttributesOnSpan?: FetchCustomAttributeFunction;
}

/**
Expand Down Expand Up @@ -187,14 +198,16 @@ export class FetchInstrumentation extends InstrumentationBase<
}
const method = (options.method || 'GET').toUpperCase();
const spanName = `HTTP ${method}`;
return this.tracer.startSpan(spanName, {
const span = this.tracer.startSpan(spanName, {
kind: api.SpanKind.CLIENT,
attributes: {
[AttributeNames.COMPONENT]: this.moduleName,
[HttpAttribute.HTTP_METHOD]: method,
[HttpAttribute.HTTP_URL]: url,
},
});

return span;
}

/**
Expand Down Expand Up @@ -306,6 +319,8 @@ export class FetchInstrumentation extends InstrumentationBase<
response: Response
) {
try {
plugin._applyAttributesAfterFetch(span, options, response);

if (response.status >= 200 && response.status < 400) {
plugin._endSpan(span, spanData, response);
} else {
Expand All @@ -326,6 +341,7 @@ export class FetchInstrumentation extends InstrumentationBase<
error: FetchError
) {
try {
plugin._applyAttributesAfterFetch(span, options, error);
plugin._endSpan(span, spanData, {
status: error.status || 0,
statusText: error.message,
Expand Down Expand Up @@ -355,6 +371,21 @@ export class FetchInstrumentation extends InstrumentationBase<
};
}

private _applyAttributesAfterFetch(
span: api.Span,
request: Request | RequestInit,
result: Response | FetchError
) {
if (this._getConfig().applyCustomAttributesOnSpan) {
safeExecuteInTheMiddle(
() =>
this._getConfig().applyCustomAttributesOnSpan!(span, request, result),
() => {},
true
);
}
}

/**
* Prepares a span data - needed later for matching appropriate network
* resources
Expand Down
12 changes: 11 additions & 1 deletion packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ const getData = (url: string, method?: string) =>
},
});

const customAttributeFunction = (span: api.Span): void => {
span.setAttribute('span kind', api.SpanKind.CLIENT);
span.setAttribute('custom', 'custom-attribute-value');
};

const defaultResource = {
connectEnd: 15,
connectStart: 13,
Expand Down Expand Up @@ -285,7 +290,10 @@ describe('fetch', () => {
describe('when request is successful', () => {
beforeEach(done => {
const propagateTraceHeaderCorsUrls = [url];
prepareData(done, url, { propagateTraceHeaderCorsUrls });
prepareData(done, url, {
propagateTraceHeaderCorsUrls,
applyCustomAttributesOnSpan: customAttributeFunction,
});
});

afterEach(() => {
Expand Down Expand Up @@ -368,6 +376,8 @@ describe('fetch', () => {
`attributes ${HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH} is <= 0`
);

assert.strictEqual(attributes['span kind'], api.SpanKind.CLIENT);

assert.strictEqual(keys.length, 9, 'number of attributes is wrong');
});

Expand Down

0 comments on commit bb62ad3

Please sign in to comment.