Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(opentelemetry-sdk-trace-web): don't crash in runtimes where location isn't defined #3715

Merged
merged 5 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/
### :bug: (Bug Fix)

* fix(instrumentation-http): fixed description for http.server.duration metric [#3710](https://github.com/open-telemetry/opentelemetry-js/pull/3710)
* fix(opentelemetry-sdk-trace-web): don't crash in runtimes where location isn't defined [#3715](https://github.com/open-telemetry/opentelemetry-js/pull/3715)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a huge deal, so if it's better to scrap this and wait for the larger refactor to not have Fetch autoinstrumentation depend on this package that's totally fine by me

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple enough change I'm fine to accept it


### :books: (Refine Doc)

Expand Down
6 changes: 5 additions & 1 deletion packages/opentelemetry-sdk-trace-web/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,11 @@ export function parseUrl(url: string): URLLike {
if (typeof URL === 'function') {
return new URL(
url,
typeof document !== 'undefined' ? document.baseURI : location.href
typeof document !== 'undefined'
? document.baseURI
: typeof location !== 'undefined'
? location.href
: undefined
);
}
const element = getUrlNormalizingAnchor();
Expand Down
15 changes: 15 additions & 0 deletions packages/opentelemetry-sdk-trace-web/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,21 @@ describe('utils', () => {
assert.strictEqual(typeof url[field], 'string');
});
});

// Deno is an example of this
it('should parse url in runtimes where global location is not defined', () => {
const actualLocationObj = globalThis.location;
globalThis.location = undefined as any; // undefined is not allowed as a value normally, hence the any

try {
const url = parseUrl('https://opentelemetry.io/foo');
urlFields.forEach(field => {
assert.strictEqual(typeof url[field], 'string');
});
} finally {
globalThis.location = actualLocationObj;
}
});
});

describe('normalizeUrl', () => {
Expand Down