Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/component_owners.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ components:
# Unmaintained
packages/instrumentation-kafkajs:
- seemk
packages/instrumentation-langchain:
- haneric00
- yiyuan-he
packages/instrumentation-lru-memoizer:
- blumamir
packages/instrumentation-mongoose:
Expand Down
14,959 changes: 11,645 additions & 3,314 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/instrumentation-langchain/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
18 changes: 18 additions & 0 deletions packages/instrumentation-langchain/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// module.exports = {
// "env": {
// "mocha": true,
// "node": true
// },
// ...require('../../eslint.config.js')
// }


module.exports = {
"env": {
"mocha": true,
"node": true
},
"extends": "../../eslint.config.js",
"ignorePatterns": ["dist/", "*.d.ts"],
"root": true
};
74 changes: 74 additions & 0 deletions packages/instrumentation-langchain/README.md
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);
```
8 changes: 8 additions & 0 deletions packages/instrumentation-langchain/jest.config.js
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'],
};
79 changes: 79 additions & 0 deletions packages/instrumentation-langchain/package.json
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"
]
}
Loading