Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions Sources/Prometheus/Histogram.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,28 @@ public final class Histogram<Value: Bucketable>: Sendable {

@usableFromInline
struct State: Sendable {
@usableFromInline var buckets: [(Value, Int)]
@usableFromInline
struct ValueCount: Sendable {
@usableFromInline
let value: Value
@usableFromInline
var count: Int

@usableFromInline
init(value: Value, count: Int) {
self.value = value
self.count = count
}
}
@usableFromInline var buckets: [ValueCount]
@usableFromInline var sum: Value
@usableFromInline var count: Int

@inlinable
init(buckets: [Value]) {
self.sum = .zero
self.count = 0
self.buckets = buckets.map { ($0, 0) }
self.buckets = buckets.map { .init(value: $0, count: 0) }
}
}

Expand All @@ -58,11 +71,25 @@ public final class Histogram<Value: Bucketable>: Sendable {

public func record(_ value: Value) {
self.box.withLockedValue { state in
for i in state.buckets.startIndex..<state.buckets.endIndex {
if state.buckets[i].0 >= value {
state.buckets[i].1 += 1

var count = state.buckets.count
var left = state.buckets.startIndex

while count > 0 {
let half = count / 2
let mid = state.buckets.index(left, offsetBy: half)
if state.buckets[mid].value >= value {
count = half
} else {
left = state.buckets.index(after: mid)
count -= half + 1
}
}

if state.buckets.indices.contains(left) {
state.buckets[left].count += 1
}

state.sum += value
state.count += 1
}
Expand Down Expand Up @@ -96,10 +123,10 @@ extension Histogram: PrometheusMetric {
buffer.append(UInt8(ascii: #","#))
}
buffer.append(contentsOf: #"le=""#.utf8)
buffer.append(contentsOf: "\(bucket.0.bucketRepresentation)".utf8)
buffer.append(contentsOf: "\(bucket.value.bucketRepresentation)".utf8)
buffer.append(UInt8(ascii: #"""#))
buffer.append(contentsOf: #"} "#.utf8)
buffer.append(contentsOf: "\(bucket.1)".utf8)
buffer.append(contentsOf: "\(bucket.count)".utf8)
buffer.append(contentsOf: #"\#n"#.utf8)
}

Expand Down