Skip to content

Commit

Permalink
Merge branch 'master' into conti/support-protobuf-schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
wconti27 authored Sep 24, 2024
2 parents 79e4945 + b079c6f commit 6028c36
Show file tree
Hide file tree
Showing 20 changed files with 169 additions and 62 deletions.
1 change: 1 addition & 0 deletions .github/workflows/appsec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ jobs:
version:
- 18
- latest
range: ['9.5.0', '11.1.4', '13.2.0', '14.2.6']
runs-on: ubuntu-latest
env:
PLUGINS: next
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/plugins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ jobs:
version:
- 18
- latest
range: ['9.5.0', '11.1.4', '13.2.0', '14.2.6']
runs-on: ubuntu-latest
env:
PLUGINS: next
Expand Down Expand Up @@ -868,7 +869,7 @@ jobs:
runs-on: ubuntu-latest
services:
mssql:
image: mcr.microsoft.com/mssql/server:2017-latest-ubuntu
image: mcr.microsoft.com/mssql/server:2019-latest
env:
ACCEPT_EULA: 'Y'
SA_PASSWORD: DD_HUNTER2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('can run tests', () => {
})
test('integration test', () => {
// Just for testing purposes, so we don't create a custom span
if (!process.env.DD_CIVISIBILITY_MANUAL_API_ENABLED) {
if (process.env.DD_CIVISIBILITY_MANUAL_API_ENABLED === 'false') {
return Promise.resolve()
}
const testSpan = tracer.scope().active()
Expand Down
6 changes: 5 additions & 1 deletion integration-tests/cucumber/cucumber.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const {
TEST_SESSION_NAME,
TEST_LEVEL_EVENT_TYPES
} = require('../../packages/dd-trace/src/plugins/util/test')
const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/env')

const isOldNode = semver.satisfies(process.version, '<=16')
const versions = ['7.0.0', isOldNode ? '9' : 'latest']
Expand Down Expand Up @@ -181,6 +182,7 @@ versions.forEach(version => {
assert.equal(testSessionId.toString(10), testSessionEventContent.test_session_id.toString(10))
assert.isTrue(meta[TEST_SOURCE_FILE].startsWith(featuresPath))
assert.equal(metrics[TEST_SOURCE_START], 1)
assert.exists(metrics[DD_HOST_CPU_COUNT])
})

assert.includeMembers(testEvents.map(test => test.content.resource), [
Expand All @@ -201,6 +203,7 @@ versions.forEach(version => {
testEvents.forEach(({
content: {
meta,
metrics,
test_suite_id: testSuiteId,
test_module_id: testModuleId,
test_session_id: testSessionId
Expand All @@ -218,6 +221,7 @@ versions.forEach(version => {
if (runMode === 'parallel') {
assert.propertyVal(meta, CUCUMBER_IS_PARALLEL, 'true')
}
assert.exists(metrics[DD_HOST_CPU_COUNT])
})

stepEvents.forEach(stepEvent => {
Expand All @@ -233,7 +237,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
6 changes: 5 additions & 1 deletion integration-tests/cypress/cypress.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const {
TEST_SESSION_NAME,
TEST_LEVEL_EVENT_TYPES
} = require('../../packages/dd-trace/src/plugins/util/test')
const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/env')
const { ERROR_MESSAGE } = require('../../packages/dd-trace/src/constants')
const { NODE_MAJOR } = require('../../version')

Expand Down Expand Up @@ -289,6 +290,7 @@ moduleTypes.forEach(({
assert.equal(testSessionId.toString(10), testSessionEventContent.test_session_id.toString(10))
assert.isTrue(meta[TEST_SOURCE_FILE].startsWith('cypress/e2e/'))
assert.equal(metrics[TEST_SOURCE_START], 1)
assert.exists(metrics[DD_HOST_CPU_COUNT])
})

assert.includeMembers(testEvents.map(test => test.content.resource), [
Expand All @@ -306,6 +308,7 @@ moduleTypes.forEach(({
testEvents.forEach(({
content: {
meta,
metrics,
test_suite_id: testSuiteId,
test_module_id: testModuleId,
test_session_id: testSessionId
Expand All @@ -320,6 +323,7 @@ moduleTypes.forEach(({
// Can read DD_TAGS
assert.propertyVal(meta, 'test.customtag', 'customvalue')
assert.propertyVal(meta, 'test.customtag2', 'customvalue2')
assert.exists(metrics[DD_HOST_CPU_COUNT])
})
}, 25000)

Expand All @@ -336,7 +340,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
7 changes: 5 additions & 2 deletions integration-tests/jest/jest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const {
TEST_SESSION_NAME,
TEST_LEVEL_EVENT_TYPES
} = require('../../packages/dd-trace/src/plugins/util/test')
const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/env')
const { ERROR_MESSAGE } = require('../../packages/dd-trace/src/constants')

const testFile = 'ci-visibility/run-jest.js'
Expand Down Expand Up @@ -169,11 +170,13 @@ describe('jest CommonJS', () => {
// Can read DD_TAGS
assert.propertyVal(testEvent.meta, 'test.customtag', 'customvalue')
assert.propertyVal(testEvent.meta, 'test.customtag2', 'customvalue2')
assert.exists(testEvent.metrics[DD_HOST_CPU_COUNT])
})

suites.forEach(testSuite => {
assert.isTrue(testSuite.meta[TEST_SOURCE_FILE].startsWith('ci-visibility/test/ci-visibility-test'))
assert.equal(testSuite.metrics[TEST_SOURCE_START], 1)
assert.exists(testSuite.metrics[DD_HOST_CPU_COUNT])
})

done()
Expand All @@ -184,7 +187,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 +447,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
7 changes: 5 additions & 2 deletions integration-tests/mocha/mocha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const {
TEST_SESSION_NAME,
TEST_LEVEL_EVENT_TYPES
} = require('../../packages/dd-trace/src/plugins/util/test')
const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/env')
const { ERROR_MESSAGE } = require('../../packages/dd-trace/src/constants')

const runTestsWithCoverageCommand = './node_modules/nyc/bin/nyc.js -r=text-summary node ./ci-visibility/run-mocha.js'
Expand Down Expand Up @@ -169,11 +170,13 @@ describe('mocha CommonJS', function () {
// Can read DD_TAGS
assert.propertyVal(testEvent.meta, 'test.customtag', 'customvalue')
assert.propertyVal(testEvent.meta, 'test.customtag2', 'customvalue2')
assert.exists(testEvent.metrics[DD_HOST_CPU_COUNT])
})

suites.forEach(testSuite => {
assert.isTrue(testSuite.meta[TEST_SOURCE_FILE].startsWith('ci-visibility/test/ci-visibility-test'))
assert.equal(testSuite.metrics[TEST_SOURCE_START], 1)
assert.exists(testSuite.metrics[DD_HOST_CPU_COUNT])
})

done()
Expand All @@ -184,7 +187,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 +381,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
5 changes: 4 additions & 1 deletion integration-tests/playwright/playwright.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const {
TEST_SESSION_NAME,
TEST_LEVEL_EVENT_TYPES
} = require('../../packages/dd-trace/src/plugins/util/test')
const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/env')
const { ERROR_MESSAGE } = require('../../packages/dd-trace/src/constants')

const NUM_RETRIES_EFD = 3
Expand Down Expand Up @@ -118,6 +119,7 @@ versions.forEach((version) => {
}
assert.isTrue(testSuiteEvent.content.meta[TEST_SOURCE_FILE].endsWith('-test.js'))
assert.equal(testSuiteEvent.content.metrics[TEST_SOURCE_START], 1)
assert.exists(testSuiteEvent.content.metrics[DD_HOST_CPU_COUNT])
})

assert.includeMembers(testEvents.map(test => test.content.resource), [
Expand Down Expand Up @@ -145,6 +147,7 @@ versions.forEach((version) => {
assert.propertyVal(testEvent.content.meta, 'test.customtag2', 'customvalue2')
// Adds the browser used
assert.propertyVal(testEvent.content.meta, TEST_CONFIGURATION_BROWSER_NAME, 'chromium')
assert.exists(testEvent.content.metrics[DD_HOST_CPU_COUNT])
})

stepEvents.forEach(stepEvent => {
Expand All @@ -168,7 +171,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
9 changes: 6 additions & 3 deletions integration-tests/test-api-manual.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('test-api-manual', () => {
'--require ./ci-visibility/test-api-manual/test.fake.js ./ci-visibility/test-api-manual/run-fake-test-framework',
{
cwd,
env: { ...getCiVisAgentlessConfig(receiver.port), DD_CIVISIBILITY_MANUAL_API_ENABLED: '1' },
env: getCiVisAgentlessConfig(receiver.port),
stdio: 'pipe'
}
)
Expand All @@ -82,7 +82,7 @@ describe('test-api-manual', () => {
})
})

it('does not report test spans if DD_CIVISIBILITY_MANUAL_API_ENABLED is not set', (done) => {
it('does not report test spans if DD_CIVISIBILITY_MANUAL_API_ENABLED is set to false', (done) => {
receiver.assertPayloadReceived(() => {
const error = new Error('should not report spans')
done(error)
Expand All @@ -93,7 +93,10 @@ describe('test-api-manual', () => {
'--require ./ci-visibility/test-api-manual/test.fake.js ./ci-visibility/test-api-manual/run-fake-test-framework',
{
cwd,
env: getCiVisAgentlessConfig(receiver.port),
env: {
...getCiVisAgentlessConfig(receiver.port),
DD_CIVISIBILITY_MANUAL_API_ENABLED: 'false'
},
stdio: 'pipe'
}
)
Expand Down
5 changes: 4 additions & 1 deletion integration-tests/vitest/vitest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {
TEST_SOURCE_FILE,
TEST_SOURCE_START
} = require('../../packages/dd-trace/src/plugins/util/test')
const { DD_HOST_CPU_COUNT } = require('../../packages/dd-trace/src/plugins/util/env')

const versions = ['1.6.0', 'latest']

Expand Down Expand Up @@ -145,6 +146,7 @@ versions.forEach((version) => {

testEvents.forEach(test => {
assert.equal(test.content.meta[TEST_COMMAND], 'vitest run')
assert.exists(test.content.metrics[DD_HOST_CPU_COUNT])
})

testSuiteEvents.forEach(testSuite => {
Expand All @@ -153,6 +155,7 @@ versions.forEach((version) => {
testSuite.content.meta[TEST_SOURCE_FILE].startsWith('ci-visibility/vitest-tests/test-visibility')
)
assert.equal(testSuite.content.metrics[TEST_SOURCE_START], 1)
assert.exists(testSuite.content.metrics[DD_HOST_CPU_COUNT])
})
// TODO: check error messages
}).then(() => done()).catch(done)
Expand All @@ -164,7 +167,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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export async function createAndStartServer (app) {
})

try {
await app.listen(0)
await app.listen({ port: 0 })
const address = app.server.address()
const port = address.port
process.send({ port })
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
Loading

0 comments on commit 6028c36

Please sign in to comment.