diff --git a/Package.swift b/Package.swift index 689519f..265e295 100644 --- a/Package.swift +++ b/Package.swift @@ -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 diff --git a/Sources/App/Management/SQLiteDatabase.swift b/Sources/App/Management/SQLiteDatabase.swift index 1d09946..86aaacf 100644 --- a/Sources/App/Management/SQLiteDatabase.swift +++ b/Sources/App/Management/SQLiteDatabase.swift @@ -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)") } diff --git a/Sources/App/Management/TableManagement.swift b/Sources/App/Management/TableManagement.swift index 4f5c191..75e1979 100644 --- a/Sources/App/Management/TableManagement.swift +++ b/Sources/App/Management/TableManagement.swift @@ -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) } /** diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 280f8a2..628501c 100644 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -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) }