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

feat: start adding support to cf pages #129

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

vanstinator
Copy link

Fixes #96

What this PR solves / how to test:

This PR adds support for automatically instrumenting Cloudflare Pages. The code is very similar to the fetch code. I'd initially intended to combine the similar bits into a shared core, but as I started to get into those weeds I realized I didn't have a ton of context on the full set of differences between Pages and Workers, and it started to seem like keeping the code paths separate would allow for a more organic growth on the Pages instrumentation.

To test this code yourself it's as simple as wrapping a Cloudflare Pages handler in instrumentPage and passing it a valid TraceConfig.


type PageHandlerArgs = Parameters<PagesFunction>

let cold_start = true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make this a bit longer to help ensure it doesn't ever conflict with user code (generally, global vars should be avoided in Workers/Functions, but this seems ok.)

e.g. let __otel_cf_is_cold_start = true

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should actually be fine, as it's not exported, so user code can never find it as cold_start.

Comment on lines +21 to +23
['faas.trigger']: 'http',
['faas.coldstart']: cold_start,
['faas.invocation_id']: request.request.headers.get('cf-ray') ?? undefined,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use semantic attributes import to remain consistent with the rest of the library? Once we update to the latest api, we will be able to tree-shake it to just these three keys being imported.

}
cold_start = false
Object.assign(attributes, gatherRequestAttributes(request.request))
Object.assign(attributes, gatherIncomingCfAttributes(request.request))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also assign version metadata, like in

Object.assign(attributes, versionAttributes(env))

I know that version metadata bindings can't be added to Pages projects (looking at you @jahands), and script versions work very differently, but hopefully this will change as they converge more.

}

const promise = tracer.startActiveSpan(
`${request.request.method} ${request.functionPath}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We recently introduced a new convention for instrumented spans, could you prefix this and the two updateNames to be prefixed with fetchHandler? Again, we could call them functionHandler, but with convergence a consistent name between Workers and Pages will make more sense in my opinion.

@shmuli9
Copy link

shmuli9 commented Sep 29, 2024

Hey this would be really useful for a project I am working on. Is this usable as is? Are there plans to merge this?

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

Successfully merging this pull request may close these issues.

[feature request]: Support for Cloudflare Pages
4 participants