Skip to content

Commit 2e42181

Browse files
marttimourujarvitrentmJamieDanielson
authored
refactor(instr-fetch): move fetch to use SEMATRR (#4632)
* refactor(instr-fetch): move fetch to use SEMATRR * Update experimental/CHANGELOG.md Co-authored-by: Trent Mick <[email protected]> * Apply suggestions from code review update readme table to use attribute strings * move changelog entry to unreleased --------- Co-authored-by: Trent Mick <[email protected]> Co-authored-by: Jamie Danielson <[email protected]>
1 parent fd911fb commit 2e42181

File tree

4 files changed

+50
-21
lines changed

4 files changed

+50
-21
lines changed

experimental/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ All notable changes to experimental packages in this project will be documented
99

1010
### :rocket: (Enhancement)
1111

12+
* refactor(instrumentation-fetch): move fetch to use SEMATRR [#4632](https://github.com/open-telemetry/opentelemetry-js/pull/4632)
13+
1214
### :bug: (Bug Fix)
1315

1416
### :books: (Refine Doc)

experimental/packages/opentelemetry-instrumentation-fetch/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ Fetch instrumentation plugin has few options available to choose from. You can s
7272
| [`applyCustomAttributesOnSpan`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts#L64) | `HttpCustomAttributeFunction` | Function for adding custom attributes |
7373
| [`ignoreNetworkEvents`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts#L67) | `boolean` | Disable network events being added as span events (network events are added by default) |
7474

75+
## Semantic Conventions
76+
77+
This package uses `@opentelemetry/semantic-conventions` version `1.22+`, which implements Semantic Convention [Version 1.7.0](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.7.0/semantic_conventions/README.md)
78+
79+
Attributes collected:
80+
81+
| Attribute | Short Description |
82+
| ------------------------------------------- | ------------------------------------------------------------------------------ |
83+
| `http.status_code` | HTTP response status code |
84+
| `http.host` | The value of the HTTP host header |
85+
| `http.user_agent` | Value of the HTTP User-Agent header sent by the client |
86+
| `http.scheme` | The URI scheme identifying the used protocol |
87+
| `http.url` | Full HTTP request URL |
88+
| `http.method` | HTTP request method |
89+
7590
## Useful links
7691

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

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@ import {
2424
import * as core from '@opentelemetry/core';
2525
import * as web from '@opentelemetry/sdk-trace-web';
2626
import { AttributeNames } from './enums/AttributeNames';
27-
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
27+
import {
28+
SEMATTRS_HTTP_STATUS_CODE,
29+
SEMATTRS_HTTP_HOST,
30+
SEMATTRS_HTTP_USER_AGENT,
31+
SEMATTRS_HTTP_SCHEME,
32+
SEMATTRS_HTTP_URL,
33+
SEMATTRS_HTTP_METHOD,
34+
} from '@opentelemetry/semantic-conventions';
2835
import { FetchError, FetchResponse, SpanData } from './types';
2936
import { VERSION } from './version';
3037
import { _globalThis } from '@opentelemetry/core';
@@ -119,20 +126,17 @@ export class FetchInstrumentation extends InstrumentationBase<FetchInstrumentati
119126
response: FetchResponse
120127
): void {
121128
const parsedUrl = web.parseUrl(response.url);
122-
span.setAttribute(SemanticAttributes.HTTP_STATUS_CODE, response.status);
129+
span.setAttribute(SEMATTRS_HTTP_STATUS_CODE, response.status);
123130
if (response.statusText != null) {
124131
span.setAttribute(AttributeNames.HTTP_STATUS_TEXT, response.statusText);
125132
}
126-
span.setAttribute(SemanticAttributes.HTTP_HOST, parsedUrl.host);
133+
span.setAttribute(SEMATTRS_HTTP_HOST, parsedUrl.host);
127134
span.setAttribute(
128-
SemanticAttributes.HTTP_SCHEME,
135+
SEMATTRS_HTTP_SCHEME,
129136
parsedUrl.protocol.replace(':', '')
130137
);
131138
if (typeof navigator !== 'undefined') {
132-
span.setAttribute(
133-
SemanticAttributes.HTTP_USER_AGENT,
134-
navigator.userAgent
135-
);
139+
span.setAttribute(SEMATTRS_HTTP_USER_AGENT, navigator.userAgent);
136140
}
137141
}
138142

@@ -207,8 +211,8 @@ export class FetchInstrumentation extends InstrumentationBase<FetchInstrumentati
207211
kind: api.SpanKind.CLIENT,
208212
attributes: {
209213
[AttributeNames.COMPONENT]: this.moduleName,
210-
[SemanticAttributes.HTTP_METHOD]: method,
211-
[SemanticAttributes.HTTP_URL]: url,
214+
[SEMATTRS_HTTP_METHOD]: method,
215+
[SEMATTRS_HTTP_URL]: url,
212216
},
213217
});
214218
}

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,15 @@ import {
4141
FetchCustomAttributeFunction,
4242
} from '../src';
4343
import { AttributeNames } from '../src/enums/AttributeNames';
44-
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
44+
import {
45+
SEMATTRS_HTTP_HOST,
46+
SEMATTRS_HTTP_METHOD,
47+
SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH,
48+
SEMATTRS_HTTP_SCHEME,
49+
SEMATTRS_HTTP_STATUS_CODE,
50+
SEMATTRS_HTTP_URL,
51+
SEMATTRS_HTTP_USER_AGENT,
52+
} from '@opentelemetry/semantic-conventions';
4553

4654
class DummySpanExporter implements tracing.SpanExporter {
4755
export(spans: any) {}
@@ -374,37 +382,37 @@ describe('fetch', () => {
374382
assert.strictEqual(
375383
attributes[keys[1]],
376384
'GET',
377-
`attributes ${SemanticAttributes.HTTP_METHOD} is wrong`
385+
`attributes ${SEMATTRS_HTTP_METHOD} is wrong`
378386
);
379387
assert.strictEqual(
380388
attributes[keys[2]],
381389
url,
382-
`attributes ${SemanticAttributes.HTTP_URL} is wrong`
390+
`attributes ${SEMATTRS_HTTP_URL} is wrong`
383391
);
384392
assert.strictEqual(
385393
attributes[keys[3]],
386394
200,
387-
`attributes ${SemanticAttributes.HTTP_STATUS_CODE} is wrong`
395+
`attributes ${SEMATTRS_HTTP_STATUS_CODE} is wrong`
388396
);
389397
assert.ok(
390398
attributes[keys[4]] === 'OK' || attributes[keys[4]] === '',
391399
`attributes ${AttributeNames.HTTP_STATUS_TEXT} is wrong`
392400
);
393401
assert.ok(
394402
(attributes[keys[5]] as string).indexOf('localhost') === 0,
395-
`attributes ${SemanticAttributes.HTTP_HOST} is wrong`
403+
`attributes ${SEMATTRS_HTTP_HOST} is wrong`
396404
);
397405
assert.ok(
398406
attributes[keys[6]] === 'http' || attributes[keys[6]] === 'https',
399-
`attributes ${SemanticAttributes.HTTP_SCHEME} is wrong`
407+
`attributes ${SEMATTRS_HTTP_SCHEME} is wrong`
400408
);
401409
assert.ok(
402410
attributes[keys[7]] !== '',
403-
`attributes ${SemanticAttributes.HTTP_USER_AGENT} is not defined`
411+
`attributes ${SEMATTRS_HTTP_USER_AGENT} is not defined`
404412
);
405413
assert.ok(
406414
(attributes[keys[8]] as number) > 0,
407-
`attributes ${SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH} is <= 0`
415+
`attributes ${SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH} is <= 0`
408416
);
409417

410418
assert.strictEqual(keys.length, 9, 'number of attributes is wrong');
@@ -865,9 +873,9 @@ describe('fetch', () => {
865873
const attributes = span.attributes;
866874

867875
assert.strictEqual(
868-
attributes[SemanticAttributes.HTTP_URL],
876+
attributes[SEMATTRS_HTTP_URL],
869877
location.origin + '/get',
870-
`attributes ${SemanticAttributes.HTTP_URL} is wrong`
878+
`attributes ${SEMATTRS_HTTP_URL} is wrong`
871879
);
872880
});
873881
});
@@ -934,7 +942,7 @@ describe('fetch', () => {
934942
assert.strictEqual(
935943
attributes[keys[3]],
936944
200,
937-
`Missing basic attribute ${SemanticAttributes.HTTP_STATUS_CODE}`
945+
`Missing basic attribute ${SEMATTRS_HTTP_STATUS_CODE}`
938946
);
939947
});
940948
});

0 commit comments

Comments
 (0)