Update metrics logging
This commit is contained in:
parent
e4f2fc547b
commit
9fcf1756ef
@ -8,7 +8,7 @@ let package = Package(
|
|||||||
],
|
],
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.package(url: "https://github.com/vapor/vapor", from: "4.0.0"),
|
.package(url: "https://github.com/vapor/vapor", from: "4.0.0"),
|
||||||
.package(url: "https://github.com/christophhagen/Clairvoyant", from: "0.4.0"),
|
.package(url: "https://github.com/christophhagen/Clairvoyant", from: "0.5.0"),
|
||||||
],
|
],
|
||||||
targets: [
|
targets: [
|
||||||
.target(name: "App",
|
.target(name: "App",
|
||||||
|
@ -41,7 +41,9 @@ final class CapServer {
|
|||||||
var classifierVersion: Int = 0 {
|
var classifierVersion: Int = 0 {
|
||||||
didSet {
|
didSet {
|
||||||
writeClassifierVersion()
|
writeClassifierVersion()
|
||||||
classifierMetric.update(classifierVersion)
|
Task {
|
||||||
|
try? await classifierMetric.update(classifierVersion)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,8 +64,10 @@ final class CapServer {
|
|||||||
private var caps = [Int: Cap]() {
|
private var caps = [Int: Cap]() {
|
||||||
didSet {
|
didSet {
|
||||||
scheduleSave()
|
scheduleSave()
|
||||||
capCountMetric.update(caps.count)
|
Task {
|
||||||
imageCountMetric.update(imageCount)
|
try? await capCountMetric.update(caps.count)
|
||||||
|
try? await imageCountMetric.update(imageCount)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,7 +83,7 @@ final class CapServer {
|
|||||||
caps.reduce(0) { $0 + $1.value.count }
|
caps.reduce(0) { $0 + $1.value.count }
|
||||||
}
|
}
|
||||||
|
|
||||||
init(in folder: URL) {
|
init(in folder: URL) async {
|
||||||
self.imageFolder = folder.appendingPathComponent("images")
|
self.imageFolder = folder.appendingPathComponent("images")
|
||||||
self.thumbnailFolder = folder.appendingPathComponent("thumbnails")
|
self.thumbnailFolder = folder.appendingPathComponent("thumbnails")
|
||||||
self.gridCountFile = folder.appendingPathComponent("count.js")
|
self.gridCountFile = folder.appendingPathComponent("count.js")
|
||||||
@ -90,6 +94,19 @@ final class CapServer {
|
|||||||
self.changedImagesFile = folder.appendingPathComponent("changes.txt")
|
self.changedImagesFile = folder.appendingPathComponent("changes.txt")
|
||||||
self.changedImageEntryDateFormatter = DateFormatter()
|
self.changedImageEntryDateFormatter = DateFormatter()
|
||||||
changedImageEntryDateFormatter.dateFormat = "yy-MM-dd-HH-mm-ss"
|
changedImageEntryDateFormatter.dateFormat = "yy-MM-dd-HH-mm-ss"
|
||||||
|
|
||||||
|
// Metric initializers only fail if observer is missing or ID is duplicate
|
||||||
|
self.capCountMetric = try! await .init("caps.count",
|
||||||
|
name: "Number of caps",
|
||||||
|
description: "The total number of caps in the database")
|
||||||
|
|
||||||
|
self.imageCountMetric = try! await .init("caps.images",
|
||||||
|
name: "Total images",
|
||||||
|
description: "The total number of images for all caps")
|
||||||
|
|
||||||
|
self.classifierMetric = try! await .init("caps.classifier",
|
||||||
|
name: "Classifier Version",
|
||||||
|
description: "The current version of the image classifier")
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadData() throws {
|
func loadData() throws {
|
||||||
@ -460,15 +477,9 @@ final class CapServer {
|
|||||||
|
|
||||||
// MARK: Monitoring
|
// MARK: Monitoring
|
||||||
|
|
||||||
private let capCountMetric = Metric<Int>("caps.count",
|
private let capCountMetric: Metric<Int>
|
||||||
name: "Number of caps",
|
|
||||||
description: "The total number of caps in the database")
|
|
||||||
|
|
||||||
private let imageCountMetric = Metric<Int>("caps.images",
|
private let imageCountMetric: Metric<Int>
|
||||||
name: "Total images",
|
|
||||||
description: "The total number of images for all caps")
|
|
||||||
|
|
||||||
private let classifierMetric = Metric<Int>("caps.classifier",
|
private let classifierMetric: Metric<Int>
|
||||||
name: "Classifier Version",
|
|
||||||
description: "The current version of the image classifier")
|
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ import Vapor
|
|||||||
import Foundation
|
import Foundation
|
||||||
import Clairvoyant
|
import Clairvoyant
|
||||||
|
|
||||||
public func configure(_ app: Application) throws {
|
public func configure(_ app: Application) async throws {
|
||||||
|
|
||||||
let resourceDirectory = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
let resourceDirectory = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
||||||
let publicDirectory = app.directory.publicDirectory
|
let publicDirectory = app.directory.publicDirectory
|
||||||
@ -10,7 +10,7 @@ public func configure(_ app: Application) throws {
|
|||||||
let config = Config(loadFrom: resourceDirectory)
|
let config = Config(loadFrom: resourceDirectory)
|
||||||
let authenticator = Authenticator(writers: config.writers)
|
let authenticator = Authenticator(writers: config.writers)
|
||||||
|
|
||||||
let monitor = MetricObserver(
|
let monitor = await MetricObserver(
|
||||||
logFolder: config.logURL,
|
logFolder: config.logURL,
|
||||||
accessManager: authenticator,
|
accessManager: authenticator,
|
||||||
logMetricId: "caps.log")
|
logMetricId: "caps.log")
|
||||||
@ -18,18 +18,18 @@ public func configure(_ app: Application) throws {
|
|||||||
// All new metrics are automatically registered with the standard observer
|
// All new metrics are automatically registered with the standard observer
|
||||||
MetricObserver.standard = monitor
|
MetricObserver.standard = monitor
|
||||||
|
|
||||||
let status = Metric<ServerStatus>("caps.status",
|
let status = try await Metric<ServerStatus>("caps.status",
|
||||||
name: "Status",
|
name: "Status",
|
||||||
description: "The general status of the service")
|
description: "The general status of the service")
|
||||||
status.update(.initializing)
|
try await status.update(.initializing)
|
||||||
|
|
||||||
let server = CapServer(in: URL(fileURLWithPath: publicDirectory))
|
|
||||||
|
|
||||||
monitor.registerRoutes(app)
|
|
||||||
|
|
||||||
app.http.server.configuration.port = config.port
|
app.http.server.configuration.port = config.port
|
||||||
app.routes.defaultMaxBodySize = .init(stringLiteral: config.maxBodySize)
|
app.routes.defaultMaxBodySize = .init(stringLiteral: config.maxBodySize)
|
||||||
|
|
||||||
|
let server = await CapServer(in: URL(fileURLWithPath: publicDirectory))
|
||||||
|
|
||||||
|
await monitor.registerRoutes(app)
|
||||||
|
|
||||||
if config.serveFiles {
|
if config.serveFiles {
|
||||||
let middleware = FileMiddleware(publicDirectory: publicDirectory)
|
let middleware = FileMiddleware(publicDirectory: publicDirectory)
|
||||||
app.middleware.use(middleware)
|
app.middleware.use(middleware)
|
||||||
@ -41,13 +41,18 @@ public func configure(_ app: Application) throws {
|
|||||||
// Initialize the server data
|
// Initialize the server data
|
||||||
do {
|
do {
|
||||||
try server.loadData()
|
try server.loadData()
|
||||||
status.update(.nominal)
|
|
||||||
} catch {
|
} catch {
|
||||||
status.update(.initializationFailure)
|
try await status.update(.initializationFailure)
|
||||||
}
|
}
|
||||||
|
try await status.update(.nominal)
|
||||||
}
|
}
|
||||||
|
|
||||||
func log(_ message: String) {
|
func log(_ message: String) {
|
||||||
MetricObserver.standard?.log(message)
|
guard let observer = MetricObserver.standard else {
|
||||||
print(message)
|
print(message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
Task {
|
||||||
|
await observer.log(message)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,5 +5,11 @@ var env = Environment.production
|
|||||||
try LoggingSystem.bootstrap(from: &env)
|
try LoggingSystem.bootstrap(from: &env)
|
||||||
let app = Application(env)
|
let app = Application(env)
|
||||||
defer { app.shutdown() }
|
defer { app.shutdown() }
|
||||||
try configure(app)
|
|
||||||
|
private let semaphore = DispatchSemaphore(value: 1)
|
||||||
|
Task {
|
||||||
|
try await configure(app)
|
||||||
|
semaphore.signal()
|
||||||
|
}
|
||||||
|
semaphore.wait()
|
||||||
try app.run()
|
try app.run()
|
||||||
|
Loading…
Reference in New Issue
Block a user