Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions Sources/App/Core/CFRayRouteLoggingMiddleware.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
// Copyright Dave Verwer, Sven A. Schmidt, and other contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Dependencies
import Vapor


// Replica of the Vapor RouteLoggingMiddleware that's tweaked to explicitly expose the cf-ray header in the logger metadata for the request.
public final class CFRayRouteLoggingMiddleware: Middleware {
public let logLevel: Logger.Level

public init(logLevel: Logger.Level = .info) {
self.logLevel = logLevel
}

public func respond(to request: Request, chainingTo next: Responder) -> EventLoopFuture<Response> {
@Dependency(\.environment) var environment

guard environment.enableCFRayLogging() else {
return next.respond(to: request)
}
guard let cfray = request.headers.first(name: "cf-ray") else {
return next.respond(to: request)
}

request.logger[metadataKey: "cf-ray"] = .string(cfray)
request.logger.log(level: self.logLevel, "\(request.method) \(request.url.path.removingPercentEncoding ?? request.url.path)")
return next.respond(to: request)
Expand Down
7 changes: 7 additions & 0 deletions Sources/App/Core/Dependencies/EnvironmentClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct EnvironmentClient {
var current: @Sendable () -> Environment = { XCTFail("current"); return .development }
var currentReferenceCache: @Sendable () -> CurrentReferenceCache?
var dbId: @Sendable () -> String?
var enableCFRayLogging: @Sendable () -> Bool = { XCTFail("enableCFRayLogging"); return true }
var mastodonCredentials: @Sendable () -> Mastodon.Credentials?
var random: @Sendable (_ range: ClosedRange<Double>) -> Double = { XCTFail("random"); return Double.random(in: $0) }

Expand Down Expand Up @@ -104,6 +105,11 @@ extension EnvironmentClient: DependencyKey {
current: { (try? Environment.detect()) ?? .development },
currentReferenceCache: { .live },
dbId: { Environment.get("DATABASE_ID") },
enableCFRayLogging: {
Environment.get("ENABLE_CF_RAY_LOGGING")
.flatMap(\.asBool)
?? false
},
mastodonCredentials: {
Environment.get("MASTODON_ACCESS_TOKEN")
.map(Mastodon.Credentials.init(accessToken:))
Expand Down Expand Up @@ -145,6 +151,7 @@ extension EnvironmentClient: TestDependencyKey {
var mock = Self()
mock.appVersion = { "test" }
mock.current = { .development }
mock.enableCFRayLogging = { true }
return mock
}
}
Expand Down
1 change: 1 addition & 0 deletions app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ x-shared: &shared
DATABASE_USERNAME: ${DATABASE_USERNAME}
DATABASE_PASSWORD: ${DATABASE_PASSWORD}
DATABASE_USE_TLS: ${DATABASE_USE_TLS}
ENABLE_CF_RAY_LOGGING: ${ENABLE_CF_RAY_LOGGING}
FAILURE_MODE: ${FAILURE_MODE}
GITHUB_TOKEN: ${GITHUB_TOKEN}
GITLAB_API_TOKEN: ${GITLAB_API_TOKEN}
Expand Down
Loading