Skip to content

[Tooling] OpenTelemetry/Traces missing Genkit Spans when flow is called from within Firestore onDocumentCreated functions #2460

@jesperstarkar

Description

@jesperstarkar

Describe the bug
In a local dev setup runnning Genkit dev UI and Firebase emulator, action-calls to flow.run() from within a Firebase onDocumentCreated function doesn't produce trace as expected. The same flow.run() call works as expected from a Firebase onRequest functions.

To Reproduce
Steps to reproduce the behavior:
Yarn command
"emulators:start": "genkit start --port 4001 -- tsx watch src/index.ts & GENKIT_TELEMETRY_SERVER=\"http://localhost:4033\" firebase emulators:start --import=./firebaseExport --export-on-exit --inspect-functions"

Example of failing minimal code example with Firebase onDocumentCreated:

import googleAI, { gemini20Flash } from '@genkit-ai/googleai'
import { onDocumentCreated } from 'firebase-functions/firestore'
import { debug, error } from 'firebase-functions/logger'
import { genkit } from 'genkit'

const ai = genkit({
	plugins: [googleAI()],
	model: gemini20Flash,
})

export const onGptClientRequestCreated = onDocumentCreated(
	{ document: 'gpt_client_requests/{docId}', region: 'europe-north1' },
	async () => {
		try {
			const exampleFlow = ai.flows.find((flow) => flow.__action.name === 'myExampleFlowName')
			const exampleRun = await exampleFlow?.run()
			if (exampleRun && exampleRun.result) {
				debug(exampleRun.result)
			}

		} catch (e) {
			error(e)
		}
	},
)

Example of working minimal code in Firebase onRequest:

import googleAI, { gemini20Flash } from '@genkit-ai/googleai'
import { onRequest } from 'firebase-functions/v2/https'
import { error } from 'firebase-functions/logger'
import { genkit } from 'genkit'

const ai = genkit({
	plugins: [googleAI()],
	model: gemini20Flash,
})

export const tvAlert = onRequest(
	{ cors: true, region: 'europe-north1', secrets: ['BEARER_TOKEN'] },
	async (_req, res) => {
		try {
			const exampleFlow = ai.flows.find((flow) => flow.__action.name === 'myExampleFlowName')
			const exampleRun = await exampleFlow?.run()
			if (exampleRun && exampleRun.result) {
				res.status(200).send(exampleRun.result)
				return
			}

			res.status(200).send('OK')
			return
		} catch (e) {
			error('Error 500', e)
			res.status(500).send('Error')
		}
	},
)

Observed result:
Running the failing code, then looking at Traces (http://localhost:4001/traces), I see a trace having been created with name DocumentReference.Create, however, this doesn't include any spans from Genkit, only Firestore spans (see screenshots).

Expected behavior
I'm expecting to see all Genkit/Flow spans, as I do when the same code is called from other parts of my application.

Screenshots
** Failing:**
Image
Image

** Working:**
Image
Image

Version:
– Genkit 1.3.0
– Firebase tools 13.31.2
– Node v20.11.0

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtooling

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions