-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add instrumentation for the openai package (#3155)
- adds support for the OpenAI package - includes many things that aren't typical with our other integrations - also sends metrics, even when runtime metrics are disabled for the tracer - also sends logs directly to the datadog backend, bypassing the agent - overall this is the most intricate of any integration we've had so far
- Loading branch information
Showing
14 changed files
with
2,924 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict' | ||
|
||
const { | ||
channel, | ||
addHook | ||
} = require('./helpers/instrument') | ||
const shimmer = require('../../datadog-shimmer') | ||
|
||
const startCh = channel('apm:openai:request:start') | ||
const finishCh = channel('apm:openai:request:finish') | ||
const errorCh = channel('apm:openai:request:error') | ||
|
||
addHook({ name: 'openai', file: 'dist/api.js', versions: ['>=3.0.0'] }, exports => { | ||
const methodNames = Object.getOwnPropertyNames(exports.OpenAIApi.prototype) | ||
methodNames.shift() // remove leading 'constructor' method | ||
|
||
for (const methodName of methodNames) { | ||
shimmer.wrap(exports.OpenAIApi.prototype, methodName, fn => function () { | ||
if (!startCh.hasSubscribers) { | ||
return fn.apply(this, arguments) | ||
} | ||
|
||
startCh.publish({ | ||
methodName, | ||
args: arguments, | ||
basePath: this.basePath, | ||
apiKey: this.configuration.apiKey | ||
}) | ||
|
||
return fn.apply(this, arguments) | ||
.then((response) => { | ||
finishCh.publish({ | ||
headers: response.headers, | ||
body: response.data, | ||
path: response.request.path, | ||
method: response.request.method | ||
}) | ||
|
||
return response | ||
}) | ||
.catch((err) => { | ||
errorCh.publish({ err }) | ||
|
||
throw err | ||
}) | ||
}) | ||
} | ||
|
||
return exports | ||
}) |
Oops, something went wrong.