From d16fa9e055747735f85161df7dbf6969a030cf1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martti=20Mouruj=C3=A4rvi?= Date: Sun, 14 Apr 2024 17:37:37 +0300 Subject: [PATCH 1/4] refactor(instr-fetch): move fetch to use SEMATRR --- experimental/CHANGELOG.md | 3 +- .../README.md | 15 ++++++++++ .../src/fetch.ts | 24 ++++++++------- .../test/fetch.test.ts | 30 ++++++++++++------- 4 files changed, 50 insertions(+), 22 deletions(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 20e3fbfdab3..b64ffd79a54 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -20,8 +20,9 @@ All notable changes to experimental packages in this project will be documented ### :rocket: (Enhancement) +* refactor(instrumentation-fetch): move fetch to use SEMATRR [#4632](https://github.com/open-telemetry/opentelemetry-js/pull/4632) * feat(instrumentation): add util to execute span customization hook in base class [#4663](https://github.com/open-telemetry/opentelemetry-js/pull/4663) @blumamir -* feat(instrumentation): generic config type in instrumentation base [#4659](https://github.com/open-telemetry/opentelemetry-js/pull/4659) @blumamir +* feat(instrumentation): generic config type in instrumentation base [#4659](https://github.com/open-telemetry/opentelemetry-js/pull/4659) @blumamirs * feat: support node 22 [#4666](https://github.com/open-telemetry/opentelemetry-js/pull/4666) @dyladan * feat(propagator-aws-xray-lambda): add AWS Xray Lambda propagator [4554](https://github.com/open-telemetry/opentelemetry-js/pull/4554) diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/README.md b/experimental/packages/opentelemetry-instrumentation-fetch/README.md index b12c0f11632..bc784ce5f1e 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/README.md +++ b/experimental/packages/opentelemetry-instrumentation-fetch/README.md @@ -72,6 +72,21 @@ Fetch instrumentation plugin has few options available to choose from. You can s | [`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 | | [`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) | +## Semantic Conventions + +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) + +Attributes collected: + +| Attribute | Short Description | +| ------------------------------------------- | ------------------------------------------------------------------------------ | +| `HTTP_STATUS_CODE` | HTTP response status cod | +| `HTTP_HOST` | The value of the HTTP host header | +| `HTTP_USER_AGENT` | Value of the HTTP User-Agent header sent by the client | +| `HTTP_SCHEME` | The URI scheme identifying the used protocol | +| `HTTP_URL` | Full HTTP request URL | +| `HTTP_METHOD` | HTTP request method | + ## Useful links - For more information on OpenTelemetry, visit: diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts index d459d3884f4..6c0c6f95c4d 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts @@ -24,7 +24,14 @@ import { import * as core from '@opentelemetry/core'; import * as web from '@opentelemetry/sdk-trace-web'; import { AttributeNames } from './enums/AttributeNames'; -import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { + SEMATTRS_HTTP_STATUS_CODE, + SEMATTRS_HTTP_HOST, + SEMATTRS_HTTP_USER_AGENT, + SEMATTRS_HTTP_SCHEME, + SEMATTRS_HTTP_URL, + SEMATTRS_HTTP_METHOD, +} from '@opentelemetry/semantic-conventions'; import { FetchError, FetchResponse, SpanData } from './types'; import { VERSION } from './version'; import { _globalThis } from '@opentelemetry/core'; @@ -119,20 +126,17 @@ export class FetchInstrumentation extends InstrumentationBase { assert.strictEqual( attributes[keys[1]], 'GET', - `attributes ${SemanticAttributes.HTTP_METHOD} is wrong` + `attributes ${SEMATTRS_HTTP_METHOD} is wrong` ); assert.strictEqual( attributes[keys[2]], url, - `attributes ${SemanticAttributes.HTTP_URL} is wrong` + `attributes ${SEMATTRS_HTTP_URL} is wrong` ); assert.strictEqual( attributes[keys[3]], 200, - `attributes ${SemanticAttributes.HTTP_STATUS_CODE} is wrong` + `attributes ${SEMATTRS_HTTP_STATUS_CODE} is wrong` ); assert.ok( attributes[keys[4]] === 'OK' || attributes[keys[4]] === '', @@ -392,19 +400,19 @@ describe('fetch', () => { ); assert.ok( (attributes[keys[5]] as string).indexOf('localhost') === 0, - `attributes ${SemanticAttributes.HTTP_HOST} is wrong` + `attributes ${SEMATTRS_HTTP_HOST} is wrong` ); assert.ok( attributes[keys[6]] === 'http' || attributes[keys[6]] === 'https', - `attributes ${SemanticAttributes.HTTP_SCHEME} is wrong` + `attributes ${SEMATTRS_HTTP_SCHEME} is wrong` ); assert.ok( attributes[keys[7]] !== '', - `attributes ${SemanticAttributes.HTTP_USER_AGENT} is not defined` + `attributes ${SEMATTRS_HTTP_USER_AGENT} is not defined` ); assert.ok( (attributes[keys[8]] as number) > 0, - `attributes ${SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH} is <= 0` + `attributes ${SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH} is <= 0` ); assert.strictEqual(keys.length, 9, 'number of attributes is wrong'); @@ -865,9 +873,9 @@ describe('fetch', () => { const attributes = span.attributes; assert.strictEqual( - attributes[SemanticAttributes.HTTP_URL], + attributes[SEMATTRS_HTTP_URL], location.origin + '/get', - `attributes ${SemanticAttributes.HTTP_URL} is wrong` + `attributes ${SEMATTRS_HTTP_URL} is wrong` ); }); }); @@ -934,7 +942,7 @@ describe('fetch', () => { assert.strictEqual( attributes[keys[3]], 200, - `Missing basic attribute ${SemanticAttributes.HTTP_STATUS_CODE}` + `Missing basic attribute ${SEMATTRS_HTTP_STATUS_CODE}` ); }); }); From 015b1df58825e98301b281804f0699b930c05512 Mon Sep 17 00:00:00 2001 From: mmouru <55876749+mmouru@users.noreply.github.com> Date: Tue, 21 May 2024 19:02:45 +0300 Subject: [PATCH 2/4] Update experimental/CHANGELOG.md Co-authored-by: Trent Mick --- experimental/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index b64ffd79a54..f206d0ccbb5 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -22,7 +22,7 @@ All notable changes to experimental packages in this project will be documented * refactor(instrumentation-fetch): move fetch to use SEMATRR [#4632](https://github.com/open-telemetry/opentelemetry-js/pull/4632) * feat(instrumentation): add util to execute span customization hook in base class [#4663](https://github.com/open-telemetry/opentelemetry-js/pull/4663) @blumamir -* feat(instrumentation): generic config type in instrumentation base [#4659](https://github.com/open-telemetry/opentelemetry-js/pull/4659) @blumamirs +* feat(instrumentation): generic config type in instrumentation base [#4659](https://github.com/open-telemetry/opentelemetry-js/pull/4659) @blumamir * feat: support node 22 [#4666](https://github.com/open-telemetry/opentelemetry-js/pull/4666) @dyladan * feat(propagator-aws-xray-lambda): add AWS Xray Lambda propagator [4554](https://github.com/open-telemetry/opentelemetry-js/pull/4554) From 5da16391222439b5105d4ddac05fec59adfbe75c Mon Sep 17 00:00:00 2001 From: Jamie Danielson Date: Thu, 6 Jun 2024 12:27:02 -0400 Subject: [PATCH 3/4] Apply suggestions from code review update readme table to use attribute strings --- .../opentelemetry-instrumentation-fetch/README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/README.md b/experimental/packages/opentelemetry-instrumentation-fetch/README.md index bc784ce5f1e..2d3c2d4aeed 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/README.md +++ b/experimental/packages/opentelemetry-instrumentation-fetch/README.md @@ -80,12 +80,12 @@ Attributes collected: | Attribute | Short Description | | ------------------------------------------- | ------------------------------------------------------------------------------ | -| `HTTP_STATUS_CODE` | HTTP response status cod | -| `HTTP_HOST` | The value of the HTTP host header | -| `HTTP_USER_AGENT` | Value of the HTTP User-Agent header sent by the client | -| `HTTP_SCHEME` | The URI scheme identifying the used protocol | -| `HTTP_URL` | Full HTTP request URL | -| `HTTP_METHOD` | HTTP request method | +| `http.status_code` | HTTP response status code | +| `http.host` | The value of the HTTP host header | +| `http.user_agent` | Value of the HTTP User-Agent header sent by the client | +| `http.scheme` | The URI scheme identifying the used protocol | +| `http.url` | Full HTTP request URL | +| `http.method` | HTTP request method | ## Useful links From 2491bdb375b4559ae7f398416c17cd9075d26e5f Mon Sep 17 00:00:00 2001 From: JamieDanielson Date: Fri, 7 Jun 2024 11:53:44 -0400 Subject: [PATCH 4/4] move changelog entry to unreleased --- experimental/CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 674a3f6e0c4..b9d176321b8 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -9,6 +9,8 @@ All notable changes to experimental packages in this project will be documented ### :rocket: (Enhancement) +* refactor(instrumentation-fetch): move fetch to use SEMATRR [#4632](https://github.com/open-telemetry/opentelemetry-js/pull/4632) + ### :bug: (Bug Fix) ### :books: (Refine Doc) @@ -45,7 +47,6 @@ All notable changes to experimental packages in this project will be documented ### :rocket: (Enhancement) * feat(instrumentation): apply unwrap before wrap in base class [#4692](https://github.com/open-telemetry/opentelemetry-js/pull/4692) -* refactor(instrumentation-fetch): move fetch to use SEMATRR [#4632](https://github.com/open-telemetry/opentelemetry-js/pull/4632) * feat(instrumentation): add util to execute span customization hook in base class [#4663](https://github.com/open-telemetry/opentelemetry-js/pull/4663) @blumamir * feat(instrumentation): generic config type in instrumentation base [#4659](https://github.com/open-telemetry/opentelemetry-js/pull/4659) @blumamir * feat: support node 22 [#4666](https://github.com/open-telemetry/opentelemetry-js/pull/4666) @dyladan