Skip to content

Commit

Permalink
added custom session name support (#140)
Browse files Browse the repository at this point in the history
moved all common tags to the global metadata
fixed CI detection
better tests
  • Loading branch information
ypopovych authored Sep 24, 2024
1 parent c192190 commit cd8aca5
Show file tree
Hide file tree
Showing 33 changed files with 216 additions and 199 deletions.
16 changes: 0 additions & 16 deletions IntegrationTests/UITests/IntegrationTestsRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,21 +168,5 @@ class IntegrationTestsRunner: XCTestCase {
XCTAssertEqual(attrib[DDTestTags.testSourceFile], "IntegrationTests/App/IntegrationTests.swift")
XCTAssertGreaterThan(Int(attrib[DDTestTags.testSourceStartLine] ?? "0") ?? 0, 0)
XCTAssertGreaterThan(Int(attrib[DDTestTags.testSourceEndLine] ?? "0") ?? 0, 0)
XCTAssertEqual(attrib[DDOSTags.osPlatform], PlatformUtils.getRunningPlatform())
XCTAssertEqual(attrib[DDOSTags.osArchitecture], PlatformUtils.getPlatformArchitecture())
XCTAssertEqual(attrib[DDOSTags.osVersion], PlatformUtils.getDeviceVersion())
XCTAssertEqual(attrib[DDRuntimeTags.runtimeName], "Xcode")
XCTAssertEqual(attrib[DDDeviceTags.deviceName], PlatformUtils.getDeviceName())
XCTAssertEqual(attrib[DDDeviceTags.deviceModel], PlatformUtils.getDeviceModel())
XCTAssertNotNil(attrib[DDGitTags.gitRepository])
XCTAssertNotNil(attrib[DDGitTags.gitBranch] ?? attrib[DDGitTags.gitTag])
XCTAssertNotNil(attrib[DDGitTags.gitCommit])
XCTAssertNotNil(attrib[DDGitTags.gitCommitMessage])
XCTAssertNotNil(attrib[DDGitTags.gitAuthorName])
XCTAssertNotNil(attrib[DDGitTags.gitAuthorEmail])
XCTAssertNotNil(attrib[DDGitTags.gitAuthorDate])
XCTAssertNotNil(attrib[DDGitTags.gitCommitterName])
XCTAssertNotNil(attrib[DDGitTags.gitCommitterEmail])
XCTAssertNotNil(attrib[DDGitTags.gitCommitterDate])
}
}
9 changes: 6 additions & 3 deletions Sources/DatadogSDKTesting/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ final class Config {
var tags: [String: String] = [:]
var customConfigurations: [String: String] = [:]
var environment: String? = nil
/// Datadog Endpoint
var endpoint: Endpoint = .us1
/// Session name
var sessionName: String? = nil

/// Instrumentation configuration values
var disableNetworkInstrumentation: Bool = false
Expand Down Expand Up @@ -45,9 +49,6 @@ final class Config {
var excludedBranches: Set<String> = []
var codeCoveragePriority: CodeCoveragePriority = .utility

/// Datadog Endpoint
var endpoint: Endpoint = .us1

/// Avoids configuring the traces exporter
var disableTracesExporting: Bool = false

Expand Down Expand Up @@ -92,6 +93,8 @@ final class Config {
}
customConfigurations = Dictionary(uniqueKeysWithValues: customConf)

sessionName = env[.sessionName]

/// Instrumentation configuration values
disableNetworkInstrumentation = env[.disableNetworkInstrumentation] ?? false
disableHeadersInjection = env[.disableHeadersInjection] ?? false
Expand Down
1 change: 1 addition & 0 deletions Sources/DatadogSDKTesting/DDTags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ internal enum DDTestSessionTags {
static let testItrSkippingCount = "test.itr.tests_skipping.count"
static let testItrSkipped = "test.itr.tests_skipping.tests_skipped"
static let testToolchain = "test.toolchain"
static let testSessionName = "test_session.name"
}

internal enum DDGitTags {
Expand Down
7 changes: 4 additions & 3 deletions Sources/DatadogSDKTesting/DDTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class DDTest: NSObject {
DDGenericTags.resource: "\(suite.name).\(name)",
DDTestTags.testName: name,
DDTestTags.testSuite: suite.name,
DDTestTags.testModule: module.bundleName,
DDTestTags.testFramework: module.testFramework,
DDTestTags.testType: DDTagValues.typeTest,
DDTestTags.testExecutionOrder: "\(currentTestExecutionOrder)",
Expand All @@ -49,17 +50,17 @@ public class DDTest: NSObject {
DDTestSuiteVisibilityTags.testSuiteId: suite.id.hexString,
DDUISettingsTags.uiSettingsSuiteLocalization: suite.localization,
DDUISettingsTags.uiSettingsModuleLocalization: module.localization,
].merging(DDTestMonitor.baseConfigurationTags) { old, _ in old }
]

span = DDTestMonitor.tracer.startSpan(name: "\(module.testFramework).test", attributes: attributes, startTime: testStartTime)

super.init()
DDTestMonitor.instance?.currentTest = self

DDTestMonitor.tracer.addPropagationsHeadersToEnvironment()
span.addTags(from: DDTestMonitor.env)

for metric in DDTestMonitor.baseMetrics {
// Move to the global when we will support global metrics
for metric in DDTestMonitor.env.baseMetrics {
span.setAttribute(key: metric.key, value: metric.value)
}

Expand Down
15 changes: 6 additions & 9 deletions Sources/DatadogSDKTesting/DDTestModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public class DDTestModule: NSObject, Encodable {

let beforeLoadingTime = DDTestMonitor.clock.now
if DDTestMonitor.instance == nil {
DDTestMonitor.baseConfigurationTags[DDTestTags.testModule] = bundleName
var success = false
Log.measure(name: "installTestMonitor") {
success = DDTestMonitor.installTestMonitor()
Expand Down Expand Up @@ -130,20 +129,18 @@ public class DDTestModule: NSObject, Encodable {
}

/// Export module event
let defaultAttributes: [String: String] = [
let moduleAttributes: [String: String] = [
DDGenericTags.type: DDTagValues.typeModuleEnd,
DDTestTags.testSuite: bundleName,
DDTestTags.testModule: bundleName,
DDTestTags.testFramework: testFramework,
DDTestTags.testStatus: moduleStatus,
DDTestSuiteVisibilityTags.testModuleId: String(id.rawValue),
]

meta.merge(DDTestMonitor.baseConfigurationTags) { _, new in new }
metrics.merge(DDTestMonitor.baseMetrics) { _, new in new }
meta.merge(moduleAttributes) { _, new in new }

// Move to the global when we will support global metrics
metrics.merge(DDTestMonitor.env.baseMetrics) { _, new in new }

meta.merge(defaultAttributes) { _, new in new }
meta.merge(DDTestMonitor.env.gitAttributes) { _, new in new }
meta.merge(DDTestMonitor.env.ciAttributes) { _, new in new }
meta[DDUISettingsTags.uiSettingsModuleLocalization] = localization
meta[DDTestSessionTags.testCodeCoverageEnabled] = (DDTestMonitor.instance?.coverageHelper != nil) ? "true" : "false"

Expand Down
19 changes: 2 additions & 17 deletions Sources/DatadogSDKTesting/DDTestMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,6 @@ internal class DDTestMonitor {

static let developerMachineHostName: String = try! Spawn.output("hostname")

static var baseConfigurationTags = [
DDOSTags.osPlatform: env.platform.osName,
DDOSTags.osArchitecture: env.platform.osArchitecture,
DDOSTags.osVersion: env.platform.osVersion,
DDDeviceTags.deviceName: env.platform.deviceName,
DDDeviceTags.deviceModel: env.platform.deviceModel,
DDRuntimeTags.runtimeName: env.platform.runtimeName,
DDRuntimeTags.runtimeVersion: env.platform.runtimeVersion,
DDUISettingsTags.uiSettingsLocalization: env.platform.localization,
]

static var baseMetrics = [
DDHostTags.hostVCPUCount: Double(env.platform.vCPUCount)
]

var coverageHelper: DDCoverageHelper?
var gitUploader: GitUploader?
var itr: IntelligentTestRunner?
Expand Down Expand Up @@ -256,7 +241,7 @@ internal class DDTestMonitor {
branch: branch,
sha: commit,
testLevel: .test,
configurations: DDTestMonitor.baseConfigurationTags,
configurations: DDTestMonitor.env.baseConfigurations,
customConfigurations: DDTestMonitor.config.customConfigurations)
}
} else {
Expand Down Expand Up @@ -305,7 +290,7 @@ internal class DDTestMonitor {
if itrBackendConfig?.testsSkipping ?? false {
Log.debug("ITR Enabled")

itr = IntelligentTestRunner(configurations: DDTestMonitor.baseConfigurationTags)
itr = IntelligentTestRunner(configurations: DDTestMonitor.env.baseConfigurations)
itr?.start()
} else {
Log.debug("ITR Disabled")
Expand Down
2 changes: 1 addition & 1 deletion Sources/DatadogSDKTesting/DDTestSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class DDTestSession: NSObject, Encodable {
self.meta[DDUISettingsTags.uiSettingsModuleLocalization] = nil

// Add spacific tags for sessions
self.meta[DDTestTags.testCommand] = "test \(testModule.bundleName)"
self.meta[DDTestTags.testCommand] = DDTestMonitor.env.testCommand
self.meta[DDTestSessionTags.testToolchain] = DDTestMonitor.env.platform.runtimeName.lowercased() + "-" + DDTestMonitor.env.platform.runtimeVersion
}
}
Expand Down
13 changes: 6 additions & 7 deletions Sources/DatadogSDKTesting/DDTestSuite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,20 @@ public class DDTestSuite: NSObject, Encodable {
suiteStatus = DDTagValues.statusSkip
}

let defaultAttributes: [String: String] = [
let suiteAttributes: [String: String] = [
DDGenericTags.type: DDTagValues.typeSuiteEnd,
DDTestTags.testSuite: name,
DDTestTags.testModule: module.bundleName,
DDTestTags.testFramework: module.testFramework,
DDTestTags.testStatus: suiteStatus,
DDTestSuiteVisibilityTags.testModuleId: String(module.id.rawValue),
DDTestSuiteVisibilityTags.testSuiteId: String(id.rawValue)
]

meta.merge(DDTestMonitor.baseConfigurationTags) { _, new in new }
metrics.merge(DDTestMonitor.baseMetrics) { _, new in new }
meta.merge(suiteAttributes) { _, new in new }

// Move to the global when we will support global metrics
metrics.merge(DDTestMonitor.env.baseMetrics) { _, new in new }

meta.merge(defaultAttributes) { _, new in new }
meta.merge(DDTestMonitor.env.gitAttributes) { _, new in new }
meta.merge(DDTestMonitor.env.ciAttributes) { _, new in new }
meta[DDUISettingsTags.uiSettingsSuiteLocalization] = localization
meta[DDUISettingsTags.uiSettingsModuleLocalization] = module.localization
if unskippable { meta[DDItrTags.itrUnskippable] = "true" }
Expand Down
37 changes: 33 additions & 4 deletions Sources/DatadogSDKTesting/DDTracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ internal class DDTracer {
}

let hostnameToReport: String? = (conf.reportHostname && !DDTestMonitor.developerMachineHostName.isEmpty) ? DDTestMonitor.developerMachineHostName : nil


let metadata = SpanMetadata(libraryVersion: DDTestMonitor.tracerVersion,
env: DDTestMonitor.env)

let exporterConfiguration = ExporterConfiguration(
serviceName: conf.service ?? env.git.repositoryName ?? "unknown-swift-repo",
applicationName: identifier,
Expand All @@ -76,7 +79,7 @@ internal class DDTracer {
hostname: hostnameToReport,
apiKey: conf.apiKey ?? "",
endpoint: conf.endpoint.exporterEndpoint,
metadata: .init(libraryVersion: DDTestMonitor.tracerVersion),
metadata: metadata,
payloadCompression: payloadCompression,
performancePreset: .instantDataDelivery,
exporterId: String(SpanId.random().rawValue),
Expand Down Expand Up @@ -374,10 +377,36 @@ internal class DDTracer {
}
}

private extension SpanMetadata {
init(libraryVersion: String) {
extension SpanMetadata {
init(libraryVersion: String,
tags: [String: String],
git: [String: String],
ci: [String: String],
sessionName: String)
{
self.init()
self[string: DDGenericTags.language] = "swift"
self[string: DDGenericTags.libraryVersion] = libraryVersion
for type in SpanType.allTest {
for tag in tags {
self[string: type, tag.key] = tag.value
}
for tag in git {
self[string: type, tag.key] = tag.value
}
for tag in ci {
self[string: type, tag.key] = tag.value
}
self[string: type, DDTestSessionTags.testSessionName] = sessionName
}
}
}

extension SpanMetadata.SpanType {
static var module: Self { .init(DDTagValues.typeModuleEnd) }
static var session: Self { .init(DDTagValues.typeSessionEnd) }
static var suite: Self { .init(DDTagValues.typeSuiteEnd) }
static var test: Self { .init(DDTagValues.typeTest) }

static var allTest: [Self] { [module, session, suite, test] }
}
2 changes: 1 addition & 1 deletion Sources/DatadogSDKTesting/Environment/CI/AppveyorCI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

internal struct AppveyorCIEnvironmentReader: CIEnvironmentReader {
func isActive(env: any EnvironmentReader) -> Bool { env.has(env: "APPVEYOR") }
func isActive(env: any EnvironmentReader) -> Bool { env["APPVEYOR"] ?? "" != "" }

func read(env: any EnvironmentReader) -> (ci: Environment.CI, git: Environment.Git) {
let pipelineId = env["APPVEYOR_BUILD_ID", String.self]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

internal struct AwsCodeBuildCIEnvironmentReader: CIEnvironmentReader {
func isActive(env: any EnvironmentReader) -> Bool { env.has(env: "CODEBUILD_BUILD_ID") }
func isActive(env: any EnvironmentReader) -> Bool { env["CODEBUILD_BUILD_ID"] ?? "" != "" }

func read(env: any EnvironmentReader) -> (ci: Environment.CI, git: Environment.Git) {
let idAndName = (env["CODEBUILD_BUILD_ID"] ?? "").components(separatedBy: ":")
Expand Down
2 changes: 1 addition & 1 deletion Sources/DatadogSDKTesting/Environment/CI/AzureCI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

internal struct AzureCIEnvironmentReader: CIEnvironmentReader {
func isActive(env: any EnvironmentReader) -> Bool { env.has(env: "TF_BUILD") }
func isActive(env: any EnvironmentReader) -> Bool { env["TF_BUILD"] ?? "" != "" }

func read(env: any EnvironmentReader) -> (ci: Environment.CI, git: Environment.Git) {
let pipelineId = env["BUILD_BUILDID", String.self]
Expand Down
2 changes: 1 addition & 1 deletion Sources/DatadogSDKTesting/Environment/CI/BitbucketCI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

internal struct BitbucketCIEnvironmentReader: CIEnvironmentReader {
func isActive(env: any EnvironmentReader) -> Bool { env.has(env: "BITBUCKET_COMMIT") }
func isActive(env: any EnvironmentReader) -> Bool { env["BITBUCKET_COMMIT"] ?? "" != "" }

func read(env: any EnvironmentReader) -> (ci: Environment.CI, git: Environment.Git) {
let pipelineId = env["BITBUCKET_PIPELINE_UUID", String.self]
Expand Down
2 changes: 1 addition & 1 deletion Sources/DatadogSDKTesting/Environment/CI/BitriseCI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

internal struct BitriseCIEnvironmentReader: CIEnvironmentReader {
func isActive(env: any EnvironmentReader) -> Bool { env.has(env: "BITRISE_BUILD_SLUG") }
func isActive(env: any EnvironmentReader) -> Bool { env["BITRISE_BUILD_SLUG"] ?? "" != "" }

func read(env: any EnvironmentReader) -> (ci: Environment.CI, git: Environment.Git) {
let commitMessage: String? = env["BITRISE_GIT_MESSAGE"] ?? env["GIT_CLONE_COMMIT_MESSAGE_SUBJECT"].flatMap({
Expand Down
2 changes: 1 addition & 1 deletion Sources/DatadogSDKTesting/Environment/CI/BuddyCI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

internal struct BuddyCIEnvironmentReader: CIEnvironmentReader {
func isActive(env: any EnvironmentReader) -> Bool { env.has(env: "BUDDY") }
func isActive(env: any EnvironmentReader) -> Bool { env["BUDDY"] ?? "" != "" }

func read(env: any EnvironmentReader) -> (ci: Environment.CI, git: Environment.Git) {
let pipelineId: String? = env["BUDDY_PIPELINE_ID"].flatMap { pId in
Expand Down
2 changes: 1 addition & 1 deletion Sources/DatadogSDKTesting/Environment/CI/BuildkiteCI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

internal struct BuildkiteCIEnvironmentReader: CIEnvironmentReader {
func isActive(env: any EnvironmentReader) -> Bool { env.has(env: "BUILDKITE") }
func isActive(env: any EnvironmentReader) -> Bool { env["BUILDKITE"] ?? "" != "" }

func read(env: any EnvironmentReader) -> (ci: Environment.CI, git: Environment.Git) {
let jobId = env["BUILDKITE_JOB_ID", String.self]
Expand Down
2 changes: 1 addition & 1 deletion Sources/DatadogSDKTesting/Environment/CI/CircleCI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

internal struct CircleCIEnvironmentReader: CIEnvironmentReader {
func isActive(env: any EnvironmentReader) -> Bool { env.has(env: "CIRCLECI") }
func isActive(env: any EnvironmentReader) -> Bool { env["CIRCLECI"] ?? "" != "" }

func read(env: any EnvironmentReader) -> (ci: Environment.CI, git: Environment.Git) {
let pipelineId = env["CIRCLE_WORKFLOW_ID", String.self]
Expand Down
2 changes: 1 addition & 1 deletion Sources/DatadogSDKTesting/Environment/CI/CodefreshCI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

internal struct CodefreshCIEnvironmentReader: CIEnvironmentReader {
func isActive(env: any EnvironmentReader) -> Bool { env.has(env: "CF_BUILD_ID") }
func isActive(env: any EnvironmentReader) -> Bool { env["CF_BUILD_ID"] ?? "" != "" }

func read(env: any EnvironmentReader) -> (ci: Environment.CI, git: Environment.Git) {
var environment = [String: SpanAttributeConvertible]()
Expand Down
2 changes: 1 addition & 1 deletion Sources/DatadogSDKTesting/Environment/CI/GithubCI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Foundation

internal struct GithubCIEnvironmentReader: CIEnvironmentReader {
func isActive(env: any EnvironmentReader) -> Bool {
env.has(env: "GITHUB_ACTIONS") || env.has(env: "GITHUB_ACTION")
env["GITHUB_ACTIONS"] ?? env["GITHUB_ACTION"] ?? "" != ""
}

func read(env: any EnvironmentReader) -> (ci: Environment.CI, git: Environment.Git) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/DatadogSDKTesting/Environment/CI/GitlabCI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

internal struct GitlabCIEnvironmentReader: CIEnvironmentReader {
func isActive(env: any EnvironmentReader) -> Bool { env.has(env: "GITLAB_CI") }
func isActive(env: any EnvironmentReader) -> Bool { env["GITLAB_CI"] ?? "" != "" }

func read(env: any EnvironmentReader) -> (ci: Environment.CI, git: Environment.Git) {
var authorName: String? = nil
Expand Down
2 changes: 1 addition & 1 deletion Sources/DatadogSDKTesting/Environment/CI/JenkinsCI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

internal struct JenkinsCIEnvironmentReader: CIEnvironmentReader {
func isActive(env: any EnvironmentReader) -> Bool { env.has(env: "JENKINS_URL") }
func isActive(env: any EnvironmentReader) -> Bool { env["JENKINS_URL"] ?? "" != "" }

func read(env: any EnvironmentReader) -> (ci: Environment.CI, git: Environment.Git) {
let (branch, isTag) = normalize(branchOrTag: env["GIT_BRANCH"])
Expand Down
2 changes: 1 addition & 1 deletion Sources/DatadogSDKTesting/Environment/CI/TeamcityCI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

internal struct TeamcityCIEnvironmentReader: CIEnvironmentReader {
func isActive(env: any EnvironmentReader) -> Bool { env.has(env: "TEAMCITY_VERSION") }
func isActive(env: any EnvironmentReader) -> Bool { env["TEAMCITY_VERSION"] ?? "" != "" }

func read(env: any EnvironmentReader) -> (ci: Environment.CI, git: Environment.Git) {
(
Expand Down
2 changes: 1 addition & 1 deletion Sources/DatadogSDKTesting/Environment/CI/TravisCI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

internal struct TravisCIEnvironmentReader: CIEnvironmentReader {
func isActive(env: any EnvironmentReader) -> Bool { env.has(env: "TRAVIS") }
func isActive(env: any EnvironmentReader) -> Bool { env["TRAVIS"] ?? "" != "" }

func read(env: any EnvironmentReader) -> (ci: Environment.CI, git: Environment.Git) {
let repositoryEnv: String? = env["TRAVIS_PULL_REQUEST_SLUG"] ?? env["TRAVIS_REPO_SLUG"]
Expand Down
2 changes: 1 addition & 1 deletion Sources/DatadogSDKTesting/Environment/CI/XcodeCI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

internal struct XcodeCIEnvironmentReader: CIEnvironmentReader {
func isActive(env: any EnvironmentReader) -> Bool { env.has(env: "CI_WORKSPACE") }
func isActive(env: any EnvironmentReader) -> Bool { env["CI_WORKSPACE"] ?? "" != "" }

func read(env: any EnvironmentReader) -> (ci: Environment.CI, git: Environment.Git) {
return (
Expand Down
Loading

0 comments on commit cd8aca5

Please sign in to comment.