|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the NIORedis open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2019 NIORedis project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of NIORedis project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import Metrics |
| 16 | + |
| 17 | +/// The system funnel for all `Metrics` interactions from the Redis library. |
| 18 | +/// |
| 19 | +/// It is highly recommended to not interact with this directly, and to let the library |
| 20 | +/// use it how it sees fit. |
| 21 | +/// |
| 22 | +/// There is a nested enum type of `RedisMetrics.Label` that is available to query, match, etc. the |
| 23 | +/// labels used for all of the `Metrics` types created by the Redis library. |
| 24 | +public struct RedisMetrics { |
| 25 | + /// An enumeration of all the labels used by the Redis library for various `Metrics` data points. |
| 26 | + /// |
| 27 | + /// Each is backed by a raw string, and this type is `CustomStringConvertible` to receive a |
| 28 | + /// namespaced description in the form of `"NIORedis.<rawValue>"`. |
| 29 | + public enum Label: String, CustomStringConvertible { |
| 30 | + case totalConnectionCount |
| 31 | + case activeConnectionCount |
| 32 | + case commandSuccessCount |
| 33 | + case commandFailureCount |
| 34 | + case commandRoundTripTime |
| 35 | + |
| 36 | + public var description: String { |
| 37 | + return "NIORedis.\(self.rawValue)" |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private static let activeConnectionCountGauge = Gauge(label: .activeConnectionCount) |
| 42 | + /// The current number of connections this library has active. |
| 43 | + /// - Note: Changing this number will update the `Metrics.Gauge` stored for recording the new value. |
| 44 | + public static var activeConnectionCount: Int = 0 { |
| 45 | + didSet { |
| 46 | + activeConnectionCountGauge.record(activeConnectionCount) |
| 47 | + } |
| 48 | + } |
| 49 | + /// The `Metrics.Counter` that retains the number of connections made since application startup. |
| 50 | + public static let totalConnectionCount = Counter(label: .totalConnectionCount) |
| 51 | + /// The `Metrics.Counter` that retains the number of commands that successfully returned from Redis |
| 52 | + /// since application startup. |
| 53 | + public static let commandSuccessCount = Counter(label: .commandSuccessCount) |
| 54 | + /// The `Metrics.Counter` that retains the number of commands that failed from errors returned |
| 55 | + /// by Redis since application startup. |
| 56 | + public static let commandFailureCount = Counter(label: .commandFailureCount) |
| 57 | + /// The `Metrics.Timer` that receives command response times in nanoseconds from when a command |
| 58 | + /// is first sent through the `NIO.Channel`, to when the response is first resolved. |
| 59 | + public static let commandRoundTripTime = Timer(label: .commandRoundTripTime) |
| 60 | + |
| 61 | + private init() { } |
| 62 | +} |
| 63 | + |
| 64 | +extension Metrics.Counter { |
| 65 | + @inline(__always) |
| 66 | + convenience init(label: RedisMetrics.Label) { |
| 67 | + self.init(label: label.description) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +extension Metrics.Gauge { |
| 72 | + @inline(__always) |
| 73 | + convenience init(label: RedisMetrics.Label) { |
| 74 | + self.init(label: label.description) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +extension Metrics.Timer { |
| 79 | + @inline(__always) |
| 80 | + convenience init(label: RedisMetrics.Label) { |
| 81 | + self.init(label: label.description) |
| 82 | + } |
| 83 | +} |
0 commit comments