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

@opentelemetry/web does not work in web workers #1575

Closed
Obi-Dann opened this issue Oct 6, 2020 · 3 comments
Closed

@opentelemetry/web does not work in web workers #1575

Obi-Dann opened this issue Oct 6, 2020 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@Obi-Dann
Copy link

Obi-Dann commented Oct 6, 2020

What version of OpenTelemetry are you using?

0.11.0

What version of Node are you using?

v12.18.3

Please provide the code you used to setup the OpenTelemetry SDK

CodeSandbox editor url https://codesandbox.io/s/dark-brook-q5dmi?fontsize=14&hidenavigation=1&theme=dark
CodeSandbox preview https://q5dmi.sse.codesandbox.io/

// JS code running inside web worker
import {
  ConsoleSpanExporter,
  SimpleSpanProcessor
} from "@opentelemetry/tracing";
import { WebTracerProvider } from "@opentelemetry/web";

const provider = new WebTracerProvider();

provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));

provider.register();

What did you do?

Just load the page

What did you expect to see?

WebWorker would run successfully with opentelemetry instrumentation

What did you see instead?

Errors when running opentelemetry libs

Additional context

Some of the modules in opentelemetry use modules unavailable in web workers. So far I found these places

const _window = window as typeof window & ENVIRONMENT_MAP;

this code uses window unavailable in web workers, it's simple to fix by replacing window with self

const urlNormalizingA = document.createElement('a');

document is not available in web workers, it's probably possible to use different logic for normalizing URLs eg, new URL or something else

@Obi-Dann Obi-Dann added the bug Something isn't working label Oct 6, 2020
@DVN237294
Copy link

DVN237294 commented Dec 29, 2020

Having the same issue trying to implement @opentelemetry/web in Next.js. My issue is essentially the same as OP, so commenting here instead of making a new issue.

I initialized WebTracerProvider in React's useEffect hook to make sure it only runs client-side, but Next.js's server side rendering process in Node seems to be resolving the dependencies server side, causing the document to be referenced here:

const urlNormalizingA = document.createElement('a');

Moving the document.createElement('a') reference to

urlNormalizingA.href = spanUrl;
where urlNormalizingA is referred to, solves the problem for me as all other document references runs client-side (I think).

(I'm not sure if useEffect is the right place to init OpenTelemetry. I'm quite new to both OpenTelemetry and Next.js so I'm open to suggestions!)

@ryhinchey
Copy link
Contributor

I've been hitting the same issues referenced in this issue and will start work on a PR fixing them

@legendecas
Copy link
Member

Closing as #2719 landed.

pichlermarc added a commit to dynatrace-oss-contrib/opentelemetry-js that referenced this issue Dec 15, 2023
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

No branches or pull requests

4 participants