Skip to content

Commit cfaedcd

Browse files
authored
Optionally enable telemetry collection during Genkit init (#8530)
1 parent fee22f8 commit cfaedcd

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/init/features/genkit/index.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,23 @@ export async function genkitSetup(
382382
default: true,
383383
}))
384384
) {
385-
generateSampleFile(modelOptions[model].plugin, plugins, projectDir, genkitInfo.templateVersion);
385+
logger.info(
386+
"Telemetry data can be used to monitor and gain insights into your AI features. There may be a cost associated with using this feature. See https://firebase.google.com/docs/genkit/observability/telemetry-collection.",
387+
);
388+
const enableTelemetry =
389+
options.nonInteractive ||
390+
(await confirm({
391+
message: "Would like you to enable telemetry collection?",
392+
default: true,
393+
}));
394+
395+
generateSampleFile(
396+
modelOptions[model].plugin,
397+
plugins,
398+
projectDir,
399+
genkitInfo.templateVersion,
400+
enableTelemetry,
401+
);
386402
}
387403
}
388404

@@ -507,6 +523,7 @@ function generateSampleFile(
507523
configPlugins: string[],
508524
projectDir: string,
509525
templateVersion: string,
526+
enableTelemetry: boolean,
510527
): void {
511528
let modelImport = "";
512529
if (modelPlugin && pluginToInfo[modelPlugin].model) {
@@ -534,6 +551,7 @@ function generateSampleFile(
534551
? pluginToInfo[modelPlugin].model || pluginToInfo[modelPlugin].modelStr || ""
535552
: "'' /* TODO: Set a model. */",
536553
),
554+
enableTelemetry,
537555
);
538556
logLabeledBullet("genkit", "Generating sample file");
539557
try {
@@ -622,7 +640,7 @@ async function updatePackageJson(nonInteractive: boolean, projectDir: string): P
622640
}
623641
}
624642

625-
function renderConfig(pluginNames: string[], template: string): string {
643+
function renderConfig(pluginNames: string[], template: string, enableTelemetry: boolean): string {
626644
const imports = pluginNames
627645
.map((pluginName) => generateImportStatement(pluginToInfo[pluginName].imports, pluginName))
628646
.join("\n");
@@ -631,7 +649,8 @@ function renderConfig(pluginNames: string[], template: string): string {
631649
" /* Add your plugins here. */";
632650
return template
633651
.replace("$GENKIT_CONFIG_IMPORTS", imports)
634-
.replace("$GENKIT_CONFIG_PLUGINS", plugins);
652+
.replace("$GENKIT_CONFIG_PLUGINS", plugins)
653+
.replaceAll("$TELEMETRY_COMMENT", enableTelemetry ? "" : "// ");
635654
}
636655

637656
function generateImportStatement(imports: string, name: string): string {

templates/genkit/firebase.1.0.0.template

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ import { onCallGenkit, hasClaim } from "firebase-functions/https";
1515
import { defineSecret } from "firebase-functions/params";
1616
const apiKey = defineSecret("GOOGLE_GENAI_API_KEY");
1717

18+
// The Firebase telemetry plugin exports a combination of metrics, traces, and logs to Google Cloud
19+
// Observability. See https://firebase.google.com/docs/genkit/observability/telemetry-collection.
20+
$TELEMETRY_COMMENTimport {enableFirebaseTelemetry} from "@genkit-ai/firebase";
21+
$TELEMETRY_COMMENTenableFirebaseTelemetry();
22+
1823
const ai = genkit({
1924
plugins: [
2025
$GENKIT_CONFIG_PLUGINS

0 commit comments

Comments
 (0)