google-logging-utils-internal 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/package/LICENSE +203 -0
  2. package/package/README.md +83 -0
  3. package/package/lib/auth.d.mts +33 -0
  4. package/package/lib/auth.d.ts +33 -0
  5. package/package/lib/auth.js +70 -0
  6. package/package/lib/auth.js.map +1 -0
  7. package/package/lib/auth.mjs +45 -0
  8. package/package/lib/auth.mjs.map +1 -0
  9. package/package/lib/gcpLogger.d.mts +25 -0
  10. package/package/lib/gcpLogger.d.ts +25 -0
  11. package/package/lib/gcpLogger.js +118 -0
  12. package/package/lib/gcpLogger.js.map +1 -0
  13. package/package/lib/gcpLogger.mjs +82 -0
  14. package/package/lib/gcpLogger.mjs.map +1 -0
  15. package/package/lib/gcpOpenTelemetry.d.mts +59 -0
  16. package/package/lib/gcpOpenTelemetry.d.ts +59 -0
  17. package/package/lib/gcpOpenTelemetry.js +374 -0
  18. package/package/lib/gcpOpenTelemetry.js.map +1 -0
  19. package/package/lib/gcpOpenTelemetry.mjs +364 -0
  20. package/package/lib/gcpOpenTelemetry.mjs.map +1 -0
  21. package/package/lib/index.d.mts +36 -0
  22. package/package/lib/index.d.ts +36 -0
  23. package/package/lib/index.js +56 -0
  24. package/package/lib/index.js.map +1 -0
  25. package/package/lib/index.mjs +29 -0
  26. package/package/lib/index.mjs.map +1 -0
  27. package/package/lib/metrics.d.mts +65 -0
  28. package/package/lib/metrics.d.ts +65 -0
  29. package/package/lib/metrics.js +91 -0
  30. package/package/lib/metrics.js.map +1 -0
  31. package/package/lib/metrics.mjs +65 -0
  32. package/package/lib/metrics.mjs.map +1 -0
  33. package/package/lib/model-armor.d.mts +59 -0
  34. package/package/lib/model-armor.d.ts +59 -0
  35. package/package/lib/model-armor.js +205 -0
  36. package/package/lib/model-armor.js.map +1 -0
  37. package/package/lib/model-armor.mjs +181 -0
  38. package/package/lib/model-armor.mjs.map +1 -0
  39. package/package/lib/telemetry/action.d.mts +27 -0
  40. package/package/lib/telemetry/action.d.ts +27 -0
  41. package/package/lib/telemetry/action.js +92 -0
  42. package/package/lib/telemetry/action.js.map +1 -0
  43. package/package/lib/telemetry/action.mjs +73 -0
  44. package/package/lib/telemetry/action.mjs.map +1 -0
  45. package/package/lib/telemetry/defaults.d.mts +30 -0
  46. package/package/lib/telemetry/defaults.d.ts +30 -0
  47. package/package/lib/telemetry/defaults.js +70 -0
  48. package/package/lib/telemetry/defaults.js.map +1 -0
  49. package/package/lib/telemetry/defaults.mjs +46 -0
  50. package/package/lib/telemetry/defaults.mjs.map +1 -0
  51. package/package/lib/telemetry/engagement.d.mts +35 -0
  52. package/package/lib/telemetry/engagement.d.ts +35 -0
  53. package/package/lib/telemetry/engagement.js +106 -0
  54. package/package/lib/telemetry/engagement.js.map +1 -0
  55. package/package/lib/telemetry/engagement.mjs +85 -0
  56. package/package/lib/telemetry/engagement.mjs.map +1 -0
  57. package/package/lib/telemetry/feature.d.mts +35 -0
  58. package/package/lib/telemetry/feature.d.ts +35 -0
  59. package/package/lib/telemetry/feature.js +142 -0
  60. package/package/lib/telemetry/feature.js.map +1 -0
  61. package/package/lib/telemetry/feature.mjs +127 -0
  62. package/package/lib/telemetry/feature.mjs.map +1 -0
  63. package/package/lib/telemetry/generate.d.mts +53 -0
  64. package/package/lib/telemetry/generate.d.ts +53 -0
  65. package/package/lib/telemetry/generate.js +326 -0
  66. package/package/lib/telemetry/generate.js.map +1 -0
  67. package/package/lib/telemetry/generate.mjs +314 -0
  68. package/package/lib/telemetry/generate.mjs.map +1 -0
  69. package/package/lib/telemetry/path.d.mts +32 -0
  70. package/package/lib/telemetry/path.d.ts +32 -0
  71. package/package/lib/telemetry/path.js +91 -0
  72. package/package/lib/telemetry/path.js.map +1 -0
  73. package/package/lib/telemetry/path.mjs +78 -0
  74. package/package/lib/telemetry/path.mjs.map +1 -0
  75. package/package/lib/types.d.mts +121 -0
  76. package/package/lib/types.d.ts +121 -0
  77. package/package/lib/types.js +17 -0
  78. package/package/lib/types.js.map +1 -0
  79. package/package/lib/types.mjs +1 -0
  80. package/package/lib/types.mjs.map +1 -0
  81. package/package/lib/utils.d.mts +57 -0
  82. package/package/lib/utils.d.ts +57 -0
  83. package/package/lib/utils.js +143 -0
  84. package/package/lib/utils.js.map +1 -0
  85. package/package/lib/utils.mjs +104 -0
  86. package/package/lib/utils.mjs.map +1 -0
  87. package/package/package.json +90 -0
  88. package/package/src/auth.ts +89 -0
  89. package/package/src/gcpLogger.ts +124 -0
  90. package/package/src/gcpOpenTelemetry.ts +485 -0
  91. package/package/src/index.ts +59 -0
  92. package/package/src/metrics.ts +122 -0
  93. package/package/src/model-armor.ts +317 -0
  94. package/package/src/telemetry/action.ts +106 -0
  95. package/package/src/telemetry/defaults.ts +72 -0
  96. package/package/src/telemetry/engagement.ts +120 -0
  97. package/package/src/telemetry/feature.ts +170 -0
  98. package/package/src/telemetry/generate.ts +454 -0
  99. package/package/src/telemetry/path.ts +111 -0
  100. package/package/src/types.ts +133 -0
  101. package/package/src/utils.ts +175 -0
  102. package/package/tests/logs_no_input_output_test.ts +267 -0
  103. package/package/tests/logs_session_test.ts +219 -0
  104. package/package/tests/logs_test.ts +633 -0
  105. package/package/tests/metrics_test.ts +792 -0
  106. package/package/tests/model_armor_test.ts +336 -0
  107. package/package/tests/traces_test.ts +380 -0
  108. package/package/typedoc.json +3 -0
  109. package/package.json +10 -0
