Skip to content

Commit

Permalink
refactor(instr-document-load): use exported strings for semconv
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieDanielson committed Mar 26, 2024
1 parent 8015d74 commit 00f43af
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions plugins/web/opentelemetry-instrumentation-document-load/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ registerInstrumentations({

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

## 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 | Notes |
| ----------------- | ------------------------------------------------------------------------------ | ------------------------------- |
| `http.url` | Full HTTP request URL in the form `scheme://host[:port]/path?query[#fragment]` | Key: `SEMATTRS_HTTP_URL` |
| `http.user_agent` | Value of the HTTP User-Agent header sent by the client | Key: `SEMATTRS_HTTP_USER_AGENT` |

## Useful links

- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"@opentelemetry/instrumentation": "^0.49.1",
"@opentelemetry/sdk-trace-base": "^1.0.0",
"@opentelemetry/sdk-trace-web": "^1.15.0",
"@opentelemetry/semantic-conventions": "^1.0.0"
"@opentelemetry/semantic-conventions": "^1.22.0"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/web/opentelemetry-instrumentation-document-load#readme"
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ import {
} from './types';
import { AttributeNames } from './enums/AttributeNames';
import { VERSION } from './version';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMATTRS_HTTP_URL,
SEMATTRS_HTTP_USER_AGENT,
} from '@opentelemetry/semantic-conventions';
import {
addSpanPerformancePaintEvents,
getPerformanceNavigationEntries,
Expand Down Expand Up @@ -115,7 +118,7 @@ export class DocumentLoadInstrumentation extends InstrumentationBase<unknown> {
entries
);
if (fetchSpan) {
fetchSpan.setAttribute(SemanticAttributes.HTTP_URL, location.href);
fetchSpan.setAttribute(SEMATTRS_HTTP_URL, location.href);
context.with(trace.setSpan(context.active(), fetchSpan), () => {
addSpanNetworkEvents(fetchSpan, entries);
this._addCustomAttributesOnSpan(
Expand All @@ -127,11 +130,8 @@ export class DocumentLoadInstrumentation extends InstrumentationBase<unknown> {
}
});

rootSpan.setAttribute(SemanticAttributes.HTTP_URL, location.href);
rootSpan.setAttribute(
SemanticAttributes.HTTP_USER_AGENT,
navigator.userAgent
);
rootSpan.setAttribute(SEMATTRS_HTTP_URL, location.href);
rootSpan.setAttribute(SEMATTRS_HTTP_USER_AGENT, navigator.userAgent);

this._addResourcesSpans(rootSpan);

Expand Down Expand Up @@ -196,7 +196,7 @@ export class DocumentLoadInstrumentation extends InstrumentationBase<unknown> {
parentSpan
);
if (span) {
span.setAttribute(SemanticAttributes.HTTP_URL, resource.name);
span.setAttribute(SEMATTRS_HTTP_URL, resource.name);
addSpanNetworkEvents(span, resource);
this._addCustomAttributesOnResourceSpan(
span,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ import {
import chai from 'chai/chai.js';
import * as sinon from 'sinon';
import { DocumentLoadInstrumentation } from '../src';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH,
SEMATTRS_HTTP_URL,
} from '@opentelemetry/semantic-conventions';
import { EventNames } from '../src/enums/EventNames';

const { assert } = chai as typeof import('chai');
Expand Down Expand Up @@ -347,7 +350,7 @@ describe('DocumentLoad Instrumentation', () => {
assert.strictEqual(rootSpan.name, 'documentFetch');
assert.ok(
(rootSpan.attributes[
SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH
SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH
] as number) > 0
);
assert.strictEqual(fetchSpan.name, 'documentLoad');
Expand Down Expand Up @@ -448,11 +451,11 @@ describe('DocumentLoad Instrumentation', () => {
const srEvents2 = spanResource2.events;

assert.strictEqual(
spanResource1.attributes[SemanticAttributes.HTTP_URL],
spanResource1.attributes[SEMATTRS_HTTP_URL],
'http://localhost:8090/bundle.js'
);
assert.strictEqual(
spanResource2.attributes[SemanticAttributes.HTTP_URL],
spanResource2.attributes[SEMATTRS_HTTP_URL],
'http://localhost:8090/sockjs-node/info?t=1572620894466'
);

Expand Down Expand Up @@ -484,7 +487,7 @@ describe('DocumentLoad Instrumentation', () => {
const srEvents1 = spanResource1.events;

assert.strictEqual(
spanResource1.attributes[SemanticAttributes.HTTP_URL],
spanResource1.attributes[SEMATTRS_HTTP_URL],
'http://localhost:8090/bundle.js'
);

Expand Down

0 comments on commit 00f43af

Please sign in to comment.