diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ab5d3cf880..df156c56e2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ - Report database backing store information for Core Data (#3231) - Add "data use" in privacy manifests (#3259) +### Fixes + +- Report correct units (nanojoules) for profiling energy metrics (#3262) + ## 8.10.0 ### Features diff --git a/Sources/Sentry/SentryMetricProfiler.mm b/Sources/Sentry/SentryMetricProfiler.mm index cb27e2b5269..fb34e3f8e0c 100644 --- a/Sources/Sentry/SentryMetricProfiler.mm +++ b/Sources/Sentry/SentryMetricProfiler.mm @@ -32,6 +32,7 @@ @implementation SentryMetricReading NSString *const kSentryMetricProfilerSerializationUnitBytes = @"byte"; NSString *const kSentryMetricProfilerSerializationUnitPercentage = @"percent"; +NSString *const kSentryMetricProfilerSerializationUnitNanoJoules = @"nanojoule"; // Currently set to 10 Hz as we don't anticipate much utility out of a higher resolution when // sampling CPU usage and memory footprint, and we want to minimize the overhead of making the @@ -133,7 +134,7 @@ - (void)stop if (cpuEnergyUsage.count > 0) { dict[kSentryMetricProfilerSerializationKeyCPUEnergyUsage] = serializeValuesWithNormalizedTime(cpuEnergyUsage, - kSentryMetricProfilerSerializationUnitBytes, startSystemTime, endSystemTime); + kSentryMetricProfilerSerializationUnitNanoJoules, startSystemTime, endSystemTime); } if (cpuUsage.count > 0) { diff --git a/Sources/Sentry/include/SentryMetricProfiler.h b/Sources/Sentry/include/SentryMetricProfiler.h index bd0e6c1cfec..747d36deab5 100644 --- a/Sources/Sentry/include/SentryMetricProfiler.h +++ b/Sources/Sentry/include/SentryMetricProfiler.h @@ -14,6 +14,7 @@ SENTRY_EXTERN NSString *const kSentryMetricProfilerSerializationKeyCPUEnergyUsag SENTRY_EXTERN NSString *const kSentryMetricProfilerSerializationUnitBytes; SENTRY_EXTERN NSString *const kSentryMetricProfilerSerializationUnitPercentage; +SENTRY_EXTERN NSString *const kSentryMetricProfilerSerializationUnitNanoJoules; // The next two types are technically the same as far as the type system is concerned, but they // actually contain different mixes of value types, so define them separately. If they ever change, diff --git a/Tests/SentryProfilerTests/SentryProfilerSwiftTests.swift b/Tests/SentryProfilerTests/SentryProfilerSwiftTests.swift index 6350ffc69e6..7982907de19 100644 --- a/Tests/SentryProfilerTests/SentryProfilerSwiftTests.swift +++ b/Tests/SentryProfilerTests/SentryProfilerSwiftTests.swift @@ -603,12 +603,12 @@ private extension SentryProfilerSwiftTests { let expectedUsageReadings = fixture.mockUsageReadingsPerBatch * metricsBatches - try assertMetricValue(measurements: measurements, key: kSentryMetricProfilerSerializationKeyCPUUsage, numberOfReadings: expectedUsageReadings, expectedValue: fixture.mockCPUUsage, transaction: transaction) + try assertMetricValue(measurements: measurements, key: kSentryMetricProfilerSerializationKeyCPUUsage, numberOfReadings: expectedUsageReadings, expectedValue: fixture.mockCPUUsage, transaction: transaction, expectedUnits: kSentryMetricProfilerSerializationUnitPercentage) - try assertMetricValue(measurements: measurements, key: kSentryMetricProfilerSerializationKeyMemoryFootprint, numberOfReadings: expectedUsageReadings, expectedValue: fixture.mockMemoryFootprint, transaction: transaction) + try assertMetricValue(measurements: measurements, key: kSentryMetricProfilerSerializationKeyMemoryFootprint, numberOfReadings: expectedUsageReadings, expectedValue: fixture.mockMemoryFootprint, transaction: transaction, expectedUnits: kSentryMetricProfilerSerializationUnitBytes) // we wind up with one less energy reading. since we must use the difference between readings to get actual values, the first one is only the baseline reading. - try assertMetricValue(measurements: measurements, key: kSentryMetricProfilerSerializationKeyCPUEnergyUsage, numberOfReadings: expectedUsageReadings - 1, expectedValue: fixture.mockEnergyUsage, transaction: transaction) + try assertMetricValue(measurements: measurements, key: kSentryMetricProfilerSerializationKeyCPUEnergyUsage, numberOfReadings: expectedUsageReadings - 1, expectedValue: fixture.mockEnergyUsage, transaction: transaction, expectedUnits: kSentryMetricProfilerSerializationUnitNanoJoules) #if !os(macOS) try assertMetricEntries(measurements: measurements, key: kSentryProfilerSerializationKeySlowFrameRenders, expectedEntries: fixture.expectedSlowFrames, transaction: transaction) @@ -655,7 +655,7 @@ private extension SentryProfilerSwiftTests { } } - func assertMetricValue(measurements: [String: Any], key: String, numberOfReadings: Int, expectedValue: T? = nil, transaction: Transaction) throws { + func assertMetricValue(measurements: [String: Any], key: String, numberOfReadings: Int, expectedValue: T? = nil, transaction: Transaction, expectedUnits: String) throws { let metricContainer = try XCTUnwrap(measurements[key] as? [String: Any]) let values = try XCTUnwrap(metricContainer["values"] as? [[String: Any]]) XCTAssertEqual(values.count, numberOfReadings, "Wrong number of values under \(key)") @@ -666,6 +666,9 @@ private extension SentryProfilerSwiftTests { let timestamp = try XCTUnwrap(values[0]["elapsed_since_start_ns"] as? NSString) try assertTimestampOccursWithinTransaction(timestamp: timestamp, transaction: transaction) + + let actualUnits = try XCTUnwrap(metricContainer["unit"] as? String) + XCTAssertEqual(actualUnits, expectedUnits) } }