-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into juan-fernandez/fix-import-path-worker
- Loading branch information
Showing
28 changed files
with
1,955 additions
and
106 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
|
@@ -338,6 +338,8 @@ for (const shim of V4_PACKAGE_SHIMS) { | |
}) | ||
}) | ||
|
||
ch.end.publish(ctx) | ||
|
||
return apiProm | ||
}) | ||
}) | ||
|
This file contains 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 |
---|---|---|
@@ -1,89 +1,21 @@ | ||
'use strict' | ||
|
||
const { MEASURED } = require('../../../ext/tags') | ||
const { storage } = require('../../datadog-core') | ||
const TracingPlugin = require('../../dd-trace/src/plugins/tracing') | ||
const LangChainTracingPlugin = require('./tracing') | ||
const LangChainLLMObsPlugin = require('../../dd-trace/src/llmobs/plugins/langchain') | ||
const CompositePlugin = require('../../dd-trace/src/plugins/composite') | ||
|
||
const API_KEY = 'langchain.request.api_key' | ||
const MODEL = 'langchain.request.model' | ||
const PROVIDER = 'langchain.request.provider' | ||
const TYPE = 'langchain.request.type' | ||
|
||
const LangChainHandler = require('./handlers/default') | ||
const LangChainChatModelHandler = require('./handlers/language_models/chat_model') | ||
const LangChainLLMHandler = require('./handlers/language_models/llm') | ||
const LangChainChainHandler = require('./handlers/chain') | ||
const LangChainEmbeddingHandler = require('./handlers/embedding') | ||
|
||
class LangChainPlugin extends TracingPlugin { | ||
class LangChainPlugin extends CompositePlugin { | ||
static get id () { return 'langchain' } | ||
static get operation () { return 'invoke' } | ||
static get system () { return 'langchain' } | ||
static get prefix () { | ||
return 'tracing:apm:langchain:invoke' | ||
} | ||
|
||
constructor () { | ||
super(...arguments) | ||
|
||
const langchainConfig = this._tracerConfig.langchain || {} | ||
this.handlers = { | ||
chain: new LangChainChainHandler(langchainConfig), | ||
chat_model: new LangChainChatModelHandler(langchainConfig), | ||
llm: new LangChainLLMHandler(langchainConfig), | ||
embedding: new LangChainEmbeddingHandler(langchainConfig), | ||
default: new LangChainHandler(langchainConfig) | ||
static get plugins () { | ||
return { | ||
// ordering here is important - the llm observability plugin must come first | ||
// so that we can add annotations associated with the span before it finishes. | ||
// however, because the tracing plugin uses `bindStart` vs the llmobs' `start`, | ||
// the span is guaranteed to be created in the tracing plugin before the llmobs one is called | ||
llmobs: LangChainLLMObsPlugin, | ||
tracing: LangChainTracingPlugin | ||
} | ||
} | ||
|
||
bindStart (ctx) { | ||
const { resource, type } = ctx | ||
const handler = this.handlers[type] | ||
|
||
const instance = ctx.instance | ||
const apiKey = handler.extractApiKey(instance) | ||
const provider = handler.extractProvider(instance) | ||
const model = handler.extractModel(instance) | ||
|
||
const tags = handler.getSpanStartTags(ctx, provider) || [] | ||
|
||
if (apiKey) tags[API_KEY] = apiKey | ||
if (provider) tags[PROVIDER] = provider | ||
if (model) tags[MODEL] = model | ||
if (type) tags[TYPE] = type | ||
|
||
const span = this.startSpan('langchain.request', { | ||
service: this.config.service, | ||
resource, | ||
kind: 'client', | ||
meta: { | ||
[MEASURED]: 1, | ||
...tags | ||
} | ||
}, false) | ||
|
||
const store = storage.getStore() || {} | ||
ctx.currentStore = { ...store, span } | ||
|
||
return ctx.currentStore | ||
} | ||
|
||
asyncEnd (ctx) { | ||
const span = ctx.currentStore.span | ||
|
||
const { type } = ctx | ||
|
||
const handler = this.handlers[type] | ||
const tags = handler.getSpanEndTags(ctx) || {} | ||
|
||
span.addTags(tags) | ||
|
||
span.finish() | ||
} | ||
|
||
getHandler (type) { | ||
return this.handlers[type] || this.handlers.default | ||
} | ||
} | ||
|
||
module.exports = LangChainPlugin |
This file contains 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,89 @@ | ||
'use strict' | ||
|
||
const { MEASURED } = require('../../../ext/tags') | ||
const { storage } = require('../../datadog-core') | ||
const TracingPlugin = require('../../dd-trace/src/plugins/tracing') | ||
|
||
const API_KEY = 'langchain.request.api_key' | ||
const MODEL = 'langchain.request.model' | ||
const PROVIDER = 'langchain.request.provider' | ||
const TYPE = 'langchain.request.type' | ||
|
||
const LangChainHandler = require('./handlers/default') | ||
const LangChainChatModelHandler = require('./handlers/language_models/chat_model') | ||
const LangChainLLMHandler = require('./handlers/language_models/llm') | ||
const LangChainChainHandler = require('./handlers/chain') | ||
const LangChainEmbeddingHandler = require('./handlers/embedding') | ||
|
||
class LangChainTracingPlugin extends TracingPlugin { | ||
static get id () { return 'langchain' } | ||
static get operation () { return 'invoke' } | ||
static get system () { return 'langchain' } | ||
static get prefix () { | ||
return 'tracing:apm:langchain:invoke' | ||
} | ||
|
||
constructor () { | ||
super(...arguments) | ||
|
||
const langchainConfig = this._tracerConfig.langchain || {} | ||
this.handlers = { | ||
chain: new LangChainChainHandler(langchainConfig), | ||
chat_model: new LangChainChatModelHandler(langchainConfig), | ||
llm: new LangChainLLMHandler(langchainConfig), | ||
embedding: new LangChainEmbeddingHandler(langchainConfig), | ||
default: new LangChainHandler(langchainConfig) | ||
} | ||
} | ||
|
||
bindStart (ctx) { | ||
const { resource, type } = ctx | ||
const handler = this.handlers[type] | ||
|
||
const instance = ctx.instance | ||
const apiKey = handler.extractApiKey(instance) | ||
const provider = handler.extractProvider(instance) | ||
const model = handler.extractModel(instance) | ||
|
||
const tags = handler.getSpanStartTags(ctx, provider) || [] | ||
|
||
if (apiKey) tags[API_KEY] = apiKey | ||
if (provider) tags[PROVIDER] = provider | ||
if (model) tags[MODEL] = model | ||
if (type) tags[TYPE] = type | ||
|
||
const span = this.startSpan('langchain.request', { | ||
service: this.config.service, | ||
resource, | ||
kind: 'client', | ||
meta: { | ||
[MEASURED]: 1, | ||
...tags | ||
} | ||
}, false) | ||
|
||
const store = storage.getStore() || {} | ||
ctx.currentStore = { ...store, span } | ||
|
||
return ctx.currentStore | ||
} | ||
|
||
asyncEnd (ctx) { | ||
const span = ctx.currentStore.span | ||
|
||
const { type } = ctx | ||
|
||
const handler = this.handlers[type] | ||
const tags = handler.getSpanEndTags(ctx) || {} | ||
|
||
span.addTags(tags) | ||
|
||
span.finish() | ||
} | ||
|
||
getHandler (type) { | ||
return this.handlers[type] || this.handlers.default | ||
} | ||
} | ||
|
||
module.exports = LangChainTracingPlugin |
This file contains 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 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
24 changes: 24 additions & 0 deletions
24
packages/dd-trace/src/llmobs/plugins/langchain/handlers/chain.js
This file contains 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,24 @@ | ||
'use strict' | ||
|
||
const LangChainLLMObsHandler = require('.') | ||
const { spanHasError } = require('../../../util') | ||
|
||
class LangChainLLMObsChainHandler extends LangChainLLMObsHandler { | ||
setMetaTags ({ span, inputs, results }) { | ||
let input, output | ||
if (inputs) { | ||
input = this.formatIO(inputs) | ||
} | ||
|
||
if (!results || spanHasError(span)) { | ||
output = '' | ||
} else { | ||
output = this.formatIO(results) | ||
} | ||
|
||
// chain spans will always be workflows | ||
this._tagger.tagTextIO(span, input, output) | ||
} | ||
} | ||
|
||
module.exports = LangChainLLMObsChainHandler |
Oops, something went wrong.