Update clairvoyant

This commit is contained in:
Christoph Hagen 2023-09-07 13:56:27 +02:00
parent e2b1fb58e9
commit 82d81b596e
4 changed files with 26 additions and 20 deletions

View File

@ -15,7 +15,9 @@ let package = Package(
// https://github.com/Joannis/SMTPKitten <- No updates since 0ct 2020
// https://github.com/Joannis/VaporSMTPKit <- No updates since 0ct. 2020, uses SMTPKitten
.package(url: "https://github.com/Kitura/Swift-SMTP", from: "6.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: [
.target(
@ -26,6 +28,8 @@ let package = Package(
.product(name: "Vapor", package: "vapor"),
.product(name: "SwiftSMTP", package: "Swift-SMTP"),
.product(name: "Clairvoyant", package: "Clairvoyant"),
.product(name: "ClairvoyantVapor", package: "ClairvoyantVapor"),
.product(name: "ClairvoyantBinaryCodable", package: "ClairvoyantBinaryCodable"),
],
swiftSettings: [
// Enable better optimizations when building in Release configuration. Despite the use of

View File

@ -90,7 +90,7 @@ final class SQLiteDatabase {
func updateRegisteredPlayerCount(from database: Database) async {
do {
let count = try await User.query(on: database).count()
try? await registeredPlayerCountMetric.update(count)
_ = try? await registeredPlayerCountMetric.update(count)
} catch {
log("Failed to update player count metric: \(error)")
}

View File

@ -65,17 +65,17 @@ final class TableManagement {
}
private func logTableCount() async {
try? await tableCountMetric.update(tables.count)
_ = try? await tableCountMetric.update(tables.count)
}
private func logPlayingPlayerCount() async {
let count = tables.values.sum { $0.playerCount }
try? await playingPlayerCountMetric.update(count)
_ = try? await playingPlayerCountMetric.update(count)
}
private func logConnectedPlayerCount() async {
let count = tables.values.sum { $0.numberOfConnectedPlayers }
try? await connectedPlayerCountMetric.update(count)
_ = try? await connectedPlayerCountMetric.update(count)
}
/**

View File

@ -1,18 +1,20 @@
import Vapor
import Fluent
import Clairvoyant
import ClairvoyantBinaryCodable
import ClairvoyantVapor
var server: SQLiteDatabase!
private var provider: VaporMetricProvider! = nil
// configures your application
public func configure(_ app: Application) async throws {
let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory)
let logFolder = storageFolder.appendingPathComponent("logs")
let accessManager = AccessTokenManager([])
let monitor = await MetricObserver(
logFolder: logFolder,
accessManager: accessManager,
let monitor = MetricObserver(
logFileFolder: logFolder,
logMetricId: "schafkopf.log")
MetricObserver.standard = monitor
@ -21,8 +23,7 @@ public func configure(_ app: Application) async throws {
name: "Status",
description: "The main status of the server")
try? await status.update(.initializing)
await monitor.registerRoutes(app)
_ = try? await status.update(.initializing)
let configPath = URL(fileURLWithPath: app.directory.resourcesDirectory)
.appendingPathComponent("config.json")
@ -31,13 +32,12 @@ public func configure(_ app: Application) async throws {
do {
configuration = try Configuration(loadFromUrl: configPath)
} catch {
try? await status.update(.initializationFailure)
await monitor.log("Failed to read configuration: \(error)")
_ = try? await status.update(.initializationFailure)
monitor.log("Failed to read configuration: \(error)")
// Note: If configuration can't be loaded, then the server will run on the wrong port
// and access to metrics is impossible, since no tokens are loaded
return
}
configuration.monitoringTokens.map { $0.data(using: .utf8)! }.forEach(accessManager.add)
app.http.server.configuration.port = configuration.serverPort
@ -60,8 +60,8 @@ public func configure(_ app: Application) async throws {
do {
try await app.autoMigrate()
} catch {
await monitor.log("Failed to migrate database: \(error)")
try? await status.update(.initializationFailure)
monitor.log("Failed to migrate database: \(error)")
_ = try? await status.update(.initializationFailure)
return
}
@ -81,7 +81,11 @@ public func configure(_ app: Application) async throws {
// register routes
routes(app)
try? await status.update(.nominal)
// Expose metrics
provider = .init(observer: monitor, accessManager: configuration.monitoringTokens)
provider.registerRoutes(app)
_ = try? await status.update(.nominal)
}
func log(_ message: String) {
@ -89,7 +93,5 @@ func log(_ message: String) {
print(message)
return
}
Task {
await observer.log(message)
}
observer.log(message)
}