apm-agent-rum-js
apm-agent-rum-js copied to clipboard
Support web/service workers
- In the first phase we should make sure the agent works properly within a web worker by adding test for custom transactions.
- We should investigate if we should report the creation of web workers in the active transaction.
relevant issues:
- https://discuss.elastic.co/t/apm-rum-js-agent-service-worker/215862
Is there any workaround to add custom transactions inside a web/service worker?
Hi there, I'm hitting the same limitation. Is it something with an ETA already or still just in the roadmap?
Hi @a-tremblay,
We don't still have a ETA for this.
Despite so, there is a workaround that allows to add custom transactions within workers.
--
// Setup that allow the agent initialisation
if (typeof window !== undefined) {
var window = {
addEventListener: function noop() {},
XMLHttpRequest: self.XMLHttpRequest,
location: {
href: 'I am within the web worker'
}
}
}
// Setup that allow the agent initialisation
if (typeof document !== undefined) {
var document = {
currentScript: null,
getElementsByTagName() {
return {
length: 0
}
}
}
}
self.importScripts("http://localhost:3000/elastic-apm-rum.umd.min.js");
window.elasticApm.init({
serviceName: 'agent-within-webworker',
serverUrl: 'http://localhost:8200',
// disable instrumentations that will allow to initiate the worker.
// Take into account that instrumentations will not work here anyway.
disableInstrumentations: ['page-load', 'click', 'history']
})
// Example using the SDK to create transactions
const transaction = window.elasticApm.startTransaction("my-transaction", "custom")
const span = transaction.startSpan("my-span", "my-span-type")
// just an example interacting with the SDK
setTimeout(() => {
span.end()
transaction.end()
}, 300)
Please, let me know if this helps you in the meanwhile.
Thanks, Alberto
Hi Alberto,
This is gold! Indeed mocking the window global variable is a smart workaround. I will have a try and let you know how goes
Cheers, Alex
More specific tickets are going to be created. The first one is: https://github.com/elastic/apm-agent-rum-js/issues/1295
For someone reading this in the future, the idea is to create those new tickets using the label "workers-support"
Note: Workers = Web Worker and Service Worker
Thanks, Alberto