Skip to content

Commit

Permalink
Merge pull request #87 from MatsMoll/develop
Browse files Browse the repository at this point in the history
[Metrics] Added new metrics logging
  • Loading branch information
MatsMoll authored Nov 21, 2020
2 parents 107bb7d + 779073a commit faed726
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var dependencies: [Package.Dependency] = [
// 💧 A server-side Swift web framework.
.package(name: "vapor", url: "https://github.com/vapor/vapor.git", from: "4.29.0"),

.package(name: "HTMLKitVaporProvider", url: "https://github.com/MatsMoll/htmlkit-vapor-provider.git", from: "1.0.0"),
.package(name: "HTMLKitVaporProvider", url: "https://github.com/MatsMoll/htmlkit-vapor-provider.git", from: "1.0.1"),
]

// Kognita Core
Expand Down
83 changes: 83 additions & 0 deletions Sources/App/HTMLRendererLogger/HTMLRendererLogger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// HTMLRendererLogger.swift
// App
//
// Created by Mats Mollestad on 21/11/2020.
//

import HTMLKit
import Vapor
import Metrics
import KognitaViews

class HTMLRendererLogger: HTMLRenderable {

static let durationLabel = "html_renderer_duration"

let renderer: HTMLRenderer
let metricsFactory: MetricsFactory

init(renderer: HTMLRenderer, metricsFactory: MetricsFactory) {
self.renderer = renderer
self.metricsFactory = metricsFactory
}

func render<T>(raw type: T.Type, with context: T.Context) throws -> String where T: HTMLTemplate {
let start = Date()
defer {
let template = String(reflecting: T.self)
metricsFactory.makeTimer(
label: HTMLRendererLogger.durationLabel,
dimensions: [
("template", template)
]
)
.recordNanoseconds(Int64(Date().timeIntervalSince(start) * 1000))
}
return try renderer.render(raw: T.self, with: context)
}

func render<T>(raw type: T.Type) throws -> String where T: HTMLPage {
let start = Date()
defer {
let page = String(reflecting: T.self)
metricsFactory.makeTimer(
label: HTMLRendererLogger.durationLabel,
dimensions: [
("page", page)
]
)
.recordNanoseconds(Int64(Date().timeIntervalSince(start) * 1000))
}
return try renderer.render(raw: T.self)
}

func add<T>(view: T) throws where T: HTMLPage {
try renderer.add(view: view)
}

func add<T>(view: T) throws where T: HTMLTemplate {
try renderer.add(view: view)
}

func registerLocalization(atPath path: String, defaultLocale: String) throws {
try renderer.registerLocalization(atPath: path, defaultLocale: defaultLocale)
}
}

struct HTMLKitLifecycle: LifecycleHandler {

let rootUrl: String

func willBoot(_ app: Application) throws {
app.htmlkit.localizationPath = app.directory.workingDirectory + "Resources/Localization"
app.htmlkit.defaultLocale = "nb"

let renderer = HTMLRenderer()
try KognitaViews.renderer(rootURL: rootUrl, renderer: renderer)
app.htmlkit.renderer = HTMLRendererLogger(renderer: renderer, metricsFactory: MetricsSystem.factory)

app.verifyEmailRenderer.use { VerifyEmailRenderer(renderer: $0.htmlkit) }
app.resetPasswordRenderer.use { ResetPasswordMailRenderer(renderer: $0.htmlkit) }
}
}
18 changes: 5 additions & 13 deletions Sources/App/configure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public func configure(_ app: Application) throws {
guard let rootUrl = Environment.get("ROOT_URL") else {
fatalError("Need to set a ROOT_URL")
}
app.lifecycle.use(HTMLKitLifecycle(rootUrl: rootUrl))
// Adds htmlkit to the app lifecycle after setting up the templates
_ = app.htmlkit

// Catches errors and converts to HTTP responses for developers
app.middleware.use(ErrorMiddleware.default(environment: app.environment))
Expand All @@ -26,17 +29,6 @@ public func configure(_ app: Application) throws {
app.logger.logLevel = .debug
}

app.htmlkit.localizationPath = app.directory.workingDirectory + "Resources/Localization"
app.htmlkit.defaultLocale = "nb"

try KognitaViews.renderer(rootURL: rootUrl, renderer: app.htmlkit.renderer)

// try renderer.registerLocalization(atPath: path, defaultLocale: "nb")
// renderer.timeZone = TimeZone(identifier: "CET") ?? .current

app.verifyEmailRenderer.use { VerifyEmailRenderer(renderer: $0.htmlkit) }
app.resetPasswordRenderer.use { ResetPasswordMailRenderer(renderer: $0.htmlkit) }

try routes(app)
}

Expand All @@ -45,7 +37,7 @@ private func registerRouter(in app: Application) throws {
}

struct ResetPasswordMailRenderer: ResetPasswordMailRenderable {
let renderer: HTMLRenderer
let renderer: HTMLRenderable

public func render(with token: User.ResetPassword.Token, for user: User) throws -> String {
try renderer.render(
Expand All @@ -59,7 +51,7 @@ struct ResetPasswordMailRenderer: ResetPasswordMailRenderable {
}

struct VerifyEmailRenderer: VerifyEmailRenderable {
let renderer: HTMLRenderer
let renderer: HTMLRenderable

func render(with content: User.VerifyEmail.EmailContent, on request: Request) throws -> EventLoopFuture<String> {
let html = try renderer.render(
Expand Down

0 comments on commit faed726

Please sign in to comment.