Skip to content

Commit

Permalink
add instrumentation for the openai package (#3155)
Browse files Browse the repository at this point in the history
- 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
tlhunter committed Jun 29, 2023
1 parent 4a4dd59 commit 8c7755c
Show file tree
Hide file tree
Showing 14 changed files with 2,924 additions and 4 deletions.
14 changes: 14 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,20 @@ declare namespace plugins {
};
}

/**
* This plugin automatically instruments the
* [openai](https://platform.openai.com/docs/api-reference?lang=node.js) module.
*
* Note that for logs to work you'll need to set the `DD_API_KEY` environment variable.
* You'll also need to adjust any firewall settings to allow the tracer to communicate
* with `http-intake.logs.datadoghq.com`.
*
* Note that for metrics to work you'll need to enable
* [DogStatsD](https://docs.datadoghq.com/developers/dogstatsd/?tab=hostagent#setup)
* in the agent.
*/
interface openai extends Instrumentation {}

/**
* This plugin automatically instruments the
* [opensearch](https://github.com/opensearch-project/opensearch-js) module.
Expand Down
1 change: 1 addition & 0 deletions packages/datadog-instrumentations/src/helpers/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ module.exports = {
'net': () => require('../net'),
'next': () => require('../next'),
'oracledb': () => require('../oracledb'),
'openai': () => require('../openai'),
'paperplane': () => require('../paperplane'),
'pg': () => require('../pg'),
'pino': () => require('../pino'),
Expand Down
50 changes: 50 additions & 0 deletions packages/datadog-instrumentations/src/openai.js
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
})
Loading

0 comments on commit 8c7755c

Please sign in to comment.