From 3f5891ab361445224c3a383aa7682c8a780ca348 Mon Sep 17 00:00:00 2001 From: Armand Grillet <2117580+armandgrillet@users.noreply.github.com> Date: Sun, 15 Oct 2023 14:44:09 +0200 Subject: [PATCH 1/6] Switch from swiftformat to swift-format --- .swift-format | 7 ++ .swiftformat | 24 ----- Sources/Prometheus/Counter.swift | 4 +- Sources/Prometheus/Gauge.swift | 2 +- Sources/Prometheus/NIOLock.swift | 93 ++++++++++--------- .../PrometheusCollectorRegistry.swift | 36 ++++--- docker/Dockerfile | 8 +- docker/docker-compose.yaml | 4 +- scripts/soundness.sh | 19 ++-- 9 files changed, 94 insertions(+), 103 deletions(-) create mode 100644 .swift-format delete mode 100644 .swiftformat diff --git a/.swift-format b/.swift-format new file mode 100644 index 0000000..7d3c122 --- /dev/null +++ b/.swift-format @@ -0,0 +1,7 @@ +{ + "version": 1, + "lineLength": 1000, + "indentation": { + "spaces": 4 + } +} diff --git a/.swiftformat b/.swiftformat deleted file mode 100644 index b5e7f9e..0000000 --- a/.swiftformat +++ /dev/null @@ -1,24 +0,0 @@ -# file options - ---swiftversion 5.7 ---exclude .build - -# format options - ---self insert ---patternlet inline ---ranges nospace ---stripunusedargs unnamed-only ---ifdef no-indent ---extensionacl on-declarations ---disable typeSugar # https://github.com/nicklockwood/SwiftFormat/issues/636 ---disable andOperator ---disable wrapMultilineStatementBraces ---disable enumNamespaces ---disable redundantExtensionACL ---disable redundantReturn ---disable preferKeyPath ---disable sortedSwitchCases ---disable numberFormatting - -# rules diff --git a/Sources/Prometheus/Counter.swift b/Sources/Prometheus/Counter.swift index 5c9ab81..035ad2b 100644 --- a/Sources/Prometheus/Counter.swift +++ b/Sources/Prometheus/Counter.swift @@ -15,8 +15,8 @@ import Atomics import CoreMetrics -/// A counter is a cumulative metric that represents a single monotonically increasing counter whose value -/// can only increase or be ``reset()`` to zero on restart. +/// A counter is a cumulative metric that represents a single monotonically increasing +/// counter whose value can only increase or be ``reset()`` to zero on restart. /// /// For example, you can use a counter to represent the number of requests served, tasks completed, or errors. /// diff --git a/Sources/Prometheus/Gauge.swift b/Sources/Prometheus/Gauge.swift index 78a8346..05cddcb 100644 --- a/Sources/Prometheus/Gauge.swift +++ b/Sources/Prometheus/Gauge.swift @@ -84,7 +84,7 @@ extension Gauge: CoreMetrics.MeterHandler { public func set(_ value: Double) { self.set(to: value) } - + public func set(_ value: Int64) { self.set(to: Double(value)) } diff --git a/Sources/Prometheus/NIOLock.swift b/Sources/Prometheus/NIOLock.swift index b8508dc..c4be246 100644 --- a/Sources/Prometheus/NIOLock.swift +++ b/Sources/Prometheus/NIOLock.swift @@ -26,82 +26,82 @@ //===----------------------------------------------------------------------===// #if canImport(Darwin) -import Darwin + import Darwin #elseif os(Windows) -import ucrt -import WinSDK + import ucrt + import WinSDK #elseif canImport(Glibc) -import Glibc + import Glibc #elseif canImport(Musl) -import Musl + import Musl #else -#error("The concurrency NIOLock module was unable to identify your C library.") + #error("The concurrency NIOLock module was unable to identify your C library.") #endif #if os(Windows) -@usableFromInline -typealias LockPrimitive = SRWLOCK + @usableFromInline + typealias LockPrimitive = SRWLOCK #else -@usableFromInline -typealias LockPrimitive = pthread_mutex_t + @usableFromInline + typealias LockPrimitive = pthread_mutex_t #endif @usableFromInline -enum LockOperations { } +enum LockOperations {} extension LockOperations { @inlinable static func create(_ mutex: UnsafeMutablePointer) { mutex.assertValidAlignment() -#if os(Windows) - InitializeSRWLock(mutex) -#else - var attr = pthread_mutexattr_t() - pthread_mutexattr_init(&attr) - debugOnly { - pthread_mutexattr_settype(&attr, .init(PTHREAD_MUTEX_ERRORCHECK)) - } - - let err = pthread_mutex_init(mutex, &attr) - precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") -#endif + #if os(Windows) + InitializeSRWLock(mutex) + #else + var attr = pthread_mutexattr_t() + pthread_mutexattr_init(&attr) + debugOnly { + pthread_mutexattr_settype(&attr, .init(PTHREAD_MUTEX_ERRORCHECK)) + } + + let err = pthread_mutex_init(mutex, &attr) + precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") + #endif } @inlinable static func destroy(_ mutex: UnsafeMutablePointer) { mutex.assertValidAlignment() -#if os(Windows) - // SRWLOCK does not need to be free'd -#else - let err = pthread_mutex_destroy(mutex) - precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") -#endif + #if os(Windows) + // SRWLOCK does not need to be free'd + #else + let err = pthread_mutex_destroy(mutex) + precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") + #endif } @inlinable static func lock(_ mutex: UnsafeMutablePointer) { mutex.assertValidAlignment() -#if os(Windows) - AcquireSRWLockExclusive(mutex) -#else - let err = pthread_mutex_lock(mutex) - precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") -#endif + #if os(Windows) + AcquireSRWLockExclusive(mutex) + #else + let err = pthread_mutex_lock(mutex) + precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") + #endif } @inlinable static func unlock(_ mutex: UnsafeMutablePointer) { mutex.assertValidAlignment() -#if os(Windows) - ReleaseSRWLockExclusive(mutex) -#else - let err = pthread_mutex_unlock(mutex) - precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") -#endif + #if os(Windows) + ReleaseSRWLockExclusive(mutex) + #else + let err = pthread_mutex_unlock(mutex) + precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") + #endif } } @@ -188,7 +188,7 @@ final class LockStorage: ManagedBuffer { } } -extension LockStorage: @unchecked Sendable { } +extension LockStorage: @unchecked Sendable {} /// A threading lock based on `libpthread` instead of `libdispatch`. /// @@ -251,7 +251,7 @@ extension NIOLock { } @inlinable - func withLockVoid(_ body: () throws -> Void) rethrows -> Void { + func withLockVoid(_ body: () throws -> Void) rethrows { try self.withLock(body) } } @@ -272,6 +272,9 @@ extension UnsafeMutablePointer { /// https://forums.swift.org/t/support-debug-only-code/11037 for a discussion. @inlinable internal func debugOnly(_ body: () -> Void) { - assert({ body(); return true }()) + assert( + { + body() + return true + }()) } - diff --git a/Sources/Prometheus/PrometheusCollectorRegistry.swift b/Sources/Prometheus/PrometheusCollectorRegistry.swift index b12c451..2b8e435 100644 --- a/Sources/Prometheus/PrometheusCollectorRegistry.swift +++ b/Sources/Prometheus/PrometheusCollectorRegistry.swift @@ -76,7 +76,8 @@ public final class PrometheusCollectorRegistry: Sendable { self.box.withLockedValue { store -> Counter in if let value = store[name] { guard case .counter(let counter) = value else { - fatalError(""" + fatalError( + """ Could not make Counter with name: \(name), since another metric type already exists for the same name. """ @@ -109,7 +110,8 @@ public final class PrometheusCollectorRegistry: Sendable { return self.box.withLockedValue { store -> Counter in if let value = store[name] { guard case .counterWithLabels(let labelNames, var dimensionLookup) = value else { - fatalError(""" + fatalError( + """ Could not make Counter with name: \(name) and labels: \(labels), since another metric type already exists for the same name. """ @@ -123,7 +125,8 @@ public final class PrometheusCollectorRegistry: Sendable { // check if all labels match the already existing ones. if labelNames != labels.allLabelNames { - fatalError(""" + fatalError( + """ Could not make Counter with name: \(name) and labels: \(labels), since the label names don't match the label names of previously registered Counters with the same name. @@ -156,7 +159,8 @@ public final class PrometheusCollectorRegistry: Sendable { self.box.withLockedValue { store -> Gauge in if let value = store[name] { guard case .gauge(let gauge) = value else { - fatalError(""" + fatalError( + """ Could not make Gauge with name: \(name), since another metric type already exists for the same name. """ @@ -189,7 +193,8 @@ public final class PrometheusCollectorRegistry: Sendable { return self.box.withLockedValue { store -> Gauge in if let value = store[name] { guard case .gaugeWithLabels(let labelNames, var dimensionLookup) = value else { - fatalError(""" + fatalError( + """ Could not make Gauge with name: \(name) and labels: \(labels), since another metric type already exists for the same name. """ @@ -203,7 +208,8 @@ public final class PrometheusCollectorRegistry: Sendable { // check if all labels match the already existing ones. if labelNames != labels.allLabelNames { - fatalError(""" + fatalError( + """ Could not make Gauge with name: \(name) and labels: \(labels), since the label names don't match the label names of previously registered Gauges with the same name. @@ -237,7 +243,8 @@ public final class PrometheusCollectorRegistry: Sendable { self.box.withLockedValue { store -> DurationHistogram in if let value = store[name] { guard case .durationHistogram(let histogram) = value else { - fatalError(""" + fatalError( + """ Could not make DurationHistogram with name: \(name), since another metric type already exists for the same name. """ @@ -271,7 +278,8 @@ public final class PrometheusCollectorRegistry: Sendable { return self.box.withLockedValue { store -> DurationHistogram in if let value = store[name] { guard case .durationHistogramWithLabels(let labelNames, var dimensionLookup, let storedBuckets) = value else { - fatalError(""" + fatalError( + """ Could not make DurationHistogram with name: \(name) and labels: \(labels), since another metric type already exists for the same name. """ @@ -285,7 +293,8 @@ public final class PrometheusCollectorRegistry: Sendable { // check if all labels match the already existing ones. if labelNames != labels.allLabelNames { - fatalError(""" + fatalError( + """ Could not make DurationHistogram with name: \(name) and labels: \(labels), since the label names don't match the label names of previously registered Gauges with the same name. @@ -293,7 +302,8 @@ public final class PrometheusCollectorRegistry: Sendable { ) } if storedBuckets != buckets { - fatalError(""" + fatalError( + """ Could not make DurationHistogram with name: \(name) and labels: \(labels), since the buckets don't match the buckets of previously registered TimeHistograms with the same name. @@ -386,7 +396,7 @@ public final class PrometheusCollectorRegistry: Sendable { } // MARK: Destroying Metrics - + /// Unregisters a ``Counter`` from the ``PrometheusCollectorRegistry``. This means that the provided ``Counter`` /// will not be included in future ``emit(into:)`` calls. /// @@ -526,7 +536,7 @@ public final class PrometheusCollectorRegistry: Sendable { } } -extension Array<(String, String)> { +extension [(String, String)] { fileprivate var allLabelNames: [String] { var result = [String]() result.reserveCapacity(self.count) @@ -539,7 +549,7 @@ extension Array<(String, String)> { } } -extension Array { +extension [UInt8] { fileprivate mutating func addTypeLine(label: String, type: String) { self.append(contentsOf: #"# TYPE "#.utf8) self.append(contentsOf: label.utf8) diff --git a/docker/Dockerfile b/docker/Dockerfile index 2d1e57d..b6fd80c 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -26,9 +26,7 @@ RUN if [ "${ubuntu_version}" = "focal" ] ; then gem install jazzy; fi RUN mkdir -p $HOME/.tools RUN echo 'export PATH="$HOME/.tools:$PATH"' >> $HOME/.profile -# swiftformat (until part of the toolchain) - -ARG swiftformat_version=0.48.8 -RUN git clone --branch $swiftformat_version --depth 1 https://github.com/nicklockwood/SwiftFormat $HOME/.tools/swift-format +# swift-format (until part of the toolchain) +RUN git clone --branch release/$swift_version --depth 1 https://github.com/apple/swift-format $HOME/.tools/swift-format RUN cd $HOME/.tools/swift-format && swift build -c release -RUN ln -s $HOME/.tools/swift-format/.build/release/swiftformat $HOME/.tools/swiftformat +RUN ln -s $HOME/.tools/swift-format/.build/release/swift-format $HOME/.tools/swift-format diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml index a53b7e5..26b0b41 100644 --- a/docker/docker-compose.yaml +++ b/docker/docker-compose.yaml @@ -1,6 +1,6 @@ # this file is not designed to be run directly # instead, use the docker-compose.. files -# eg docker-compose -f docker/docker-compose.yaml -f docker/docker-compose.1804.50.yaml run test +# eg docker-compose -f docker/docker-compose.yaml -f docker/docker-compose.2204.57.yaml run test version: "3" services: @@ -29,7 +29,7 @@ services: documentation-check: <<: *common command: /bin/bash -xcl "./scripts/check-docs.sh" - + test: <<: *common depends_on: [runtime-setup] diff --git a/scripts/soundness.sh b/scripts/soundness.sh index a49c74e..2bd575e 100755 --- a/scripts/soundness.sh +++ b/scripts/soundness.sh @@ -37,17 +37,14 @@ if git grep --color=never -i "${unacceptable_terms[@]}" > /dev/null; then fi printf "\033[0;32mokay.\033[0m\n" -#printf "=> Checking format... " -#FIRST_OUT="$(git status --porcelain)" -#swiftformat . > /dev/null 2>&1 -#SECOND_OUT="$(git status --porcelain)" -#if [[ "$FIRST_OUT" != "$SECOND_OUT" ]]; then -# printf "\033[0;31mformatting issues!\033[0m\n" -# git --no-pager diff -# exit 1 -#else -# printf "\033[0;32mokay.\033[0m\n" -#fi +printf "=> Checking format... " +output=$(swift-format lint --configuration .swift-format --recursive Sources 2>&1) # Capture the command's output +if [ -n "$output" ]; then + printf "\033[0;31mformatting issues!\033[0m\n" + exit 1 +else + printf "\033[0;32mokay.\033[0m\n" +fi printf "=> Checking license headers\n" tmp=$(mktemp /tmp/.prometheus-soundness_XXXXXX) From 3bdbac41714aeb1ba01095362dcea9687dcc6fdd Mon Sep 17 00:00:00 2001 From: Armand Grillet <2117580+armandgrillet@users.noreply.github.com> Date: Mon, 16 Oct 2023 07:46:01 +0200 Subject: [PATCH 2/6] Format Tests according to swift-format --- Tests/PrometheusTests/CounterTests.swift | 107 ++++++++------ Tests/PrometheusTests/GaugeTests.swift | 107 ++++++++------ Tests/PrometheusTests/HistogramTests.swift | 136 ++++++++++++------ .../PrometheusCollectorRegistryTests.swift | 37 +++-- .../PrometheusMetricsFactoryTests.swift | 28 ++-- scripts/soundness.sh | 2 +- 6 files changed, 263 insertions(+), 154 deletions(-) diff --git a/Tests/PrometheusTests/CounterTests.swift b/Tests/PrometheusTests/CounterTests.swift index 08c948c..440a53e 100644 --- a/Tests/PrometheusTests/CounterTests.swift +++ b/Tests/PrometheusTests/CounterTests.swift @@ -12,8 +12,8 @@ // //===----------------------------------------------------------------------===// -import XCTest import Prometheus +import XCTest final class CounterTests: XCTestCase { func testCounterWithoutLabels() { @@ -22,17 +22,21 @@ final class CounterTests: XCTestCase { var buffer = [UInt8]() client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ - # TYPE foo counter - foo 0 + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ + # TYPE foo counter + foo 0 - """) + """) // Increment by 1 buffer.removeAll(keepingCapacity: true) counter.increment() client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo counter foo 1 @@ -43,41 +47,49 @@ final class CounterTests: XCTestCase { buffer.removeAll(keepingCapacity: true) counter.increment() client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ - # TYPE foo counter - foo 2 + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ + # TYPE foo counter + foo 2 - """) + """) // Increment by 2 buffer.removeAll(keepingCapacity: true) counter.increment(by: Int64(2)) client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ - # TYPE foo counter - foo 4 + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ + # TYPE foo counter + foo 4 - """) + """) // Increment by 2.5 buffer.removeAll(keepingCapacity: true) counter.increment(by: 2.5) client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ - # TYPE foo counter - foo 6.5 + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ + # TYPE foo counter + foo 6.5 - """) + """) // Reset buffer.removeAll(keepingCapacity: true) counter.reset() client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ - # TYPE foo counter - foo 0 + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ + # TYPE foo counter + foo 0 - """) + """) } func testCounterWithLabels() { @@ -86,51 +98,60 @@ final class CounterTests: XCTestCase { var buffer = [UInt8]() client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ - # TYPE foo counter - foo{bar="baz"} 0 + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ + # TYPE foo counter + foo{bar="baz"} 0 - """) + """) // Increment by 1 buffer.removeAll(keepingCapacity: true) counter.increment() client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ - # TYPE foo counter - foo{bar="baz"} 1 + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ + # TYPE foo counter + foo{bar="baz"} 1 - """) + """) // Increment by 1 buffer.removeAll(keepingCapacity: true) counter.increment() client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ - # TYPE foo counter - foo{bar="baz"} 2 + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ + # TYPE foo counter + foo{bar="baz"} 2 - """) + """) // Increment by 2 buffer.removeAll(keepingCapacity: true) counter.increment(by: Int64(2)) client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ - # TYPE foo counter - foo{bar="baz"} 4 + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ + # TYPE foo counter + foo{bar="baz"} 4 - """) + """) // Reset buffer.removeAll(keepingCapacity: true) counter.reset() client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ - # TYPE foo counter - foo{bar="baz"} 0 + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ + # TYPE foo counter + foo{bar="baz"} 0 - """) + """) } } - diff --git a/Tests/PrometheusTests/GaugeTests.swift b/Tests/PrometheusTests/GaugeTests.swift index 23a4e9f..ed361bf 100644 --- a/Tests/PrometheusTests/GaugeTests.swift +++ b/Tests/PrometheusTests/GaugeTests.swift @@ -12,8 +12,8 @@ // //===----------------------------------------------------------------------===// -import XCTest import Prometheus +import XCTest final class GaugeTests: XCTestCase { func testGaugeWithoutLabels() { @@ -22,51 +22,61 @@ final class GaugeTests: XCTestCase { var buffer = [UInt8]() client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ - # TYPE foo gauge - foo 0.0 + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ + # TYPE foo gauge + foo 0.0 - """) + """) // Set to 1 buffer.removeAll(keepingCapacity: true) gauge.record(Int64(1)) client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ - # TYPE foo gauge - foo 1.0 + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ + # TYPE foo gauge + foo 1.0 - """) + """) // Set to 2 buffer.removeAll(keepingCapacity: true) gauge.record(Int64(2)) client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ - # TYPE foo gauge - foo 2.0 + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ + # TYPE foo gauge + foo 2.0 - """) + """) // Set to 4 buffer.removeAll(keepingCapacity: true) gauge.record(Int64(4)) client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ - # TYPE foo gauge - foo 4.0 + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ + # TYPE foo gauge + foo 4.0 - """) + """) // Reset buffer.removeAll(keepingCapacity: true) gauge.record(Int64(0)) client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ - # TYPE foo gauge - foo 0.0 + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ + # TYPE foo gauge + foo 0.0 - """) + """) } func testGaugeWithLabels() { @@ -75,57 +85,67 @@ final class GaugeTests: XCTestCase { var buffer = [UInt8]() client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ - # TYPE foo gauge - foo{bar="baz"} 0.0 + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ + # TYPE foo gauge + foo{bar="baz"} 0.0 - """) + """) // Set to 1 buffer.removeAll(keepingCapacity: true) gauge.record(Int64(1)) client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ - # TYPE foo gauge - foo{bar="baz"} 1.0 + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ + # TYPE foo gauge + foo{bar="baz"} 1.0 - """) + """) // Set to 2 buffer.removeAll(keepingCapacity: true) gauge.record(Int64(2)) client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ - # TYPE foo gauge - foo{bar="baz"} 2.0 + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ + # TYPE foo gauge + foo{bar="baz"} 2.0 - """) + """) // Set to 4 buffer.removeAll(keepingCapacity: true) gauge.record(Int64(4)) client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ - # TYPE foo gauge - foo{bar="baz"} 4.0 + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ + # TYPE foo gauge + foo{bar="baz"} 4.0 - """) + """) // Reset buffer.removeAll(keepingCapacity: true) gauge.record(Int64(0)) client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ - # TYPE foo gauge - foo{bar="baz"} 0.0 + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ + # TYPE foo gauge + foo{bar="baz"} 0.0 - """) + """) } func testGaugeSetToFromMultipleTasks() async { let client = PrometheusCollectorRegistry() let gauge = client.makeGauge(name: "foo", labels: [("bar", "baz")]) - await withTaskGroup(of: Void.self){ group in + await withTaskGroup(of: Void.self) { group in for _ in 0..<100_000 { group.addTask { gauge.set(to: Double.random(in: 0..<20)) @@ -137,7 +157,7 @@ final class GaugeTests: XCTestCase { func testIncByFromMultipleTasks() async { let client = PrometheusCollectorRegistry() let gauge = client.makeGauge(name: "foo", labels: [("bar", "baz")]) - await withTaskGroup(of: Void.self){ group in + await withTaskGroup(of: Void.self) { group in for _ in 0..<100_000 { group.addTask { gauge.increment(by: Double.random(in: 0..<1)) @@ -146,4 +166,3 @@ final class GaugeTests: XCTestCase { } } } - diff --git a/Tests/PrometheusTests/HistogramTests.swift b/Tests/PrometheusTests/HistogramTests.swift index 9928189..c60376d 100644 --- a/Tests/PrometheusTests/HistogramTests.swift +++ b/Tests/PrometheusTests/HistogramTests.swift @@ -12,23 +12,29 @@ // //===----------------------------------------------------------------------===// -import XCTest import Prometheus +import XCTest final class HistogramTests: XCTestCase { func testHistogramWithoutDimensions() { let client = PrometheusCollectorRegistry() - let histogram = client.makeDurationHistogram(name: "foo", labels: [], buckets: [ - .milliseconds(100), - .milliseconds(250), - .milliseconds(500), - .seconds(1), - ]) + let histogram = client.makeDurationHistogram( + name: "foo", + labels: [], + buckets: [ + .milliseconds(100), + .milliseconds(250), + .milliseconds(500), + .seconds(1), + ] + ) var buffer = [UInt8]() client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo histogram foo_bucket{le="0.1"} 0 foo_bucket{le="0.25"} 0 @@ -43,9 +49,13 @@ final class HistogramTests: XCTestCase { // Record 400ms buffer.removeAll(keepingCapacity: true) - histogram.recordNanoseconds(400_000_000) // 400ms + histogram.recordNanoseconds(400_000_000) // 400ms client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String( + decoding: buffer, + as: Unicode.UTF8.self), + """ # TYPE foo histogram foo_bucket{le="0.1"} 0 foo_bucket{le="0.25"} 0 @@ -60,9 +70,11 @@ final class HistogramTests: XCTestCase { // Record 600ms buffer.removeAll(keepingCapacity: true) - histogram.recordNanoseconds(600_000_000) // 600ms + histogram.recordNanoseconds(600_000_000) // 600ms client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo histogram foo_bucket{le="0.1"} 0 foo_bucket{le="0.25"} 0 @@ -77,9 +89,11 @@ final class HistogramTests: XCTestCase { // Record 1200ms buffer.removeAll(keepingCapacity: true) - histogram.recordNanoseconds(1_200_000_000) // 1200ms + histogram.recordNanoseconds(1_200_000_000) // 1200ms client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo histogram foo_bucket{le="0.1"} 0 foo_bucket{le="0.25"} 0 @@ -94,9 +108,11 @@ final class HistogramTests: XCTestCase { // Record 80ms buffer.removeAll(keepingCapacity: true) - histogram.recordNanoseconds(80_000_000) // 80ms + histogram.recordNanoseconds(80_000_000) // 80ms client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo histogram foo_bucket{le="0.1"} 1 foo_bucket{le="0.25"} 1 @@ -112,16 +128,22 @@ final class HistogramTests: XCTestCase { func testHistogramWithOneDimension() { let client = PrometheusCollectorRegistry() - let histogram = client.makeDurationHistogram(name: "foo", labels: [("bar", "baz")], buckets: [ - .milliseconds(100), - .milliseconds(250), - .milliseconds(500), - .seconds(1), - ]) + let histogram = client.makeDurationHistogram( + name: "foo", + labels: [("bar", "baz")], + buckets: [ + .milliseconds(100), + .milliseconds(250), + .milliseconds(500), + .seconds(1), + ] + ) var buffer = [UInt8]() client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo histogram foo_bucket{bar="baz",le="0.1"} 0 foo_bucket{bar="baz",le="0.25"} 0 @@ -136,9 +158,11 @@ final class HistogramTests: XCTestCase { // Record 400ms buffer.removeAll(keepingCapacity: true) - histogram.recordNanoseconds(400_000_000) // 400ms + histogram.recordNanoseconds(400_000_000) // 400ms client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo histogram foo_bucket{bar="baz",le="0.1"} 0 foo_bucket{bar="baz",le="0.25"} 0 @@ -153,9 +177,11 @@ final class HistogramTests: XCTestCase { // Record 600ms buffer.removeAll(keepingCapacity: true) - histogram.recordNanoseconds(600_000_000) // 600ms + histogram.recordNanoseconds(600_000_000) // 600ms client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo histogram foo_bucket{bar="baz",le="0.1"} 0 foo_bucket{bar="baz",le="0.25"} 0 @@ -170,9 +196,11 @@ final class HistogramTests: XCTestCase { // Record 1200ms buffer.removeAll(keepingCapacity: true) - histogram.recordNanoseconds(1_200_000_000) // 1200ms + histogram.recordNanoseconds(1_200_000_000) // 1200ms client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo histogram foo_bucket{bar="baz",le="0.1"} 0 foo_bucket{bar="baz",le="0.25"} 0 @@ -187,9 +215,11 @@ final class HistogramTests: XCTestCase { // Record 80ms buffer.removeAll(keepingCapacity: true) - histogram.recordNanoseconds(80_000_000) // 80ms + histogram.recordNanoseconds(80_000_000) // 80ms client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo histogram foo_bucket{bar="baz",le="0.1"} 1 foo_bucket{bar="baz",le="0.25"} 1 @@ -205,16 +235,22 @@ final class HistogramTests: XCTestCase { func testHistogramWithTwoDimension() { let client = PrometheusCollectorRegistry() - let histogram = client.makeDurationHistogram(name: "foo", labels: [("bar", "baz"), ("abc", "xyz")], buckets: [ - .milliseconds(100), - .milliseconds(250), - .milliseconds(500), - .seconds(1), - ]) + let histogram = client.makeDurationHistogram( + name: "foo", + labels: [("bar", "baz"), ("abc", "xyz")], + buckets: [ + .milliseconds(100), + .milliseconds(250), + .milliseconds(500), + .seconds(1), + ] + ) var buffer = [UInt8]() client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo histogram foo_bucket{bar="baz",abc="xyz",le="0.1"} 0 foo_bucket{bar="baz",abc="xyz",le="0.25"} 0 @@ -229,9 +265,11 @@ final class HistogramTests: XCTestCase { // Record 400ms buffer.removeAll(keepingCapacity: true) - histogram.recordNanoseconds(400_000_000) // 400ms + histogram.recordNanoseconds(400_000_000) // 400ms client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo histogram foo_bucket{bar="baz",abc="xyz",le="0.1"} 0 foo_bucket{bar="baz",abc="xyz",le="0.25"} 0 @@ -246,9 +284,11 @@ final class HistogramTests: XCTestCase { // Record 600ms buffer.removeAll(keepingCapacity: true) - histogram.recordNanoseconds(600_000_000) // 600ms + histogram.recordNanoseconds(600_000_000) // 600ms client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo histogram foo_bucket{bar="baz",abc="xyz",le="0.1"} 0 foo_bucket{bar="baz",abc="xyz",le="0.25"} 0 @@ -263,9 +303,11 @@ final class HistogramTests: XCTestCase { // Record 1200ms buffer.removeAll(keepingCapacity: true) - histogram.recordNanoseconds(1_200_000_000) // 1200ms + histogram.recordNanoseconds(1_200_000_000) // 1200ms client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo histogram foo_bucket{bar="baz",abc="xyz",le="0.1"} 0 foo_bucket{bar="baz",abc="xyz",le="0.25"} 0 @@ -280,9 +322,11 @@ final class HistogramTests: XCTestCase { // Record 80ms buffer.removeAll(keepingCapacity: true) - histogram.recordNanoseconds(80_000_000) // 80ms + histogram.recordNanoseconds(80_000_000) // 80ms client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo histogram foo_bucket{bar="baz",abc="xyz",le="0.1"} 1 foo_bucket{bar="baz",abc="xyz",le="0.25"} 1 diff --git a/Tests/PrometheusTests/PrometheusCollectorRegistryTests.swift b/Tests/PrometheusTests/PrometheusCollectorRegistryTests.swift index 6766a5e..3475c72 100644 --- a/Tests/PrometheusTests/PrometheusCollectorRegistryTests.swift +++ b/Tests/PrometheusTests/PrometheusCollectorRegistryTests.swift @@ -12,8 +12,8 @@ // //===----------------------------------------------------------------------===// -import XCTest import Prometheus +import XCTest final class PrometheusCollectorRegistryTests: XCTestCase { func testAskingForTheSameCounterReturnsTheSameCounter() { @@ -27,7 +27,9 @@ final class PrometheusCollectorRegistryTests: XCTestCase { var buffer = [UInt8]() client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo counter foo 3 @@ -46,7 +48,9 @@ final class PrometheusCollectorRegistryTests: XCTestCase { var buffer = [UInt8]() client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo counter foo{bar="baz"} 3 @@ -71,7 +75,6 @@ final class PrometheusCollectorRegistryTests: XCTestCase { XCTAssert(output.contains(#"foo{bar="xyz"} 2\#n"#)) } - func testAskingForTheSameGaugeReturnsTheSameGauge() { let client = PrometheusCollectorRegistry() let gauge1 = client.makeGauge(name: "foo") @@ -84,7 +87,9 @@ final class PrometheusCollectorRegistryTests: XCTestCase { var buffer = [UInt8]() client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo gauge foo 3.0 @@ -104,7 +109,9 @@ final class PrometheusCollectorRegistryTests: XCTestCase { var buffer = [UInt8]() client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo gauge foo{bar="baz"} 3.0 @@ -112,7 +119,6 @@ final class PrometheusCollectorRegistryTests: XCTestCase { ) } - func testAskingForTheSameTimeHistogramReturnsTheSameTimeHistogram() { let client = PrometheusCollectorRegistry() let histogram1 = client.makeDurationHistogram(name: "foo", buckets: [.seconds(1), .seconds(2), .seconds(3)]) @@ -124,7 +130,9 @@ final class PrometheusCollectorRegistryTests: XCTestCase { var buffer = [UInt8]() client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo histogram foo_bucket{le="1.0"} 0 foo_bucket{le="2.0"} 1 @@ -148,7 +156,9 @@ final class PrometheusCollectorRegistryTests: XCTestCase { var buffer = [UInt8]() client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo histogram foo_bucket{bar="baz",le="1.0"} 0 foo_bucket{bar="baz",le="2.0"} 1 @@ -161,7 +171,6 @@ final class PrometheusCollectorRegistryTests: XCTestCase { ) } - func testAskingForTheSameValueHistogramReturnsTheSameTimeHistogram() { let client = PrometheusCollectorRegistry() let histogram1 = client.makeValueHistogram(name: "foo", buckets: [1, 2, 3]) @@ -173,7 +182,9 @@ final class PrometheusCollectorRegistryTests: XCTestCase { var buffer = [UInt8]() client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo histogram foo_bucket{le="1.0"} 0 foo_bucket{le="2.0"} 1 @@ -197,7 +208,9 @@ final class PrometheusCollectorRegistryTests: XCTestCase { var buffer = [UInt8]() client.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo histogram foo_bucket{bar="baz",le="1.0"} 0 foo_bucket{bar="baz",le="2.0"} 1 diff --git a/Tests/PrometheusTests/PrometheusMetricsFactoryTests.swift b/Tests/PrometheusTests/PrometheusMetricsFactoryTests.swift index c1592e2..714a9e4 100644 --- a/Tests/PrometheusTests/PrometheusMetricsFactoryTests.swift +++ b/Tests/PrometheusTests/PrometheusMetricsFactoryTests.swift @@ -12,8 +12,8 @@ // //===----------------------------------------------------------------------===// -import XCTest import Prometheus +import XCTest final class PrometheusMetricsFactoryTests: XCTestCase { func testMakeTimers() { @@ -52,7 +52,9 @@ final class PrometheusMetricsFactoryTests: XCTestCase { var buffer = [UInt8]() registry.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo counter foo{bar="baz"} 3.5 @@ -62,7 +64,9 @@ final class PrometheusMetricsFactoryTests: XCTestCase { factory.destroyCounter(maybeCounter) buffer.removeAll(keepingCapacity: true) registry.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo counter """ @@ -81,7 +85,9 @@ final class PrometheusMetricsFactoryTests: XCTestCase { var buffer = [UInt8]() registry.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo gauge foo{bar="baz"} -6.0 @@ -92,7 +98,9 @@ final class PrometheusMetricsFactoryTests: XCTestCase { maybeGauge.set(12.45) buffer.removeAll(keepingCapacity: true) registry.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo gauge foo{bar="baz"} 12.45 @@ -100,10 +108,12 @@ final class PrometheusMetricsFactoryTests: XCTestCase { ) // set to int value - maybeGauge.set(Int64(42)) // needs explicit cast... otherwise ambigious + maybeGauge.set(Int64(42)) // needs explicit cast... otherwise ambigious buffer.removeAll(keepingCapacity: true) registry.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo gauge foo{bar="baz"} 42.0 @@ -113,7 +123,9 @@ final class PrometheusMetricsFactoryTests: XCTestCase { factory.destroyMeter(maybeGauge) buffer.removeAll(keepingCapacity: true) registry.emit(into: &buffer) - XCTAssertEqual(String(decoding: buffer, as: Unicode.UTF8.self), """ + XCTAssertEqual( + String(decoding: buffer, as: Unicode.UTF8.self), + """ # TYPE foo gauge """ diff --git a/scripts/soundness.sh b/scripts/soundness.sh index 2bd575e..d6a884e 100755 --- a/scripts/soundness.sh +++ b/scripts/soundness.sh @@ -38,7 +38,7 @@ fi printf "\033[0;32mokay.\033[0m\n" printf "=> Checking format... " -output=$(swift-format lint --configuration .swift-format --recursive Sources 2>&1) # Capture the command's output +output=$(swift-format lint --configuration .swift-format --recursive Sources Tests 2>&1) # Capture the command's output if [ -n "$output" ]; then printf "\033[0;31mformatting issues!\033[0m\n" exit 1 From 36c683e118f3745957e8ff5404858d485e004e71 Mon Sep 17 00:00:00 2001 From: Armand Grillet <2117580+armandgrillet@users.noreply.github.com> Date: Mon, 16 Oct 2023 12:29:21 +0200 Subject: [PATCH 3/6] Apply same .swift-format as apple/swift-certificates --- .swift-format | 57 ++- Sources/Prometheus/Counter.swift | 6 +- Sources/Prometheus/Gauge.swift | 6 +- Sources/Prometheus/NIOLock.swift | 59 ++-- .../PrometheusCollectorRegistry.swift | 331 +++++++++--------- .../Prometheus/PrometheusMetricsFactory.swift | 16 +- Tests/PrometheusTests/CounterTests.swift | 30 +- Tests/PrometheusTests/GaugeTests.swift | 30 +- Tests/PrometheusTests/HistogramTests.swift | 3 +- .../PrometheusCollectorRegistryTests.swift | 12 +- 10 files changed, 320 insertions(+), 230 deletions(-) diff --git a/.swift-format b/.swift-format index 7d3c122..04f9fc0 100644 --- a/.swift-format +++ b/.swift-format @@ -1,7 +1,58 @@ { - "version": 1, - "lineLength": 1000, + "fileScopedDeclarationPrivacy": { + "accessLevel": "private" + }, "indentation": { "spaces": 4 - } + }, + "indentConditionalCompilationBlocks": false, + "indentSwitchCaseLabels": false, + "lineBreakAroundMultilineExpressionChainComponents": false, + "lineBreakBeforeControlFlowKeywords": false, + "lineBreakBeforeEachArgument": true, + "lineBreakBeforeEachGenericRequirement": true, + "lineLength": 120, + "maximumBlankLines": 1, + "prioritizeKeepingFunctionOutputTogether": true, + "respectsExistingLineBreaks": true, + "rules": { + "AllPublicDeclarationsHaveDocumentation": false, + "AlwaysUseLowerCamelCase": false, + "AmbiguousTrailingClosureOverload": true, + "BeginDocumentationCommentWithOneLineSummary": false, + "DoNotUseSemicolons": true, + "DontRepeatTypeInStaticProperties": true, + "FileScopedDeclarationPrivacy": true, + "FullyIndirectEnum": true, + "GroupNumericLiterals": true, + "IdentifiersMustBeASCII": true, + "NeverForceUnwrap": false, + "NeverUseForceTry": false, + "NeverUseImplicitlyUnwrappedOptionals": false, + "NoAccessLevelOnExtensionDeclaration": false, + "NoAssignmentInExpressions": true, + "NoBlockComments": true, + "NoCasesWithOnlyFallthrough": true, + "NoEmptyTrailingClosureParentheses": true, + "NoLabelsInCasePatterns": false, + "NoLeadingUnderscores": false, + "NoParensAroundConditions": true, + "NoVoidReturnOnFunctionSignature": true, + "OneCasePerLine": true, + "OneVariableDeclarationPerLine": true, + "OnlyOneTrailingClosureArgument": true, + "OrderedImports": false, + "ReturnVoidInsteadOfEmptyTuple": true, + "UseEarlyExits": true, + "UseLetInEveryBoundCaseVariable": false, + "UseShorthandTypeNames": true, + "UseSingleLinePropertyGetter": false, + "UseSynthesizedInitializer": false, + "UseTripleSlashForDocumentationComments": true, + "UseWhereClausesInForLoops": false, + "ValidateDocumentationComments": false + }, + "spacesAroundRangeFormationOperators": false, + "tabWidth": 8, + "version": 1 } diff --git a/Sources/Prometheus/Counter.swift b/Sources/Prometheus/Counter.swift index 035ad2b..6a90a6f 100644 --- a/Sources/Prometheus/Counter.swift +++ b/Sources/Prometheus/Counter.swift @@ -68,7 +68,11 @@ public final class Counter: Sendable { while true { let bits = self.floatAtomic.load(ordering: .relaxed) let value = Double(bitPattern: bits) + amount - let (exchanged, _) = self.floatAtomic.compareExchange(expected: bits, desired: value.bitPattern, ordering: .relaxed) + let (exchanged, _) = self.floatAtomic.compareExchange( + expected: bits, + desired: value.bitPattern, + ordering: .relaxed + ) if exchanged { break } diff --git a/Sources/Prometheus/Gauge.swift b/Sources/Prometheus/Gauge.swift index 05cddcb..c74b57c 100644 --- a/Sources/Prometheus/Gauge.swift +++ b/Sources/Prometheus/Gauge.swift @@ -58,7 +58,11 @@ public final class Gauge: Sendable { while true { let bits = self.atomic.load(ordering: .relaxed) let value = Double(bitPattern: bits) + amount - let (exchanged, _) = self.atomic.compareExchange(expected: bits, desired: value.bitPattern, ordering: .relaxed) + let (exchanged, _) = self.atomic.compareExchange( + expected: bits, + desired: value.bitPattern, + ordering: .relaxed + ) if exchanged { break } diff --git a/Sources/Prometheus/NIOLock.swift b/Sources/Prometheus/NIOLock.swift index c4be246..cf8b6a3 100644 --- a/Sources/Prometheus/NIOLock.swift +++ b/Sources/Prometheus/NIOLock.swift @@ -26,24 +26,24 @@ //===----------------------------------------------------------------------===// #if canImport(Darwin) - import Darwin +import Darwin #elseif os(Windows) - import ucrt - import WinSDK +import ucrt +import WinSDK #elseif canImport(Glibc) - import Glibc +import Glibc #elseif canImport(Musl) - import Musl +import Musl #else - #error("The concurrency NIOLock module was unable to identify your C library.") +#error("The concurrency NIOLock module was unable to identify your C library.") #endif #if os(Windows) - @usableFromInline - typealias LockPrimitive = SRWLOCK +@usableFromInline +typealias LockPrimitive = SRWLOCK #else - @usableFromInline - typealias LockPrimitive = pthread_mutex_t +@usableFromInline +typealias LockPrimitive = pthread_mutex_t #endif @usableFromInline @@ -55,16 +55,16 @@ extension LockOperations { mutex.assertValidAlignment() #if os(Windows) - InitializeSRWLock(mutex) + InitializeSRWLock(mutex) #else - var attr = pthread_mutexattr_t() - pthread_mutexattr_init(&attr) - debugOnly { - pthread_mutexattr_settype(&attr, .init(PTHREAD_MUTEX_ERRORCHECK)) - } - - let err = pthread_mutex_init(mutex, &attr) - precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") + var attr = pthread_mutexattr_t() + pthread_mutexattr_init(&attr) + debugOnly { + pthread_mutexattr_settype(&attr, .init(PTHREAD_MUTEX_ERRORCHECK)) + } + + let err = pthread_mutex_init(mutex, &attr) + precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") #endif } @@ -73,10 +73,10 @@ extension LockOperations { mutex.assertValidAlignment() #if os(Windows) - // SRWLOCK does not need to be free'd + // SRWLOCK does not need to be free'd #else - let err = pthread_mutex_destroy(mutex) - precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") + let err = pthread_mutex_destroy(mutex) + precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") #endif } @@ -85,10 +85,10 @@ extension LockOperations { mutex.assertValidAlignment() #if os(Windows) - AcquireSRWLockExclusive(mutex) + AcquireSRWLockExclusive(mutex) #else - let err = pthread_mutex_lock(mutex) - precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") + let err = pthread_mutex_lock(mutex) + precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") #endif } @@ -97,10 +97,10 @@ extension LockOperations { mutex.assertValidAlignment() #if os(Windows) - ReleaseSRWLockExclusive(mutex) + ReleaseSRWLockExclusive(mutex) #else - let err = pthread_mutex_unlock(mutex) - precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") + let err = pthread_mutex_unlock(mutex) + precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") #endif } } @@ -276,5 +276,6 @@ internal func debugOnly(_ body: () -> Void) { { body() return true - }()) + }() + ) } diff --git a/Sources/Prometheus/PrometheusCollectorRegistry.swift b/Sources/Prometheus/PrometheusCollectorRegistry.swift index 2b8e435..e25c65a 100644 --- a/Sources/Prometheus/PrometheusCollectorRegistry.swift +++ b/Sources/Prometheus/PrometheusCollectorRegistry.swift @@ -74,22 +74,21 @@ public final class PrometheusCollectorRegistry: Sendable { /// - Returns: A ``Counter`` that is registered with this ``PrometheusCollectorRegistry`` public func makeCounter(name: String) -> Counter { self.box.withLockedValue { store -> Counter in - if let value = store[name] { - guard case .counter(let counter) = value else { - fatalError( - """ - Could not make Counter with name: \(name), since another metric type - already exists for the same name. - """ - ) - } - - return counter - } else { + guard let value = store[name] else { let counter = Counter(name: name, labels: []) store[name] = .counter(counter) return counter } + guard case .counter(let counter) = value else { + fatalError( + """ + Could not make Counter with name: \(name), since another metric type + already exists for the same name. + """ + ) + } + + return counter } } @@ -108,43 +107,42 @@ public final class PrometheusCollectorRegistry: Sendable { } return self.box.withLockedValue { store -> Counter in - if let value = store[name] { - guard case .counterWithLabels(let labelNames, var dimensionLookup) = value else { - fatalError( - """ - Could not make Counter with name: \(name) and labels: \(labels), since another - metric type already exists for the same name. - """ - ) - } - - let key = LabelsKey(labels) - if let counter = dimensionLookup[key] { - return counter - } - - // check if all labels match the already existing ones. - if labelNames != labels.allLabelNames { - fatalError( - """ - Could not make Counter with name: \(name) and labels: \(labels), since the - label names don't match the label names of previously registered Counters with - the same name. - """ - ) - } - - let counter = Counter(name: name, labels: labels) - dimensionLookup[key] = counter - store[name] = .counterWithLabels(labelNames, dimensionLookup) - return counter - } else { + guard let value = store[name] else { let labelNames = labels.allLabelNames let counter = Counter(name: name, labels: labels) store[name] = .counterWithLabels(labelNames, [LabelsKey(labels): counter]) return counter } + guard case .counterWithLabels(let labelNames, var dimensionLookup) = value else { + fatalError( + """ + Could not make Counter with name: \(name) and labels: \(labels), since another + metric type already exists for the same name. + """ + ) + } + + let key = LabelsKey(labels) + if let counter = dimensionLookup[key] { + return counter + } + + // check if all labels match the already existing ones. + if labelNames != labels.allLabelNames { + fatalError( + """ + Could not make Counter with name: \(name) and labels: \(labels), since the + label names don't match the label names of previously registered Counters with + the same name. + """ + ) + } + + let counter = Counter(name: name, labels: labels) + dimensionLookup[key] = counter + store[name] = .counterWithLabels(labelNames, dimensionLookup) + return counter } } @@ -157,22 +155,21 @@ public final class PrometheusCollectorRegistry: Sendable { /// - Returns: A ``Gauge`` that is registered with this ``PrometheusCollectorRegistry`` public func makeGauge(name: String) -> Gauge { self.box.withLockedValue { store -> Gauge in - if let value = store[name] { - guard case .gauge(let gauge) = value else { - fatalError( - """ - Could not make Gauge with name: \(name), since another metric type already - exists for the same name. - """ - ) - } - - return gauge - } else { + guard let value = store[name] else { let gauge = Gauge(name: name, labels: []) store[name] = .gauge(gauge) return gauge } + guard case .gauge(let gauge) = value else { + fatalError( + """ + Could not make Gauge with name: \(name), since another metric type already + exists for the same name. + """ + ) + } + + return gauge } } @@ -191,43 +188,42 @@ public final class PrometheusCollectorRegistry: Sendable { } return self.box.withLockedValue { store -> Gauge in - if let value = store[name] { - guard case .gaugeWithLabels(let labelNames, var dimensionLookup) = value else { - fatalError( - """ - Could not make Gauge with name: \(name) and labels: \(labels), since another - metric type already exists for the same name. - """ - ) - } - - let key = LabelsKey(labels) - if let gauge = dimensionLookup[key] { - return gauge - } - - // check if all labels match the already existing ones. - if labelNames != labels.allLabelNames { - fatalError( - """ - Could not make Gauge with name: \(name) and labels: \(labels), since the - label names don't match the label names of previously registered Gauges with - the same name. - """ - ) - } - - let gauge = Gauge(name: name, labels: labels) - dimensionLookup[key] = gauge - store[name] = .gaugeWithLabels(labelNames, dimensionLookup) - return gauge - } else { + guard let value = store[name] else { let labelNames = labels.allLabelNames let gauge = Gauge(name: name, labels: labels) store[name] = .gaugeWithLabels(labelNames, [LabelsKey(labels): gauge]) return gauge } + guard case .gaugeWithLabels(let labelNames, var dimensionLookup) = value else { + fatalError( + """ + Could not make Gauge with name: \(name) and labels: \(labels), since another + metric type already exists for the same name. + """ + ) + } + + let key = LabelsKey(labels) + if let gauge = dimensionLookup[key] { + return gauge + } + + // check if all labels match the already existing ones. + if labelNames != labels.allLabelNames { + fatalError( + """ + Could not make Gauge with name: \(name) and labels: \(labels), since the + label names don't match the label names of previously registered Gauges with + the same name. + """ + ) + } + + let gauge = Gauge(name: name, labels: labels) + dimensionLookup[key] = gauge + store[name] = .gaugeWithLabels(labelNames, dimensionLookup) + return gauge } } @@ -241,22 +237,21 @@ public final class PrometheusCollectorRegistry: Sendable { /// - Returns: A ``DurationHistogram`` that is registered with this ``PrometheusCollectorRegistry`` public func makeDurationHistogram(name: String, buckets: [Duration]) -> DurationHistogram { self.box.withLockedValue { store -> DurationHistogram in - if let value = store[name] { - guard case .durationHistogram(let histogram) = value else { - fatalError( - """ - Could not make DurationHistogram with name: \(name), since another - metric type already exists for the same name. - """ - ) - } - - return histogram - } else { + guard let value = store[name] else { let gauge = DurationHistogram(name: name, labels: [], buckets: buckets) store[name] = .durationHistogram(gauge) return gauge } + guard case .durationHistogram(let histogram) = value else { + fatalError( + """ + Could not make DurationHistogram with name: \(name), since another + metric type already exists for the same name. + """ + ) + } + + return histogram } } @@ -270,60 +265,64 @@ public final class PrometheusCollectorRegistry: Sendable { /// what’s actually being measured in a Prometheus metric. /// - Parameter buckets: Define the buckets that shall be used within the ``DurationHistogram`` /// - Returns: A ``DurationHistogram`` that is registered with this ``PrometheusCollectorRegistry`` - public func makeDurationHistogram(name: String, labels: [(String, String)], buckets: [Duration]) -> DurationHistogram { + public func makeDurationHistogram( + name: String, + labels: [(String, String)], + buckets: [Duration] + ) -> DurationHistogram { guard !labels.isEmpty else { return self.makeDurationHistogram(name: name, buckets: buckets) } return self.box.withLockedValue { store -> DurationHistogram in - if let value = store[name] { - guard case .durationHistogramWithLabels(let labelNames, var dimensionLookup, let storedBuckets) = value else { - fatalError( - """ - Could not make DurationHistogram with name: \(name) and labels: \(labels), since another - metric type already exists for the same name. - """ - ) - } - - let key = LabelsKey(labels) - if let histogram = dimensionLookup[key] { - return histogram - } - - // check if all labels match the already existing ones. - if labelNames != labels.allLabelNames { - fatalError( - """ - Could not make DurationHistogram with name: \(name) and labels: \(labels), since the - label names don't match the label names of previously registered Gauges with - the same name. - """ - ) - } - if storedBuckets != buckets { - fatalError( - """ - Could not make DurationHistogram with name: \(name) and labels: \(labels), since the - buckets don't match the buckets of previously registered TimeHistograms with - the same name. - """ - ) - } - - precondition(storedBuckets == buckets) - - let histogram = DurationHistogram(name: name, labels: labels, buckets: storedBuckets) - dimensionLookup[key] = histogram - store[name] = .durationHistogramWithLabels(labelNames, dimensionLookup, storedBuckets) - return histogram - } else { + guard let value = store[name] else { let labelNames = labels.allLabelNames let histogram = DurationHistogram(name: name, labels: labels, buckets: buckets) store[name] = .durationHistogramWithLabels(labelNames, [LabelsKey(labels): histogram], buckets) return histogram } + guard case .durationHistogramWithLabels(let labelNames, var dimensionLookup, let storedBuckets) = value + else { + fatalError( + """ + Could not make DurationHistogram with name: \(name) and labels: \(labels), since another + metric type already exists for the same name. + """ + ) + } + + let key = LabelsKey(labels) + if let histogram = dimensionLookup[key] { + return histogram + } + + // check if all labels match the already existing ones. + if labelNames != labels.allLabelNames { + fatalError( + """ + Could not make DurationHistogram with name: \(name) and labels: \(labels), since the + label names don't match the label names of previously registered Gauges with + the same name. + """ + ) + } + if storedBuckets != buckets { + fatalError( + """ + Could not make DurationHistogram with name: \(name) and labels: \(labels), since the + buckets don't match the buckets of previously registered TimeHistograms with + the same name. + """ + ) + } + + precondition(storedBuckets == buckets) + + let histogram = DurationHistogram(name: name, labels: labels, buckets: storedBuckets) + dimensionLookup[key] = histogram + store[name] = .durationHistogramWithLabels(labelNames, dimensionLookup, storedBuckets) + return histogram } } @@ -337,17 +336,16 @@ public final class PrometheusCollectorRegistry: Sendable { /// - Returns: A ``ValueHistogram`` that is registered with this ``PrometheusCollectorRegistry`` public func makeValueHistogram(name: String, buckets: [Double]) -> ValueHistogram { self.box.withLockedValue { store -> ValueHistogram in - if let value = store[name] { - guard case .valueHistogram(let histogram) = value else { - fatalError() - } - - return histogram - } else { + guard let value = store[name] else { let gauge = ValueHistogram(name: name, labels: [], buckets: buckets) store[name] = .valueHistogram(gauge) return gauge } + guard case .valueHistogram(let histogram) = value else { + fatalError() + } + + return histogram } } @@ -367,31 +365,30 @@ public final class PrometheusCollectorRegistry: Sendable { } return self.box.withLockedValue { store -> ValueHistogram in - if let value = store[name] { - guard case .valueHistogramWithLabels(let labelNames, var dimensionLookup, let storedBuckets) = value else { - fatalError() - } - - let key = LabelsKey(labels) - if let histogram = dimensionLookup[key] { - return histogram - } - - // check if all labels match the already existing ones. - precondition(labelNames == labels.allLabelNames) - precondition(storedBuckets == buckets) - - let histogram = ValueHistogram(name: name, labels: labels, buckets: storedBuckets) - dimensionLookup[key] = histogram - store[name] = .valueHistogramWithLabels(labelNames, dimensionLookup, storedBuckets) - return histogram - } else { + guard let value = store[name] else { let labelNames = labels.allLabelNames let histogram = ValueHistogram(name: name, labels: labels, buckets: buckets) store[name] = .valueHistogramWithLabels(labelNames, [LabelsKey(labels): histogram], buckets) return histogram } + guard case .valueHistogramWithLabels(let labelNames, var dimensionLookup, let storedBuckets) = value else { + fatalError() + } + + let key = LabelsKey(labels) + if let histogram = dimensionLookup[key] { + return histogram + } + + // check if all labels match the already existing ones. + precondition(labelNames == labels.allLabelNames) + precondition(storedBuckets == buckets) + + let histogram = ValueHistogram(name: name, labels: labels, buckets: storedBuckets) + dimensionLookup[key] = histogram + store[name] = .valueHistogramWithLabels(labelNames, dimensionLookup, storedBuckets) + return histogram } } diff --git a/Sources/Prometheus/PrometheusMetricsFactory.swift b/Sources/Prometheus/PrometheusMetricsFactory.swift index a0593e7..8ac3c9a 100644 --- a/Sources/Prometheus/PrometheusMetricsFactory.swift +++ b/Sources/Prometheus/PrometheusMetricsFactory.swift @@ -45,7 +45,8 @@ public struct PrometheusMetricsFactory: Sendable { /// A closure to modify the name and labels used in the Swift Metrics API. This allows users /// to overwrite the Metric names in third party packages. - public var nameAndLabelSanitizer: @Sendable (_ name: String, _ labels: [(String, String)]) -> (String, [(String, String)]) + public var nameAndLabelSanitizer: + @Sendable (_ name: String, _ labels: [(String, String)]) -> (String, [(String, String)]) public init(registry: PrometheusCollectorRegistry = Self.defaultRegistry) { self.registry = registry @@ -95,14 +96,17 @@ extension PrometheusMetricsFactory: CoreMetrics.MetricsFactory { return self.registry.makeCounter(name: label, labels: dimensions) } - public func makeRecorder(label: String, dimensions: [(String, String)], aggregate: Bool) -> CoreMetrics.RecorderHandler { + public func makeRecorder( + label: String, + dimensions: [(String, String)], + aggregate: Bool + ) -> CoreMetrics.RecorderHandler { let (label, dimensions) = self.nameAndLabelSanitizer(label, dimensions) - if aggregate { - let buckets = self.valueHistogramBuckets[label] ?? self.defaultValueHistogramBuckets - return self.registry.makeValueHistogram(name: label, labels: dimensions, buckets: buckets) - } else { + guard aggregate else { return self.registry.makeGauge(name: label, labels: dimensions) } + let buckets = self.valueHistogramBuckets[label] ?? self.defaultValueHistogramBuckets + return self.registry.makeValueHistogram(name: label, labels: dimensions, buckets: buckets) } public func makeMeter(label: String, dimensions: [(String, String)]) -> CoreMetrics.MeterHandler { diff --git a/Tests/PrometheusTests/CounterTests.swift b/Tests/PrometheusTests/CounterTests.swift index 440a53e..ab78914 100644 --- a/Tests/PrometheusTests/CounterTests.swift +++ b/Tests/PrometheusTests/CounterTests.swift @@ -28,7 +28,8 @@ final class CounterTests: XCTestCase { # TYPE foo counter foo 0 - """) + """ + ) // Increment by 1 buffer.removeAll(keepingCapacity: true) @@ -53,7 +54,8 @@ final class CounterTests: XCTestCase { # TYPE foo counter foo 2 - """) + """ + ) // Increment by 2 buffer.removeAll(keepingCapacity: true) @@ -65,7 +67,8 @@ final class CounterTests: XCTestCase { # TYPE foo counter foo 4 - """) + """ + ) // Increment by 2.5 buffer.removeAll(keepingCapacity: true) @@ -77,7 +80,8 @@ final class CounterTests: XCTestCase { # TYPE foo counter foo 6.5 - """) + """ + ) // Reset buffer.removeAll(keepingCapacity: true) @@ -89,7 +93,8 @@ final class CounterTests: XCTestCase { # TYPE foo counter foo 0 - """) + """ + ) } func testCounterWithLabels() { @@ -104,7 +109,8 @@ final class CounterTests: XCTestCase { # TYPE foo counter foo{bar="baz"} 0 - """) + """ + ) // Increment by 1 buffer.removeAll(keepingCapacity: true) @@ -116,7 +122,8 @@ final class CounterTests: XCTestCase { # TYPE foo counter foo{bar="baz"} 1 - """) + """ + ) // Increment by 1 buffer.removeAll(keepingCapacity: true) @@ -128,7 +135,8 @@ final class CounterTests: XCTestCase { # TYPE foo counter foo{bar="baz"} 2 - """) + """ + ) // Increment by 2 buffer.removeAll(keepingCapacity: true) @@ -140,7 +148,8 @@ final class CounterTests: XCTestCase { # TYPE foo counter foo{bar="baz"} 4 - """) + """ + ) // Reset buffer.removeAll(keepingCapacity: true) @@ -152,6 +161,7 @@ final class CounterTests: XCTestCase { # TYPE foo counter foo{bar="baz"} 0 - """) + """ + ) } } diff --git a/Tests/PrometheusTests/GaugeTests.swift b/Tests/PrometheusTests/GaugeTests.swift index ed361bf..aef6d0d 100644 --- a/Tests/PrometheusTests/GaugeTests.swift +++ b/Tests/PrometheusTests/GaugeTests.swift @@ -28,7 +28,8 @@ final class GaugeTests: XCTestCase { # TYPE foo gauge foo 0.0 - """) + """ + ) // Set to 1 buffer.removeAll(keepingCapacity: true) @@ -40,7 +41,8 @@ final class GaugeTests: XCTestCase { # TYPE foo gauge foo 1.0 - """) + """ + ) // Set to 2 buffer.removeAll(keepingCapacity: true) @@ -52,7 +54,8 @@ final class GaugeTests: XCTestCase { # TYPE foo gauge foo 2.0 - """) + """ + ) // Set to 4 buffer.removeAll(keepingCapacity: true) @@ -64,7 +67,8 @@ final class GaugeTests: XCTestCase { # TYPE foo gauge foo 4.0 - """) + """ + ) // Reset buffer.removeAll(keepingCapacity: true) @@ -76,7 +80,8 @@ final class GaugeTests: XCTestCase { # TYPE foo gauge foo 0.0 - """) + """ + ) } func testGaugeWithLabels() { @@ -91,7 +96,8 @@ final class GaugeTests: XCTestCase { # TYPE foo gauge foo{bar="baz"} 0.0 - """) + """ + ) // Set to 1 buffer.removeAll(keepingCapacity: true) @@ -103,7 +109,8 @@ final class GaugeTests: XCTestCase { # TYPE foo gauge foo{bar="baz"} 1.0 - """) + """ + ) // Set to 2 buffer.removeAll(keepingCapacity: true) @@ -115,7 +122,8 @@ final class GaugeTests: XCTestCase { # TYPE foo gauge foo{bar="baz"} 2.0 - """) + """ + ) // Set to 4 buffer.removeAll(keepingCapacity: true) @@ -127,7 +135,8 @@ final class GaugeTests: XCTestCase { # TYPE foo gauge foo{bar="baz"} 4.0 - """) + """ + ) // Reset buffer.removeAll(keepingCapacity: true) @@ -139,7 +148,8 @@ final class GaugeTests: XCTestCase { # TYPE foo gauge foo{bar="baz"} 0.0 - """) + """ + ) } func testGaugeSetToFromMultipleTasks() async { diff --git a/Tests/PrometheusTests/HistogramTests.swift b/Tests/PrometheusTests/HistogramTests.swift index c60376d..8c5705a 100644 --- a/Tests/PrometheusTests/HistogramTests.swift +++ b/Tests/PrometheusTests/HistogramTests.swift @@ -54,7 +54,8 @@ final class HistogramTests: XCTestCase { XCTAssertEqual( String( decoding: buffer, - as: Unicode.UTF8.self), + as: Unicode.UTF8.self + ), """ # TYPE foo histogram foo_bucket{le="0.1"} 0 diff --git a/Tests/PrometheusTests/PrometheusCollectorRegistryTests.swift b/Tests/PrometheusTests/PrometheusCollectorRegistryTests.swift index 3475c72..df1a441 100644 --- a/Tests/PrometheusTests/PrometheusCollectorRegistryTests.swift +++ b/Tests/PrometheusTests/PrometheusCollectorRegistryTests.swift @@ -147,8 +147,16 @@ final class PrometheusCollectorRegistryTests: XCTestCase { func testAskingForTheSameTimeHistogramWithLabelsReturnsTheSameTimeHistogram() { let client = PrometheusCollectorRegistry() - let histogram1 = client.makeDurationHistogram(name: "foo", labels: [("bar", "baz")], buckets: [.seconds(1), .seconds(2), .seconds(3)]) - let histogram2 = client.makeDurationHistogram(name: "foo", labels: [("bar", "baz")], buckets: [.seconds(1), .seconds(2), .seconds(3)]) + let histogram1 = client.makeDurationHistogram( + name: "foo", + labels: [("bar", "baz")], + buckets: [.seconds(1), .seconds(2), .seconds(3)] + ) + let histogram2 = client.makeDurationHistogram( + name: "foo", + labels: [("bar", "baz")], + buckets: [.seconds(1), .seconds(2), .seconds(3)] + ) XCTAssert(histogram1 === histogram2) histogram1.record(.milliseconds(2500)) From 481f3a090734e213c510029e64706fea2fb59213 Mon Sep 17 00:00:00 2001 From: Armand Grillet <2117580+armandgrillet@users.noreply.github.com> Date: Tue, 17 Oct 2023 08:33:34 +0200 Subject: [PATCH 4/6] Fix docker/docker-compose.2204.59.yaml to be like other Dockerfiles --- docker/docker-compose.2204.59.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docker/docker-compose.2204.59.yaml b/docker/docker-compose.2204.59.yaml index 1ae4771..90a0799 100644 --- a/docker/docker-compose.2204.59.yaml +++ b/docker/docker-compose.2204.59.yaml @@ -6,7 +6,8 @@ services: image: prometheus:22.04-5.9 build: args: - base_image: "swiftlang/swift:nightly-5.9-jammy" + ubuntu_version: "jammy" + swift_version: "5.9" documentation-check: image: prometheus:22.04-5.9 From a9f3d11598c30a32e4d89dfd76000d648c5aebc6 Mon Sep 17 00:00:00 2001 From: Armand Grillet <2117580+armandgrillet@users.noreply.github.com> Date: Tue, 17 Oct 2023 09:04:41 +0200 Subject: [PATCH 5/6] Update default Swift version to 5.8 in Dockerfile As of Swift 5.8, swift-format depends on the version of SwiftSyntax whose parser has been rewritten in Swift and no longer has dependencies on libraries in the Swift toolchain. Defaulting to Swift 5.8 fixes the usage of swift-format in nightly builds. --- docker/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index b6fd80c..e36c55a 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,4 +1,4 @@ -ARG swift_version=5.7 +ARG swift_version=5.8 ARG ubuntu_version=jammy ARG base_image=swift:$swift_version-$ubuntu_version FROM $base_image From c0eecc76be6f9d9341c5e1886f2e58adfb4c42c6 Mon Sep 17 00:00:00 2001 From: Armand Grillet <2117580+armandgrillet@users.noreply.github.com> Date: Tue, 17 Oct 2023 19:47:19 +0200 Subject: [PATCH 6/6] Fix Dockerfile to have swift-format accessible --- docker/Dockerfile | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index e36c55a..b2d7e83 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -27,6 +27,7 @@ RUN mkdir -p $HOME/.tools RUN echo 'export PATH="$HOME/.tools:$PATH"' >> $HOME/.profile # swift-format (until part of the toolchain) -RUN git clone --branch release/$swift_version --depth 1 https://github.com/apple/swift-format $HOME/.tools/swift-format -RUN cd $HOME/.tools/swift-format && swift build -c release -RUN ln -s $HOME/.tools/swift-format/.build/release/swift-format $HOME/.tools/swift-format +RUN mkdir -p $HOME/.deps +RUN git clone --branch release/$swift_version --depth 1 https://github.com/apple/swift-format $HOME/.deps/swift-format +RUN cd $HOME/.deps/swift-format && swift build -c release +RUN ln -s $HOME/.deps/swift-format/.build/release/swift-format $HOME/.tools