Skip to content
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

refactor APIs to make them closer to swift-log APIs #9

Merged
merged 3 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
263 changes: 169 additions & 94 deletions Sources/CoreMetrics/Metrics.swift

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions Sources/Examples/Example1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ enum Example1 {
static func main() {
// bootstrap with our example metrics library
let metrics = ExampleMetricsLibrary()
Metrics.bootstrap(metrics)
MetricsSystem.bootstrap(metrics)

let server = Server()
let client = Client(server: server)
Expand All @@ -38,17 +38,17 @@ enum Example1 {
}

class Client {
private let activeRequestsGauge = Metrics.global.makeGauge(label: "Client::ActiveRequests")
private let activeRequestsGauge = Gauge(label: "Client::ActiveRequests")
private let server: Server
init(server: Server) {
self.server = server
}

func run(iterations: Int) {
let group = DispatchGroup()
let requestsCounter = Metrics.global.makeCounter(label: "Client::TotalRequests")
let requestTimer = Metrics.global.makeTimer(label: "Client::doSomethig")
let resultRecorder = Metrics.global.makeRecorder(label: "Client::doSomethig::result")
let requestsCounter = Counter(label: "Client::TotalRequests")
let requestTimer = Timer(label: "Client::doSomethig")
let resultRecorder = Recorder(label: "Client::doSomethig::result")
for _ in 0 ... iterations {
group.enter()
let start = Date()
Expand Down Expand Up @@ -78,10 +78,10 @@ enum Example1 {

class Server {
let library = RandomLibrary()
let requestsCounter = Metrics.global.makeCounter(label: "Server::TotalRequests")
let requestsCounter = Counter(label: "Server::TotalRequests")

func doSomethig(callback: @escaping (Int64) -> Void) {
let timer = Metrics.global.makeTimer(label: "Server::doSomethig")
let timer = Timer(label: "Server::doSomethig")
let start = Date()
requestsCounter.increment()
DispatchQueue.global().asyncAfter(deadline: .now() + .milliseconds(Int.random(in: 5 ... 500))) {
Expand Down
20 changes: 10 additions & 10 deletions Sources/Examples/ExampleMetricsLibrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import Metrics

class ExampleMetricsLibrary: MetricsHandler {
class ExampleMetricsLibrary: MetricsFactory {
private let config: Config
private let lock = NSLock()
var counters = [ExampleCounter]()
Expand All @@ -29,16 +29,16 @@ class ExampleMetricsLibrary: MetricsHandler {
self.config = config
}

func makeCounter(label: String, dimensions: [(String, String)]) -> Counter {
func makeCounter(label: String, dimensions: [(String, String)]) -> CounterHandler {
return self.register(label: label, dimensions: dimensions, registry: &self.counters, maker: ExampleCounter.init)
}

func makeRecorder(label: String, dimensions: [(String, String)], aggregate: Bool) -> Recorder {
func makeRecorder(label: String, dimensions: [(String, String)], aggregate: Bool) -> RecorderHandler {
let options = aggregate ? self.config.recorder.aggregationOptions : nil
return self.makeRecorder(label: label, dimensions: dimensions, options: options)
}

func makeRecorder(label: String, dimensions: [(String, String)], options: [AggregationOption]?) -> Recorder {
func makeRecorder(label: String, dimensions: [(String, String)], options: [AggregationOption]?) -> RecorderHandler {
guard let options = options else {
return self.register(label: label, dimensions: dimensions, registry: &self.gauges, maker: ExampleGauge.init)
}
Expand All @@ -48,11 +48,11 @@ class ExampleMetricsLibrary: MetricsHandler {
return self.register(label: label, dimensions: dimensions, registry: &self.recorders, maker: maker)
}

func makeTimer(label: String, dimensions: [(String, String)]) -> Timer {
func makeTimer(label: String, dimensions: [(String, String)]) -> TimerHandler {
return self.makeTimer(label: label, dimensions: dimensions, options: self.config.timer.aggregationOptions)
}

func makeTimer(label: String, dimensions: [(String, String)], options: [AggregationOption]) -> Timer {
func makeTimer(label: String, dimensions: [(String, String)], options: [AggregationOption]) -> TimerHandler {
let maker = { (label: String, dimensions: [(String, String)]) -> ExampleTimer in
ExampleTimer(label: label, dimensions: dimensions, options: options)
}
Expand Down Expand Up @@ -99,7 +99,7 @@ class ExampleMetricsLibrary: MetricsHandler {
}
}

class ExampleCounter: Counter, CustomStringConvertible {
class ExampleCounter: CounterHandler, CustomStringConvertible {
let label: String
let dimensions: [(String, String)]
init(label: String, dimensions: [(String, String)]) {
Expand All @@ -120,7 +120,7 @@ class ExampleCounter: Counter, CustomStringConvertible {
}
}

class ExampleRecorder: Recorder, CustomStringConvertible {
class ExampleRecorder: RecorderHandler, CustomStringConvertible {
let label: String
let dimensions: [(String, String)]
let options: [AggregationOption]
Expand Down Expand Up @@ -226,7 +226,7 @@ class ExampleRecorder: Recorder, CustomStringConvertible {
}
}

class ExampleGauge: Recorder, CustomStringConvertible {
class ExampleGauge: RecorderHandler, CustomStringConvertible {
let label: String
let dimensions: [(String, String)]
init(label: String, dimensions: [(String, String)]) {
Expand All @@ -250,7 +250,7 @@ class ExampleGauge: Recorder, CustomStringConvertible {
}
}

class ExampleTimer: ExampleRecorder, Timer {
class ExampleTimer: ExampleRecorder, TimerHandler {
func recordNanoseconds(_ duration: Int64) {
super.record(duration)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Examples/RandomLibrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
import Metrics

class RandomLibrary {
let methodCallsCounter = Metrics.global.makeCounter(label: "RandomLibrary::TotalMethodCalls")
let methodCallsCounter = Counter(label: "RandomLibrary::TotalMethodCalls")

func doSomething() {
self.methodCallsCounter.increment()
}

func doSomethingSlow(callback: @escaping () -> Void) {
self.methodCallsCounter.increment()
Metrics.global.withTimer(label: "RandomLibrary::doSomethingSlow") { timer in
Timer.do(label: "RandomLibrary::doSomethingSlow") { timer in
let start = Date()
DispatchQueue.global().asyncAfter(deadline: .now() + .milliseconds(Int.random(in: 5 ... 500))) {
timer.record(Date().timeIntervalSince(start))
Expand Down
18 changes: 9 additions & 9 deletions Sources/Examples/SimpleMetricsLibrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@

import Metrics

class SimpleMetricsLibrary: MetricsHandler {
class SimpleMetricsLibrary: MetricsFactory {
init() {}

func makeCounter(label: String, dimensions: [(String, String)]) -> Counter {
func makeCounter(label: String, dimensions: [(String, String)]) -> CounterHandler {
return ExampleCounter(label, dimensions)
}

func makeRecorder(label: String, dimensions: [(String, String)], aggregate: Bool) -> Recorder {
let maker: (String, [(String, String)]) -> Recorder = aggregate ? ExampleRecorder.init : ExampleGauge.init
func makeRecorder(label: String, dimensions: [(String, String)], aggregate: Bool) -> RecorderHandler {
let maker: (String, [(String, String)]) -> RecorderHandler = aggregate ? ExampleRecorder.init : ExampleGauge.init
return maker(label, dimensions)
}

func makeTimer(label: String, dimensions: [(String, String)]) -> Timer {
func makeTimer(label: String, dimensions: [(String, String)]) -> TimerHandler {
return ExampleTimer(label, dimensions)
}

private class ExampleCounter: Counter {
private class ExampleCounter: CounterHandler {
init(_: String, _: [(String, String)]) {}

let lock = NSLock()
Expand All @@ -45,7 +45,7 @@ class SimpleMetricsLibrary: MetricsHandler {
}
}

private class ExampleRecorder: Recorder {
private class ExampleRecorder: RecorderHandler {
init(_: String, _: [(String, String)]) {}

private let lock = NSLock()
Expand Down Expand Up @@ -88,7 +88,7 @@ class SimpleMetricsLibrary: MetricsHandler {
}
}

private class ExampleGauge: Recorder {
private class ExampleGauge: RecorderHandler {
init(_: String, _: [(String, String)]) {}

let lock = NSLock()
Expand All @@ -103,7 +103,7 @@ class SimpleMetricsLibrary: MetricsHandler {
}
}

private class ExampleTimer: ExampleRecorder, Timer {
private class ExampleTimer: ExampleRecorder, TimerHandler {
func recordNanoseconds(_ duration: Int64) {
super.record(duration)
}
Expand Down
14 changes: 8 additions & 6 deletions Sources/Metrics/Metrics.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
@_exported import CoreMetrics
@_exported import protocol CoreMetrics.Timer
@_exported import class CoreMetrics.Timer
@_exported import Foundation

public extension MetricsHandler {
// Convenience for measuring duration of a closure
public extension Timer {
@inlinable
func timed<T>(label: String, dimensions: [(String, String)] = [], body: @escaping () throws -> T) rethrows -> T {
let timer = self.makeTimer(label: label, dimensions: dimensions)
public static func measure<T>(label: String, dimensions: [(String, String)] = [], body: @escaping () throws -> T) rethrows -> T {
let timer = Timer(label: label, dimensions: dimensions)
let start = Date()
defer {
timer.record(Date().timeIntervalSince(start))
Expand All @@ -14,14 +15,15 @@ public extension MetricsHandler {
}
}

// Convenience for using Foundation and Dispatch
public extension Timer {
@inlinable
func record(_ duration: TimeInterval) {
public func record(_ duration: TimeInterval) {
self.recordSeconds(duration)
}

@inlinable
func record(_ duration: DispatchTimeInterval) {
public func record(_ duration: DispatchTimeInterval) {
switch duration {
case .nanoseconds(let value):
self.recordNanoseconds(Int64(value))
Expand Down
Loading