Log more metrics
This commit is contained in:
parent
b3c58ce4c7
commit
e96b85b1cc
@ -1,6 +1,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import WebSocketKit
|
import WebSocketKit
|
||||||
import Vapor
|
import Vapor
|
||||||
|
import Clairvoyant
|
||||||
|
|
||||||
final class DeviceManager {
|
final class DeviceManager {
|
||||||
|
|
||||||
@ -20,6 +21,10 @@ final class DeviceManager {
|
|||||||
|
|
||||||
private let deviceTimeout: Int64
|
private let deviceTimeout: Int64
|
||||||
|
|
||||||
|
private let deviceConnectedMetric: Metric<Bool>
|
||||||
|
|
||||||
|
private let messagesToDeviceMetric: Metric<Int>
|
||||||
|
|
||||||
/// Indicator for device availability
|
/// Indicator for device availability
|
||||||
var deviceIsConnected: Bool {
|
var deviceIsConnected: Bool {
|
||||||
deviceIsAuthenticated && !(connection?.isClosed ?? true)
|
deviceIsAuthenticated && !(connection?.isClosed ?? true)
|
||||||
@ -32,6 +37,23 @@ final class DeviceManager {
|
|||||||
self.deviceKey = deviceKey
|
self.deviceKey = deviceKey
|
||||||
self.remoteKey = remoteKey
|
self.remoteKey = remoteKey
|
||||||
self.deviceTimeout = deviceTimeout
|
self.deviceTimeout = deviceTimeout
|
||||||
|
self.deviceConnectedMetric = .init(
|
||||||
|
"sesame.connected",
|
||||||
|
name: "Device connected",
|
||||||
|
description: "Shows if the device is connected via WebSocket")
|
||||||
|
self.messagesToDeviceMetric = .init(
|
||||||
|
"sesame.messages",
|
||||||
|
name: "Forwarded Messages",
|
||||||
|
description: "The number of messages transmitted to the device")
|
||||||
|
}
|
||||||
|
|
||||||
|
private func updateDeviceConnectionMetric() {
|
||||||
|
deviceConnectedMetric.update(deviceIsConnected)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func updateMessageCountMetric() {
|
||||||
|
let lastValue = messagesToDeviceMetric.lastValue()?.value ?? 0
|
||||||
|
messagesToDeviceMetric.update(lastValue + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: API
|
// MARK: API
|
||||||
@ -50,6 +72,7 @@ final class DeviceManager {
|
|||||||
}
|
}
|
||||||
requestInProgress = eventLoop.makePromise(of: DeviceResponse.self)
|
requestInProgress = eventLoop.makePromise(of: DeviceResponse.self)
|
||||||
socket.send(message.bytes, promise: nil)
|
socket.send(message.bytes, promise: nil)
|
||||||
|
updateMessageCountMetric()
|
||||||
eventLoop.scheduleTask(in: .seconds(deviceTimeout)) { [weak self] in
|
eventLoop.scheduleTask(in: .seconds(deviceTimeout)) { [weak self] in
|
||||||
guard let promise = self?.requestInProgress else {
|
guard let promise = self?.requestInProgress else {
|
||||||
return
|
return
|
||||||
@ -61,6 +84,7 @@ final class DeviceManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func authenticateDevice(hash: String) {
|
func authenticateDevice(hash: String) {
|
||||||
|
defer { updateDeviceConnectionMetric() }
|
||||||
guard let key = Data(fromHexEncodedString: hash),
|
guard let key = Data(fromHexEncodedString: hash),
|
||||||
SHA256.hash(data: key) == self.deviceKey else {
|
SHA256.hash(data: key) == self.deviceKey else {
|
||||||
log("Invalid device key")
|
log("Invalid device key")
|
||||||
@ -86,6 +110,7 @@ final class DeviceManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func didCloseDeviceSocket() {
|
func didCloseDeviceSocket() {
|
||||||
|
defer { updateDeviceConnectionMetric() }
|
||||||
guard !isOpeningNewConnection else {
|
guard !isOpeningNewConnection else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -99,6 +124,7 @@ final class DeviceManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func removeDeviceConnection() {
|
func removeDeviceConnection() {
|
||||||
|
defer { updateDeviceConnectionMetric() }
|
||||||
deviceIsAuthenticated = false
|
deviceIsAuthenticated = false
|
||||||
guard let socket = connection else {
|
guard let socket = connection else {
|
||||||
return
|
return
|
||||||
@ -109,6 +135,7 @@ final class DeviceManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func createNewDeviceConnection(_ socket: WebSocket) {
|
func createNewDeviceConnection(_ socket: WebSocket) {
|
||||||
|
defer { updateDeviceConnectionMetric() }
|
||||||
isOpeningNewConnection = true
|
isOpeningNewConnection = true
|
||||||
removeDeviceConnection()
|
removeDeviceConnection()
|
||||||
connection = socket
|
connection = socket
|
||||||
|
Loading…
Reference in New Issue
Block a user