question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cloudflare Workers

See original GitHub issue

Is your feature request related to a problem? Please describe. I’m unable to integrate the application insights JavaScript library into my Cloudflare JavaScript Worker. https://workers.cloudflare.com/

Describe the solution you’d like To use the application insights library to track errors, events and dependencies in my Cloudflare Worker so I can get visibility into any interesting events in the worker. Have also tried @microsoft/applicationinsights-web-basic but not luck.

Describe alternatives you’ve considered I’ve tried the NodeJS library (https://github.com/Microsoft/ApplicationInsights-node.js) as well, however Cloudflare workers are web workers so is not a NodeJS process but a v8 thread instead.

Additional context Example Worker

import { ApplicationInsights, SeverityLevel } from '@microsoft/applicationinsights-web';
import { v4 as uuidv4 } from 'uuid';

const responseOk: Response = new Response(null, { status: 200, statusText: 'OK' });
const responseMethodNotAllowed: Response = new Response(null, { status: 405, statusText: 'Method Not Allowed' });
const responseInternalServerError: Response = new Response(null, { status: 500, statusText: 'Internal Server Error' });

const appInsights = new ApplicationInsights({
    config: {
        instrumentationKey: '{{ Instrumentation Key Here }}'
    }
});

async function handleGETRequests(request: Request): Promise<Response> {
    try {
        // Do some orchestration fetch calls

        return responseOk;
    } catch (ex) {
        appInsights.trackException({
            id: uuidv4(),
            exception: ex,
            severityLevel: SeverityLevel.Critical
        })
        return responseInternalServerError;
    }
}

function handler(event: FetchEvent) {
    switch (event.request.method) {
        case 'GET':
            event.respondWith(handleGETRequests(event.request).then(response => {
                appInsights.flush();
                return response;
            }));
        default:
            event.respondWith(responseMethodNotAllowed);
    }
}

appInsights.loadAppInsights();
addEventListener('fetch', handler);

Resulting Error The fetch API is supported, but not XMLHttpRequest. image

Even if fetch was used, the promise might need to be exposed from application insights to hook into the lifecycle of the request & response.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
vansllycommented, Jun 13, 2020

@sazzy4o Thank you for creating, I am very keen to give this go. I was going to loop back and create something to fill the gap and you beat me to it!

To launch our product I had to switch to logging into Elasticsearch for MVP for the interim, but the latency between my Elasticsearch instance and CF edge locations is huge, and I didn’t do any log batching which you can so easily do with the Application Insights endpoint.

Thanks again, look forward to reunifying logging with App Insights.

1reaction
sazzy4ocommented, Jun 13, 2020

@vanslly I have recently created an npm package that allows you to use application insights with cloudflare workers: https://www.npmjs.com/package/applicationinsights-cloudflareworkers Maybe it might help 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cloudflare Workers®
Deploy serverless code instantly across the globe to give it exceptional performance, reliability, and scale. ... “Cloudflare Workers has changed the way we...
Read more >
Introducing Cloudflare Workers
A "Cloudflare Service Worker" is specifically a worker which handles HTTP traffic and is written against the Service Worker API. Currently, this ...
Read more >
Cloudflare Workers
Transform requests on the edge using JavaScript. Learn more about Cloudflare Workers. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11....
Read more >
workers.cloudflare.com - GitHub
The Cloudflare Workers website. Contribute to cloudflare/workers.cloudflare.com development by creating an account on GitHub.
Read more >
Cloudflare Workers - Optimizely
Cloudflare Workers provides a serverless execution environment that allows you to create new applications or augment existing ones without configuring or ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found