apm-agent-rum-js icon indicating copy to clipboard operation
apm-agent-rum-js copied to clipboard

Support web/service workers

Open hmdhk opened this issue 6 years ago • 1 comments

  • 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

hmdhk avatar Nov 19 '19 14:11 hmdhk

Is there any workaround to add custom transactions inside a web/service worker?

vibaiher-qatium avatar Oct 06 '21 06:10 vibaiher-qatium

Hi there, I'm hitting the same limitation. Is it something with an ETA already or still just in the roadmap?

a-tremblay avatar Oct 07 '22 05:10 a-tremblay

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

devcorpio avatar Oct 10 '22 15:10 devcorpio

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

a-tremblay avatar Oct 10 '22 22:10 a-tremblay

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

devcorpio avatar Oct 20 '22 15:10 devcorpio