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

Force flush of metrics after local run #567

Merged
merged 2 commits into from
Jul 10, 2024
Merged
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
27 changes: 22 additions & 5 deletions js/core/src/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { NodeSDK } from '@opentelemetry/sdk-node';
import { NodeSDKConfiguration } from '@opentelemetry/sdk-node/build/src/types';
import {
BatchSpanProcessor,
SimpleSpanProcessor,
Expand All @@ -35,6 +36,7 @@ export * from './tracing/types.js';

const processors: SpanProcessor[] = [];
let telemetrySDK: NodeSDK | null = null;
let nodeOtelConfig: Partial<NodeSDKConfiguration> | null = null;

/**
* Enables trace spans to be written to the trace store.
Expand All @@ -55,7 +57,7 @@ export function enableTracingAndMetrics(
);
}

const nodeOtelConfig = telemetryConfig.getConfig() || {};
nodeOtelConfig = telemetryConfig.getConfig() || {};

addProcessor(nodeOtelConfig.spanProcessor);
nodeOtelConfig.spanProcessor = new MultiSpanProcessor(processors);
Expand All @@ -67,10 +69,17 @@ export function enableTracingAndMetrics(
export async function cleanUpTracing(): Promise<void> {
return new Promise((resolve) => {
if (telemetrySDK) {
return telemetrySDK.shutdown().then(() => {
logger.debug('OpenTelemetry SDK shut down.');
telemetrySDK = null;
resolve();
// Metrics are not flushed as part of the shutdown operation. If metrics
// are enabled, we need to manually flush them *before* the reader
// receives shutdown order.
const metricFlush = maybeFlushMetrics();

return metricFlush.then(() => {
return telemetrySDK!.shutdown().then(() => {
logger.debug('OpenTelemetry SDK shut down.');
telemetrySDK = null;
resolve();
});
});
} else {
resolve();
Expand Down Expand Up @@ -98,6 +107,14 @@ function addProcessor(processor: SpanProcessor | undefined) {
if (processor) processors.push(processor);
}

/** Flush metrics if in dev mode. */
function maybeFlushMetrics(): Promise<void> {
if (process.env.GENKIT_ENV === 'dev' && nodeOtelConfig?.metricReader) {
return nodeOtelConfig.metricReader.forceFlush();
}
return Promise.resolve();
}

/**
* Flushes all configured span processors
*/
Expand Down
9 changes: 5 additions & 4 deletions js/testapps/flow-simple-ai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,20 @@ configureGenkit({
googleAI(),
vertexAI(),
googleCloud({
// Forces telemetry export in 'dev'
forceDevExport: true,
// These are configured for demonstration purposes. Sensible defaults are
// in place in the event that telemetryConfig is absent.
telemetryConfig: {
// Forces telemetry export in 'dev'
forceDevExport: true,
sampler: new AlwaysOnSampler(),
autoInstrumentation: true,
autoInstrumentationConfig: {
'@opentelemetry/instrumentation-fs': { enabled: false },
'@opentelemetry/instrumentation-dns': { enabled: false },
'@opentelemetry/instrumentation-net': { enabled: false },
},
// metricExportIntervalMillis: 5_000,
metricExportIntervalMillis: 5_000,
metricExportTimeoutMillis: 5_000,
},
}),
dotprompt(),
Expand Down Expand Up @@ -289,7 +290,7 @@ export const searchDestinations = defineFlow(
const result = await generate({
model: geminiPro,
prompt: `Give me a list of vacation options based on the provided context. Use only the options provided below, and describe how it fits with my query.

Query: ${input}

Available Options:\n- ${docs.map((d) => `${d.metadata!.name}: ${d.text()}`).join('\n- ')}`,
Expand Down
Loading