Skip to content

Commit ac92dfd

Browse files
authored
Fix add, introduce isEmpty (#42)
* Add isEmpty, fix add() isEmpty * New tests + organization * Warnings * precondition -> assert * PR feedback
1 parent b13f0ce commit ac92dfd

42 files changed

Lines changed: 3433 additions & 17 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Sources/NautilusTelemetry/Exporters/Exporter+Metrics.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ extension Exporter {
5959
let startTimeUnixNano = convertToOTLP(time: counter.startTime)
6060

6161
let doubleValue: Double? = asDouble(value)
62-
var intValueString: String? = asIntString(value)
62+
let intValueString: String? = asIntString(value)
6363

6464
let timeUnixNano = convertToOTLP(time: ContinuousClock.now)
6565

@@ -107,7 +107,7 @@ extension Exporter {
107107
let startTimeUnixNano = convertToOTLP(time: counter.startTime)
108108

109109
let doubleValue: Double? = asDouble(value)
110-
var intValueString: String? = asIntString(value)
110+
let intValueString: String? = asIntString(value)
111111

112112
let timeUnixNano = convertToOTLP(time: ContinuousClock.now)
113113

@@ -155,7 +155,7 @@ extension Exporter {
155155
let startTimeUnixNano = convertToOTLP(time: counter.startTime)
156156

157157
let doubleValue: Double? = asDouble(value)
158-
var intValueString: String? = asIntString(value)
158+
let intValueString: String? = asIntString(value)
159159

160160
let timeUnixNano = convertToOTLP(time: ContinuousClock.now)
161161

@@ -203,7 +203,7 @@ extension Exporter {
203203
let startTimeUnixNano = convertToOTLP(time: gauge.startTime)
204204

205205
let doubleValue: Double? = asDouble(value)
206-
var intValueString: String? = asIntString(value)
206+
let intValueString: String? = asIntString(value)
207207

208208
let timeUnixNano = convertToOTLP(time: ContinuousClock.now)
209209

Sources/NautilusTelemetry/ResourceAttributes.swift renamed to Sources/NautilusTelemetry/Instrumentation/ResourceAttributes.swift

File renamed without changes.

Sources/NautilusTelemetry/Metrics/Counter.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,14 @@ public class Counter<T: MetricNumeric>: Instrument, ExportableInstrument {
2929

3030
public var isMonotonic: Bool { true }
3131

32+
public var isEmpty: Bool { lock.withLock { values.isEmpty } }
33+
3234
public func add(_ number: T, attributes: TelemetryAttributes = [:]) {
33-
precondition(number >= 0, "counters can only be increased")
35+
if isMonotonic, number < 0 {
36+
// UpDownCounter is not monotonic
37+
assert(false, "monotonic counters can only be increased")
38+
return
39+
}
3440
lock.withLockUnchecked {
3541
values.add(number, attributes: attributes)
3642
}

Sources/NautilusTelemetry/Metrics/Histogram.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,14 @@ public class Histogram<T: MetricNumeric>: Instrument, ExportableInstrument {
3434
public private(set) var endTime: ContinuousClock.Instant? = nil
3535
public var aggregationTemporality = AggregationTemporality.delta
3636

37+
public var isEmpty: Bool { lock.withLock { values.isEmpty } }
38+
3739
public func record(_ number: T, attributes: TelemetryAttributes = [:]) {
38-
precondition(number >= 0, "counters can only be increased")
40+
if number < 0 {
41+
assert(false, "histograms can only be increased")
42+
return
43+
}
44+
3945
lock.withLockUnchecked {
4046
values.record(number, attributes: attributes)
4147
}

Sources/NautilusTelemetry/Metrics/HistogramValues.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ struct HistogramBuckets<T: MetricNumeric> {
2626
var data: [UInt64]
2727
let explicitBounds: [T]
2828

29+
var isEmpty: Bool {
30+
return data.isEmpty || data.allSatisfy { $0 == 0 }
31+
}
32+
2933
mutating func record(_ number: T) {
3034
sum += number
3135
count += 1
@@ -42,6 +46,7 @@ struct HistogramBuckets<T: MetricNumeric> {
4246
// In the range of (lastBound...infinity).
4347
data[count] += 1
4448
}
49+
4550
}
4651

4752
// MARK: - HistogramValues
@@ -64,6 +69,10 @@ struct HistogramValues<T: MetricNumeric> {
6469

6570
var values = [TelemetryAttributes: HistogramBuckets<T>]()
6671

72+
var isEmpty: Bool {
73+
values.isEmpty || values.values.allSatisfy { $0.isEmpty }
74+
}
75+
6776
mutating func record(_ number: T, attributes: TelemetryAttributes = [:]) {
6877
var value = values[attributes] ?? HistogramBuckets<T>(explicitBounds: explicitBounds)
6978
value.record(number)
@@ -81,4 +90,5 @@ struct HistogramValues<T: MetricNumeric> {
8190

8291
return copy
8392
}
93+
8494
}

Sources/NautilusTelemetry/Metrics/Instrument.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public protocol Instrument: AnyObject {
3636
/// This model assumes delta mode metrics.
3737
/// TBD: figure out aggregation model: https://opentelemetry.io/docs/specs/otel/metrics/data-model/#sums-delta-to-cumulative
3838
func snapshotAndReset() -> Instrument
39+
40+
var isEmpty: Bool { get }
3941
}
4042

4143
// MARK: - ExportableInstrument

Sources/NautilusTelemetry/Metrics/MetricValues.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ struct MetricValues<T: MetricNumeric> {
1212

1313
var values = [TelemetryAttributes: T]()
1414

15+
var isEmpty: Bool {
16+
values.isEmpty || values.values.allSatisfy { $0 == 0 }
17+
}
18+
1519
mutating func add(_ number: T, attributes: TelemetryAttributes = [:]) {
16-
var metricValue = values[attributes] ?? number
20+
var metricValue = values[attributes] ?? 0
1721
metricValue += number
1822
values[attributes] = metricValue
1923
}

Sources/NautilusTelemetry/Metrics/ObservableCounter.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public class ObservableCounter<T: MetricNumeric>: Instrument, ExportableInstrume
3030

3131
public var isMonotonic: Bool { true }
3232

33+
public var isEmpty: Bool { lock.withLock { values.isEmpty } }
34+
3335
/// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#asynchronous-counter-creation
3436
public func observe(_ number: T, attributes: TelemetryAttributes = [:]) {
3537
lock.withLockUnchecked {

Sources/NautilusTelemetry/Metrics/ObservableGauge.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class ObservableGauge<T: MetricNumeric>: Instrument, ExportableInstrument
2828
public private(set) var endTime: ContinuousClock.Instant? = nil
2929
public let aggregationTemporality = AggregationTemporality.unspecified
3030

31+
public var isEmpty: Bool { false }
32+
3133
public func observe(_ number: T, attributes: TelemetryAttributes = [:]) {
3234
lock.withLockUnchecked {
3335
values.set(number, attributes: attributes)

Sources/NautilusTelemetry/Metrics/ObservableUpDownCounter.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public class ObservableUpDownCounter<T: MetricNumeric>: Instrument, ExportableIn
3030

3131
public var isMonotonic: Bool { false }
3232

33+
public var isEmpty: Bool { lock.withLock { values.isEmpty } }
34+
3335
public func observe(_ number: T, attributes: TelemetryAttributes = [:]) {
3436
lock.withLockUnchecked {
3537
values.set(number, attributes: attributes)

0 commit comments

Comments
 (0)