Update clairvoyant

This commit is contained in:
Christoph Hagen 2023-09-07 14:13:28 +02:00
parent e77efe795c
commit 04248b04af
3 changed files with 17 additions and 17 deletions

View File

@ -8,7 +8,9 @@ let package = Package(
], ],
dependencies: [ dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"), .package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"),
.package(url: "https://github.com/christophhagen/clairvoyant.git", from: "0.5.0"), .package(url: "https://github.com/christophhagen/Clairvoyant", from: "0.9.0"),
.package(url: "https://github.com/christophhagen/ClairvoyantVapor", from: "0.2.0"),
.package(url: "https://github.com/christophhagen/ClairvoyantBinaryCodable", from: "0.3.1"),
], ],
targets: [ targets: [
.target( .target(
@ -16,7 +18,8 @@ let package = Package(
dependencies: [ dependencies: [
.product(name: "Vapor", package: "vapor"), .product(name: "Vapor", package: "vapor"),
.product(name: "Clairvoyant", package: "Clairvoyant"), .product(name: "Clairvoyant", package: "Clairvoyant"),
.product(name: "ClairvoyantVapor", package: "ClairvoyantVapor"),
.product(name: "ClairvoyantBinaryCodable", package: "ClairvoyantBinaryCodable"),
], ],
swiftSettings: [ swiftSettings: [
// Enable better optimizations when building in Release configuration. Despite the use of // Enable better optimizations when building in Release configuration. Despite the use of

View File

@ -56,7 +56,7 @@ final class DeviceManager {
private func updateMessageCountMetric() { private func updateMessageCountMetric() {
Task { Task {
let lastValue = await messagesToDeviceMetric.lastValue()?.value ?? 0 let lastValue = await messagesToDeviceMetric.lastValue()?.value ?? 0
try? await messagesToDeviceMetric.update(lastValue + 1) _ = try? await messagesToDeviceMetric.update(lastValue + 1)
} }
} }

View File

@ -1,8 +1,12 @@
import Vapor import Vapor
import Clairvoyant import Clairvoyant
import ClairvoyantVapor
import ClairvoyantBinaryCodable
var deviceManager: DeviceManager! var deviceManager: DeviceManager!
private var provider: VaporMetricProvider!
enum ServerError: Error { enum ServerError: Error {
case invalidAuthenticationFileContent case invalidAuthenticationFileContent
case invalidAuthenticationToken case invalidAuthenticationToken
@ -20,23 +24,15 @@ public func configure(_ app: Application) async throws {
let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory) let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory)
let logFolder = storageFolder.appendingPathComponent("logs") let logFolder = storageFolder.appendingPathComponent("logs")
let accessManager = AccessTokenManager([]) let monitor = MetricObserver(logFileFolder: logFolder, logMetricId: "sesame.log")
let monitor = await MetricObserver(
logFolder: logFolder,
accessManager: accessManager,
logMetricId: "sesame.log")
MetricObserver.standard = monitor MetricObserver.standard = monitor
let status = try await Metric<ServerStatus>("sesame.status") let status = try await Metric<ServerStatus>("sesame.status")
try await status.update(.initializing) _ = try await status.update(.initializing)
await monitor.registerRoutes(app)
let configUrl = storageFolder.appendingPathComponent("config.json") let configUrl = storageFolder.appendingPathComponent("config.json")
let config = try Config(loadFrom: configUrl) let config = try Config(loadFrom: configUrl)
config.authenticationTokens.map { $0.data(using: .utf8)! }.forEach(accessManager.add)
app.http.server.configuration.port = config.port app.http.server.configuration.port = config.port
let keyFile = storageFolder.appendingPathComponent(config.keyFileName) let keyFile = storageFolder.appendingPathComponent(config.keyFileName)
@ -48,6 +44,9 @@ public func configure(_ app: Application) async throws {
deviceTimeout: config.deviceTimeout) deviceTimeout: config.deviceTimeout)
try routes(app) try routes(app)
provider = .init(observer: monitor, accessManager: config.authenticationTokens)
provider.registerRoutes(app)
// Gracefully shut down by closing potentially open socket // Gracefully shut down by closing potentially open socket
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + .seconds(5)) { DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + .seconds(5)) {
@ -56,7 +55,7 @@ public func configure(_ app: Application) async throws {
} }
} }
try await status.update(.nominal) _ = try await status.update(.nominal)
print("[\(dateFormatter.string(from: Date()))] Server started") print("[\(dateFormatter.string(from: Date()))] Server started")
} }
@ -85,7 +84,5 @@ func log(_ message: String) {
print(message) print(message)
return return
} }
Task { observer.log(message)
await observer.log(message)
}
} }