Skip to content

Commit

Permalink
Merge branch 'master' into juan-fernandez/remove-manual-api-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
juan-fernandez committed Sep 20, 2024
2 parents 4b5e2ab + faa76cf commit 3605665
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 38 deletions.
2 changes: 1 addition & 1 deletion integration-tests/cucumber/cucumber.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ versions.forEach(version => {
env: {
...envVars,
DD_TAGS: 'test.customtag:customvalue,test.customtag2:customvalue2',
DD_SESSION_NAME: 'my-test-session'
DD_TEST_SESSION_NAME: 'my-test-session'
},
stdio: 'pipe'
}
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/cypress/cypress.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ moduleTypes.forEach(({
...restEnvVars,
CYPRESS_BASE_URL: `http://localhost:${webAppPort}`,
DD_TAGS: 'test.customtag:customvalue,test.customtag2:customvalue2',
DD_SESSION_NAME: 'my-test-session'
DD_TEST_SESSION_NAME: 'my-test-session'
},
stdio: 'pipe'
}
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/jest/jest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ describe('jest CommonJS', () => {
env: {
...envVars,
DD_TAGS: 'test.customtag:customvalue,test.customtag2:customvalue2',
DD_SESSION_NAME: 'my-test-session'
DD_TEST_SESSION_NAME: 'my-test-session'
},
stdio: 'pipe'
})
Expand Down Expand Up @@ -444,7 +444,7 @@ describe('jest CommonJS', () => {
env: {
...getCiVisAgentlessConfig(receiver.port),
RUN_IN_PARALLEL: true,
DD_SESSION_NAME: 'my-test-session'
DD_TEST_SESSION_NAME: 'my-test-session'
},
stdio: 'pipe'
})
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/mocha/mocha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ describe('mocha CommonJS', function () {
env: {
...envVars,
DD_TAGS: 'test.customtag:customvalue,test.customtag2:customvalue2',
DD_SESSION_NAME: 'my-test-session'
DD_TEST_SESSION_NAME: 'my-test-session'
},
stdio: 'pipe'
})
Expand Down Expand Up @@ -378,7 +378,7 @@ describe('mocha CommonJS', function () {
RUN_IN_PARALLEL: true,
DD_TRACE_DEBUG: 1,
DD_TRACE_LOG_LEVEL: 'warn',
DD_SESSION_NAME: 'my-test-session'
DD_TEST_SESSION_NAME: 'my-test-session'
},
stdio: 'pipe'
})
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/playwright/playwright.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ versions.forEach((version) => {
...envVars,
PW_BASE_URL: `http://localhost:${webAppPort}`,
DD_TAGS: 'test.customtag:customvalue,test.customtag2:customvalue2',
DD_SESSION_NAME: 'my-test-session'
DD_TEST_SESSION_NAME: 'my-test-session'
},
stdio: 'pipe'
}
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/vitest/vitest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ versions.forEach((version) => {
env: {
...getCiVisAgentlessConfig(receiver.port),
NODE_OPTIONS: '--import dd-trace/register.js -r dd-trace/ci/init', // ESM requires more flags
DD_SESSION_NAME: 'my-test-session'
DD_TEST_SESSION_NAME: 'my-test-session'
},
stdio: 'pipe'
}
Expand Down
45 changes: 27 additions & 18 deletions packages/datadog-plugin-openai/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,25 +276,34 @@ class OpenApiPlugin extends TracingPlugin {
const completionTokens = spanTags['openai.response.usage.completion_tokens']
const completionTokensEstimated = spanTags['openai.response.usage.completion_tokens_estimated']

const totalTokens = spanTags['openai.response.usage.total_tokens']

if (!error) {
if (promptTokensEstimated) {
this.metrics.distribution(
'openai.tokens.prompt', promptTokens, [...tags, 'openai.estimated:true'])
} else {
this.metrics.distribution('openai.tokens.prompt', promptTokens, tags)
if (promptTokens != null) {
if (promptTokensEstimated) {
this.metrics.distribution(
'openai.tokens.prompt', promptTokens, [...tags, 'openai.estimated:true'])
} else {
this.metrics.distribution('openai.tokens.prompt', promptTokens, tags)
}
}
if (completionTokensEstimated) {
this.metrics.distribution(
'openai.tokens.completion', completionTokens, [...tags, 'openai.estimated:true'])
} else {
this.metrics.distribution('openai.tokens.completion', completionTokens, tags)

if (completionTokens != null) {
if (completionTokensEstimated) {
this.metrics.distribution(
'openai.tokens.completion', completionTokens, [...tags, 'openai.estimated:true'])
} else {
this.metrics.distribution('openai.tokens.completion', completionTokens, tags)
}
}

if (promptTokensEstimated || completionTokensEstimated) {
this.metrics.distribution(
'openai.tokens.total', promptTokens + completionTokens, [...tags, 'openai.estimated:true'])
} else {
this.metrics.distribution('openai.tokens.total', promptTokens + completionTokens, tags)
if (totalTokens != null) {
if (promptTokensEstimated || completionTokensEstimated) {
this.metrics.distribution(
'openai.tokens.total', totalTokens, [...tags, 'openai.estimated:true'])
} else {
this.metrics.distribution('openai.tokens.total', totalTokens, tags)
}
}
}

Expand Down Expand Up @@ -777,9 +786,9 @@ function usageExtraction (tags, body, methodName, openaiStore) {
if (completionEstimated) tags['openai.response.usage.completion_tokens_estimated'] = true
}

if (promptTokens) tags['openai.response.usage.prompt_tokens'] = promptTokens
if (completionTokens) tags['openai.response.usage.completion_tokens'] = completionTokens
if (totalTokens) tags['openai.response.usage.total_tokens'] = totalTokens
if (promptTokens != null) tags['openai.response.usage.prompt_tokens'] = promptTokens
if (completionTokens != null) tags['openai.response.usage.completion_tokens'] = completionTokens
if (totalTokens != null) tags['openai.response.usage.total_tokens'] = totalTokens
}

function truncateApiKey (apiKey) {
Expand Down
51 changes: 51 additions & 0 deletions packages/datadog-plugin-openai/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,57 @@ describe('Plugin', () => {
})
})

describe('embedding with missing usages', () => {
afterEach(() => {
nock.cleanAll()
})

it('makes a successful call', async () => {
nock('https://api.openai.com:443')
.post('/v1/embeddings')
.reply(200, {
object: 'list',
data: [{
object: 'embedding',
index: 0,
embedding: [-0.0034387498, -0.026400521]
}],
model: 'text-embedding-ada-002-v2',
usage: {
prompt_tokens: 0
}
}, [])

const checkTraces = agent
.use(traces => {
expect(traces[0][0].metrics).to.have.property('openai.response.usage.prompt_tokens', 0)
expect(traces[0][0].metrics).to.not.have.property('openai.response.usage.completion_tokens')
expect(traces[0][0].metrics).to.not.have.property('openai.response.usage.total_tokens')
})

const params = {
model: 'text-embedding-ada-002',
input: '',
user: 'hunter2'
}

if (semver.satisfies(realVersion, '>=4.0.0')) {
const result = await openai.embeddings.create(params)
expect(result.model).to.eql('text-embedding-ada-002-v2')
} else {
const result = await openai.createEmbedding(params)
expect(result.data.model).to.eql('text-embedding-ada-002-v2')
}

await checkTraces

expect(metricStub).to.have.been.calledWith('openai.request.duration') // timing value not guaranteed
expect(metricStub).to.have.been.calledWith('openai.tokens.prompt')
expect(metricStub).to.not.have.been.calledWith('openai.tokens.completion')
expect(metricStub).to.not.have.been.calledWith('openai.tokens.total')
})
})

describe('list models', () => {
let scope

Expand Down
6 changes: 3 additions & 3 deletions packages/dd-trace/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ class Config {
this._setValue(defaults, 'isGitUploadEnabled', false)
this._setValue(defaults, 'isIntelligentTestRunnerEnabled', false)
this._setValue(defaults, 'isManualApiEnabled', false)
this._setValue(defaults, 'ciVisibilitySessionName', '')
this._setValue(defaults, 'ciVisibilityTestSessionName', '')
this._setValue(defaults, 'logInjection', false)
this._setValue(defaults, 'lookup', undefined)
this._setValue(defaults, 'memcachedCommandEnabled', false)
Expand Down Expand Up @@ -1035,7 +1035,7 @@ class Config {
DD_CIVISIBILITY_EARLY_FLAKE_DETECTION_ENABLED,
DD_CIVISIBILITY_FLAKY_RETRY_ENABLED,
DD_CIVISIBILITY_FLAKY_RETRY_COUNT,
DD_SESSION_NAME
DD_TEST_SESSION_NAME
} = process.env

if (DD_CIVISIBILITY_AGENTLESS_URL) {
Expand All @@ -1051,7 +1051,7 @@ class Config {
this._setValue(calc, 'flakyTestRetriesCount', coalesce(maybeInt(DD_CIVISIBILITY_FLAKY_RETRY_COUNT), 5))
this._setBoolean(calc, 'isIntelligentTestRunnerEnabled', isTrue(this._isCiVisibilityItrEnabled()))
this._setBoolean(calc, 'isManualApiEnabled', !isFalse(this._isCiVisibilityManualApiEnabled()))
this._setString(calc, 'ciVisibilitySessionName', DD_SESSION_NAME)
this._setString(calc, 'ciVisibilityTestSessionName', DD_TEST_SESSION_NAME)
}
this._setString(calc, 'dogstatsd.hostname', this._getHostname())
this._setBoolean(calc, 'isGitUploadEnabled',
Expand Down
4 changes: 2 additions & 2 deletions packages/dd-trace/src/plugin_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ module.exports = class PluginManager {
dsmEnabled,
clientIpEnabled,
memcachedCommandEnabled,
ciVisibilitySessionName
ciVisibilityTestSessionName
} = this._tracerConfig

const sharedConfig = {
Expand All @@ -147,7 +147,7 @@ module.exports = class PluginManager {
site,
url,
headers: headerTags || [],
ciVisibilitySessionName
ciVisibilityTestSessionName
}

if (logInjection !== undefined) {
Expand Down
4 changes: 2 additions & 2 deletions packages/dd-trace/src/plugins/util/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,8 @@ function getIsFaultyEarlyFlakeDetection (projectSuites, testsBySuiteName, faulty
}

function getTestSessionName (config, testCommand, envTags) {
if (config.ciVisibilitySessionName) {
return config.ciVisibilitySessionName
if (config.ciVisibilityTestSessionName) {
return config.ciVisibilityTestSessionName
}
if (envTags[CI_JOB_NAME]) {
return `${envTags[CI_JOB_NAME]}-${testCommand}`
Expand Down
10 changes: 5 additions & 5 deletions packages/dd-trace/test/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ describe('Config', () => {
{ name: 'isGitUploadEnabled', value: false, origin: 'default' },
{ name: 'isIntelligentTestRunnerEnabled', value: false, origin: 'default' },
{ name: 'isManualApiEnabled', value: false, origin: 'default' },
{ name: 'ciVisibilitySessionName', value: '', origin: 'default' },
{ name: 'ciVisibilityTestSessionName', value: '', origin: 'default' },
{ name: 'logInjection', value: false, origin: 'default' },
{ name: 'lookup', value: undefined, origin: 'default' },
{ name: 'openAiLogsEnabled', value: false, origin: 'default' },
Expand Down Expand Up @@ -1798,7 +1798,7 @@ describe('Config', () => {
delete process.env.DD_CIVISIBILITY_EARLY_FLAKE_DETECTION_ENABLED
delete process.env.DD_CIVISIBILITY_FLAKY_RETRY_ENABLED
delete process.env.DD_CIVISIBILITY_FLAKY_RETRY_COUNT
delete process.env.DD_SESSION_NAME
delete process.env.DD_TEST_SESSION_NAME
delete process.env.JEST_WORKER_ID
options = {}
})
Expand Down Expand Up @@ -1883,10 +1883,10 @@ describe('Config', () => {
const config = new Config(options)
expect(config).to.have.property('flakyTestRetriesCount', 5)
})
it('should set the session name if DD_SESSION_NAME is set', () => {
process.env.DD_SESSION_NAME = 'my-test-session'
it('should set the session name if DD_TEST_SESSION_NAME is set', () => {
process.env.DD_TEST_SESSION_NAME = 'my-test-session'
const config = new Config(options)
expect(config).to.have.property('ciVisibilitySessionName', 'my-test-session')
expect(config).to.have.property('ciVisibilityTestSessionName', 'my-test-session')
})
})
context('ci visibility mode is not enabled', () => {
Expand Down

0 comments on commit 3605665

Please sign in to comment.