-
Notifications
You must be signed in to change notification settings - Fork 681
feat(js): add open telementry fetch provider #4820
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
Open
Ehesp
wants to merge
8
commits into
firebase:main
Choose a base branch
from
Ehesp:fetch-opentel
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+14,162
−108
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0790c97
feat(js): add open telementry fetch provider
Ehesp 66feed5
fix: default realtime flag to true
Ehesp f56ffaa
feat: support custom headers
Ehesp 99bdee8
chore: add cf-worker testapp
Ehesp a2bac8f
chore: cleanup test app
Ehesp 9f7211b
chore: formatting
Ehesp f738292
fix: improve code readability
Ehesp 71a401b
fix: handle failed telemetryProviderInitializer calls
Ehesp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| /** | ||
| * Copyright 2024 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { context, trace } from '@opentelemetry/api'; | ||
| import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks'; | ||
| import { | ||
| BasicTracerProvider, | ||
| BatchSpanProcessor, | ||
| SimpleSpanProcessor, | ||
| type SpanProcessor, | ||
| } from '@opentelemetry/sdk-trace-base'; | ||
| import type { TelemetryConfig } from '../telemetryTypes.js'; | ||
| import type { TelemetryProvider } from '../tracing.js'; | ||
| import { TraceServerExporter, setTelemetryServerConfig } from './exporter.js'; | ||
|
|
||
| /** | ||
| * Options for the fetch-compatible telemetry provider. | ||
| * Uses only fetch and standard APIs (no Node.js SDK), suitable for workers. | ||
| */ | ||
| export interface FetchTelemetryProviderOptions { | ||
| /** | ||
| * URL of the Genkit telemetry server (e.g. from dev UI or your own collector). | ||
| * If not set, GENKIT_TELEMETRY_SERVER env var is used when enableTelemetry runs. | ||
| */ | ||
| serverUrl?: string; | ||
| /** | ||
| * If true, use SimpleSpanProcessor for more real-time export (e.g. in dev). | ||
| * Default: true. Set to false for BatchSpanProcessor (production). | ||
| */ | ||
| realtime?: boolean; | ||
| /** | ||
| * Optional headers to send with each trace export request (e.g. Authorization). | ||
| */ | ||
| headers?: Record<string, string>; | ||
| } | ||
|
|
||
| /** | ||
| * Telemetry provider that uses only fetch-compatible APIs. | ||
| * Use this in worker environments (Cloudflare Workers, Vercel Edge, etc.) where | ||
| * the Node.js OpenTelemetry SDK is not available. | ||
| * | ||
| * When the runtime has AsyncLocalStorage (e.g. Cloudflare Workers with `nodejs_compat`), | ||
| * OTel context propagation is enabled so parent-child span links are preserved across | ||
| * async boundaries and traces are fully hierarchical. | ||
| * | ||
| * Set via runtime config before any other genkit imports: | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * import { setGenkitRuntimeConfig } from 'genkit'; | ||
| * import { FetchTelemetryProvider } from 'genkit/tracing'; | ||
| * | ||
| * setGenkitRuntimeConfig({ | ||
| * jsonSchemaMode: 'interpret', | ||
| * sandboxedRuntime: true, | ||
| * telemetry: new FetchTelemetryProvider({ | ||
| * serverUrl: 'https://your-telemetry-server.example.com', | ||
| * }), | ||
| * }); | ||
| * | ||
| * // Then import flows/actions | ||
| * import { flow, run } from 'genkit'; | ||
| * ``` | ||
| */ | ||
| export class FetchTelemetryProvider implements TelemetryProvider { | ||
| private readonly options: FetchTelemetryProviderOptions; | ||
| private spanProcessors: SpanProcessor[] = []; | ||
|
|
||
| constructor(options: FetchTelemetryProviderOptions = {}) { | ||
| this.options = options; | ||
| } | ||
|
|
||
| async enableTelemetry( | ||
| telemetryConfig: TelemetryConfig | Promise<TelemetryConfig> | ||
| ): Promise<void> { | ||
| const config = | ||
| telemetryConfig instanceof Promise | ||
| ? await telemetryConfig | ||
| : telemetryConfig; | ||
| const serverUrl = | ||
| this.options.serverUrl ?? | ||
| (typeof process !== 'undefined' | ||
| ? process.env?.GENKIT_TELEMETRY_SERVER | ||
| : undefined); | ||
| setTelemetryServerConfig({ | ||
| ...(serverUrl && { url: serverUrl }), | ||
| ...(this.options.headers && | ||
| Object.keys(this.options.headers).length > 0 && { | ||
| headers: this.options.headers, | ||
| }), | ||
| }); | ||
|
|
||
| const exporter = new TraceServerExporter(); | ||
| const processor: SpanProcessor = | ||
| this.options.realtime !== false | ||
| ? new SimpleSpanProcessor(exporter) | ||
| : new BatchSpanProcessor(exporter); | ||
|
|
||
| this.spanProcessors = [processor]; | ||
|
|
||
| if (config?.spanProcessors?.length) { | ||
| this.spanProcessors.push(...config.spanProcessors); | ||
| } | ||
|
|
||
| // When AsyncLocalStorage is available (e.g. Node, Cloudflare Workers with nodejs_compat), | ||
| // enable OTel context propagation so parent-child span links work across await. | ||
| try { | ||
| const contextManager = new AsyncLocalStorageContextManager(); | ||
| contextManager.enable(); | ||
| context.setGlobalContextManager(contextManager); | ||
| } catch { | ||
| // No async_hooks / AsyncLocalStorage (e.g. some edge runtimes); spans still work, hierarchy may be flat. | ||
| } | ||
|
|
||
| const provider = new BasicTracerProvider(); | ||
| for (const p of this.spanProcessors) { | ||
| provider.addSpanProcessor(p); | ||
| } | ||
| trace.setGlobalTracerProvider(provider); | ||
| } | ||
|
|
||
| async flushTracing(): Promise<void> { | ||
| await Promise.all(this.spanProcessors.map((p) => p.forceFlush())); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.