We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
0.19
I try to use opentelemetry-web & opentelemetry-tracing within a Service Worker
A working tracer.
An error that window is not defined in getEnv, since Service Worker do have a different global object, i.e. self
window
getEnv
self
Looking into getEnv() for the browser, it's clear that this issue comes from a dependency on window:
getEnv()
export function getEnv(): Required<ENVIRONMENT> { const _window = window as typeof window & RAW_ENVIRONMENT; const globalEnv = parseEnvironment(_window); return Object.assign({}, DEFAULT_ENVIRONMENT, globalEnv); }
I could work around this issue by adding the following code to my service worker:
export type {}; interface MyServiceWorkerGlobalScope extends ServiceWorkerGlobalScope { window: unknown; } declare const self: MyServiceWorkerGlobalScope; if (self.constructor.name !== 'Window') { self.window = self; }
I am not sure if something like the following would do the trick:
const _window = window ? (window as typeof window & RAW_ENVIRONMENT) : (self as ???);
or, since all modern browsers have window.self as well (source) the following:
window.self
// const _window = window as typeof window & RAW_ENVIRONMENT; const _window = self as typeof window & RAW_ENVIRONMENT;
The text was updated successfully, but these errors were encountered:
const _window = window ? (window as typeof window & RAW_ENVIRONMENT) : (self as ???); is similar to what we've done in the API and other places https://github.com/open-telemetry/opentelemetry-js-api/blob/41109c83a9784d689f319f2c5d953b3874c694a3/src/platform/browser/globalThis.ts#L19
Sorry, something went wrong.
Looks like there is still a lot of TypeScript I have to learn 🤣
I'll try to come up with an PR for that, thanks for calling out the existing solution in the api
Successfully merging a pull request may close this issue.
What version of OpenTelemetry are you using?
0.19
What did you do?
I try to use opentelemetry-web & opentelemetry-tracing within a Service Worker
What did you expect to see?
A working tracer.
What did you see instead?
An error that
window
is not defined ingetEnv
, since Service Worker do have a different global object, i.e.self
Additional context
Looking into
getEnv()
for the browser, it's clear that this issue comes from a dependency onwindow
:I could work around this issue by adding the following code to my service worker:
I am not sure if something like the following would do the trick:
or, since all modern browsers have
window.self
as well (source) the following:The text was updated successfully, but these errors were encountered: