{"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,W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