Get started with Genkit Monitoring
This quickstart guide describes how to set up Genkit Monitoring foryour deployed Genkit features, so that you can collect and view real-timetelemetry data. With Genkit Monitoring, you get visibility into howyour Genkit features are performing in production.
Key capabilities of Genkit Monitoring include:
- Viewing quantitative metrics like Genkit feature latency, errors, andtoken usage.
- Inspecting traces to see your Genkit’s feature steps, inputs, and outputs,to help with debugging and quality improvement.
- Exporting production traces to run evals within Genkit.
Setting up Genkit Monitoring requires completing tasks in both your codebaseand on the Google Cloud Console.
Before you begin
Section titled “Before you begin”If you haven’t already, create a Firebase project.
In theFirebase console, clickAdd a project, then follow the on-screen instructions. You cancreate a new project or add Firebase services to an already-existing Google Cloud project.
Ensure your project is on theBlaze pricing plan.
Genkit Monitoring relies on telemetry data written to Google CloudLogging, Metrics, and Trace, which are paid services. View theGoogle Cloud Observability pricing page for pricing details and to learn about free-of-charge tier limits.
Write a Genkit feature by following theGet Started Guide, and prepare your code for deployment by using one of the following guides:
a.Deploy flows using Cloud Functions for Firebaseb.Deploy flows using Cloud Runc.Deploy flows to any Node.js platform
Step 1. Add the Firebase plugin
Section titled “Step 1. Add the Firebase plugin”Install the@genkit-ai/firebase
plugin in your project:
npminstall@genkit-ai/firebase
Environment-based configuration
Section titled “Environment-based configuration”If you intend to use the default configuration for Firebase GenkitMonitoring, you can enable telemetry by setting theENABLE_FIREBASE_MONITORING
environment variable in your deploymentenvironment.
export ENABLE_FIREBASE_MONITORING=true
This will use default configuration values. Tooverride configuration options, use “Programmatic configuration”.
Programmatic configuration
Section titled “Programmatic configuration”You can also enable Firebase Genkit Monitoring in code. This is usefulif you want to tweak any configuration settings like the metric exportinterval or to set up your local environment to export telemetry data.
ImportenableFirebaseTelemetry
into your Genkit configuration file (thefile wheregenkit(...)
is initalized), and call it:
import { enableFirebaseTelemetry }from'@genkit-ai/firebase';enableFirebaseTelemetry();
You can also enable Firebase Genkit Monitoring in code. This is usefulif you want to tweak any configuration settings like the metric exportinterval or to set up your local environment to export telemetry data.
packagemainimport ("context""github.com/firebase/genkit/go/genkit""github.com/firebase/genkit/go/plugins/firebase")funcmain() {ctx:= context.Background()firebase.EnableFirebaseTelemetry(nil)g:= genkit.Init(ctx,nil)}
Step 2. Enable the required APIs
Section titled “Step 2. Enable the required APIs”Make sure that the following APIs are enabled for your Google Cloud project:
These APIs should be listed in theAPI dashboard for yourproject.
Step 3. Set up permissions
Section titled “Step 3. Set up permissions”The Firebase plugin needs to use aservice account to authenticate withGoogle Cloud Logging, Metrics, and Trace services.
Grant the following roles to whichever service account is configured to run your code within theGoogle Cloud IAM Console. For Cloud Functions for Firebase and Cloud Run, that’s typically the default compute service account.
- Monitoring Metric Writer (
roles/monitoring.metricWriter
) - Cloud Trace Agent (
roles/cloudtrace.agent
) - Logs Writer (
roles/logging.logWriter
)
Step 4. (Optional) Test your configuration locally
Section titled “Step 4. (Optional) Test your configuration locally”Before deploying, you can run your Genkit code locally to confirm thattelemetry data is being collected, and is viewable in the Genkit Monitoringdashboard.
- In your Genkit code, set
forceDevExport
totrue
to send telemetry fromyour local environment.
- In your Genkit code, set
ForceDevExport
totrue
to send telemetry fromyour local environment.
Use your service account to authenticate and test your configuration.
In order to impersonate the service account, you will need to havethe
roles/iam.serviceAccountTokenCreator
IAM role applied to youruser account.With theGoogle Cloud CLI tool,authenticate using the service account:
Terminal window gcloudauthapplication-defaultlogin--impersonate-service-accountSERVICE_ACCT_EMAILRun and invoke your Genkit feature, and then view metrics on theGenkit Monitoring dashboard.Allow for up to 5 minutes to collect the first metric. You can reduce thisdelay by setting
metricExportIntervalMillis
in the telemetry configuration.If metrics are not appearing in the Genkit Monitoring dashboard, view theTroubleshooting guide for stepsto debug.
Step 5. Re-build and deploy code
Section titled “Step 5. Re-build and deploy code”Re-build, deploy, and invoke your Genkit feature to start collecting data.After Genkit Monitoring receives your metrics, you can view them byvisiting theGenkit Monitoring dashboard