Skip to content

make metrics collection independed from metrics creation #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 24, 2021
Merged
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
18 changes: 8 additions & 10 deletions Sources/Prometheus/Prometheus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ public class PrometheusClient {
/// - Parameters:
/// - succeed: Closure that will be called with a newline separated string with metrics for all Metrics this PrometheusClient handles
public func collect(_ succeed: (String) -> ()) {
self.lock.withLock {
succeed(self.metrics.isEmpty ? "": "\(self.metrics.values.map { $0.collect() }.joined(separator: "\n"))\n")
}
let metrics = self.lock.withLock { self.metrics }
succeed(metrics.isEmpty ? "" : "\(metrics.values.map { $0.collect() }.joined(separator: "\n"))\n")
}

/// Creates prometheus formatted metrics
Expand All @@ -43,14 +42,13 @@ public class PrometheusClient {
/// - Parameters:
/// - succeed: Closure that will be called with a `ByteBuffer` containing a newline separated string with metrics for all Metrics this PrometheusClient handles
public func collect(_ succeed: (ByteBuffer) -> ()) {
self.lock.withLock {
var buffer = ByteBufferAllocator().buffer(capacity: 0)
self.metrics.values.forEach {
$0.collect(into: &buffer)
buffer.writeString("\n")
}
succeed(buffer)
var buffer = ByteBufferAllocator().buffer(capacity: 0)
let metrics = self.lock.withLock { self.metrics }
metrics.values.forEach {
$0.collect(into: &buffer)
buffer.writeString("\n")
}
succeed(buffer)
}

/// Creates prometheus formatted metrics
Expand Down