UNPKG

dd-trace

Version:

Datadog APM tracing client for JavaScript

175 lines (157 loc) 5.58 kB
'use strict' const getConfig = require('../../config') const id = require('../../id') const log = require('../../log') const { incrementCountMetric, distributionMetric, TELEMETRY_GIT_REQUESTS_SETTINGS, TELEMETRY_GIT_REQUESTS_SETTINGS_MS, TELEMETRY_GIT_REQUESTS_SETTINGS_ERRORS, TELEMETRY_GIT_REQUESTS_SETTINGS_RESPONSE, } = require('../telemetry') const { writeSettingsToCache } = require('../test-optimization-cache') const { validateSettingsResponse } = require('../test-optimization-http-cache-schema') const request = require('./request') const DEFAULT_EARLY_FLAKE_DETECTION_NUM_RETRIES = 2 const DEFAULT_EARLY_FLAKE_DETECTION_SLOW_TEST_RETRIES = { '5s': 10, '10s': 5, '30s': 3, '5m': 2 } const DEFAULT_EARLY_FLAKE_DETECTION_ERROR_THRESHOLD = 30 function parseJsonResponse (rawJson) { return typeof rawJson === 'string' ? JSON.parse(rawJson) : rawJson } function parseLibraryConfigurationResponse (rawJson, config = getConfig(), options = {}) { const parsedResponse = parseJsonResponse(rawJson) if (options.validateRequiredFields) { validateSettingsResponse(parsedResponse, options) } const { code_coverage: isCodeCoverageEnabled, tests_skipping: isSuitesSkippingEnabled, itr_enabled: isItrEnabled, require_git: requireGit, early_flake_detection: earlyFlakeDetectionConfig, flaky_test_retries_enabled: isFlakyTestRetriesEnabled, di_enabled: isDiEnabled, known_tests_enabled: isKnownTestsEnabled, test_management: testManagementConfig, impacted_tests_enabled: isImpactedTestsEnabled, coverage_report_upload_enabled: isCoverageReportUploadEnabled, } = parsedResponse?.data?.attributes ?? parsedResponse const settings = { isCodeCoverageEnabled, isSuitesSkippingEnabled, isItrEnabled, requireGit, isEarlyFlakeDetectionEnabled: isKnownTestsEnabled && (earlyFlakeDetectionConfig?.enabled ?? false), earlyFlakeDetectionNumRetries: earlyFlakeDetectionConfig?.slow_test_retries?.['5s'] ?? DEFAULT_EARLY_FLAKE_DETECTION_NUM_RETRIES, earlyFlakeDetectionSlowTestRetries: earlyFlakeDetectionConfig?.slow_test_retries ?? DEFAULT_EARLY_FLAKE_DETECTION_SLOW_TEST_RETRIES, earlyFlakeDetectionFaultyThreshold: earlyFlakeDetectionConfig?.faulty_session_threshold ?? DEFAULT_EARLY_FLAKE_DETECTION_ERROR_THRESHOLD, isFlakyTestRetriesEnabled, isDiEnabled: isDiEnabled && isFlakyTestRetriesEnabled, isKnownTestsEnabled, isTestManagementEnabled: (testManagementConfig?.enabled ?? false), testManagementAttemptToFixRetries: testManagementConfig?.attempt_to_fix_retries, isImpactedTestsEnabled, isCoverageReportUploadEnabled: isCoverageReportUploadEnabled ?? false, } log.debug('Remote settings: %j', settings) if (config.testOptimization.DD_CIVISIBILITY_DANGEROUSLY_FORCE_COVERAGE) { settings.isCodeCoverageEnabled = true log.debug('Dangerously set code coverage to true') } if (config.testOptimization.DD_CIVISIBILITY_DANGEROUSLY_FORCE_TEST_SKIPPING) { settings.isSuitesSkippingEnabled = true log.debug('Dangerously set test skipping to true') } if ( settings.isCoverageReportUploadEnabled && !config.testOptimization.DD_CIVISIBILITY_CODE_COVERAGE_REPORT_UPLOAD_ENABLED ) { settings.isCoverageReportUploadEnabled = false log.debug('Code coverage report upload was disabled by the environment variable') } return settings } function getLibraryConfiguration ({ url, isEvpProxy, evpProxyPrefix, env, service, repositoryUrl, sha, osVersion, osPlatform, osArchitecture, runtimeName, runtimeVersion, branch, testLevel = 'suite', custom, tag, }, done) { const config = getConfig() const options = { path: '/api/v2/libraries/tests/services/setting', method: 'POST', headers: { 'Content-Type': 'application/json', }, url, timeout: 20_000, } if (isEvpProxy) { options.path = `${evpProxyPrefix}/api/v2/libraries/tests/services/setting` options.headers['X-Datadog-EVP-Subdomain'] = 'api' } else { if (!config.DD_API_KEY) { return done(new Error('Request to settings endpoint was not done because Datadog API key is not defined.')) } options.headers['dd-api-key'] = config.DD_API_KEY } const data = JSON.stringify({ data: { id: id().toString(10), type: 'ci_app_test_service_libraries_settings', attributes: { test_level: testLevel, configurations: { 'os.platform': osPlatform, 'os.version': osVersion, 'os.architecture': osArchitecture, 'runtime.name': runtimeName, 'runtime.version': runtimeVersion, custom, }, service, env, repository_url: repositoryUrl, sha, branch: branch || tag, }, }, }) incrementCountMetric(TELEMETRY_GIT_REQUESTS_SETTINGS) const startTime = Date.now() request(data, options, (err, res, statusCode) => { distributionMetric(TELEMETRY_GIT_REQUESTS_SETTINGS_MS, {}, Date.now() - startTime) if (err) { incrementCountMetric(TELEMETRY_GIT_REQUESTS_SETTINGS_ERRORS, { statusCode }) done(err) } else { try { const settings = parseLibraryConfigurationResponse(res, config) incrementCountMetric(TELEMETRY_GIT_REQUESTS_SETTINGS_RESPONSE, settings) writeSettingsToCache(settings) done(null, settings) } catch (err) { done(err) } } }) } module.exports = { getLibraryConfiguration, parseLibraryConfigurationResponse }