APM RUM JS Agent + Service worker

Kibana version: 7.4.0

Elasticsearch version: 7.4.0

APM Server version: 7.4.0

APM Agent language and version: @elastic/apm-rum@4.7.0

Original install method (e.g. download page, yum, deb, from source, etc.) and version: Downloaded the JavaScript agent from GitHub

Fresh install or upgraded from other version? Fresh

Is there anything special in your setup? No

Description of the problem including expected versus actual behavior. Please include screenshots (if relevant):

I want to use APM RUM with a service worker. This service worker is used for fetch catching. It enqueues some requests that are sent in offline mode and send them again as soon as the navigator is online.

I am trying to use APM RUM importing the agent script via importScripts() without getting any error at all. This is the line:

importScripts('/lib/localforage.min.js', 'elastic-apm-rum.umd.min.js');

Then I get an error in the following line:

var apm = elasticApm.init({ serviceName: 'ServiceName', serverUrl: 'http://localhost:8200' });

This is the error:

Uncaught ReferenceError: elasticApm is not defined

NOTE: The line that produces the error is inside another function which is called when I am sending the requests.

I also have tried importing a custom .js file which instantiates the elasticApm object but I get the same error so I guess the agent script is not loaded at the moment I am trying to access it.

Is there a way to use it inside my service worker?

Actual behavior:

I cant use the elasticApm object inside a service worker.

Expected behavior:

Use the elasticApm object inside a service worker just as I do in other .js files for starting transactions and spans and also capturing errors.

Errors in browser console (if relevant): Uncaught ReferenceError: elasticApm is not defined

Hi @AngelLR,

Thanks for using Elastic APM and welcome to the forum.

At the moment we don't support web workers, however, we have an issue to keep track of this feature request.

Knowing about your use-case would help us a lot with providing this feature. Would you please explain what kind of metrics you would like to capture from within the web worker?



I have a .net core application. This application has some javascript code to record audio and take pictures (using getUserMedia). I also have a service worker whose purpose is check every request looking for audio and pictures uploads and, if after trying to fetch a request I find there is no internet connection, it saves them so I can resend them once we are online again.

At this moment I am using the APM RUM JS Agent in some .js files to start transactions and spans and get some info about the methods that record the audio or take the pictures. I am also capturing any error and sending it to the apm server manually.

I wanted to get the metrics involved in the method of the service worker that resend the request one by one, and I wanted to catch any exception for debugging purposes.

Thank you.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.