DataDog / datadog-api-client-typescript

Typescript client for the Datadog API
https://www.npmjs.com/package/@datadog/datadog-api-client
Apache License 2.0
82 stars 15 forks source link

datadog api client causing some issues within CF workers for versions 1.26+ #1801

Open buildbreaker opened 2 months ago

buildbreaker commented 2 months ago

Describe the bug I'm currently using the datadog api client within my cloudflare worker and I recently updated the client from version 1.24 to 1.27. I ran into this error since the update:

"TypeError: Illegal invocation: function called with incorrect `this` reference. See https://developers.cloudflare.com/workers/observability/errors/#illegal-invocation-errors for details."

I took a look at the changes across the different versions and found this PR: https://github.com/DataDog/datadog-api-client-typescript/pull/1646

It looks like that might be that change that caused the error I started observing. I was able to resolve my problem by downgrading back to 1.24.

To Reproduce Steps to reproduce the behavior: Try calling a datadog api client within a CF worker context. It's tail logs describe the error I shared. (I can provide a repo which can be used to test if that's desired)

Expected behavior I would like to expect that the latest datadog client continues to send requests in my cloudflare worker.

Screenshots n/a

Environment and Versions (please complete the following information): A clear and precise description of your setup:

handler: ExportedHandler = {
    fetch(request) {
        const logItems: HTTPLogItem[] = new Array<HTTPLogItem>();
        const conf = client.createConfiguration({
            authMethods: {
                apiKeyAuth: env?.["DD_API_KEY_SECRET"],
                appKeyAuth: env?.["DD_APP_KEY_SECRET"],
            },
        });
        const logsApi = new v2.LogsApi(conf);
        const params: v2.LogsApiSubmitLogRequest = {
            ddtags: "env:" + env?.["APP_ENV"],
            body: logItems,
        };
// This line throws
// vvvvvvvvvvvvvvvvvvvvv
        await logsApi.submitLog(params).catch((e) => {
// This log is recorded 100% of the time
            console.error("Error submitting logs to datadog", e);
        });
// ^^^^^^^^^^^^^^^^^^
        return new Response("OK");
    },
}

Additional context I am guessing this has to do with the CF runtime releasing the this reference in the PR when the client attempts to make the fetch request call.

cc @linnea-moment

matheusvellone commented 2 months ago

I'm using dd-api-client on frontend with a proxy to my backend to proxy the requests.

When updating to 1.26.0 I started receiving this error on the frontend

An error occurred during the HTTP request: TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation

I feel like this error is related to the one reported on this issue

smartmike commented 1 month ago

I have been running into the same issues on CF pages functions. I passed in a custom http client (fetch as an option didn't work) with .bind as per the help page, and it seems to be working.

// I'm not sure if this is the best way to import this...
import { IsomorphicFetchHttpLibrary } from '@datadog/datadog-api-client/dist/packages/datadog-api-client-common'
import { client } from '@datadog/datadog-api-client'

...

const httpApi = new IsomorphicFetchHttpLibrary({ fetch: fetch.bind(this) })
const config = client.createConfiguration({ httpApi })
const apiInstance = new v2.LogsApi(config)

...