From fd409f133fc0e60de58d32e4715e1dc2f6665a90 Mon Sep 17 00:00:00 2001 From: Eric Navarro <137877238+ericlaz@users.noreply.github.com> Date: Mon, 10 Jul 2023 10:45:06 +0200 Subject: [PATCH] [ci-visibility] Change `test.type` in browser tests (#3358) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add framework type checking * Improve code readability Co-authored-by: Juan Antonio Fernández de Alba * Fix imports * Fix styling Co-authored-by: Juan Antonio Fernández de Alba * Add cypress and playwright tests * Change browser conditions to detect all playwright instances * Fix include string * Fix some suites still being reported as test type --------- Co-authored-by: Juan Antonio Fernández de Alba --- .../playwright/playwright.spec.js | 5 +++-- packages/datadog-plugin-cypress/src/plugin.js | 2 +- .../datadog-plugin-cypress/test/index.spec.js | 4 ++-- packages/dd-trace/src/plugins/ci_plugin.js | 7 ++++++- packages/dd-trace/src/plugins/util/test.js | 21 ++++++++++++------- 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/integration-tests/playwright/playwright.spec.js b/integration-tests/playwright/playwright.spec.js index 914315b105d..f78e2a78032 100644 --- a/integration-tests/playwright/playwright.spec.js +++ b/integration-tests/playwright/playwright.spec.js @@ -13,7 +13,7 @@ const { } = require('../helpers') const { FakeCiVisIntake } = require('../ci-visibility-intake') const webAppServer = require('../ci-visibility/web-app-server') -const { TEST_STATUS, TEST_SOURCE_START } = require('../../packages/dd-trace/src/plugins/util/test') +const { TEST_STATUS, TEST_SOURCE_START, TEST_TYPE } = require('../../packages/dd-trace/src/plugins/util/test') // TODO: remove when 2.x support is removed. // This is done because from playwright@>=1.22.0 node 12 is not supported @@ -73,7 +73,8 @@ versions.forEach((version) => { assert.equal(testSessionEvent.content.meta[TEST_STATUS], 'fail') assert.include(testModuleEvent.content.resource, 'test_module.playwright test') assert.equal(testModuleEvent.content.meta[TEST_STATUS], 'fail') - + assert.equal(testSessionEvent.content.meta[TEST_TYPE], 'browser') + assert.equal(testModuleEvent.content.meta[TEST_TYPE], 'browser') assert.includeMembers(testSuiteEvents.map(suite => suite.content.resource), [ 'test_suite.todo-list-page-test.js', 'test_suite.landing-page-test.js', diff --git a/packages/datadog-plugin-cypress/src/plugin.js b/packages/datadog-plugin-cypress/src/plugin.js index 2a6d209079b..759daa8a55e 100644 --- a/packages/datadog-plugin-cypress/src/plugin.js +++ b/packages/datadog-plugin-cypress/src/plugin.js @@ -37,7 +37,7 @@ const CYPRESS_STATUS_TO_TEST_STATUS = { function getTestSpanMetadata (tracer, testName, testSuite, cypressConfig) { const childOf = getTestParentSpan(tracer) - const commonTags = getTestCommonTags(testName, testSuite, cypressConfig.version) + const commonTags = getTestCommonTags(testName, testSuite, cypressConfig.version, TEST_FRAMEWORK_NAME) return { childOf, diff --git a/packages/datadog-plugin-cypress/test/index.spec.js b/packages/datadog-plugin-cypress/test/index.spec.js index ba8c8825c76..293855ca522 100644 --- a/packages/datadog-plugin-cypress/test/index.spec.js +++ b/packages/datadog-plugin-cypress/test/index.spec.js @@ -78,7 +78,7 @@ describe('Plugin', function () { [TEST_STATUS]: 'pass', [TEST_SUITE]: 'cypress/integration/integration-test.js', [TEST_SOURCE_FILE]: 'cypress/integration/integration-test.js', - [TEST_TYPE]: 'test', + [TEST_TYPE]: 'browser', [ORIGIN_KEY]: CI_APP_ORIGIN, [TEST_IS_RUM_ACTIVE]: 'true', [TEST_CODE_OWNERS]: JSON.stringify(['@datadog']), @@ -103,7 +103,7 @@ describe('Plugin', function () { [TEST_STATUS]: 'fail', [TEST_SUITE]: 'cypress/integration/integration-test.js', [TEST_SOURCE_FILE]: 'cypress/integration/integration-test.js', - [TEST_TYPE]: 'test', + [TEST_TYPE]: 'browser', [ORIGIN_KEY]: CI_APP_ORIGIN, [ERROR_TYPE]: 'AssertionError', [TEST_IS_RUM_ACTIVE]: 'true', diff --git a/packages/dd-trace/src/plugins/ci_plugin.js b/packages/dd-trace/src/plugins/ci_plugin.js index db7474618d5..4809972dd2d 100644 --- a/packages/dd-trace/src/plugins/ci_plugin.js +++ b/packages/dd-trace/src/plugins/ci_plugin.js @@ -111,7 +111,12 @@ module.exports = class CiPlugin extends Plugin { const childOf = getTestParentSpan(this.tracer) let testTags = { - ...getTestCommonTags(testName, testSuite, this.frameworkVersion), + ...getTestCommonTags( + testName, + testSuite, + this.frameworkVersion, + this.constructor.id + ), [COMPONENT]: this.constructor.id, ...extraTags } diff --git a/packages/dd-trace/src/plugins/util/test.js b/packages/dd-trace/src/plugins/util/test.js index 62d8de4aed6..6e7abb39358 100644 --- a/packages/dd-trace/src/plugins/util/test.js +++ b/packages/dd-trace/src/plugins/util/test.js @@ -210,6 +210,13 @@ function getTestParametersString (parametersByTestName, testName) { } } +function getTestTypeFromFramework (testFramework) { + if (testFramework === 'playwright' || testFramework === 'cypress') { + return 'browser' + } + return 'test' +} + function finishAllTraceSpans (span) { span.context()._trace.started.forEach(traceSpan => { if (traceSpan !== span) { @@ -225,10 +232,10 @@ function getTestParentSpan (tracer) { }) } -function getTestCommonTags (name, suite, version) { +function getTestCommonTags (name, suite, version, testFramework) { return { [SPAN_TYPE]: 'test', - [TEST_TYPE]: 'test', + [TEST_TYPE]: getTestTypeFromFramework(testFramework), [SAMPLING_RULE_DECISION]: 1, [SAMPLING_PRIORITY]: AUTO_KEEP, [TEST_NAME]: name, @@ -306,12 +313,12 @@ function getCodeOwnersForFilename (filename, entries) { return null } -function getTestLevelCommonTags (command, testFrameworkVersion) { +function getTestLevelCommonTags (command, testFrameworkVersion, testFramework) { return { [TEST_FRAMEWORK_VERSION]: testFrameworkVersion, [LIBRARY_VERSION]: ddTraceVersion, [TEST_COMMAND]: command, - [TEST_TYPE]: 'test' + [TEST_TYPE]: getTestTypeFromFramework(testFramework) } } @@ -321,7 +328,7 @@ function getTestSessionCommonTags (command, testFrameworkVersion, testFramework) [RESOURCE_NAME]: `test_session.${command}`, [TEST_MODULE]: testFramework, [TEST_TOOLCHAIN]: getPkgManager(), - ...getTestLevelCommonTags(command, testFrameworkVersion) + ...getTestLevelCommonTags(command, testFrameworkVersion, testFramework) } } @@ -330,7 +337,7 @@ function getTestModuleCommonTags (command, testFrameworkVersion, testFramework) [SPAN_TYPE]: 'test_module_end', [RESOURCE_NAME]: `test_module.${command}`, [TEST_MODULE]: testFramework, - ...getTestLevelCommonTags(command, testFrameworkVersion) + ...getTestLevelCommonTags(command, testFrameworkVersion, testFramework) } } @@ -340,7 +347,7 @@ function getTestSuiteCommonTags (command, testFrameworkVersion, testSuite, testF [RESOURCE_NAME]: `test_suite.${testSuite}`, [TEST_MODULE]: testFramework, [TEST_SUITE]: testSuite, - ...getTestLevelCommonTags(command, testFrameworkVersion) + ...getTestLevelCommonTags(command, testFrameworkVersion, testFramework) } }