Skip to content

Commit 82a35b5

Browse files
committed
feat(instrumentation-langchain): added langchain.js instrumentation package with implementation and tests. Current WIP as tests do not have adequate code coverage
1 parent 4723a55 commit 82a35b5

17 files changed

+10837
-824
lines changed

.github/component_owners.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ components:
6464
# Unmaintained
6565
packages/instrumentation-kafkajs:
6666
- seemk
67+
packages/instrumentation-langchain:
68+
- haneric00
69+
- yiyuan-he
6770
packages/instrumentation-lru-memoizer:
6871
- blumamir
6972
packages/instrumentation-mongoose:

package-lock.json

Lines changed: 9307 additions & 824 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# OpenTelemetry Spec Instrumentation for LangChain.js
2+
3+
4+
[component owners](https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/.github/component_owners.yml): @haneric00
5+
6+
7+
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.
8+
9+
10+
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.
11+
12+
This module is currently under active development and not ready for general use.
13+
14+
## Installation
15+
16+
```bash
17+
npm install --save @opentelemetry/instrumentation-langchain
18+
```
19+
20+
21+
## Usage
22+
23+
24+
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`.
25+
26+
```typescript
27+
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
28+
import { LangChainInstrumentation } from "@opentelemetry/instrumentation-langchain";
29+
import * as CallbackManagerModule from "@langchain/core/callbacks/manager";
30+
31+
const provider = new NodeTracerProvider();
32+
provider.register();
33+
34+
const lcInstrumentation = new LangChainInstrumentation();
35+
// LangChain must be manually instrumented as it doesn't have a traditional module structure
36+
lcInstrumentation.manuallyInstrument(CallbackManagerModule);
37+
```
38+
39+
For more information on OpenTelemetry Node.js SDK, see the [OpenTelemetry Node.js SDK documentation](https://opentelemetry.io/docs/instrumentation/js/getting-started/nodejs/).
40+
41+
42+
## Using a Custom Tracer Provider
43+
44+
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.
45+
46+
```typescript
47+
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
48+
import { Resource } from "@opentelemetry/resources";
49+
import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
50+
import { LangChainInstrumentation } from "@opentelemetry/instrumentation-langchain";
51+
import * as CallbackManagerModule from "@langchain/core/callbacks/manager";
52+
53+
// Create a custom tracer provider
54+
const customTracerProvider = new NodeTracerProvider({
55+
resource: new Resource({
56+
[ATTR_SERVICE_NAME]: "my-langchain-project",
57+
}),
58+
});
59+
60+
// Pass the custom tracer provider to the instrumentation
61+
const lcInstrumentation = new LangChainInstrumentation({
62+
tracerProvider: customTracerProvider,
63+
});
64+
65+
// Manually instrument the LangChain module
66+
lcInstrumentation.manuallyInstrument(CallbackManagerModule);
67+
```
68+
69+
Alternatively, you can set the tracer provider after creating the instrumentation:
70+
71+
```typescript
72+
const lcInstrumentation = new LangChainInstrumentation();
73+
lcInstrumentation.setTracerProvider(customTracerProvider);
74+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
transform: {
5+
'^.+\\.tsx?\$': 'ts-jest',
6+
},
7+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
8+
};
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"name": "instrumentation-langchain",
3+
"version": "1.0.0",
4+
"description": "Langchain.js instrumentation following OpenTelemetry semantic convention.",
5+
"main": "dist/src/index.js",
6+
"module": "dist/esm/index.js",
7+
"esnext": "dist/esnext/index.js",
8+
"types": "dist/src/index.d.ts",
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/open-telemetry/opentelemetry-js-contrib.git",
12+
"directory": "packages/instrumentation-langchain"
13+
},
14+
"author": "[email protected]",
15+
"license": "ISC",
16+
"type": "commonjs",
17+
"dependencies": {
18+
"@langchain/aws": "^0.1.14",
19+
"@opentelemetry/api": "^1.9.0",
20+
"@opentelemetry/core": "^1.25.1",
21+
"@opentelemetry/instrumentation": "^0.46.0"
22+
},
23+
"peerDependencies": {
24+
"@langchain/core": "^0.2.0 || ^0.3.0"
25+
},
26+
"devDependencies": {
27+
"@langchain/aws": "^0.1.14",
28+
"@langchain/community": "^0.3.53",
29+
"@langchain/core": "^0.3.13",
30+
"@langchain/coreV0.2": "npm:@langchain/core@^0.2.0",
31+
"@opentelemetry/exporter-trace-otlp-proto": "^0.50.0",
32+
"@opentelemetry/resources": "^1.25.1",
33+
"@opentelemetry/sdk-trace-base": "^1.25.1",
34+
"@opentelemetry/sdk-trace-node": "^1.25.1",
35+
"@opentelemetry/semantic-conventions": "^1.25.1",
36+
"@types/jest": "^29.5.12",
37+
"@types/node": "^20.14.11",
38+
"dotenv": "^16.4.5",
39+
"jest": "^29.7.0",
40+
"langchain": "^0.3.3",
41+
"rimraf": "^5.0.0",
42+
"tsc-alias": "^1.8.7",
43+
"typescript": "^5.0.0",
44+
"nyc": "17.1.0",
45+
"semver": "7.7.2",
46+
"sinon": "15.2.0",
47+
"test-all-versions": "6.1.0"
48+
},
49+
"scripts": {
50+
"build": "tsc --build tsconfig.json tsconfig.esm.json tsconfig.esnext.json && tsc-alias -p tsconfig.esm.json",
51+
"postbuild": "echo '{\"type\": \"module\"}' > ./dist/esm/package.json && rimraf dist/test",
52+
"type:check": "tsc --noEmit",
53+
"clean": "rimraf build/*",
54+
"compile": "tsc -p .",
55+
"compile:with-dependencies": "nx run-many -t compile -p @opentelemetry/instrumentation-koa",
56+
"lint": "eslint . --ext .ts",
57+
"lint:fix": "eslint . --ext .ts --fix",
58+
"lint:readme": "node ../../scripts/lint-readme.js",
59+
"prepublishOnly": "npm run compile",
60+
"tdd": "npm run test -- --watch-extensions ts --watch",
61+
"test": "nyc mocha 'test/**/*.ts'",
62+
"test-all-versions": "tav",
63+
"version:update": "node ../../scripts/version-update.js",
64+
"watch": "tsc -w"
65+
},
66+
"exports": {
67+
".": {
68+
"import": "./dist/esm/index.js",
69+
"require": "./dist/src/index.js",
70+
"types": "./dist/src/index.d.ts"
71+
}
72+
},
73+
"keywords": [],
74+
"files": [
75+
"dist",
76+
"src"
77+
]
78+
}

0 commit comments

Comments
 (0)