Skip to content

Commit

Permalink
[SDTEST-873] Send CPU count (#139)
Browse files Browse the repository at this point in the history
* added cpu count tag. Refactored generic metadata
* updated tag name
* properly encode metadata to metadata or metrics
* moved common metadata keys to enum strings
* cpu count in the each span
* fixed metadata encoding
  • Loading branch information
ypopovych authored Sep 19, 2024
1 parent d58caa0 commit c192190
Show file tree
Hide file tree
Showing 16 changed files with 286 additions and 22 deletions.
4 changes: 4 additions & 0 deletions DatadogSDKTesting.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
A7B747C02C60E1E7009B4B93 /* SwiftUtilsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7B747BF2C60E1E7009B4B93 /* SwiftUtilsTests.swift */; };
A7CC6D862C07D624003C13BC /* Tags.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7CC6D852C07D624003C13BC /* Tags.swift */; };
A7CC6D882C0F3F83003C13BC /* Tags+ObjC.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7CC6D872C0F3F83003C13BC /* Tags+ObjC.swift */; };
A7D23AF82C9B10A8006AB41D /* SpanMetadata.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7D23AF72C9B10A8006AB41D /* SpanMetadata.swift */; };
A7E0056D2C766EF7004D4B78 /* CoreTelephony.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A7E0056C2C766EF7004D4B78 /* CoreTelephony.framework */; platformFilters = (ios, maccatalyst, macos, ); };
A7E232E52BC6E80A0087D8F8 /* CodeCoverage.h in Headers */ = {isa = PBXBuildFile; fileRef = A7E232E42BC6E80A0087D8F8 /* CodeCoverage.h */; settings = {ATTRIBUTES = (Public, ); }; };
A7E232F52BC6EB470087D8F8 /* CoverageExporterJson.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7E232EA2BC6EB460087D8F8 /* CoverageExporterJson.cpp */; };
Expand Down Expand Up @@ -336,6 +337,7 @@
A7C292162B9B2DE0009C2979 /* DatadogSDKTesting.podspec */ = {isa = PBXFileReference; lastKnownFileType = text; path = DatadogSDKTesting.podspec; sourceTree = "<group>"; };
A7CC6D852C07D624003C13BC /* Tags.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tags.swift; sourceTree = "<group>"; };
A7CC6D872C0F3F83003C13BC /* Tags+ObjC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Tags+ObjC.swift"; sourceTree = "<group>"; };
A7D23AF72C9B10A8006AB41D /* SpanMetadata.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpanMetadata.swift; sourceTree = "<group>"; };
A7E0056C2C766EF7004D4B78 /* CoreTelephony.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreTelephony.framework; path = System/Library/Frameworks/CoreTelephony.framework; sourceTree = SDKROOT; };
A7E232DC2BC6E68B0087D8F8 /* CDatadogSDKTesting.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = CDatadogSDKTesting.framework; sourceTree = BUILT_PRODUCTS_DIR; };
A7E232E42BC6E80A0087D8F8 /* CodeCoverage.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CodeCoverage.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -848,6 +850,7 @@
E143CEFE27D7790200F4018A /* SpansExporter.swift */,
E143CEFF27D7790200F4018A /* SpanEncoder.swift */,
E1EBBD4627F1FFE70059BC3E /* SpanSanitizer.swift */,
A7D23AF72C9B10A8006AB41D /* SpanMetadata.swift */,
);
path = Spans;
sourceTree = "<group>";
Expand Down Expand Up @@ -1585,6 +1588,7 @@
A7FC26462BA1ECEF00067E26 /* FilesOrchestrator.swift in Sources */,
A7FC26482BA1ECEF00067E26 /* FileWriter.swift in Sources */,
A7FC263C2BA1ECCF00067E26 /* DataCompression.swift in Sources */,
A7D23AF82C9B10A8006AB41D /* SpanMetadata.swift in Sources */,
A7FC261A2BA1EC5A00067E26 /* DataUploadStatus.swift in Sources */,
A7FC263A2BA1ECCF00067E26 /* EncodableValue.swift in Sources */,
A7FC26352BA1ECCF00067E26 /* PerformancePreset.swift in Sources */,
Expand Down
4 changes: 4 additions & 0 deletions Sources/DatadogSDKTesting/DDTags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ internal enum DDTestTags {
static let testSkippedByITR = "test.skipped_by_itr"
}

internal enum DDHostTags {
static let hostVCPUCount = "_dd.host.vcpu_count"
}

