{"version":3,"file":"instrumentation.js","sources":["../../../../../src/integrations/tracing/langgraph/instrumentation.ts"],"sourcesContent":["import {\n InstrumentationBase,\n type InstrumentationConfig,\n type InstrumentationModuleDefinition,\n InstrumentationNodeModuleDefinition,\n} from '@opentelemetry/instrumentation';\nimport { InstrumentationNodeModuleFile } from '../InstrumentationNodeModuleFile';\nimport type { CompiledGraph, LangGraphOptions } from '@sentry/core';\nimport { getClient, instrumentCreateReactAgent, instrumentLangGraph, SDK_VERSION } from '@sentry/core';\n\nconst supportedVersions = ['>=0.0.0 <2.0.0'];\n\ntype LangGraphInstrumentationOptions = InstrumentationConfig & LangGraphOptions;\n\n/**\n * Represents the patched shape of the LangGraph module export.\n */\ninterface PatchedModuleExports {\n [key: string]: unknown;\n StateGraph?: abstract new (...args: unknown[]) => unknown;\n createReactAgent?: (...args: unknown[]) => CompiledGraph;\n}\n\n/**\n * Sentry LangGraph instrumentation using OpenTelemetry.\n */\nexport class SentryLangGraphInstrumentation extends InstrumentationBase {\n public constructor(config: LangGraphInstrumentationOptions = {}) {\n super('@sentry/instrumentation-langgraph', SDK_VERSION, config);\n }\n\n /**\n * Initializes the instrumentation by defining the modules to be patched.\n */\n public init(): InstrumentationModuleDefinition[] {\n return [\n new InstrumentationNodeModuleDefinition(\n '@langchain/langgraph',\n supportedVersions,\n this._patch.bind(this),\n exports => exports,\n [\n new InstrumentationNodeModuleFile(\n /**\n * In CJS, LangGraph packages re-export from dist/index.cjs files.\n * Patching only the root module sometimes misses the real implementation or\n * gets overwritten when that file is loaded. We add a file-level patch so that\n * _patch runs again on the concrete implementation\n */\n '@langchain/langgraph/dist/index.cjs',\n supportedVersions,\n this._patch.bind(this),\n exports => exports,\n ),\n new InstrumentationNodeModuleFile(\n /**\n * In CJS, the prebuilt submodule re-exports from dist/prebuilt/index.cjs.\n * We add a file-level patch under the main module so that CJS require()\n * of @langchain/langgraph/prebuilt gets patched.\n */\n '@langchain/langgraph/dist/prebuilt/index.cjs',\n supportedVersions,\n this._patch.bind(this),\n exports => exports,\n ),\n ],\n ),\n new InstrumentationNodeModuleDefinition(\n '@langchain/langgraph/prebuilt',\n supportedVersions,\n this._patch.bind(this),\n exports => exports,\n [\n new InstrumentationNodeModuleFile(\n /**\n * In CJS, the prebuilt submodule re-exports from dist/prebuilt/index.cjs.\n * We add file-level patches so _patch runs on the concrete implementation.\n */\n '@langchain/langgraph/dist/prebuilt/index.cjs',\n supportedVersions,\n this._patch.bind(this),\n exports => exports,\n ),\n ],\n ),\n ];\n }\n\n /**\n * Core patch logic applying instrumentation to the LangGraph module.\n */\n private _patch(exports: PatchedModuleExports): PatchedModuleExports | void {\n const client = getClient();\n const genAI = client?.getDataCollectionOptions().genAI;\n const options = {\n ...this.getConfig(),\n recordInputs: this.getConfig().recordInputs ?? genAI?.inputs ?? false,\n recordOutputs: this.getConfig().recordOutputs ?? genAI?.outputs ?? false,\n };\n\n // Patch StateGraph.compile to instrument both compile() and invoke()\n if (exports.StateGraph && typeof exports.StateGraph === 'function') {\n instrumentLangGraph(exports.StateGraph.prototype as { compile: (...args: unknown[]) => unknown }, options);\n }\n\n // Patch createReactAgent to instrument agent creation and invocation\n if (exports.createReactAgent && typeof exports.createReactAgent === 'function') {\n const originalCreateReactAgent = exports.createReactAgent;\n Object.defineProperty(exports, 'createReactAgent', {\n value: instrumentCreateReactAgent(originalCreateReactAgent as (...args: unknown[]) => CompiledGraph, options),\n writable: true,\n enumerable: true,\n configurable: true,\n });\n }\n\n return exports;\n }\n}\n"],"names":["InstrumentationBase","SDK_VERSION","InstrumentationNodeModuleDefinition","InstrumentationNodeModuleFile","getClient","instrumentLangGraph","instrumentCreateReactAgent"],"mappings":";;;;;;AAUA,MAAM,iBAAA,GAAoB,CAAC,gBAAgB,CAAA;AAgBpC,MAAM,uCAAuCA,mCAAA,CAAqD;AAAA,EAChG,WAAA,CAAY,MAAA,GAA0C,EAAC,EAAG;AAC/D,IAAA,KAAA,CAAM,mCAAA,EAAqCC,kBAAa,MAAM,CAA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