Skip to content

Commit 14e5103

Browse files
committed
docs(instrumentation-fetch): document applyCustomAttributesOnSpan
1 parent bb62ad3 commit 14e5103

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

packages/opentelemetry-instrumentation-fetch/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ fetch('http://localhost:8090/fetch.js');
5959

6060
See [examples/tracer-web/fetch](https://github.com/open-telemetry/opentelemetry-js/tree/main/examples/tracer-web) for a short example.
6161

62+
### Fetch Instrumentation options
63+
64+
Fetch instrumentation plugin has few options available to choose from. You can set the following:
65+
66+
| Options | Type | Description |
67+
| ------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------- | ------------------------------------- |
68+
| [`applyCustomAttributesOnSpan`](https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-instrumentation-fetch/src/fetch.ts#L47) | `HttpCustomAttributeFunction` | Function for adding custom attributes |
69+
6270
## Useful links
6371

6472
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>

packages/opentelemetry-instrumentation-fetch/src/fetch.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,14 @@ export class FetchInstrumentation extends InstrumentationBase<
198198
}
199199
const method = (options.method || 'GET').toUpperCase();
200200
const spanName = `HTTP ${method}`;
201-
const span = this.tracer.startSpan(spanName, {
201+
return this.tracer.startSpan(spanName, {
202202
kind: api.SpanKind.CLIENT,
203203
attributes: {
204204
[AttributeNames.COMPONENT]: this.moduleName,
205205
[HttpAttribute.HTTP_METHOD]: method,
206206
[HttpAttribute.HTTP_URL]: url,
207207
},
208208
});
209-
210-
return span;
211209
}
212210

213211
/**
@@ -320,7 +318,6 @@ export class FetchInstrumentation extends InstrumentationBase<
320318
) {
321319
try {
322320
plugin._applyAttributesAfterFetch(span, options, response);
323-
324321
if (response.status >= 200 && response.status < 400) {
325322
plugin._endSpan(span, spanData, response);
326323
} else {

packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts

+29-12
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ const getData = (url: string, method?: string) =>
5959

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

6564
const defaultResource = {
@@ -337,48 +336,51 @@ describe('fetch', () => {
337336
const keys = Object.keys(attributes);
338337

339338
assert.ok(
340-
attributes[keys[0]] !== '',
339+
attributes[AttributeNames.COMPONENT] !== '',
341340
`attributes ${AttributeNames.COMPONENT} is not defined`
342341
);
343342
assert.strictEqual(
344-
attributes[keys[1]],
343+
attributes[HttpAttribute.HTTP_METHOD],
345344
'GET',
346345
`attributes ${HttpAttribute.HTTP_METHOD} is wrong`
347346
);
348347
assert.strictEqual(
349-
attributes[keys[2]],
348+
attributes[HttpAttribute.HTTP_URL],
350349
url,
351350
`attributes ${HttpAttribute.HTTP_URL} is wrong`
352351
);
353352
assert.strictEqual(
354-
attributes[keys[3]],
353+
attributes[HttpAttribute.HTTP_STATUS_CODE],
355354
200,
356355
`attributes ${HttpAttribute.HTTP_STATUS_CODE} is wrong`
357356
);
358357
assert.ok(
359-
attributes[keys[4]] === 'OK' || attributes[keys[4]] === '',
358+
attributes[HttpAttribute.HTTP_STATUS_TEXT] === 'OK' ||
359+
attributes[HttpAttribute.HTTP_STATUS_TEXT] === '',
360360
`attributes ${HttpAttribute.HTTP_STATUS_TEXT} is wrong`
361361
);
362362
assert.ok(
363-
(attributes[keys[5]] as string).indexOf('localhost') === 0,
363+
(attributes[HttpAttribute.HTTP_HOST] as string).indexOf('localhost') ===
364+
0,
364365
`attributes ${HttpAttribute.HTTP_HOST} is wrong`
365366
);
366367
assert.ok(
367-
attributes[keys[6]] === 'http' || attributes[keys[6]] === 'https',
368+
attributes[HttpAttribute.HTTP_SCHEME] === 'http' ||
369+
attributes[HttpAttribute.HTTP_SCHEME] === 'https',
368370
`attributes ${HttpAttribute.HTTP_SCHEME} is wrong`
369371
);
370372
assert.ok(
371-
attributes[keys[7]] !== '',
373+
attributes[HttpAttribute.HTTP_USER_AGENT] !== '',
372374
`attributes ${HttpAttribute.HTTP_USER_AGENT} is not defined`
373375
);
374376
assert.ok(
375-
(attributes[keys[8]] as number) > 0,
377+
(attributes[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH] as number) > 0,
376378
`attributes ${HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH} is <= 0`
377379
);
378380

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

381-
assert.strictEqual(keys.length, 9, 'number of attributes is wrong');
383+
assert.strictEqual(keys.length, 10, 'number of attributes is wrong');
382384
});
383385

384386
it('span should have correct events', () => {
@@ -610,11 +612,15 @@ describe('fetch', () => {
610612
describe('when request is NOT successful (wrong url)', () => {
611613
beforeEach(done => {
612614
const propagateTraceHeaderCorsUrls = badUrl;
613-
prepareData(done, badUrl, { propagateTraceHeaderCorsUrls });
615+
prepareData(done, badUrl, {
616+
propagateTraceHeaderCorsUrls,
617+
applyCustomAttributesOnSpan: customAttributeFunction,
618+
});
614619
});
615620
afterEach(() => {
616621
clearData();
617622
});
623+
618624
it('should create a span with correct root span', () => {
619625
const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
620626
assert.strictEqual(
@@ -623,6 +629,17 @@ describe('fetch', () => {
623629
'parent span is not root span'
624630
);
625631
});
632+
633+
it('should apply custom attributes', () => {
634+
const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
635+
const attributes = span.attributes;
636+
637+
assert.strictEqual(
638+
attributes['span kind'],
639+
api.SpanKind.CLIENT,
640+
'Custom attribute was not applied'
641+
);
642+
});
626643
});
627644

628645
describe('when request is NOT successful (405)', () => {

0 commit comments

Comments
 (0)