feat(sdk): add instrumentations config option to the SDK - #415
Conversation
instrumentations config option to the SDK
joaquin-diaz
left a comment
There was a problem hiding this comment.
Looks good! Thanks for adding this! 🚀
|
@manototh yesterday we had a discussion about the registering of the instrumentations. Why? because the register function from Instrumentations have a logic in the base class constructor. The constructor inspects the configuration passed and decides to enable itself or not depending on the const instr = new MyInstrumentation({ enabled: false });gives you an instrumentation instance that is not enabled an therefore not emitting LogRecords or starting Spans. What happens with
I brought it up the the SIG yesterday since I think this might be misleading for devs. They might want to start the SDK with only a subset of instrumentations enabled so they may think the following code prevents the SDK for sending web vitals. const fetchInstr = new FetchInstrumentation(),
const vitalsInstr = new WebVitalsInstrumentation({ enabled: false }), // <- expecting this to not collect events
startBrowserSdk({
// other config options
instrumentations: [ fetchInstr, vitalsInstr ],
});
// to actually not collect data we should do this after starting the SDK
vitalsInstr.disable();To make thins more complex the We are going to keep the discussion in this PR. As for now there is a initiative to refactor the instrumentation base class in #278 |
|
I wasn't part of the project when the instrumentation package was created so I do not know the context then. Here is my 2cts. I guess the In this new context (we can ensure SDK is loaded 1st) we can see
IMHO patching and enabling are 2 different operations. We want to do the 1st as soon as possible (to avoid the possibility of 3rd party scripts tampering the target APIs) while the 2nd can be done much later. If we do this decoupling we have more control on when the instrumentation can send data or not. Why do we want this control? a simple example is to control the volume of data sent by the SDK. My idea of an instrumentations is an object with:
Having this patch/enable separated the SDK can tell all instrumentations to patch at start and only enable the ones where the configuration says so. Use Case // This could be rendered by a server that reads the flags from a datasource
// so the devs have control on the traffic from their bckend
const enabledInstrs= {
fetch: true,
xhr: true,
'web-vitals': true,
'my-instr': false,
};
startBrowserSdk({
instrumentations: [
new FetchInstrumentation({enabled: enabledInstrs['fetch']}),
new XhrInstrumentation({enabled: enabledInstrs['xhr']}),
new WebVitalsInstrumentation({enabled: enabledInstrs['web-vitals']}),
new MyCustomInstrumentation({enabled: enabledInstrs['my-instr']}),
]
}) |
wolfgangcodes
left a comment
There was a problem hiding this comment.
This provides a reasonable ergonomic interface for setting up instrumentation.
Which problem is this PR solving?
If you want third-party instrumentations, you currently have to call
registerInstrumentations()yourself afterstartBrowserSdk(), wire up the providers manually, and remember to deregister them on shutdown. This is easy to get wrong and duplicates logic the SDK could own.This PR lets you pass instrumentations directly to the SDK.
Short description of the changes
instrumentationsoption to the SDK config. The SDK registers instrumentations once the global providers are set and disables them on shutdown. Instrumentations aren't registered when the SDK is disabled or the export URL is invalid.@opentelemetry/instrumentation@^0.221.0as a dependency of@opentelemetry/browser-sdk.registerInstrumentations()manually.Type of change
How Has This Been Tested?
Checklist: