-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add redis health check * style: rename function * test: refactor test
- Loading branch information
1 parent
ea74280
commit 377bcf0
Showing
8 changed files
with
346 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
Sources/HealthChecks/RedisHealthChecks/RedisChecksProtocol.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// FS App Health Checks | ||
// Copyright (C) 2024 FREEDOM SPACE, LLC | ||
|
||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published | ||
// by the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
// | ||
// RedisChecksProtocol.swift | ||
// | ||
// | ||
// Created by Mykola Buhaiov on 21.02.2024. | ||
// | ||
|
||
import Vapor | ||
|
||
/// Groups func for get redis health check | ||
public protocol RedisChecksProtocol { | ||
/// Get redis connection | ||
/// - Returns: `HealthCheckItem` | ||
func connection() async -> HealthCheckItem | ||
|
||
/// Get response time from redis | ||
/// - Returns: `HealthCheckItem` | ||
func getResponseTime() async -> HealthCheckItem | ||
|
||
/// Get pong from redis | ||
/// - Returns: `String` | ||
func pong() async -> String | ||
} |
101 changes: 101 additions & 0 deletions
101
Sources/HealthChecks/RedisHealthChecks/RedisHealthChecks.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// FS App Health Checks | ||
// Copyright (C) 2024 FREEDOM SPACE, LLC | ||
|
||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published | ||
// by the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
// | ||
// RedisHealthChecks.swift | ||
// | ||
// | ||
// Created by Mykola Buhaiov on 21.02.2024. | ||
// | ||
|
||
import Vapor | ||
import Fluent | ||
import Redis | ||
|
||
/// Service that provides redis health check functionality | ||
public struct RedisHealthChecks: RedisHealthChecksProtocol { | ||
/// Instance of app as `Application` | ||
public let app: Application | ||
|
||
/// Get redis connection | ||
/// - Returns: `HealthCheckItem` | ||
public func connection() async -> HealthCheckItem { | ||
let dateNow = Date().timeIntervalSinceReferenceDate | ||
let response = await pong() | ||
let result = HealthCheckItem( | ||
componentId: app.redisId, | ||
componentType: .datastore, | ||
observedValue: Date().timeIntervalSinceReferenceDate - dateNow, | ||
observedUnit: "s", | ||
status: response.lowercased().contains("pong") ? .pass : .fail, | ||
time: app.dateTimeISOFormat.string(from: Date()), | ||
output: !response.lowercased().contains("pong") ? response : nil, | ||
links: nil, | ||
node: nil | ||
) | ||
return result | ||
} | ||
|
||
/// Get response time from redis | ||
/// - Returns: `HealthCheckItem` | ||
public func getResponseTime() async -> HealthCheckItem { | ||
let dateNow = Date().timeIntervalSinceReferenceDate | ||
let response = await pong() | ||
let result = HealthCheckItem( | ||
componentId: app.redisId, | ||
componentType: .datastore, | ||
observedValue: Date().timeIntervalSinceReferenceDate - dateNow, | ||
observedUnit: "s", | ||
status: response.lowercased().contains("pong") ? .pass : .fail, | ||
time: app.dateTimeISOFormat.string(from: Date()), | ||
output: !response.lowercased().contains("pong") ? response : nil, | ||
links: nil, | ||
node: nil | ||
) | ||
return result | ||
} | ||
|
||
/// Get pong from redis | ||
/// - Returns: `String` | ||
public func pong() async -> String { | ||
let result = try? await app.redis.ping().get() | ||
var connectionDescription = "ERROR: No connect to Redis database" | ||
if let result, result.lowercased().contains("pong") { | ||
connectionDescription = result | ||
} | ||
return connectionDescription | ||
} | ||
|
||
/// Check with setup options | ||
/// - Parameter options: array of `MeasurementType` | ||
/// - Returns: dictionary `[String: HealthCheckItem]` | ||
public func checkHealth(for options: [MeasurementType]) async -> [String: HealthCheckItem] { | ||
var result = ["": HealthCheckItem()] | ||
let measurementTypes = Array(Set(options)) | ||
for type in measurementTypes { | ||
switch type { | ||
case .responseTime: | ||
result["\(ComponentName.redis):\(MeasurementType.responseTime)"] = await getResponseTime() | ||
case .connections: | ||
result["\(ComponentName.redis):\(MeasurementType.connections)"] = await connection() | ||
default: | ||
break | ||
} | ||
} | ||
return result | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Sources/HealthChecks/RedisHealthChecks/RedisHealthChecksProtocol.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// FS App Health Checks | ||
// Copyright (C) 2024 FREEDOM SPACE, LLC | ||
|
||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published | ||
// by the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
// | ||
// RedisHealthChecksProtocol.swift | ||
// | ||
// | ||
// Created by Mykola Buhaiov on 21.02.2024. | ||
// | ||
|
||
import Vapor | ||
|
||
/// Groups func for get redis health check | ||
public protocol RedisHealthChecksProtocol: RedisChecksProtocol, ChecksProtocol {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// FS App Health Checks | ||
// Copyright (C) 2024 FREEDOM SPACE, LLC | ||
|
||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published | ||
// by the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
// | ||
// RedisHealthChecksMock.swift | ||
// | ||
// | ||
// Created by Mykola Buhaiov on 21.02.2024. | ||
// | ||
|
||
import Vapor | ||
@testable import HealthChecks | ||
|
||
public struct RedisHealthChecksMock: RedisHealthChecksProtocol { | ||
static let redisId = "adca7c3d-55f4-4ab3-a842-18b35f50cb0f" | ||
static let healthCheckItem = HealthCheckItem( | ||
componentId: redisId, | ||
componentType: .datastore, | ||
observedValue: 1, | ||
observedUnit: "s", | ||
status: .pass, | ||
affectedEndpoints: nil, | ||
time: "2024-02-01T11:11:59.364", | ||
output: "Ok", | ||
links: nil, | ||
node: nil | ||
) | ||
|
||
public func connection() async -> HealthCheckItem { | ||
RedisHealthChecksMock.healthCheckItem | ||
} | ||
|
||
public func getResponseTime() async -> HealthCheckItem { | ||
RedisHealthChecksMock.healthCheckItem | ||
} | ||
|
||
public func pong() async -> String { | ||
"PONG" | ||
} | ||
|
||
public func checkHealth(for options: [MeasurementType]) async -> [String: HealthCheckItem] { | ||
let result = [ | ||
"\(ComponentName.redis):\(MeasurementType.responseTime)": RedisHealthChecksMock.healthCheckItem, | ||
"\(ComponentName.redis):\(MeasurementType.connections)": RedisHealthChecksMock.healthCheckItem | ||
] | ||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// FS App Health Checks | ||
// Copyright (C) 2024 FREEDOM SPACE, LLC | ||
|
||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published | ||
// by the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
// | ||
// RedisHealthChecksTests.swift | ||
// | ||
// | ||
// Created by Mykola Buhaiov on 21.02.2024. | ||
// | ||
|
||
import Vapor | ||
import XCTest | ||
@testable import HealthChecks | ||
|
||
final class RedisHealthChecksTests: XCTestCase { | ||
func testConnection() async throws { | ||
let app = Application(.testing) | ||
defer { app.shutdown() } | ||
app.redisId = UUID().uuidString | ||
app.redisHealthChecks = RedisHealthChecksMock() | ||
let result = await app.redisHealthChecks?.connection() | ||
XCTAssertEqual(result, RedisHealthChecksMock.healthCheckItem) | ||
} | ||
|
||
func testGetResponseTime() async throws { | ||
let app = Application(.testing) | ||
defer { app.shutdown() } | ||
let redisId = UUID().uuidString | ||
app.redisId = redisId | ||
app.redisHealthChecks = RedisHealthChecksMock() | ||
let result = await app.redisHealthChecks?.getResponseTime() | ||
XCTAssertEqual(result, RedisHealthChecksMock.healthCheckItem) | ||
XCTAssertEqual(app.redisId, redisId) | ||
} | ||
|
||
func testCheckHealth() async throws { | ||
let app = Application(.testing) | ||
defer { app.shutdown() } | ||
app.redisHealthChecks = RedisHealthChecksMock() | ||
let result = await app.redisHealthChecks?.checkHealth(for: [MeasurementType.responseTime, MeasurementType.connections]) | ||
let redisConnections = result?["\(ComponentName.redis):\(MeasurementType.connections)"] | ||
XCTAssertEqual(redisConnections, RedisHealthChecksMock.healthCheckItem) | ||
let redisResponseTimes = result?["\(ComponentName.redis):\(MeasurementType.responseTime)"] | ||
XCTAssertEqual(redisResponseTimes, RedisHealthChecksMock.healthCheckItem) | ||
} | ||
|
||
func testGetPong() async throws { | ||
let app = Application(.testing) | ||
defer { app.shutdown() } | ||
app.redisHealthChecks = RedisHealthChecksMock() | ||
let result = await app.redisHealthChecks?.pong() | ||
XCTAssertEqual(result, "PONG") | ||
} | ||
} |