@@ -0,0 +1,314 @@
1
+ import { ValueType } from "@opentelemetry/api";
2
+ import { createHash } from "crypto";
3
+ import {
4
+ GENKIT_VERSION
5
+ } from "genkit";
6
+ import { logger } from "genkit/logging";
7
+ import { toDisplayPath } from "genkit/tracing";
8
+ import {
9
+ MetricCounter,
10
+ MetricHistogram,
11
+ internalMetricNamespaceWrap
12
+ } from "../metrics.js";
13
+ import {
14
+ createCommonLogAttributes,
15
+ extractErrorName,
16
+ extractOuterFeatureNameFromPath,
17
+ truncate,
18
+ truncatePath
19
+ } from "../utils.js";
20
+ class GenerateTelemetry {
21
+ /**
22
+ * Wraps the declared metrics in a Genkit-specific, internal namespace.
23
+ */
24
+ _N = internalMetricNamespaceWrap.bind(null, "ai");
25
+ actionCounter = new MetricCounter(this._N("generate/requests"), {
26
+ description: "Counts calls to genkit generate actions.",
27
+ valueType: ValueType.INT
28
+ });
29
+ latencies = new MetricHistogram(this._N("generate/latency"), {
30
+ description: "Latencies when interacting with a Genkit model.",
31
+ valueType: ValueType.DOUBLE,
32
+ unit: "ms"
33
+ });
34
+ inputCharacters = new MetricCounter(
35
+ this._N("generate/input/characters"),
36
+ {
37
+ description: "Counts input characters to any Genkit model.",
38
+ valueType: ValueType.INT
39
+ }
40
+ );
41
+ inputTokens = new MetricCounter(this._N("generate/input/tokens"), {
42
+ description: "Counts input tokens to a Genkit model.",
43
+ valueType: ValueType.INT
44
+ });
45
+ inputImages = new MetricCounter(this._N("generate/input/images"), {
46
+ description: "Counts input images to a Genkit model.",
47
+ valueType: ValueType.INT
48
+ });
49
+ outputCharacters = new MetricCounter(
50
+ this._N("generate/output/characters"),
51
+ {
52
+ description: "Counts output characters from a Genkit model.",
53
+ valueType: ValueType.INT
54
+ }
55
+ );
56
+ outputTokens = new MetricCounter(this._N("generate/output/tokens"), {
57
+ description: "Counts output tokens from a Genkit model.",
58
+ valueType: ValueType.INT
59
+ });
60
+ thinkingTokens = new MetricCounter(
61
+ this._N("generate/thinking/tokens"),
62
+ {
63
+ description: "Counts thinking tokens from a Genkit model.",
64
+ valueType: ValueType.INT
65
+ }
66
+ );
67
+ outputImages = new MetricCounter(this._N("generate/output/images"), {
68
+ description: "Count output images from a Genkit model.",
69
+ valueType: ValueType.INT
70
+ });
71
+ tick(span, logInputAndOutput, projectId) {
72
+ const attributes = span.attributes;
73
+ const modelName = truncate(attributes["genkit:name"], 1024);
74
+ const path = attributes["genkit:path"] || "";
75
+ const input = "genkit:input" in attributes ? JSON.parse(
76
+ attributes["genkit:input"]
77
+ ) : void 0;
78
+ const output = "genkit:output" in attributes ? JSON.parse(
79
+ attributes["genkit:output"]
80
+ ) : void 0;
81
+ const errName = extractErrorName(span.events);
82
+ let featureName = truncate(
83
+ attributes["genkit:metadata:flow:name"] || extractOuterFeatureNameFromPath(path)
84
+ );
85
+ if (!featureName || featureName === "<unknown>") {
86
+ featureName = "generate";
87
+ }
88
+ const sessionId = attributes["genkit:sessionId"];
89
+ const threadName = attributes["genkit:threadName"];
90
+ if (input) {
91
+ this.recordGenerateActionMetrics(modelName, featureName, path, {
92
+ response: output,
93
+ errName
94
+ });
95
+ this.recordGenerateActionConfigLogs(
96
+ span,
97
+ modelName,
98
+ featureName,
99
+ path,
100
+ input,
101
+ projectId,
102
+ sessionId,
103
+ threadName
104
+ );
105
+ if (logInputAndOutput) {
106
+ this.recordGenerateActionInputLogs(
107
+ span,
108
+ modelName,
109
+ featureName,
110
+ path,
111
+ input,
112
+ projectId,
113
+ sessionId,
114
+ threadName
115
+ );
116
+ }
117
+ }
118
+ if (output && logInputAndOutput) {
119
+ this.recordGenerateActionOutputLogs(
120
+ span,
121
+ modelName,
122
+ featureName,
123
+ path,
124
+ output,
125
+ projectId,
126
+ sessionId,
127
+ threadName
128
+ );
129
+ }
130
+ }
131
+ recordGenerateActionMetrics(modelName, featureName, path, opts) {
132
+ this.doRecordGenerateActionMetrics(modelName, opts.response?.usage || {}, {
133
+ featureName,
134
+ path,
135
+ latencyMs: opts.response?.latencyMs,
136
+ errName: opts.errName,
137
+ source: "ts",
138
+ sourceVersion: GENKIT_VERSION
139
+ });
140
+ }
141
+ recordGenerateActionConfigLogs(span, model, featureName, qualifiedPath, input, projectId, sessionId, threadName) {
142
+ const path = truncatePath(toDisplayPath(qualifiedPath));
143
+ const sharedMetadata = {
144
+ ...createCommonLogAttributes(span, projectId),
145
+ model,
146
+ path,
147
+ qualifiedPath,
148
+ featureName,
149
+ sessionId,
150
+ threadName
151
+ };
152
+ logger.logStructured(`Config[${path}, ${model}]`, {
153
+ ...sharedMetadata,
154
+ maxOutputTokens: input.config?.maxOutputTokens,
155
+ stopSequences: input.config?.stopSequences,
156
+ // array
157
+ source: "ts",
158
+ sourceVersion: GENKIT_VERSION
159
+ });
160
+ }
161
+ recordGenerateActionInputLogs(span, model, featureName, qualifiedPath, input, projectId, sessionId, threadName) {
162
+ const path = truncatePath(toDisplayPath(qualifiedPath));
163
+ const sharedMetadata = {
164
+ ...createCommonLogAttributes(span, projectId),
165
+ model,
166
+ path,
167
+ qualifiedPath,
168
+ featureName,
169
+ sessionId,
170
+ threadName
171
+ };
172
+ const messages = input.messages.length;
173
+ input.messages.forEach((msg, msgIdx) => {
174
+ const parts = msg.content.length;
175
+ msg.content.forEach((part, partIdx) => {
176
+ const partCounts = this.toPartCounts(partIdx, parts, msgIdx, messages);
177
+ logger.logStructured(`Input[${path}, ${model}] ${partCounts}`, {
178
+ ...sharedMetadata,
179
+ content: this.toPartLogContent(part),
180
+ role: msg.role,
181
+ partIndex: partIdx,
182
+ totalParts: parts,
183
+ messageIndex: msgIdx,
184
+ totalMessages: messages
185
+ });
186
+ });
187
+ });
188
+ }
189
+ recordGenerateActionOutputLogs(span, model, featureName, qualifiedPath, output, projectId, sessionId, threadName) {
190
+ const path = truncatePath(toDisplayPath(qualifiedPath));
191
+ const sharedMetadata = {
192
+ ...createCommonLogAttributes(span, projectId),
193
+ model,
194
+ path,
195
+ qualifiedPath,
196
+ featureName,
197
+ sessionId,
198
+ threadName
199
+ };
200
+ const message = output.message || output.candidates?.[0]?.message;
201
+ if (message?.content) {
202
+ const parts = message.content.length;
203
+ message.content.forEach((part, partIdx) => {
204
+ const partCounts = this.toPartCounts(partIdx, parts, 0, 1);
205
+ const initial = output.finishMessage ? { finishMessage: truncate(output.finishMessage) } : {};
206
+ logger.logStructured(`Output[${path}, ${model}] ${partCounts}`, {
207
+ ...initial,
208
+ ...sharedMetadata,
209
+ content: this.toPartLogContent(part),
210
+ role: message.role,
211
+ partIndex: partIdx,
212
+ totalParts: parts,
213
+ candidateIndex: 0,
214
+ totalCandidates: 1,
215
+ messageIndex: 0,
216
+ finishReason: output.finishReason
217
+ });
218
+ });
219
+ }
220
+ }
221
+ toPartCounts(partOrdinal, parts, msgOrdinal, messages) {
222
+ if (parts > 1 && messages > 1) {
223
+ return `(part ${this.xOfY(partOrdinal, parts)} in message ${this.xOfY(
224
+ msgOrdinal,
225
+ messages
226
+ )})`;
227
+ }
228
+ if (parts > 1) {
229
+ return `(part ${this.xOfY(partOrdinal, parts)})`;
230
+ }
231
+ if (messages > 1) {
232
+ return `(message ${this.xOfY(msgOrdinal, messages)})`;
233
+ }
234
+ return "";
235
+ }
236
+ xOfY(x, y) {
237
+ return `${x + 1} of ${y}`;
238
+ }
239
+ toPartLogContent(part) {
240
+ if (part.text) {
241
+ return truncate(part.text);
242
+ }
243
+ if (part.data) {
244
+ return truncate(JSON.stringify(part.data));
245
+ }
246
+ if (part.media) {
247
+ return this.toPartLogMedia(part);
248
+ }
249
+ if (part.toolRequest) {
250
+ return this.toPartLogToolRequest(part);
251
+ }
252
+ if (part.toolResponse) {
253
+ return this.toPartLogToolResponse(part);
254
+ }
255
+ if (part.custom) {
256
+ return truncate(JSON.stringify(part.custom));
257
+ }
258
+ return "<unknown format>";
259
+ }
260
+ toPartLogMedia(part) {
261
+ if (part.media.url.startsWith("data:")) {
262
+ const splitIdx = part.media.url.indexOf("base64,");
263
+ if (splitIdx < 0) {
264
+ return "<unknown media format>";
265
+ }
266
+ const prefix = part.media.url.substring(0, splitIdx + 7);
267
+ const hashedContent = createHash("sha256").update(part.media.url.substring(splitIdx + 7)).digest("hex");
268
+ return `${prefix}<sha256(${hashedContent})>`;
269
+ }
270
+ return truncate(part.media.url);
271
+ }
272
+ toPartLogToolRequest(part) {
273
+ const inputText = typeof part.toolRequest.input === "string" ? part.toolRequest.input : JSON.stringify(part.toolRequest.input);
274
+ return truncate(
275
+ `Tool request: ${part.toolRequest.name}, ref: ${part.toolRequest.ref}, input: ${inputText}`
276
+ );
277
+ }
278
+ toPartLogToolResponse(part) {
279
+ const outputText = typeof part.toolResponse.output === "string" ? part.toolResponse.output : JSON.stringify(part.toolResponse.output);
280
+ return truncate(
281
+ `Tool response: ${part.toolResponse.name}, ref: ${part.toolResponse.ref}, output: ${outputText}`
282
+ );
283
+ }
284
+ /**
285
+ * Records all metrics associated with performing a GenerateAction.
286
+ */
287
+ doRecordGenerateActionMetrics(modelName, usage, dimensions) {
288
+ const shared = {
289
+ modelName,
290
+ featureName: dimensions.featureName,
291
+ path: dimensions.path,
292
+ source: dimensions.source,
293
+ sourceVersion: dimensions.sourceVersion,
294
+ status: dimensions.errName ? "failure" : "success"
295
+ };
296
+ this.actionCounter.add(1, {
297
+ error: dimensions.errName,
298
+ ...shared
299
+ });
300
+ this.latencies.record(dimensions.latencyMs, shared);
301
+ this.inputTokens.add(usage.inputTokens, shared);
302
+ this.inputCharacters.add(usage.inputCharacters, shared);
303
+ this.inputImages.add(usage.inputImages, shared);
304
+ this.outputTokens.add(usage.outputTokens, shared);
305
+ this.outputCharacters.add(usage.outputCharacters, shared);
306
+ this.outputImages.add(usage.outputImages, shared);
307
+ this.thinkingTokens.add(usage.thoughtsTokens, shared);
308
+ }
309
+ }
310
+ const generateTelemetry = new GenerateTelemetry();
311
+ export {
312
+ generateTelemetry
313
+ };
314
+ //# sourceMappingURL=generate.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/telemetry/generate.ts"],"sourcesContent":["/**\n * Copyright 2024 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ValueType } from '@opentelemetry/api';\nimport type { ReadableSpan } from '@opentelemetry/sdk-trace-base';\nimport { createHash } from 'crypto';\nimport {\n GENKIT_VERSION,\n type GenerateRequestData,\n type GenerateResponseData,\n type GenerationUsage,\n type MediaPart,\n type Part,\n type ToolRequestPart,\n type ToolResponsePart,\n} from 'genkit';\nimport { logger } from 'genkit/logging';\nimport { toDisplayPath } from 'genkit/tracing';\nimport {\n MetricCounter,\n MetricHistogram,\n internalMetricNamespaceWrap,\n type Telemetry,\n} from '../metrics.js';\nimport {\n createCommonLogAttributes,\n extractErrorName,\n extractOuterFeatureNameFromPath,\n truncate,\n truncatePath,\n} from '../utils.js';\n\ntype SharedDimensions = {\n modelName?: string;\n featureName?: string;\n path?: string;\n status?: string;\n source?: string;\n sourceVersion?: string;\n};\n\nclass GenerateTelemetry implements Telemetry {\n /**\n * Wraps the declared metrics in a Genkit-specific, internal namespace.\n */\n private _N = internalMetricNamespaceWrap.bind(null, 'ai');\n\n private actionCounter = new MetricCounter(this._N('generate/requests'), {\n description: 'Counts calls to genkit generate actions.',\n valueType: ValueType.INT,\n });\n\n private latencies = new MetricHistogram(this._N('generate/latency'), {\n description: 'Latencies when interacting with a Genkit model.',\n valueType: ValueType.DOUBLE,\n unit: 'ms',\n });\n\n private inputCharacters = new MetricCounter(\n this._N('generate/input/characters'),\n {\n description: 'Counts input characters to any Genkit model.',\n valueType: ValueType.INT,\n }\n );\n\n private inputTokens = new MetricCounter(this._N('generate/input/tokens'), {\n description: 'Counts input tokens to a Genkit model.',\n valueType: ValueType.INT,\n });\n\n private inputImages = new MetricCounter(this._N('generate/input/images'), {\n description: 'Counts input images to a Genkit model.',\n valueType: ValueType.INT,\n });\n\n private outputCharacters = new MetricCounter(\n this._N('generate/output/characters'),\n {\n description: 'Counts output characters from a Genkit model.',\n valueType: ValueType.INT,\n }\n );\n\n private outputTokens = new MetricCounter(this._N('generate/output/tokens'), {\n description: 'Counts output tokens from a Genkit model.',\n valueType: ValueType.INT,\n });\n\n private thinkingTokens = new MetricCounter(\n this._N('generate/thinking/tokens'),\n {\n description: 'Counts thinking tokens from a Genkit model.',\n valueType: ValueType.INT,\n }\n );\n\n private outputImages = new MetricCounter(this._N('generate/output/images'), {\n description: 'Count output images from a Genkit model.',\n valueType: ValueType.INT,\n });\n\n tick(\n span: ReadableSpan,\n logInputAndOutput: boolean,\n projectId?: string\n ): void {\n const attributes = span.attributes;\n const modelName = truncate(attributes['genkit:name'] as string, 1024);\n const path = (attributes['genkit:path'] as string) || '';\n const input =\n 'genkit:input' in attributes\n ? (JSON.parse(\n attributes['genkit:input']! as string\n ) as GenerateRequestData)\n : undefined;\n const output =\n 'genkit:output' in attributes\n ? (JSON.parse(\n attributes['genkit:output']! as string\n ) as GenerateResponseData)\n : undefined;\n\n const errName = extractErrorName(span.events);\n let featureName = truncate(\n (attributes['genkit:metadata:flow:name'] ||\n extractOuterFeatureNameFromPath(path)) as string\n );\n if (!featureName || featureName === '<unknown>') {\n featureName = 'generate';\n }\n\n const sessionId = attributes['genkit:sessionId'] as string;\n const threadName = attributes['genkit:threadName'] as string;\n\n if (input) {\n this.recordGenerateActionMetrics(modelName, featureName, path, {\n response: output,\n errName,\n });\n this.recordGenerateActionConfigLogs(\n span,\n modelName,\n featureName,\n path,\n input,\n projectId,\n sessionId,\n threadName\n );\n\n if (logInputAndOutput) {\n this.recordGenerateActionInputLogs(\n span,\n modelName,\n featureName,\n path,\n input,\n projectId,\n sessionId,\n threadName\n );\n }\n }\n\n if (output && logInputAndOutput) {\n this.recordGenerateActionOutputLogs(\n span,\n modelName,\n featureName,\n path,\n output,\n projectId,\n sessionId,\n threadName\n );\n }\n }\n\n private recordGenerateActionMetrics(\n modelName: string,\n featureName: string,\n path: string,\n opts: {\n response?: GenerateResponseData;\n errName?: string;\n }\n ) {\n this.doRecordGenerateActionMetrics(modelName, opts.response?.usage || {}, {\n featureName,\n path,\n latencyMs: opts.response?.latencyMs,\n errName: opts.errName,\n source: 'ts',\n sourceVersion: GENKIT_VERSION,\n });\n }\n\n private recordGenerateActionConfigLogs(\n span: ReadableSpan,\n model: string,\n featureName: string,\n qualifiedPath: string,\n input: GenerateRequestData,\n projectId?: string,\n sessionId?: string,\n threadName?: string\n ) {\n const path = truncatePath(toDisplayPath(qualifiedPath));\n const sharedMetadata = {\n ...createCommonLogAttributes(span, projectId),\n model,\n path,\n qualifiedPath,\n featureName,\n sessionId,\n threadName,\n };\n logger.logStructured(`Config[${path}, ${model}]`, {\n ...sharedMetadata,\n maxOutputTokens: input.config?.maxOutputTokens,\n stopSequences: input.config?.stopSequences, // array\n source: 'ts',\n sourceVersion: GENKIT_VERSION,\n });\n }\n\n private recordGenerateActionInputLogs(\n span: ReadableSpan,\n model: string,\n featureName: string,\n qualifiedPath: string,\n input: GenerateRequestData,\n projectId?: string,\n sessionId?: string,\n threadName?: string\n ) {\n const path = truncatePath(toDisplayPath(qualifiedPath));\n const sharedMetadata = {\n ...createCommonLogAttributes(span, projectId),\n model,\n path,\n qualifiedPath,\n featureName,\n sessionId,\n threadName,\n };\n\n const messages = input.messages.length;\n input.messages.forEach((msg, msgIdx) => {\n const parts = msg.content.length;\n msg.content.forEach((part, partIdx) => {\n const partCounts = this.toPartCounts(partIdx, parts, msgIdx, messages);\n logger.logStructured(`Input[${path}, ${model}] ${partCounts}`, {\n ...sharedMetadata,\n content: this.toPartLogContent(part),\n role: msg.role,\n partIndex: partIdx,\n totalParts: parts,\n messageIndex: msgIdx,\n totalMessages: messages,\n });\n });\n });\n }\n\n private recordGenerateActionOutputLogs(\n span: ReadableSpan,\n model: string,\n featureName: string,\n qualifiedPath: string,\n output: GenerateResponseData,\n projectId?: string,\n sessionId?: string,\n threadName?: string\n ) {\n const path = truncatePath(toDisplayPath(qualifiedPath));\n const sharedMetadata = {\n ...createCommonLogAttributes(span, projectId),\n model,\n path,\n qualifiedPath,\n featureName,\n sessionId,\n threadName,\n };\n const message = output.message || output.candidates?.[0]?.message!;\n\n if (message?.content) {\n const parts = message.content.length;\n message.content.forEach((part, partIdx) => {\n const partCounts = this.toPartCounts(partIdx, parts, 0, 1);\n const initial = output.finishMessage\n ? { finishMessage: truncate(output.finishMessage) }\n : {};\n logger.logStructured(`Output[${path}, ${model}] ${partCounts}`, {\n ...initial,\n ...sharedMetadata,\n content: this.toPartLogContent(part),\n role: message.role,\n partIndex: partIdx,\n totalParts: parts,\n candidateIndex: 0,\n totalCandidates: 1,\n messageIndex: 0,\n finishReason: output.finishReason,\n });\n });\n }\n }\n\n private toPartCounts(\n partOrdinal: number,\n parts: number,\n msgOrdinal: number,\n messages: number\n ): string {\n if (parts > 1 && messages > 1) {\n return `(part ${this.xOfY(partOrdinal, parts)} in message ${this.xOfY(\n msgOrdinal,\n messages\n )})`;\n }\n if (parts > 1) {\n return `(part ${this.xOfY(partOrdinal, parts)})`;\n }\n if (messages > 1) {\n return `(message ${this.xOfY(msgOrdinal, messages)})`;\n }\n return '';\n }\n\n private xOfY(x: number, y: number): string {\n return `${x + 1} of ${y}`;\n }\n\n private toPartLogContent(part: Part): string {\n if (part.text) {\n return truncate(part.text);\n }\n if (part.data) {\n return truncate(JSON.stringify(part.data));\n }\n if (part.media) {\n return this.toPartLogMedia(part);\n }\n if (part.toolRequest) {\n return this.toPartLogToolRequest(part);\n }\n if (part.toolResponse) {\n return this.toPartLogToolResponse(part);\n }\n if (part.custom) {\n return truncate(JSON.stringify(part.custom));\n }\n return '<unknown format>';\n }\n\n private toPartLogMedia(part: MediaPart): string {\n if (part.media.url.startsWith('data:')) {\n const splitIdx = part.media.url.indexOf('base64,');\n if (splitIdx < 0) {\n return '<unknown media format>';\n }\n const prefix = part.media.url.substring(0, splitIdx + 7);\n const hashedContent = createHash('sha256')\n .update(part.media.url.substring(splitIdx + 7))\n .digest('hex');\n return `${prefix}<sha256(${hashedContent})>`;\n }\n return truncate(part.media.url);\n }\n\n private toPartLogToolRequest(part: ToolRequestPart): string {\n const inputText =\n typeof part.toolRequest.input === 'string'\n ? part.toolRequest.input\n : JSON.stringify(part.toolRequest.input);\n return truncate(\n `Tool request: ${part.toolRequest.name}, ref: ${part.toolRequest.ref}, input: ${inputText}`\n );\n }\n\n private toPartLogToolResponse(part: ToolResponsePart): string {\n const outputText =\n typeof part.toolResponse.output === 'string'\n ? part.toolResponse.output\n : JSON.stringify(part.toolResponse.output);\n return truncate(\n `Tool response: ${part.toolResponse.name}, ref: ${part.toolResponse.ref}, output: ${outputText}`\n );\n }\n\n /**\n * Records all metrics associated with performing a GenerateAction.\n */\n private doRecordGenerateActionMetrics(\n modelName: string,\n usage: GenerationUsage,\n dimensions: {\n featureName?: string;\n path?: string;\n latencyMs?: number;\n errName?: string;\n source?: string;\n sourceVersion: string;\n }\n ) {\n const shared: SharedDimensions = {\n modelName: modelName,\n featureName: dimensions.featureName,\n path: dimensions.path,\n source: dimensions.source,\n sourceVersion: dimensions.sourceVersion,\n status: dimensions.errName ? 'failure' : 'success',\n };\n\n this.actionCounter.add(1, {\n error: dimensions.errName,\n ...shared,\n });\n\n this.latencies.record(dimensions.latencyMs, shared);\n\n // inputs\n this.inputTokens.add(usage.inputTokens, shared);\n this.inputCharacters.add(usage.inputCharacters, shared);\n this.inputImages.add(usage.inputImages, shared);\n\n // outputs\n this.outputTokens.add(usage.outputTokens, shared);\n this.outputCharacters.add(usage.outputCharacters, shared);\n this.outputImages.add(usage.outputImages, shared);\n\n // thoughts\n this.thinkingTokens.add(usage.thoughtsTokens, shared);\n }\n}\n\nconst generateTelemetry = new GenerateTelemetry();\nexport { generateTelemetry };\n"],"mappings":"AAgBA,SAAS,iBAAiB;AAE1B,SAAS,kBAAkB;AAC3B;AAAA,EACE;AAAA,OAQK;AACP,SAAS,cAAc;AACvB,SAAS,qBAAqB;AAC9B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAWP,MAAM,kBAAuC;AAAA;AAAA;AAAA;AAAA,EAInC,KAAK,4BAA4B,KAAK,MAAM,IAAI;AAAA,EAEhD,gBAAgB,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AM;AAC7C,WAAO;AAAA,MACL,kBAAkB,KAAK,aAAa,IAAI,UAAU,KAAK,aAAa,GAAG,aAAa,UAAU;AAAA,IAChG;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKQ,8BACN,WACA,OACA,YAQA;AACA,UAAM,SAA2B;AAAA,MAC/B;AAAA,MACA,aAAa,WAAW;AAAA,MACxB,MAAM,WAAW;AAAA,MACjB,QAAQ,WAAW;AAAA,MACnB,eAAe,WAAW;AAAA,MAC1B,QAAQ,WAAW,UAAU,YAAY;AAAA,IAC3C;AAEA,SAAK,cAAc,IAAI,GAAG;AAAA,MACxB,OAAO,WAAW;AAAA,MAClB,GAAG;AAAA,IACL,CAAC;AAED,SAAK,UAAU,OAAO,WAAW,WAAW,MAAM;AAGlD,SAAK,YAAY,IAAI,MAAM,aAAa,MAAM;AAC9C,SAAK,gBAAgB,IAAI,MAAM,iBAAiB,MAAM;AACtD,SAAK,YAAY,IAAI,MAAM,aAAa,MAAM;AAG9C,SAAK,aAAa,IAAI,MAAM,cAAc,MAAM;AAChD,SAAK,iBAAiB,IAAI,MAAM,kBAAkB,MAAM;AACxD,SAAK,aAAa,IAAI,MAAM,cAAc,MAAM;AAGhD,SAAK,eAAe,IAAI,MAAM,gBAAgB,MAAM;AAAA,EACtD;AACF;AAEA,MAAM,oBAAoB,IAAI,kBAAkB;","names":[]}
@@ -0,0 +1,32 @@
1
+ import { ReadableSpan } from '@opentelemetry/sdk-trace-base';
2
+ import { Telemetry } from '../metrics.mjs';
3
+ import '@opentelemetry/api';
4
+
5
+ /**
6
+ * Copyright 2024 Google LLC
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ declare class PathsTelemetry implements Telemetry {
22
+ /**
23
+ * Wraps the declared metrics in a Genkit-specific, internal namespace.
24
+ */
25
+ private _N;
26
+ private pathCounter;
27
+ private pathLatencies;
28
+ tick(span: ReadableSpan, logInputAndOutput: boolean, projectId?: string): void;
29
+ }
30
+ declare const pathsTelemetry: PathsTelemetry;
31
+
32
+ export { pathsTelemetry };
@@ -0,0 +1,32 @@
1
+ import { ReadableSpan } from '@opentelemetry/sdk-trace-base';
2
+ import { Telemetry } from '../metrics.js';
3
+ import '@opentelemetry/api';
4
+
5
+ /**
6
+ * Copyright 2024 Google LLC
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ declare class PathsTelemetry implements Telemetry {
22
+ /**
23
+ * Wraps the declared metrics in a Genkit-specific, internal namespace.
24
+ */
25
+ private _N;
26
+ private pathCounter;
27
+ private pathLatencies;
28
+ tick(span: ReadableSpan, logInputAndOutput: boolean, projectId?: string): void;
29
+ }
30
+ declare const pathsTelemetry: PathsTelemetry;
31
+
32
+ export { pathsTelemetry };
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var path_exports = {};
20
+ __export(path_exports, {
21
+ pathsTelemetry: () => pathsTelemetry
22
+ });
23
+ module.exports = __toCommonJS(path_exports);
24
+ var import_api = require("@opentelemetry/api");
25
+ var import_core = require("@opentelemetry/core");
26
+ var import_genkit = require("genkit");
27
+ var import_logging = require("genkit/logging");
28
+ var import_tracing = require("genkit/tracing");
29
+ var import_metrics = require("../metrics.js");
30
+ var import_utils = require("../utils.js");
31
+ class PathsTelemetry {
32
+ /**
33
+ * Wraps the declared metrics in a Genkit-specific, internal namespace.
34
+ */
35
+ _N = import_metrics.internalMetricNamespaceWrap.bind(null, "feature");
36
+ pathCounter = new import_metrics.MetricCounter(this._N("path/requests"), {
37
+ description: "Tracks unique flow paths per flow.",
38
+ valueType: import_api.ValueType.INT
39
+ });
40
+ pathLatencies = new import_metrics.MetricHistogram(this._N("path/latency"), {
41
+ description: "Latencies per flow path.",
42
+ ValueType: import_api.ValueType.DOUBLE,
43
+ unit: "ms"
44
+ });
45
+ tick(span, logInputAndOutput, projectId) {
46
+ const attributes = span.attributes;
47
+ const path = attributes["genkit:path"];
48
+ const isFailureSource = !!span.attributes["genkit:isFailureSource"];
49
+ const state = attributes["genkit:state"];
50
+ if (!path || !isFailureSource || state !== "error") {
51
+ return;
52
+ }
53
+ const sessionId = attributes["genkit:sessionId"];
54
+ const threadName = attributes["genkit:threadName"];
55
+ const errorName = (0, import_utils.extractErrorName)(span.events) || "<unknown>";
56
+ const errorMessage = (0, import_utils.extractErrorMessage)(span.events) || "<unknown>";
57
+ const errorStack = (0, import_utils.extractErrorStack)(span.events) || "";
58
+ const latency = (0, import_core.hrTimeToMilliseconds)(
59
+ (0, import_core.hrTimeDuration)(span.startTime, span.endTime)
60
+ );
61
+ const pathDimensions = {
62
+ featureName: (0, import_utils.extractOuterFeatureNameFromPath)(path),
63
+ status: "failure",
64
+ error: errorName,
65
+ path,
66
+ source: "ts",
67
+ sourceVersion: import_genkit.GENKIT_VERSION
68
+ };
69
+ this.pathCounter.add(1, pathDimensions);
70
+ this.pathLatencies.record(latency, pathDimensions);
71
+ const displayPath = (0, import_utils.truncatePath)((0, import_tracing.toDisplayPath)(path));
72
+ import_logging.logger.logStructuredError(`Error[${displayPath}, ${errorName}]`, {
73
+ ...(0, import_utils.createCommonLogAttributes)(span, projectId),
74
+ path: displayPath,
75
+ qualifiedPath: path,
76
+ name: errorName,
77
+ message: errorMessage,
78
+ stack: errorStack,
79
+ source: "ts",
80
+ sourceVersion: import_genkit.GENKIT_VERSION,
81
+ sessionId,
82
+ threadName
83
+ });
84
+ }
85
+ }
86
+ const pathsTelemetry = new PathsTelemetry();
87
+ // Annotate the CommonJS export names for ESM import in node:
88
+ 0 && (module.exports = {
89
+ pathsTelemetry
90
+ });
91
+ //# sourceMappingURL=path.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/telemetry/path.ts"],"sourcesContent":["/**\n * Copyright 2024 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ValueType } from '@opentelemetry/api';\nimport { hrTimeDuration, hrTimeToMilliseconds } from '@opentelemetry/core';\nimport type { ReadableSpan } from '@opentelemetry/sdk-trace-base';\nimport { GENKIT_VERSION } from 'genkit';\nimport { logger } from 'genkit/logging';\nimport { toDisplayPath } from 'genkit/tracing';\nimport {\n MetricCounter,\n MetricHistogram,\n internalMetricNamespaceWrap,\n type Telemetry,\n} from '../metrics.js';\nimport {\n createCommonLogAttributes,\n extractErrorMessage,\n extractErrorName,\n extractErrorStack,\n extractOuterFeatureNameFromPath,\n truncatePath,\n} from '../utils.js';\n\nclass PathsTelemetry implements Telemetry {\n /**\n * Wraps the declared metrics in a Genkit-specific, internal namespace.\n */\n private _N = internalMetricNamespaceWrap.bind(null, 'feature');\n\n private pathCounter = new MetricCounter(this._N('path/requests'), {\n description: 'Tracks unique flow paths per flow.',\n valueType: ValueType.INT,\n });\n\n private pathLatencies = new MetricHistogram(this._N('path/latency'), {\n description: 'Latencies per flow path.',\n ValueType: ValueType.DOUBLE,\n unit: 'ms',\n });\n\n tick(\n span: ReadableSpan,\n logInputAndOutput: boolean,\n projectId?: string\n ): void {\n const attributes = span.attributes;\n\n const path = attributes['genkit:path'] as string;\n\n const isFailureSource = !!span.attributes['genkit:isFailureSource'];\n const state = attributes['genkit:state'] as string;\n\n if (!path || !isFailureSource || state !== 'error') {\n // Only tick metrics for failing, leaf spans.\n return;\n }\n\n const sessionId = attributes['genkit:sessionId'] as string;\n const threadName = attributes['genkit:threadName'] as string;\n\n const errorName = extractErrorName(span.events) || '<unknown>';\n const errorMessage = extractErrorMessage(span.events) || '<unknown>';\n const errorStack = extractErrorStack(span.events) || '';\n\n const latency = hrTimeToMilliseconds(\n hrTimeDuration(span.startTime, span.endTime)\n );\n\n const pathDimensions = {\n featureName: extractOuterFeatureNameFromPath(path),\n status: 'failure',\n error: errorName,\n path: path,\n source: 'ts',\n sourceVersion: GENKIT_VERSION,\n };\n this.pathCounter.add(1, pathDimensions);\n this.pathLatencies.record(latency, pathDimensions);\n\n const displayPath = truncatePath(toDisplayPath(path));\n logger.logStructuredError(`Error[${displayPath}, ${errorName}]`, {\n ...createCommonLogAttributes(span, projectId),\n path: displayPath,\n qualifiedPath: path,\n name: errorName,\n message: errorMessage,\n stack: errorStack,\n source: 'ts',\n sourceVersion: GENKIT_VERSION,\n sessionId,\n threadName,\n });\n }\n}\n\nconst pathsTelemetry = new PathsTelemetry();\nexport { pathsTelemetry };\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAgBA,iBAA0B;AAC1B,kBAAqD;AAErD,oBAA+B;AAC/B,qBAAuB;AACvB,qBAA8B;AAC9B,qBAKO;AACP,mBAOO;AAEP,MAAM,eAAoC;AAAA;AAAA;AAAA;AAAA,EAIhC,KAAK,2CAA4B,KAAK,MAAM,SAAS;AAAA,EAErD,cAAc,IAAI,6BAAc,KAAK,GAAG,eAAe,GAAG;AAAA,IAChE,aAAa;AAAA,IACb,WAAW,qBAAU;AAAA,EACvB,CAAC;AAAA,EAEO,gBAAgB,IAAI,+BAAgB,KAAK,GAAG,cAAc,GAAG;AAAA,IACnE,aAAa;AAAA,IACb,WAAW,qBAAU;AAAA,IACrB,MAAM;AAAA,EACR,CAAC;AAAA,EAED,KACE,MACA,mBACA,WACM;AACN,UAAM,aAAa,KAAK;AAExB,UAAM,OAAO,WAAW,aAAa;AAErC,UAAM,kBAAkB,CAAC,CAAC,KAAK,WAAW,wBAAwB;AAClE,UAAM,QAAQ,WAAW,cAAc;AAEvC,QAAI,CAAC,QAAQ,CAAC,mBAAmB,UAAU,SAAS;AAElD;AAAA,IACF;AAEA,UAAM,YAAY,WAAW,kBAAkB;AAC/C,UAAM,aAAa,WAAW,mBAAmB;AAEjD,UAAM,gBAAY,+BAAiB,KAAK,MAAM,KAAK;AACnD,UAAM,mBAAe,kCAAoB,KAAK,MAAM,KAAK;AACzD,UAAM,iBAAa,gCAAkB,KAAK,MAAM,KAAK;AAErD,UAAM,cAAU;AAAA,UACd,4BAAe,KAAK,WAAW,KAAK,OAAO;AAAA,IAC7C;AAEA,UAAM,iBAAiB;AAAA,MACrB,iBAAa,8CAAgC,IAAI;AAAA,MACjD,QAAQ;AAAA,MACR,OAAO;AAAA,MACP;AAAA,MACA,QAAQ;AAAA,MACR,eAAe;AAAA,IACjB;AACA,SAAK,YAAY,IAAI,GAAG,cAAc;AACtC,SAAK,cAAc,OAAO,SAAS,cAAc;AAEjD,UAAM,kBAAc,+BAAa,8BAAc,IAAI,CAAC;AACpD,0BAAO,mBAAmB,SAAS,WAAW,KAAK,SAAS,KAAK;AAAA,MAC/D,OAAG,wCAA0B,MAAM,SAAS;AAAA,MAC5C,MAAM;AAAA,MACN,eAAe;AAAA,MACf,MAAM;AAAA,MACN,SAAS;AAAA,MACT,OAAO;AAAA,MACP,QAAQ;AAAA,MACR,eAAe;AAAA,MACf;AAAA,MACA;AAAA,IACF,CAAC;AAAA,EACH;AACF;AAEA,MAAM,iBAAiB,IAAI,eAAe;","names":[]}
@@ -0,0 +1,78 @@
1
+ import { ValueType } from "@opentelemetry/api";
2
+ import { hrTimeDuration, hrTimeToMilliseconds } from "@opentelemetry/core";
3
+ import { GENKIT_VERSION } from "genkit";
4
+ import { logger } from "genkit/logging";
5
+ import { toDisplayPath } from "genkit/tracing";
6
+ import {
7
+ MetricCounter,
8
+ MetricHistogram,
9
+ internalMetricNamespaceWrap
10
+ } from "../metrics.js";
11
+ import {
12
+ createCommonLogAttributes,
13
+ extractErrorMessage,
14
+ extractErrorName,
15
+ extractErrorStack,
16
+ extractOuterFeatureNameFromPath,
17
+ truncatePath
18
+ } from "../utils.js";
19
+ class PathsTelemetry {
20
+ /**
21
+ * Wraps the declared metrics in a Genkit-specific, internal namespace.
22
+ */
23
+ _N = internalMetricNamespaceWrap.bind(null, "feature");
24
+ pathCounter = new MetricCounter(this._N("path/requests"), {
25
+ description: "Tracks unique flow paths per flow.",
26
+ valueType: ValueType.INT
27
+ });
28
+ pathLatencies = new MetricHistogram(this._N("path/latency"), {
29
+ description: "Latencies per flow path.",
30
+ ValueType: ValueType.DOUBLE,
31
+ unit: "ms"
32
+ });
33
+ tick(span, logInputAndOutput, projectId) {
34
+ const attributes = span.attributes;
35
+ const path = attributes["genkit:path"];
36
+ const isFailureSource = !!span.attributes["genkit:isFailureSource"];
37
+ const state = attributes["genkit:state"];
38
+ if (!path || !isFailureSource || state !== "error") {
39
+ return;
40
+ }
41
+ const sessionId = attributes["genkit:sessionId"];
42
+ const threadName = attributes["genkit:threadName"];
43
+ const errorName = extractErrorName(span.events) || "<unknown>";
44
+ const errorMessage = extractErrorMessage(span.events) || "<unknown>";
45
+ const errorStack = extractErrorStack(span.events) || "";
46
+ const latency = hrTimeToMilliseconds(
47
+ hrTimeDuration(span.startTime, span.endTime)
48
+ );
49
+ const pathDimensions = {
50
+ featureName: extractOuterFeatureNameFromPath(path),
51
+ status: "failure",
52
+ error: errorName,
53
+ path,
54
+ source: "ts",
55
+ sourceVersion: GENKIT_VERSION
56
+ };
57
+ this.pathCounter.add(1, pathDimensions);
58
+ this.pathLatencies.record(latency, pathDimensions);
59
+ const displayPath = truncatePath(toDisplayPath(path));
60
+ logger.logStructuredError(`Error[${displayPath}, ${errorName}]`, {
61
+ ...createCommonLogAttributes(span, projectId),
62
+ path: displayPath,
63
+ qualifiedPath: path,
64
+ name: errorName,
65
+ message: errorMessage,
66
+ stack: errorStack,
67
+ source: "ts",
68
+ sourceVersion: GENKIT_VERSION,
69
+ sessionId,
70
+ threadName
71
+ });
72
+ }
73
+ }
74
+ const pathsTelemetry = new PathsTelemetry();
75
+ export {
76
+ pathsTelemetry
77
+ };
78
+ //# sourceMappingURL=path.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/telemetry/path.ts"],"sourcesContent":["/**\n * Copyright 2024 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ValueType } from '@opentelemetry/api';\nimport { hrTimeDuration, hrTimeToMilliseconds } from '@opentelemetry/core';\nimport type { ReadableSpan } from '@opentelemetry/sdk-trace-base';\nimport { GENKIT_VERSION } from 'genkit';\nimport { logger } from 'genkit/logging';\nimport { toDisplayPath } from 'genkit/tracing';\nimport {\n MetricCounter,\n MetricHistogram,\n internalMetricNamespaceWrap,\n type Telemetry,\n} from '../metrics.js';\nimport {\n createCommonLogAttributes,\n extractErrorMessage,\n extractErrorName,\n extractErrorStack,\n extractOuterFeatureNameFromPath,\n truncatePath,\n} from '../utils.js';\n\nclass PathsTelemetry implements Telemetry {\n /**\n * Wraps the declared metrics in a Genkit-specific, internal namespace.\n */\n private _N = internalMetricNamespaceWrap.bind(null, 'feature');\n\n private pathCounter = new MetricCounter(this._N('path/requests'), {\n description: 'Tracks unique flow paths per flow.',\n valueType: ValueType.INT,\n });\n\n private pathLatencies = new MetricHistogram(this._N('path/latency'), {\n description: 'Latencies per flow path.',\n ValueType: ValueType.DOUBLE,\n unit: 'ms',\n });\n\n tick(\n span: ReadableSpan,\n logInputAndOutput: boolean,\n projectId?: string\n ): void {\n const attributes = span.attributes;\n\n const path = attributes['genkit:path'] as string;\n\n const isFailureSource = !!span.attributes['genkit:isFailureSource'];\n const state = attributes['genkit:state'] as string;\n\n if (!path || !isFailureSource || state !== 'error') {\n // Only tick metrics for failing, leaf spans.\n return;\n }\n\n const sessionId = attributes['genkit:sessionId'] as string;\n const threadName = attributes['genkit:threadName'] as string;\n\n const errorName = extractErrorName(span.events) || '<unknown>';\n const errorMessage = extractErrorMessage(span.events) || '<unknown>';\n const errorStack = extractErrorStack(span.events) || '';\n\n const latency = hrTimeToMilliseconds(\n hrTimeDuration(span.startTime, span.endTime)\n );\n\n const pathDimensions = {\n featureName: extractOuterFeatureNameFromPath(path),\n status: 'failure',\n error: errorName,\n path: path,\n source: 'ts',\n sourceVersion: GENKIT_VERSION,\n };\n this.pathCounter.add(1, pathDimensions);\n this.pathLatencies.record(latency, pathDimensions);\n\n const displayPath = truncatePath(toDisplayPath(path));\n logger.logStructuredError(`Error[${displayPath}, ${errorName}]`, {\n ...createCommonLogAttributes(span, projectId),\n path: displayPath,\n qualifiedPath: path,\n name: errorName,\n message: errorMessage,\n stack: errorStack,\n source: 'ts',\n sourceVersion: GENKIT_VERSION,\n sessionId,\n threadName,\n });\n }\n}\n\nconst pathsTelemetry = new PathsTelemetry();\nexport { pathsTelemetry };\n"],"mappings":"AAgBA,SAAS,iBAAiB;AAC1B,SAAS,gBAAgB,4BAA4B;AAErD,SAAS,sBAAsB;AAC/B,SAAS,cAAc;AACvB,SAAS,qBAAqB;AAC9B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,MAAM,eAAoC;AAAA;AAAA;AAAA;AAAA,EAIhC,KAAK,4BAA4B,KAAK,MAAM,SAAS;AAAA,EAErD,cAAc,IAAI,cAAc,KAAK,GAAG,eAAe,GAAG;AAAA,IAChE,aAAa;AAAA,IACb,WAAW,UAAU;AAAA,EACvB,CAAC;AAAA,EAEO,gBAAgB,IAAI,gBAAgB,KAAK,GAAG,cAAc,GAAG;AAAA,IACnE,aAAa;AAAA,IACb,WAAW,UAAU;AAAA,IACrB,MAAM;AAAA,EACR,CAAC;AAAA,EAED,KACE,MACA,mBACA,WACM;AACN,UAAM,aAAa,KAAK;AAExB,UAAM,OAAO,WAAW,aAAa;AAErC,UAAM,kBAAkB,CAAC,CAAC,KAAK,WAAW,wBAAwB;AAClE,UAAM,QAAQ,WAAW,cAAc;AAEvC,QAAI,CAAC,QAAQ,CAAC,mBAAmB,UAAU,SAAS;AAElD;AAAA,IACF;AAEA,UAAM,YAAY,WAAW,kBAAkB;AAC/C,UAAM,aAAa,WAAW,mBAAmB;AAEjD,UAAM,YAA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