Skip to content

Commit 196573e

Browse files
avolokhovMrLotU
andauthored
make metrics collection independed from metrics creation (#60)
Co-authored-by: Jari (LotU) <[email protected]>
1 parent b31a6e6 commit 196573e

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

Sources/Prometheus/Prometheus.swift

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ public class PrometheusClient {
2525
/// - Parameters:
2626
/// - succeed: Closure that will be called with a newline separated string with metrics for all Metrics this PrometheusClient handles
2727
public func collect(_ succeed: (String) -> ()) {
28-
self.lock.withLock {
29-
succeed(self.metrics.isEmpty ? "": "\(self.metrics.values.map { $0.collect() }.joined(separator: "\n"))\n")
30-
}
28+
let metrics = self.lock.withLock { self.metrics }
29+
succeed(metrics.isEmpty ? "" : "\(metrics.values.map { $0.collect() }.joined(separator: "\n"))\n")
3130
}
3231

3332
/// Creates prometheus formatted metrics
@@ -43,14 +42,13 @@ public class PrometheusClient {
4342
/// - Parameters:
4443
/// - succeed: Closure that will be called with a `ByteBuffer` containing a newline separated string with metrics for all Metrics this PrometheusClient handles
4544
public func collect(_ succeed: (ByteBuffer) -> ()) {
46-
self.lock.withLock {
47-
var buffer = ByteBufferAllocator().buffer(capacity: 0)
48-
self.metrics.values.forEach {
49-
$0.collect(into: &buffer)
50-
buffer.writeString("\n")
51-
}
52-
succeed(buffer)
45+
var buffer = ByteBufferAllocator().buffer(capacity: 0)
46+
let metrics = self.lock.withLock { self.metrics }
47+
metrics.values.forEach {
48+
$0.collect(into: &buffer)
49+
buffer.writeString("\n")
5350
}
51+
succeed(buffer)
5452
}
5553

5654
/// Creates prometheus formatted metrics

0 commit comments

Comments
 (0)