Skip to content

feat(js): add open telementry fetch provider#4820

Draft
Ehesp wants to merge 1 commit intofirebase:mainfrom
Ehesp:fetch-opentel
Draft

feat(js): add open telementry fetch provider#4820
Ehesp wants to merge 1 commit intofirebase:mainfrom
Ehesp:fetch-opentel

Conversation

@Ehesp
Copy link
Member

@Ehesp Ehesp commented Feb 25, 2026

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:

  • No or limited Node APIs the SDK relies on
  • Different process/lifecycle (no long-lived Node process, no process.on('SIGTERM'), etc.)
  • Bundling or security constraints (no async_hooks, different globals, etc.)
  • So today, Genkit tracing doesn’t work in those environments.

This PR introduces a new FetchTelemetryProvider which uses @opentelemetry/api to build the traces, and pass them onto the TraceServerExporter (which uses fetch so is compatible already). Additionally, if the environment supports AsyncLocalStorage (such as CF Workers) then it'll also use that to provide structured traces.

If AsyncLocalStorage is 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:

import { setGenkitRuntimeConfig } from 'genkit';
import { FetchTelemetryProvider } from 'genkit/tracing';

setGenkitRuntimeConfig({
  telemetry: new FetchTelemetryProvider({
    serverUrl: 'https://your-telemetry-server.example.com', // optional
    realtime: true, // optional
  }),
});

By default, FetchTelemetryProvider uses the environment, or users can specify a custom URL (or the dev ui overrides when it starts). The realtime flag (defaults to true) pipes spans across as they come in vs buffered.

Checklist (if applicable):

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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 FetchTelemetryProvider to address the limitations of the default OpenTelemetry setup in worker/edge runtimes. It enables Genkit tracing in environments where the Node.js SDK is not fully supported, such as Cloudflare Workers and Vercel Edge. The new provider uses fetch for exporting traces and supports AsyncLocalStorage for enhanced trace structure when available.

Highlights

  • New Telemetry Provider: Introduces FetchTelemetryProvider for environments lacking full Node.js SDK support, enabling tracing in worker/edge runtimes.
  • AsyncLocalStorage Support: Leverages AsyncLocalStorage for structured traces in compatible environments like Cloudflare Workers.
  • Configuration: Allows setting a custom telemetry provider via setGenkitRuntimeConfig, offering flexibility in telemetry setup.

🧠 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
  • js/core/src/config.ts
    • Added telemetry option to GenkitRuntimeConfig to allow setting a custom telemetry provider.
  • js/core/src/node.ts
    • Modified initNodeFeatures to conditionally initialize telemetry based on runtime config.
    • Added ensureTelemetryProvider to lazily initialize the telemetry provider.
  • js/core/src/tracing.ts
    • Exported FetchTelemetryProvider and setTelemetryProviderInitializer to allow setting a custom telemetry provider after importing genkit.
  • js/core/src/tracing/fetch-telemetry-provider.ts
    • Added FetchTelemetryProvider class for fetch-compatible telemetry in worker environments.
  • js/core/tests/action_test.ts
    • Added initNodeTelemetryProvider to initialize telemetry in action tests.
  • js/core/tests/dynamic-action-provider_test.ts
    • Added initNodeTelemetryProvider to initialize telemetry in dynamic action provider tests.
  • js/core/tests/fetch_telemetry_provider_test.ts
    • Added tests for the FetchTelemetryProvider to ensure it enables telemetry and exports spans via fetch.
  • js/core/tests/flow_test.ts
    • Added initNodeTelemetryProvider to initialize telemetry in flow tests.
  • js/core/tests/registry_test.ts
    • Added initNodeTelemetryProvider to initialize telemetry in registry tests.
  • js/genkit/src/tracing.ts
    • Exported FetchTelemetryProvider and FetchTelemetryProviderOptions from genkit tracing.
Activity
  • Ehesp authored the pull request.
  • The pull request title follows conventional commits.
  • The changes have been manually tested.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

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.

Comment on lines +97 to +100
if (telemetryProviderInitializer) {
telemetryProviderInitializer();
return global[telemetryProviderKey];
}
Copy link
Contributor

Choose a reason for hiding this comment

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

high

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];
    }
  }

Comment on lines +89 to +94
const serverUrl =
this.options.serverUrl ??
(typeof process !== 'undefined' && process.env?.GENKIT_TELEMETRY_SERVER);
if (typeof serverUrl === 'string') {
setTelemetryServerUrl(serverUrl);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

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.

Suggested change
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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant