Cloudflare Workers
See original GitHub issueIs 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
.
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:
- Created 3 years ago
- Reactions:3
- Comments:11 (5 by maintainers)
Top GitHub Comments
@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.
@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 👍