-
Notifications
You must be signed in to change notification settings - Fork 603
feat(instrumentation-langchain): Add LangChain.js instrumentation skeleton #3024
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
haneric00
wants to merge
1
commit into
open-telemetry:main
Choose a base branch
from
haneric00:skeleton-pr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module.exports = { | ||
"env": { | ||
"mocha": true, | ||
"node": true | ||
}, | ||
"extends": "../../eslint.config.js", | ||
"ignorePatterns": ["dist/", "*.d.ts"], | ||
"root": true | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# OpenTelemetry Spec Instrumentation for LangChain.js | ||
|
||
|
||
[component owners](https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/.github/component_owners.yml): @haneric00 | ||
|
||
|
||
This module provides automatic instrumentation for the [`AWS Lambda`](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html) module, which may be loaded using the [`@opentelemetry/sdk-trace-node`](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-sdk-trace-node) package and is included in the [`@opentelemetry/auto-instrumentations-node`](https://www.npmjs.com/package/@opentelemetry/auto-instrumentations-node) bundle. | ||
|
||
|
||
This module provides automatic instrumentation for the ['langchain.js'] (https://v02.api.js.langchain.com/modules/langchain.html) module, which may be loaded using the [`@langchain/...`] packages. | ||
|
||
This module is currently under active development and not ready for general use. | ||
|
||
## Installation | ||
|
||
```bash | ||
npm install --save @opentelemetry/instrumentation-langchain | ||
``` | ||
|
||
|
||
## Usage | ||
|
||
|
||
To load the Langchain instrumentation, manually instrument the `@langchain/core/callbacks/manager` module. The callbacks manager must be manually instrumented due to the non-traditional module structure in `@langchain/core`. | ||
|
||
```typescript | ||
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node"; | ||
import { LangChainInstrumentation } from "@opentelemetry/instrumentation-langchain"; | ||
import * as CallbackManagerModule from "@langchain/core/callbacks/manager"; | ||
|
||
const provider = new NodeTracerProvider(); | ||
provider.register(); | ||
|
||
const lcInstrumentation = new LangChainInstrumentation(); | ||
// LangChain must be manually instrumented as it doesn't have a traditional module structure | ||
lcInstrumentation.manuallyInstrument(CallbackManagerModule); | ||
``` | ||
|
||
For more information on OpenTelemetry Node.js SDK, see the [OpenTelemetry Node.js SDK documentation](https://opentelemetry.io/docs/instrumentation/js/getting-started/nodejs/). | ||
|
||
|
||
## Using a Custom Tracer Provider | ||
|
||
You can specify a custom tracer provider when creating the LangChain instrumentation. This is useful when you want to use a non-global tracer provider or have more control over the tracing configuration. | ||
|
||
```typescript | ||
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node"; | ||
import { Resource } from "@opentelemetry/resources"; | ||
import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions'; | ||
import { LangChainInstrumentation } from "@opentelemetry/instrumentation-langchain"; | ||
import * as CallbackManagerModule from "@langchain/core/callbacks/manager"; | ||
|
||
// Create a custom tracer provider | ||
const customTracerProvider = new NodeTracerProvider({ | ||
resource: new Resource({ | ||
[ATTR_SERVICE_NAME]: "my-langchain-project", | ||
}), | ||
}); | ||
|
||
// Pass the custom tracer provider to the instrumentation | ||
const lcInstrumentation = new LangChainInstrumentation({ | ||
tracerProvider: customTracerProvider, | ||
}); | ||
|
||
// Manually instrument the LangChain module | ||
lcInstrumentation.manuallyInstrument(CallbackManagerModule); | ||
``` | ||
|
||
Alternatively, you can set the tracer provider after creating the instrumentation: | ||
|
||
```typescript | ||
const lcInstrumentation = new LangChainInstrumentation(); | ||
lcInstrumentation.setTracerProvider(customTracerProvider); | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
transform: { | ||
'^.+\\.tsx?\$': 'ts-jest', | ||
}, | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
{ | ||
"name": "@opentelemetry/instrumentation-langchain", | ||
"private": true, | ||
"version": "1.0.0", | ||
"description": "Langchain.js instrumentation following OpenTelemetry semantic convention.", | ||
"main": "dist/src/index.js", | ||
"module": "dist/esm/index.js", | ||
"esnext": "dist/esnext/index.js", | ||
"types": "dist/src/index.d.ts", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/open-telemetry/opentelemetry-js-contrib.git", | ||
"directory": "packages/instrumentation-langchain" | ||
}, | ||
"author": "[email protected]", | ||
"license": "ISC", | ||
"type": "commonjs", | ||
"dependencies": { | ||
"@langchain/aws": "^0.1.14", | ||
"@opentelemetry/api": "^1.9.0", | ||
"@opentelemetry/core": "^1.25.1", | ||
"@opentelemetry/instrumentation": "^0.46.0" | ||
}, | ||
"peerDependencies": { | ||
"@langchain/core": "^0.2.0 || ^0.3.0" | ||
}, | ||
"devDependencies": { | ||
"@langchain/aws": "^0.1.14", | ||
"@langchain/community": "^0.3.53", | ||
"@langchain/core": "^0.3.13", | ||
"@langchain/coreV0.2": "npm:@langchain/core@^0.2.0", | ||
"@opentelemetry/exporter-trace-otlp-proto": "^0.50.0", | ||
"@opentelemetry/resources": "^1.25.1", | ||
"@opentelemetry/sdk-trace-base": "^1.25.1", | ||
"@opentelemetry/sdk-trace-node": "^1.25.1", | ||
"@opentelemetry/semantic-conventions": "^1.25.1", | ||
"@types/jest": "^29.5.12", | ||
"@types/node": "^20.14.11", | ||
"dotenv": "^16.4.5", | ||
"jest": "^29.7.0", | ||
"langchain": "^0.3.3", | ||
"rimraf": "^5.0.0", | ||
"tsc-alias": "^1.8.7", | ||
"typescript": "^5.0.0", | ||
"nyc": "17.1.0", | ||
"semver": "7.7.2", | ||
"sinon": "15.2.0", | ||
"test-all-versions": "6.1.0" | ||
}, | ||
"scripts": { | ||
"build": "tsc --build tsconfig.json tsconfig.esm.json tsconfig.esnext.json && tsc-alias -p tsconfig.esm.json", | ||
"postbuild": "echo '{\"type\": \"module\"}' > ./dist/esm/package.json && rimraf dist/test", | ||
"type:check": "tsc --noEmit", | ||
"clean": "rimraf build/*", | ||
"compile": "tsc -p .", | ||
"compile:with-dependencies": "nx run-many -t compile -p @opentelemetry/instrumentation-langchain", | ||
"lint": "eslint . --ext .ts", | ||
"lint:fix": "eslint . --ext .ts --fix", | ||
"lint:readme": "node ../../scripts/lint-readme.js", | ||
"prepublishOnly": "npm run compile", | ||
"tdd": "npm run test -- --watch-extensions ts --watch", | ||
"test": "nyc mocha 'test/**/*.ts'", | ||
"test-all-versions": "tav", | ||
"version:update": "node ../../scripts/version-update.js", | ||
"watch": "tsc -w" | ||
}, | ||
"exports": { | ||
".": { | ||
"import": "./dist/esm/index.js", | ||
"require": "./dist/src/index.js", | ||
"types": "./dist/src/index.d.ts" | ||
} | ||
}, | ||
"keywords": [], | ||
"files": [ | ||
"dist", | ||
"src" | ||
] | ||
} |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export * from './instrumentation'; |
15 changes: 15 additions & 0 deletions
15
packages/instrumentation-langchain/src/instrumentation-utils.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export class LangChainInstrumentation { | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ |
15 changes: 15 additions & 0 deletions
15
packages/instrumentation-langchain/tests/callback-handler.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"module": "ES2020", | ||
"moduleResolution": "node", | ||
"outDir": "dist/esm", | ||
"rootDir": "src", | ||
"tsBuildInfoFile": "dist/esm/tsconfig.esm.tsbuildinfo", | ||
|
||
"strict": false, | ||
"esModuleInterop": true, | ||
"skipLibCheck": true, | ||
"forceConsistentCasingInFileNames": false, | ||
"declaration": true, | ||
"sourceMap": true, | ||
"composite": true, | ||
"incremental": true, | ||
|
||
"baseUrl": ".", | ||
|
||
"paths": { | ||
"langchainV0.1/*": ["node_modules/langchainV0.1/*"], | ||
"@langchain/coreV0.1/*": ["node_modules/@langchain/coreV0.1/*"], | ||
"@langchain/aws": ["node_modules/@langchain/aws"] | ||
} | ||
}, | ||
"include": ["src/**/*.ts"], | ||
"references": [], | ||
|
||
"tsc-alias": { | ||
"verbose": false, | ||
"resolveFullPaths": true | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"compilerOptions": { | ||
"outDir": "dist/esnext", | ||
"rootDir": "src", | ||
"tsBuildInfoFile": "dist/esnext/tsconfig.esnext.tsbuildinfo", | ||
|
||
"module": "esnext", | ||
"target": "es2017", | ||
"moduleResolution": "node", | ||
|
||
"strict": false, | ||
"esModuleInterop": true, | ||
"skipLibCheck": true, | ||
"forceConsistentCasingInFileNames": false, | ||
"declaration": true, | ||
"sourceMap": true, | ||
"composite": true, | ||
"incremental": true, | ||
|
||
"baseUrl": ".", | ||
|
||
"paths": { | ||
"langchainV0.1/*": ["node_modules/langchainV0.1/*"], | ||
"@langchain/coreV0.1/*": ["node_modules/@langchain/coreV0.1/*"], | ||
"@langchain/aws": ["node_modules/@langchain/aws"] | ||
} | ||
}, | ||
"include": ["src/**/*.ts"], | ||
"references": [] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"compilerOptions": { | ||
"incremental": true, | ||
"composite": true, | ||
"outDir": "dist", | ||
"rootDir": ".", | ||
|
||
"target": "es2016", | ||
"lib": ["esnext"], | ||
"module": "Node16", | ||
"moduleResolution": "node16", | ||
"baseUrl": ".", | ||
|
||
"strict": false, | ||
"skipLibCheck": true, | ||
"forceConsistentCasingInFileNames": false, | ||
|
||
"declaration": true, | ||
"declarationMap": true, | ||
"sourceMap": true, | ||
"esModuleInterop": true, | ||
|
||
"paths": { | ||
"langchainV0.1/*": ["node_modules/langchainV0.1/*"], | ||
"@langchain/coreV0.1/*": ["node_modules/@langchain/coreV0.1/*"], | ||
"@langchain/aws": ["node_modules/@langchain/aws"] | ||
}, | ||
"types": ["node", "jest"] | ||
}, | ||
"include": ["src/**/*.ts", | ||
"test/**/*.ts" | ||
] | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider also adding @mxiamxia as codeowner.