Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Below, some compatible visualisation tools:

- pprof's [`/debug/pprof/profile` endpoint](https://pkg.go.dev/net/http/pprof)
- Swift Profile Recorder's own `/sample` endpoint
- `/health` endpoint for health checks (returns `200 OK`)

## Example profiles

Expand Down
15 changes: 15 additions & 0 deletions Sources/ProfileRecorderServer/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,21 @@ public struct ProfileRecorderServer: Sendable {
) != nil:
// Native Swift Profile Recorder Sampling server
sampleRequest = try JSONDecoder().decode(SampleRequest.self, from: request.body ?? ByteBuffer())
case (.GET, .some(let decodedURI))
where decodedURI.components.matches(prefix: [], oneOfPaths: [["health"]]) != nil:
// Health check endpoint
try await outbound.write(
.head(
HTTPResponseHead(
version: .http1_1,
status: .ok,
headers: ["connection": "close"]
)
)
)
try await outbound.write(.body(ByteBuffer(string: "OK")))
try await outbound.write(.end(nil))
return
case (let verb, .some(let decodedURI)):
let extraRouteHandlers = self.state.withLockedValue { state in
state.extraRouteHandlers
Expand Down
18 changes: 18 additions & 0 deletions Tests/ProfileRecorderServerTests/ProfileRecorderServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ final class ProfileRecorderServerTests: XCTestCase {
}
}

func testHealthEndpoint() async throws {
let server = ProfileRecorderServer(
configuration: try ProfileRecorderServerConfiguration.makeTCPListener(host: "127.0.0.1", port: 0)
)
try await server.withProfileRecordingServer(logger: Logger(label: "")) { server in
guard case .successful(let serverAddress) = server.startResult else {
XCTFail("failed to start server")
return
}

let response = try await HTTPClient.shared.get(
url: "http://127.0.0.1:\(serverAddress.port!)/health"
).get()
XCTAssertEqual(.ok, response.status)
XCTAssertEqual(ByteBuffer(string: "OK"), response.body)
}
}

func testUserExtraHandlerBasic() async throws {
let server = ProfileRecorderServer(
configuration: try ProfileRecorderServerConfiguration.makeTCPListener(host: "127.0.0.1", port: 0)
Expand Down
Loading