Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add app health checks #9

Merged
merged 25 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e765440
feat: add app health checks
gulivero1773 Jan 30, 2024
eb64be9
refactor: refactor documentation
gulivero1773 Jan 30, 2024
10f5a44
refactor: add documentation and test
gulivero1773 Jan 30, 2024
5c9a67c
docs: change documentation
gulivero1773 Jan 30, 2024
dd52104
refactor: refactor description
gulivero1773 Jan 30, 2024
3d11ed3
feat: add function for get app health check
gulivero1773 Jan 30, 2024
1582245
docs: change documentation
gulivero1773 Jan 30, 2024
80c6fbe
docs: change documentations
gulivero1773 Jan 30, 2024
1c5839d
refactor: change license in all files and add getApplicationHealth
gulivero1773 Jan 31, 2024
5922889
refactor: refactor function for get HealthCheck for application
gulivero1773 Jan 31, 2024
d5a2013
docs: change error documentation
gulivero1773 Jan 31, 2024
3d4b094
refactor: add extension for ComponentName
gulivero1773 Jan 31, 2024
7fe7e55
fix: change codecov version to 4
gulivero1773 Jan 31, 2024
fece2c7
refactor: change actions version in test to 4
gulivero1773 Jan 31, 2024
95756a0
fix: refactor documentation
gulivero1773 Jan 31, 2024
c985190
docs: change error description
gulivero1773 Jan 31, 2024
8c86004
docs: change documentation
gulivero1773 Jan 31, 2024
b8e78ed
fix: change test version
gulivero1773 Jan 31, 2024
51b5a9f
refactor: refactor test
gulivero1773 Jan 31, 2024
e1ab434
docs: refactor documentation
gulivero1773 Jan 31, 2024
4b89456
refactor: change getPublicVersion
gulivero1773 Jan 31, 2024
76b750b
refactor: refactor test
gulivero1773 Jan 31, 2024
9c21f16
test: refactor test
gulivero1773 Jan 31, 2024
544a469
test: delete not use test
gulivero1773 Jan 31, 2024
f1e145d
refactor: change getPublicVersion
gulivero1773 Jan 31, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4
gulivero1773 marked this conversation as resolved.
Show resolved Hide resolved

- name: Initialize CodeQL
uses: github/codeql-action/init@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
runs-on: ubuntu-latest
container: swift:jammy
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
gulivero1773 marked this conversation as resolved.
Show resolved Hide resolved
- name: Build
run: swift build -v
- name: Run tests
Expand Down
194 changes: 194 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
{
"pins" : [
{
"identity" : "async-http-client",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swift-server/async-http-client.git",
"state" : {
"revision" : "291438696abdd48d2a83b52465c176efbd94512b",
"version" : "1.20.1"
}
},
{
"identity" : "async-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/async-kit.git",
"state" : {
"revision" : "7ece208cd401687641c88367a00e3ea2b04311f1",
"version" : "1.19.0"
}
},
{
"identity" : "console-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/console-kit.git",
"state" : {
"revision" : "a31f44ebfbd15a2cc0fda705279676773ac16355",
"version" : "4.14.1"
}
},
{
"identity" : "multipart-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/multipart-kit.git",
"state" : {
"revision" : "12ee56f25bd3fc4c2d09c2aa16e69de61dc786e8",
"version" : "4.6.0"
}
},
{
"identity" : "routing-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/routing-kit.git",
"state" : {
"revision" : "2a92a7eac411a82fb3a03731be5e76773ebe1b3e",
"version" : "4.9.0"
}
},
{
"identity" : "swift-algorithms",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-algorithms.git",
"state" : {
"revision" : "f6919dfc309e7f1b56224378b11e28bab5bccc42",
"version" : "1.2.0"
}
},
{
"identity" : "swift-atomics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-atomics.git",
"state" : {
"revision" : "cd142fd2f64be2100422d658e7411e39489da985",
"version" : "1.2.0"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "d029d9d39c87bed85b1c50adee7c41795261a192",
"version" : "1.0.6"
}
},
{
"identity" : "swift-crypto",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-crypto.git",
"state" : {
"revision" : "cc76b894169a3c86b71bac10c78a4db6beb7a9ad",
"version" : "3.2.0"
}
},
{
"identity" : "swift-http-types",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-http-types",
"state" : {
"revision" : "12358d55a3824bd5fed310b999ea8cf83a9a1a65",
"version" : "1.0.3"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"revision" : "e97a6fcb1ab07462881ac165fdbb37f067e205d5",
"version" : "1.5.4"
}
},
{
"identity" : "swift-metrics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-metrics.git",
"state" : {
"revision" : "971ba26378ab69c43737ee7ba967a896cb74c0d1",
"version" : "2.4.1"
}
},
{
"identity" : "swift-nio",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio.git",
"state" : {
"revision" : "635b2589494c97e48c62514bc8b37ced762e0a62",
"version" : "2.63.0"
}
},
{
"identity" : "swift-nio-extras",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-extras.git",
"state" : {
"revision" : "363da63c1966405764f380c627409b2f9d9e710b",
"version" : "1.21.0"
}
},
{
"identity" : "swift-nio-http2",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-http2.git",
"state" : {
"revision" : "0904bf0feb5122b7e5c3f15db7df0eabe623dd87",
"version" : "1.30.0"
}
},
{
"identity" : "swift-nio-ssl",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-ssl.git",
"state" : {
"revision" : "7c381eb6083542b124a6c18fae742f55001dc2b5",
"version" : "2.26.0"
}
},
{
"identity" : "swift-nio-transport-services",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-transport-services.git",
"state" : {
"revision" : "6cbe0ed2b394f21ab0d46b9f0c50c6be964968ce",
"version" : "1.20.1"
}
},
{
"identity" : "swift-numerics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-numerics.git",
"state" : {
"revision" : "0a5bc04095a675662cf24757cc0640aa2204253b",
"version" : "1.0.2"
}
},
{
"identity" : "swift-system",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-system.git",
"state" : {
"revision" : "025bcb1165deab2e20d4eaba79967ce73013f496",
"version" : "1.2.1"
}
},
{
"identity" : "vapor",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/vapor.git",
"state" : {
"revision" : "4942d74e8493fc918ed6144c835c8a0e6affd4f4",
"version" : "4.92.1"
}
},
{
"identity" : "websocket-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/websocket-kit.git",
"state" : {
"revision" : "53fe0639a98903858d0196b699720decb42aee7b",
"version" : "2.14.0"
}
}
],
"version" : 2
}
15 changes: 11 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
// swift-tools-version: 5.8
// swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "fs-app-health-checks",
platforms: [
.macOS(.v13),
.iOS(.v16)
.macOS(.v13)
],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "AppHealthChecks",
targets: ["AppHealthChecks"])
],
dependencies: [
// 💧 A server-side Swift web framework.
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0")
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(
name: "AppHealthChecks"),
name: "AppHealthChecks",
dependencies: [
.product(name: "Vapor", package: "vapor")
]
),
.testTarget(
name: "AppHealthChecksTests",
dependencies: ["AppHealthChecks"])
Expand Down
6 changes: 0 additions & 6 deletions Sources/AppHealthChecks/AppHealthChecks.swift

