Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
buildbreaker opened this issue Sep 5, 2024 · 2 comments
Labels

Comments

@buildbreaker
Copy link

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:
#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");
	},
}
  • version for this project in use.
    1.27 (but it looks like any version that is 1.26 or later)

  • services, libraries, languages and tools list and versions.
    package.json:

{
	"name": "cloudflare-logger",
	"version": "0.0.0",
	"private": true,
	"dependencies": {
		"@cloudflare/workers-types": "^4.20240405.0",
		"@datadog/datadog-api-client": "1.27.0",
		"cloudflare": "^3.0.0",
		"tough-cookie": "^4.1.3",
		"zod": "^3.22.4"
	},
	"devDependencies": {
		"@babel/runtime": "^7.24.4",
		"@testing-library/jest-dom": "^6.4.2",
		"@types/jest": "^29.5.12",
		"@typescript-eslint/eslint-plugin": "^6.21.0",
		"eslint": "^8.57.0",
		"eslint-config-prettier": "^9.1.0",
		"eslint-config-standard-with-typescript": "^43.0.1",
		"eslint-plugin-import": "^2.29.1",
		"eslint-plugin-jest": "^27.9.0",
		"eslint-plugin-n": "^16.6.2",
		"eslint-plugin-prettier": "^5.1.3",
		"eslint-plugin-promise": "^6.1.1",
		"jest": "^29.7.0",
		"jest-axe": "^8.0.0",
		"jest-environment-jsdom": "^29.7.0",
		"jest-esm-transformer-2": "^1.0.0",
		"npm": "^10.5.2",
		"prettier": "^3.2.5",
		"ts-jest": "^29.1.2",
		"typescript": "^5.4.5",
		"wrangler": "^3.50.0"
	}
}

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
Copy link

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
Copy link

smartmike commented Oct 18, 2024

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)

...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants