open-telemetry / opentelemetry-js-contrib

OpenTelemetry instrumentation for JavaScript modules
https://opentelemetry.io
Apache License 2.0
641 stars 480 forks source link

User interaction instrumentation not working as expected #2262

Open Abinet18 opened 1 month ago

Abinet18 commented 1 month ago

When testing user interaction instrumentation with an app, there is an issue of spans not getting created if the app scripts execute earlier than the instrumentation. I.e if the click handlers in the app script run before the instrumentation started, it will not be using the wrapped listener.

In apps like react where additional event listeners are added , the instrumentation may catch some of the late coming listeners and it creates a few spans per click, but still if there are fetch calls made from the click, these are not created as children of the root click span , probably because the first event handler is missed(not wrapped) by the instrumentation.

Is there a way to handle such scenario where we cannot ensure the instrumentation load and execute before the app code. Also would it be possible to keep only the root span for the event discarding the children

related issue/pr

https://github.com/open-telemetry/opentelemetry-js-contrib/pull/1749 https://github.com/open-telemetry/opentelemetry-js-contrib/issues/1750 https://github.com/open-telemetry/opentelemetry-js-contrib/issues/548

Abinet18 commented 1 month ago

@t2t2 @JamieDanielson @pkanal

t2t2 commented 1 month ago

Is there a way to handle such scenario where we cannot ensure the instrumentation load and execute before the app code

No

 

 

 

Ok fine I'll expand on the answer

If all you care about is tracking if user clicked, then yes adding event handlers for the events you want to track like the approach in #1749 / #1750 works. That however wouln't fix the context/parent-child relationship issue (xhr, fetch, ... spans won't be children of click span) - which you mentioned earlier in the comment so that's not suitable - and separate question of how useful is data about clicks that did nothing

There might be something into registering 2 event listeners - one with capture: true, other with false and running all code between those two in the context of click span, but then you'd open up another can of worms in case someone did $.live before instrumenting or event listener on window after instrumenting

Really it's just easiest to declare that if you don't include instrumentation as the first thing, data accuracy can't be guaranteed. And this applies to instrumenting in general, because if you do fetch() before FetchInstrumentation - you're also missing that span

I have a soft suspicion that the underlaying reason for this request is fetching bootstrap information from the server before setting up otel

 

So No

Abinet18 commented 1 month ago

I agree that ideally otel script should load and execute before app scripts. But even if executes later , it doesn't affect the other instrumentations as much. e.g For fetch instrumentation only the fetch calls made before fetch is patched will be missed, but such fetch calls are not that many while in the user interaction case, all click events will be missed if the listener is attached before the instrumentation started.