This file was deleted.

62 changes: 62 additions & 0 deletions Sources/AppHealthChecks/Extensions/Application+Extensions.swift
gulivero1773 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// FS Dependency Injection
gulivero1773 marked this conversation as resolved.
Show resolved Hide resolved
// 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/>.

//
// Application+Extensions.swift
//
//
// Created by Mykola Buhaiov on 31.01.2024.
//

import Vapor

/// Extension for core type representing a Vapor application.
extension Application {
/// A `ServiceIdKey` conform to StorageKey protocol
private struct ServiceIdKey: StorageKey {
/// Less verbose typealias for `UUID`.
typealias Value = UUID
}

/// Setup `serviceId` in application storage
public var serviceId: UUID {
get {
guard let serviceId = storage[ServiceIdKey.self] else {
fatalError("ServiceId not setup for application")

Check warning on line 39 in Sources/AppHealthChecks/Extensions/Application+Extensions.swift

View check run for this annotation

Codecov / codecov/patch

Sources/AppHealthChecks/Extensions/Application+Extensions.swift#L39

Added line #L39 was not covered by tests
gulivero1773 marked this conversation as resolved.
Show resolved Hide resolved
}
return serviceId
}
set { storage[ServiceIdKey.self] = newValue }
}

/// A `ReleaseIdKey` conform to StorageKey protocol
private struct ReleaseIdKey: StorageKey {
/// Less verbose typealias for `String`.
typealias Value = String
}

/// Setup `releaseId` in application storage
public var releaseId: String {
get {
guard let releaseId = storage[ReleaseIdKey.self] else {
fatalError("ReleaseId not setup for application")

Check warning on line 56 in Sources/AppHealthChecks/Extensions/Application+Extensions.swift

View check run for this annotation

Codecov / codecov/patch

Sources/AppHealthChecks/Extensions/Application+Extensions.swift#L56

Added line #L56 was not covered by tests
gulivero1773 marked this conversation as resolved.
Show resolved Hide resolved
}
return releaseId
}
set { storage[ReleaseIdKey.self] = newValue }
}
}
gulivero1773 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// FS Dependency Injection
// 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/>.

//
// AppHealthChecks.swift
//
//
// Created by Mykola Buhaiov on 30.01.2024.
//

import Vapor

/// Service that provides app health check functionality
public struct AppHealthChecks {
private let logger = Logger(label: "AppHealthChecks")

/// Get app major version
/// - Parameter serverVersion: `String`
/// - Returns: `Int`
public func getPublicVersion(from version: String?) -> Int {
let components = version?.components(separatedBy: ".")
guard let version = Int(components?.first ?? "0") else {
logger.error("In version: \(String(describing: version)) not found first index")
return 0
}
return version
}
gulivero1773 marked this conversation as resolved.
Show resolved Hide resolved

/// Get health for application
/// - Parameter app: `Application`
/// - Returns: `HealthCheck`
public func getHealth(from app: Application) -> HealthCheck {
let healthCheck = HealthCheck(
version: self.getPublicVersion(from: app.releaseId),
releaseId: app.releaseId,
serviceId: app.serviceId
)
return healthCheck
}
}
Loading
Loading