Skip to content

Commit 2546654

Browse files
committed
Add input and output logs for prompt rendering.
1 parent 572db35 commit 2546654

File tree

3 files changed

+59
-20
lines changed

3 files changed

+59
-20
lines changed

js/plugins/google-cloud/src/gcpOpenTelemetry.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -351,27 +351,15 @@ class AdjustingTraceExporter implements SpanExporter {
351351
featuresTelemetry.tick(span, this.logInputAndOutput, this.projectId);
352352
// Set root status explicitly
353353
span.attributes['genkit:rootState'] = span.attributes['genkit:state'];
354-
} else {
355-
if (type === 'action' && subtype === 'model') {
356-
// Report generate metrics () for all model actions
357-
generateTelemetry.tick(span, this.logInputAndOutput, this.projectId);
358-
}
359-
if (type === 'action' && subtype === 'tool') {
360-
// TODO: Report input and output for tool actions
361-
}
362-
if (
363-
type === 'action' ||
364-
type === 'flow' ||
365-
type == 'flowStep' ||
366-
type == 'util'
367-
) {
368-
// Report request and latency metrics for all actions
369-
actionTelemetry.tick(span, this.logInputAndOutput, this.projectId);
370-
}
371-
}
372-
if (type === 'userEngagement') {
354+
} else if (type === 'action' && subtype === 'model') {
355+
// Report generate metrics () for all model actions
356+
generateTelemetry.tick(span, this.logInputAndOutput, this.projectId);
357+
} else if (type === 'userEngagement') {
373358
// Report user acceptance and feedback metrics
374359
engagementTelemetry.tick(span, this.logInputAndOutput, this.projectId);
360+
} else {
361+
// Report request and latency metrics for all actions
362+
actionTelemetry.tick(span, this.logInputAndOutput, this.projectId);
375363
}
376364
}
377365

js/plugins/google-cloud/src/telemetry/action.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@ class ActionTelemetry implements Telemetry {
3636
}
3737
const attributes = span.attributes;
3838
const actionName = (attributes['genkit:name'] as string) || '<unknown>';
39+
const type = attributes['genkit:type'] as string;
3940
const subtype = attributes['genkit:metadata:subtype'] as string;
4041

41-
if (subtype === 'tool' || actionName === 'generate') {
42+
if (
43+
type === 'promptTemplate' ||
44+
subtype === 'tool' ||
45+
actionName === 'generate'
46+
) {
4247
const path = (attributes['genkit:path'] as string) || '<unknown>';
4348
const input = truncate(attributes['genkit:input'] as string);
4449
const output = truncate(attributes['genkit:output'] as string);

js/plugins/google-cloud/tests/logs_test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,52 @@ describe('GoogleCloudLogs', () => {
524524
expect(logObjectMessages).toContain('UserAcceptance[flowName]');
525525
});
526526

527+
it('writes prompt template input and output logs', async () => {
528+
const testPrompt = ai.definePrompt(
529+
{
530+
name: 'testPrompt',
531+
input: {
532+
schema: z.object({ name: z.string() }),
533+
},
534+
},
535+
async (input) => {
536+
return {
537+
messages: [
538+
{ role: 'user', content: [{ text: `Hello, ${input.name}` }] },
539+
],
540+
};
541+
}
542+
);
543+
const testFlow = createFlowWithInput(ai, 'testFlow', async (input) => {
544+
return await testPrompt.render({ name: input });
545+
});
546+
547+
await testFlow('test');
548+
549+
await getExportedSpans();
550+
const logs = await getLogs(1, 100, logLines);
551+
const logObjectMessages = getStructuredLogMessages(logs);
552+
expect(logObjectMessages).toContain('Input[testFlow > render, testFlow]');
553+
expect(logObjectMessages).toContain('Output[testFlow > render, testFlow]');
554+
// Ensure the structured data is as expected
555+
logs.map((log) => {
556+
const structuredLog = JSON.parse(log as string);
557+
if (structuredLog.message === 'Input[testFlow > render, testFlow]') {
558+
expect(JSON.parse(structuredLog.content)).toEqual({ name: 'test' });
559+
}
560+
if (structuredLog.message === 'Output[testFlow > render, testFlow]') {
561+
expect(JSON.parse(structuredLog.content)).toEqual({
562+
messages: [
563+
{
564+
content: [{ text: 'Hello, test' }],
565+
role: 'user',
566+
},
567+
],
568+
});
569+
}
570+
});
571+
});
572+
527573
it('writes tool input and output logs', async () => {
528574
const echoTool = ai.defineTool(
529575
{ name: 'echoTool', description: 'echo' },

0 commit comments

Comments
 (0)