Skip to content

Commit

Permalink
Add initial tests for open telemetry metrics exported via the Google …
Browse files Browse the repository at this point in the history
…Cloud plugin (#288)
  • Loading branch information
bryanatkinson authored and randall77 committed Jun 10, 2024
1 parent 63c6074 commit 009b6b8
Show file tree
Hide file tree
Showing 7 changed files with 400 additions and 13 deletions.
3 changes: 1 addition & 2 deletions js/ai/src/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,7 @@ function doRecordGenerateActionMetrics(

generateActionCounter.add(1, {
maxOutputTokens: dimensions.maxOutputTokens,
errorCode: dimensions.err?.code,
errorMessage: dimensions.err?.message,
error: dimensions.err?.name,
...shared,
});

Expand Down
3 changes: 1 addition & 2 deletions js/core/src/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ export function writeActionFailure(
) {
const dimensions = {
name: actionName,
errorCode: err?.code,
errorMessage: err?.message,
source: 'ts',
sourceVersion: GENKIT_VERSION,
error: err?.name,
};
actionCounter.add(1, dimensions);
actionLatencies.record(latencyMs, dimensions);
Expand Down
3 changes: 1 addition & 2 deletions js/flow/src/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,9 @@ export function writeFlowFailure(
) {
const dimensions = {
name: flowName,
errorCode: err?.code,
errorMessage: err?.message,
source: 'ts',
sourceVersion: GENKIT_VERSION,
error: err.name,
};
flowCounter.add(1, dimensions);
flowLatencies.record(latencyMs, dimensions);
Expand Down
3 changes: 2 additions & 1 deletion js/plugins/google-cloud/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"compile": "tsup-node",
"build:clean": "rm -rf ./lib",
"build": "npm-run-all build:clean check compile",
"build:watch": "tsup-node --watch"
"build:watch": "tsup-node --watch",
"test": "tsx --test ./tests/*_test.ts"
},
"repository": {
"type": "git",
Expand Down
22 changes: 16 additions & 6 deletions js/plugins/google-cloud/src/gcpOpenTelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import { Resource } from '@opentelemetry/resources';
import {
AggregationTemporality,
InMemoryMetricExporter,
MetricReader,
PeriodicExportingMetricReader,
PushMetricExporter,
} from '@opentelemetry/sdk-metrics';
Expand All @@ -42,6 +41,9 @@ import {
} from '@opentelemetry/sdk-trace-base';
import { PluginOptions } from './index.js';

let metricExporter: PushMetricExporter;
let metricReader: PeriodicExportingMetricReader;

/**
* Provides a {TelemetryConfig} for exporting OpenTelemetry data (Traces,
* Metrics, and Logs) to the Google Cloud Operations Suite.
Expand Down Expand Up @@ -128,27 +130,31 @@ export class GcpOpenTelemetry implements TelemetryConfig {
const exporter: SpanExporter = this.shouldExport()
? new this.AdjustingTraceExporter()
: new InMemorySpanExporter();
metricReader = this.createMetricReader();
return {
resource: this.resource,
spanProcessor: new BatchSpanProcessor(exporter),
sampler: this.options?.telemetryConfig?.sampler || new AlwaysOnSampler(),
instrumentations: this.getInstrumentations(),
metricReader: this.createMetricReader(),
metricReader: metricReader,
};
}

/**
* Creates a {MetricReader} for pushing metrics out to GCP via OpenTelemetry.
*/
private createMetricReader(): MetricReader {
const exporter: PushMetricExporter = this.shouldExport()
private createMetricReader(): PeriodicExportingMetricReader {
const shouldExport = this.shouldExport();
metricExporter = this.shouldExport()
? new MetricExporter({ projectId: this.options.projectId })
: new InMemoryMetricExporter(AggregationTemporality.CUMULATIVE);
return new PeriodicExportingMetricReader({
exportIntervalMillis:
this.options?.telemetryConfig?.metricExportIntervalMillis || 10_000,
exporter,
}) as MetricReader;
exportTimeoutMillis:
this.options?.telemetryConfig?.metricExportTimeoutMillis || 10_000,
exporter: metricExporter,
});
}

/** Gets all open telemetry instrumentations as configured by the plugin. */
Expand All @@ -173,3 +179,7 @@ export class GcpOpenTelemetry implements TelemetryConfig {
];
}
}

export function __getMetricExporterForTesting(): InMemoryMetricExporter {
return metricExporter as InMemoryMetricExporter;
}
2 changes: 2 additions & 0 deletions js/plugins/google-cloud/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface TelemetryConfig {
autoInstrumentation?: boolean;
autoInstrumentationConfig?: InstrumentationConfigMap;
metricExportIntervalMillis?: number;
metricExportTimeoutMillis?: number;
instrumentations?: Instrumentation[];
}

Expand Down Expand Up @@ -65,3 +66,4 @@ export const googleCloud: Plugin<[PluginOptions] | []> = genkitPlugin(
);

export default googleCloud;
export * from './gcpOpenTelemetry.js';
Loading

0 comments on commit 009b6b8

Please sign in to comment.