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

getEnv() for platform "browser" does not work with service workers #2211

Closed
svrnm opened this issue May 18, 2021 · 2 comments · Fixed by #2465
Closed

getEnv() for platform "browser" does not work with service workers #2211

svrnm opened this issue May 18, 2021 · 2 comments · Fixed by #2465
Labels
bug Something isn't working

Comments

@svrnm
Copy link
Member

svrnm commented May 18, 2021

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 in getEnv, 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 on window:

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:

// const _window = window as typeof window & RAW_ENVIRONMENT;
const _window = self as typeof window & RAW_ENVIRONMENT;
@svrnm svrnm added the bug Something isn't working label May 18, 2021
@dyladan
Copy link
Member

dyladan commented May 18, 2021

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

@svrnm
Copy link
Member Author

svrnm commented May 19, 2021

const _window = window ? (window as typeof window & RAW_ENVIRONMENT) : (self as ???);

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants