feat(js): add open telementry fetch provider#4820
feat(js): add open telementry fetch provider#4820Ehesp wants to merge 1 commit intofirebase:mainfrom
Conversation
Summary of ChangesHello @Ehesp, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a new Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a FetchTelemetryProvider to enable Genkit tracing in non-Node.js environments like Cloudflare Workers, which is a great enhancement. The implementation uses a lazy-initialization pattern for the telemetry provider, allowing for runtime configuration, which is well-designed. The code is generally of high quality, with new tests for the provider. I've identified a couple of areas for improvement: one to enhance code readability and another to improve the robustness of the telemetry provider initialization logic to prevent potential runtime errors. Overall, this is a solid contribution.
| if (telemetryProviderInitializer) { | ||
| telemetryProviderInitializer(); | ||
| return global[telemetryProviderKey]; | ||
| } |
There was a problem hiding this comment.
There's a potential robustness issue here. If telemetryProviderInitializer() is called but fails to set global[telemetryProviderKey] (e.g., due to an internal error), this function will return undefined, likely causing a TypeError in the calling code. It's safer to re-check for the provider after the initializer runs and fall through to the error if it's still not present.
if (telemetryProviderInitializer) {
telemetryProviderInitializer();
// After the initializer runs, check again for the provider.
if (global[telemetryProviderKey]) {
return global[telemetryProviderKey];
}
}| const serverUrl = | ||
| this.options.serverUrl ?? | ||
| (typeof process !== 'undefined' && process.env?.GENKIT_TELEMETRY_SERVER); | ||
| if (typeof serverUrl === 'string') { | ||
| setTelemetryServerUrl(serverUrl); | ||
| } |
There was a problem hiding this comment.
The logic to determine serverUrl is a bit subtle because the && operator can result in serverUrl being a boolean (false) if process is not defined. While the typeof serverUrl === 'string' check handles this correctly, the code could be made more explicit and readable by avoiding mixed types for the serverUrl variable.
| const serverUrl = | |
| this.options.serverUrl ?? | |
| (typeof process !== 'undefined' && process.env?.GENKIT_TELEMETRY_SERVER); | |
| if (typeof serverUrl === 'string') { | |
| setTelemetryServerUrl(serverUrl); | |
| } | |
| const serverUrl = | |
| this.options.serverUrl ?? | |
| (typeof process !== 'undefined' ? process.env?.GENKIT_TELEMETRY_SERVER : undefined); | |
| if (serverUrl) { | |
| setTelemetryServerUrl(serverUrl); | |
| } |
Genkit’s default tracing uses
@opentelemetry/sdk-node, which assumes a Node.js environment. In worker/edge runtimes (e.g. Cloudflare Workers, Vercel Edge, or other environments that don’t support the full Node SDK), that setup fails or isn’t available because of:This PR introduces a new
FetchTelemetryProviderwhich uses@opentelemetry/apito build the traces, and pass them onto theTraceServerExporter(which usesfetchso is compatible already). Additionally, if the environment supportsAsyncLocalStorage(such as CF Workers) then it'll also use that to provide structured traces.If
AsyncLocalStorageis not available, it will simply provide high level traces (which currently, the DevUI doesn't support but that's fine, I think).Implementation wise, this is fairly opinionated and not sure if it's the correct place, however with this PR you can do:
By default,
FetchTelemetryProvideruses the environment, or users can specify a custom URL (or the dev ui overrides when it starts). Therealtimeflag (defaults to true) pipes spans across as they come in vs buffered.Checklist (if applicable):