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

fix: allow passing promise of TelemetryConfig to enableTelemetry #1169

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions js/core/src/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,27 @@ let nodeOtelConfig: TelemetryConfig | null = null;

const instrumentationKey = '__GENKIT_TELEMETRY_INSTRUMENTED';

export function ensureBasicTelemetryInstrumentation() {
export async function ensureBasicTelemetryInstrumentation() {
if (global[instrumentationKey]) {
return;
return await global[instrumentationKey];
}
enableTelemetry({});
global[instrumentationKey] = true;
await enableTelemetry({});
}

/**
* Enables tracing and metrics open telemetry configuration.
*/
export function enableTelemetry(telemetryConfig: TelemetryConfig) {
export async function enableTelemetry(
telemetryConfig: TelemetryConfig | Promise<TelemetryConfig>
) {
global[instrumentationKey] =
telemetryConfig instanceof Promise ? telemetryConfig : Promise.resolve();

telemetryConfig =
telemetryConfig instanceof Promise
? await telemetryConfig
: telemetryConfig;

nodeOtelConfig = telemetryConfig || {};

const processors: SpanProcessor[] = [createTelemetryServerProcessor()];
Expand Down
4 changes: 2 additions & 2 deletions js/core/src/tracing/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function newTrace<T>(
},
fn: (metadata: SpanMetadata, rootSpan: ApiSpan) => Promise<T>
) {
ensureBasicTelemetryInstrumentation();
await ensureBasicTelemetryInstrumentation();
const traceMetadata: TraceMetadata = traceMetadataAls.getStore() || {
paths: new Set<PathMetadata>(),
timestamp: performance.now(),
Expand Down Expand Up @@ -78,7 +78,7 @@ export async function runInNewSpan<T>(
},
fn: (metadata: SpanMetadata, otSpan: ApiSpan, isRoot: boolean) => Promise<T>
): Promise<T> {
ensureBasicTelemetryInstrumentation();
await ensureBasicTelemetryInstrumentation();

const tracer = trace.getTracer(TRACER_NAME, TRACER_VERSION);
const parentStep = spanMetadataAls.getStore();
Expand Down
12 changes: 7 additions & 5 deletions js/plugins/google-cloud/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ import { GcpOpenTelemetry } from './gcpOpenTelemetry.js';
import { TelemetryConfigs } from './telemetry/defaults.js';
import { GcpTelemetryConfig, GcpTelemetryConfigOptions } from './types.js';

export async function enableGoogleCloudTelemetry(
export function enableGoogleCloudTelemetry(
options?: GcpTelemetryConfigOptions
) {
const pluginConfig = await configureGcpPlugin(options);

enableTelemetry(await new GcpOpenTelemetry(pluginConfig).getConfig());
logger.init(await new GcpLogger(pluginConfig).getLogger(getCurrentEnv()));
return enableTelemetry(
configureGcpPlugin(options).then(async (pluginConfig) => {
logger.init(await new GcpLogger(pluginConfig).getLogger(getCurrentEnv()));
return new GcpOpenTelemetry(pluginConfig).getConfig();
})
);
}

/**
Expand Down
Loading