internal enum DDOSTags {
static let osPlatform = "os.platform"
static let osArchitecture = "os.architecture"
Expand Down
4 changes: 4 additions & 0 deletions Sources/DatadogSDKTesting/DDTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public class DDTest: NSObject {

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

for metric in DDTestMonitor.baseMetrics {
span.setAttribute(key: metric.key, value: metric.value)
}

let functionName = suite.name + "." + name
if let functionInfo = DDTestModule.bundleFunctionInfo[functionName] {
Expand Down
2 changes: 2 additions & 0 deletions Sources/DatadogSDKTesting/DDTestModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ public class DDTestModule: NSObject, Encodable {
]

meta.merge(DDTestMonitor.baseConfigurationTags) { _, new in new }
metrics.merge(DDTestMonitor.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 }
Expand Down
4 changes: 4 additions & 0 deletions Sources/DatadogSDKTesting/DDTestMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ internal class DDTestMonitor {
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?
Expand Down
5 changes: 5 additions & 0 deletions Sources/DatadogSDKTesting/DDTestSuite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class DDTestSuite: NSObject, Encodable {
let startTime: Date
var duration: UInt64
var meta: [String: String] = [:]
var metrics: [String: Double] = [:]
var status: DDTestStatus
var unskippable: Bool = false
var localization: String
Expand Down Expand Up @@ -63,6 +64,8 @@ public class DDTestSuite: NSObject, Encodable {
]

meta.merge(DDTestMonitor.baseConfigurationTags) { _, new in new }
metrics.merge(DDTestMonitor.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 }
Expand Down Expand Up @@ -112,6 +115,7 @@ extension DDTestSuite {
case start
case duration
case meta
case metrics
case error
case name
case resource
Expand All @@ -126,6 +130,7 @@ extension DDTestSuite {
try container.encode(startTime.timeIntervalSince1970.toNanoseconds, forKey: .start)
try container.encode(duration, forKey: .duration)
try container.encode(meta, forKey: .meta)
try container.encode(metrics, forKey: .metrics)
try container.encode(status == .fail ? 1 : 0, forKey: .error)
try container.encode("\(module.testFramework).suite", forKey: .name)
try container.encode("\(name)", forKey: .resource)
Expand Down
10 changes: 9 additions & 1 deletion Sources/DatadogSDKTesting/DDTracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ internal class DDTracer {

let exporterConfiguration = ExporterConfiguration(
serviceName: conf.service ?? env.git.repositoryName ?? "unknown-swift-repo",
libraryVersion: DDTestMonitor.tracerVersion,
applicationName: identifier,
applicationVersion: version,
environment: env.environment,
hostname: hostnameToReport,
apiKey: conf.apiKey ?? "",
endpoint: conf.endpoint.exporterEndpoint,
metadata: .init(libraryVersion: DDTestMonitor.tracerVersion),
payloadCompression: payloadCompression,
performancePreset: .instantDataDelivery,
exporterId: String(SpanId.random().rawValue),
Expand Down Expand Up @@ -373,3 +373,11 @@ internal class DDTracer {
return eventsExporter?.endpointURLs() ?? Set<String>()
}
}

private extension SpanMetadata {
init(libraryVersion: String) {
self.init()
self[string: DDGenericTags.language] = "swift"
self[string: DDGenericTags.libraryVersion] = libraryVersion
}
}
6 changes: 5 additions & 1 deletion Sources/DatadogSDKTesting/Environment/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ internal final class Environment {
osArchitecture: PlatformUtils.getPlatformArchitecture(),
osVersion: PlatformUtils.getDeviceVersion(),
runtimeName: runtimeName, runtimeVersion: runtimeVersion,
localization: PlatformUtils.getLocalization())
localization: PlatformUtils.getLocalization(),
vCPUCount: PlatformUtils.getCpuCount())


let ciInfo = ciReaders.reduce(nil) { (ci, reader) in
Expand Down Expand Up @@ -220,6 +221,8 @@ internal extension Environment {

let localization: String

let vCPUCount: Int

var debugDescription: String {
"""
Platform:
Expand All @@ -230,6 +233,7 @@ internal extension Environment {
OS Version: \(osVersion)
Runtime Name: \(runtimeName)
Runtime Version: \(runtimeVersion)
vCPU Count: \(vCPUCount)
Localization: \(localization)
"""
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/DatadogSDKTesting/Utils/PlatformUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ struct PlatformUtils {
return (ProcessInfo.processInfo.processName, (Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String) ?? "")
}
}

static func getCpuCount() -> Int {
ProcessInfo.processInfo.processorCount
}

static func getAppearance() -> String {
#if os(iOS) || os(tvOS) || os(watchOS)
Expand Down
24 changes: 17 additions & 7 deletions Sources/EventsExporter/ExporterConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ internal struct ExporterError: Error, CustomStringConvertible {
public struct ExporterConfiguration {
/// The name of the service, resource, version,... that will be reported to the backend.
var serviceName: String
var libraryVersion: String
var applicationName: String
var version: String
var environment: String
var hostname: String?

/// API key for authentication
Expand All @@ -34,30 +32,42 @@ public struct ExporterConfiguration {
/// Exporter ID for tracing
var exporterId: String

var metadata: SpanMetadata

var environment: String {
didSet { _setEnv() }
}

var logger: Logger
var debug: Debug

public init(
serviceName: String, libraryVersion: String, applicationName: String,
applicationVersion: String, environment: String, hostname: String?, apiKey: String,
endpoint: Endpoint, payloadCompression: Bool = true, source: String = "ios",
serviceName: String, applicationName: String, applicationVersion: String,
environment: String, hostname: String?, apiKey: String,
endpoint: Endpoint, metadata: SpanMetadata,
payloadCompression: Bool = true, source: String = "ios",
performancePreset: PerformancePreset = .default, exporterId: String, logger: Logger,
debug: Debug = .init()
) {
self.serviceName = serviceName
self.libraryVersion = libraryVersion
self.applicationName = applicationName
self.version = applicationVersion
self.environment = environment
self.hostname = hostname
self.apiKey = apiKey
self.endpoint = endpoint
self.payloadCompression = payloadCompression
self.source = source
self.performancePreset = performancePreset
self.exporterId = exporterId
self.metadata = metadata
self.logger = logger
self.debug = debug
self.environment = environment
_setEnv()
}

private mutating func _setEnv() {
metadata[string: "env"] = environment
}
}

Expand Down
Loading

0 comments on commit c192190

Please sign in to comment.