Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 21 additions & 11 deletions js/plugins/google-cloud/src/gcpOpenTelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import {
import { PluginOptions } from './index.js';

let metricExporter: PushMetricExporter;
let metricReader: PeriodicExportingMetricReader;

/**
* Provides a {TelemetryConfig} for exporting OpenTelemetry data (Traces,
Expand Down Expand Up @@ -127,25 +126,26 @@ export class GcpOpenTelemetry implements TelemetryConfig {
}

getConfig(): Partial<NodeSDKConfiguration> {
const exporter: SpanExporter = this.shouldExport()
? new this.AdjustingTraceExporter()
: new InMemorySpanExporter();
metricReader = this.createMetricReader();
return {
resource: this.resource,
spanProcessor: new BatchSpanProcessor(exporter),
spanProcessor: new BatchSpanProcessor(this.createSpanExporter()),
sampler: this.options?.telemetryConfig?.sampler || new AlwaysOnSampler(),
instrumentations: this.getInstrumentations(),
metricReader: metricReader,
metricReader: this.createMetricReader(),
};
}

private createSpanExporter(): SpanExporter {
return this.shouldExportTraces()
? new this.AdjustingTraceExporter()
: new InMemorySpanExporter();
}

/**
* Creates a {MetricReader} for pushing metrics out to GCP via OpenTelemetry.
*/
private createMetricReader(): PeriodicExportingMetricReader {
const shouldExport = this.shouldExport();
metricExporter = this.shouldExport()
metricExporter = this.shouldExportMetrics()
? new MetricExporter({ projectId: this.options.projectId })
: new InMemoryMetricExporter(AggregationTemporality.CUMULATIVE);
return new PeriodicExportingMetricReader({
Expand All @@ -167,8 +167,18 @@ export class GcpOpenTelemetry implements TelemetryConfig {
return this.getDefaultLoggingInstrumentations();
}

private shouldExport(): boolean {
return this.options.forceDevExport || process.env.GENKIT_ENV !== 'dev';
private shouldExportTraces(): boolean {
return (
(this.options.forceDevExport || process.env.GENKIT_ENV !== 'dev') &&
!this.options.telemetryConfig?.disableTraces
);
}

private shouldExportMetrics(): boolean {
return (
(this.options.forceDevExport || process.env.GENKIT_ENV !== 'dev') &&
!this.options.telemetryConfig?.disableMetrics
);
}

/** Always configure the Pino and Winston instrumentations */
Expand Down
6 changes: 6 additions & 0 deletions js/plugins/google-cloud/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export interface TelemetryConfig {
metricExportIntervalMillis?: number;
metricExportTimeoutMillis?: number;
instrumentations?: Instrumentation[];

/** When true, metrics are not sent to GCP. */
disableMetrics?: boolean;

/** When true, traces are not sent to GCP. */
disableTraces?: boolean;
}

/**
Expand Down
26 changes: 26 additions & 0 deletions js/plugins/google-cloud/tests/metrics_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import { registerFlowStateStore } from '@genkit-ai/core/registry';
import { defineFlow, runAction, runFlow } from '@genkit-ai/flow';
import {
GcpOpenTelemetry,
__getMetricExporterForTesting,
googleCloud,
} from '@genkit-ai/google-cloud';
Expand Down Expand Up @@ -271,6 +272,31 @@ describe('GoogleCloudMetrics', () => {
assert.ok(requestCounter.attributes.sourceVersion);
});

describe('Configuration', () => {
it('should export only traces', async () => {
const telemetry = new GcpOpenTelemetry({
forceDevExport: true,
telemetryConfig: {
disableMetrics: true,
},
});
assert.equal(telemetry['shouldExportTraces'](), true);
assert.equal(telemetry['shouldExportMetrics'](), false);
});

it('should export only metrics', async () => {
const telemetry = new GcpOpenTelemetry({
forceDevExport: true,
telemetryConfig: {
disableTraces: true,
disableMetrics: false,
},
});
assert.equal(telemetry['shouldExportTraces'](), false);
assert.equal(telemetry['shouldExportMetrics'](), true);
});
});

/** Polls the in memory metric exporter until the genkit scope is found. */
async function getGenkitMetrics(
name: string = 'genkit',
Expand Down