Skip to content

🔥 feat: Add response format support to healthcheck middleware#4178

Merged
ReneWerner87 merged 6 commits intomainfrom
claude/update-healthcheck-endpoints
Apr 5, 2026
Merged

🔥 feat: Add response format support to healthcheck middleware#4178
ReneWerner87 merged 6 commits intomainfrom
claude/update-healthcheck-endpoints

Conversation

@Claude
Copy link
Copy Markdown
Contributor

@Claude Claude AI commented Apr 4, 2026

Description

The healthcheck middleware now supports multiple response formats (JSON, XML, MessagePack, CBOR) in addition to the default plain text format. This allows applications to maintain consistent API contracts across all endpoints, including health checks.

Changes introduced

  • New ResponseFormat configuration: Added ResponseFormat field to Config with 5 format options:

    • FormatText (default, backward compatible)
    • FormatJSON
    • FormatXML
    • FormatMsgPack
    • FormatCBOR
  • Response structure: Introduced healthResponse struct that serializes consistently across all formats using proper struct tags (json, xml, msgpack, cbor) to ensure lowercase "status" key in all encodings:

    • Success: {"status":"OK"} (JSON example)
    • Failure: {"status":"Service Unavailable"}
  • Documentation Update:

    • Added "Response Formats" section with usage examples
    • Documented encoder configuration requirements for MsgPack and CBOR with self-contained code snippets including all necessary imports (fiber/v3, middleware/healthcheck, encoder libraries)
    • Updated config documentation with new field and correct ResponseFormat type definition with iota
    • docs/middleware/healthcheck.md
  • Comprehensive test coverage: Added test cases for all 5 formats validating:

    • Correct Content-Type headers
    • Success and failure response bodies
    • Status code correctness (200 OK / 503 Service Unavailable)
    • MsgPack and CBOR body decoding with field key casing and value assertions
    • Tests use exported LivenessEndpoint and ReadinessEndpoint constants for consistency with the rest of the test suite
  • Changelog/What's New: Healthcheck middleware now supports configurable response formats (Text, JSON, XML, MsgPack, CBOR) while maintaining full backward compatibility.

  • API Longevity: The ResponseFormat enum pattern provides extensibility for future formats without breaking changes. Default behavior (plain text) is unchanged, ensuring zero impact on existing deployments. Constant names follow Fiber's established naming conventions for brevity and clarity.

  • Examples:

// JSON format
app.Get(healthcheck.LivenessEndpoint, healthcheck.New(healthcheck.Config{
    ResponseFormat: healthcheck.FormatJSON,
}))
// GET /livez → {"status":"OK"}

// XML with custom probe
app.Get(healthcheck.ReadinessEndpoint, healthcheck.New(healthcheck.Config{
    ResponseFormat: healthcheck.FormatXML,
    Probe: func(c fiber.Ctx) bool {
        return database.Ping() == nil
    },
}))
// GET /readyz → <healthResponse><status>OK</status></healthResponse>

// MsgPack (requires encoder configuration)
app := fiber.New(fiber.Config{
    MsgPackEncoder: msgpack.Marshal,
})
app.Get(healthcheck.LivenessEndpoint, healthcheck.New(healthcheck.Config{
    ResponseFormat: healthcheck.FormatMsgPack,
}))

Type of change

  • New feature (non-breaking change which adds functionality)
  • Enhancement (improvement to existing features and functionality)
  • Documentation update (changes to documentation)

Checklist

  • Conducted a self-review of the code and provided comments for complex or critical parts.
  • Updated the documentation in the /docs/ directory for Fiber's documentation.
  • Added or updated unit tests to validate the effectiveness of the changes or new features.
  • Ensured that new and existing unit tests pass locally with the changes.
  • Verified that any new dependencies are essential and have been agreed upon by the maintainers/community.
  • Aimed for optimal performance with minimal allocations in the new code.

- Add ResponseFormat type with constants for Text, JSON, XML, MsgPack, and CBOR
- Update Config struct with ResponseFormat field (defaults to Text for backward compatibility)
- Implement format-specific response handling in healthcheck middleware
- Add comprehensive tests for all response formats
- Update documentation with examples and usage notes

Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/9c742a5a-1106-4e9e-aef3-50cd4e5d3ee2

Co-authored-by: gaby <[email protected]>
@Claude Claude AI changed the title [WIP] Update middleware/healthcheck to support custom JSON responses 🔥 feat: Add response format support to healthcheck middleware Apr 4, 2026
@Claude Claude AI requested a review from gaby April 4, 2026 03:09
Claude finished work on behalf of gaby April 4, 2026 03:09
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.23%. Comparing base (bf952a1) to head (0d4ec53).
⚠️ Report is 7 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4178      +/-   ##
==========================================
+ Coverage   91.22%   91.23%   +0.01%     
==========================================
  Files         123      123              
  Lines       11832    11847      +15     
==========================================
+ Hits        10794    10809      +15     
  Misses        653      653              
  Partials      385      385              
Flag Coverage Δ
unittests 91.23% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gaby
Copy link
Copy Markdown
Member

gaby commented Apr 4, 2026

@claude[agent] The response format constants names are too verbose.

- Rename ResponseFormatText -> FormatText
- Rename ResponseFormatJSON -> FormatJSON
- Rename ResponseFormatXML -> FormatXML
- Rename ResponseFormatMsgPack -> FormatMsgPack
- Rename ResponseFormatCBOR -> FormatCBOR
- Update all references in code, tests, and documentation

Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/9ea6fcf4-ca4d-4353-8e6d-5a3131270d59

Co-authored-by: gaby <[email protected]>
@Claude
Copy link
Copy Markdown
Contributor Author

Claude AI commented Apr 4, 2026

@claude[agent] The response format constants names are too verbose.

Shortened the constant names to FormatText, FormatJSON, FormatXML, FormatMsgPack, and FormatCBOR in commit a3fd22e.

Claude finished work on behalf of gaby April 4, 2026 08:39
@gaby gaby marked this pull request as ready for review April 4, 2026 14:55
@gaby gaby requested a review from a team as a code owner April 4, 2026 14:55
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds configurable response formats to the healthcheck middleware so health endpoints can return plain text (default) or structured responses (JSON/XML/MsgPack/CBOR) with consistent semantics across success/failure.

Changes:

  • Introduces ResponseFormat config + constants to select output format.
  • Updates middleware handler to emit {"status": ...}-style payloads for structured formats while preserving the text default.
  • Adds documentation and unit tests covering the new formats.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
middleware/healthcheck/healthcheck.go Adds healthResponse struct and switches handler output based on configured response format.
middleware/healthcheck/config.go Adds ResponseFormat type/field and format constants.
middleware/healthcheck/healthcheck_test.go Adds new tests for each response format and header/status expectations.
docs/middleware/healthcheck.md Documents response formats and required encoder setup for MsgPack/CBOR.

Comment thread middleware/healthcheck/healthcheck.go Outdated
Comment thread middleware/healthcheck/healthcheck_test.go
Comment thread middleware/healthcheck/healthcheck_test.go
@gaby
Copy link
Copy Markdown
Member

gaby commented Apr 4, 2026

@copilot apply changes based on the comments in this thread

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 4, 2026

@copilot apply changes based on the comments in this thread

Applied all three review suggestions in commit 7ee05cf:

  1. Fixed struct tags to use msgpack:"status" and cbor:"status" for consistent key serialization across all formats
  2. Added MsgPack body decoding + field key/value assertions to Test_HealthCheck_MsgPack_Format
  3. Added CBOR body decoding + field key/value assertions to Test_HealthCheck_CBOR_Format

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

Comment thread middleware/healthcheck/healthcheck_test.go
Comment thread middleware/healthcheck/healthcheck_test.go
Comment thread docs/middleware/healthcheck.md
@gaby
Copy link
Copy Markdown
Member

gaby commented Apr 4, 2026

@copilot apply changes based on the comments in this thread

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 4, 2026

@copilot apply changes based on the comments in this thread

Applied the documentation fix in commit 0ee784f — added the missing github.com/gofiber/fiber/v3 and github.com/gofiber/fiber/v3/middleware/healthcheck imports to the MsgPack/CBOR encoder configuration example so the snippet is self-contained.

Regarding the JSON test string comments (lines 291-294 and 302-305): those are already correct — the backtick raw string literals contain valid JSON without any backslash escaping, and the tests pass. No changes needed there.

Copilot finished work on behalf of gaby April 4, 2026 17:32
Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 0ee784f Previous: bf952a1 Ratio
Benchmark_Redirect_Route (github.com/gofiber/fiber/v3) 283.2 ns/op 16 B/op 1 allocs/op 174.4 ns/op 16 B/op 1 allocs/op 1.62
Benchmark_Redirect_Route (github.com/gofiber/fiber/v3) - ns/op 283.2 ns/op 174.4 ns/op 1.62

This comment was automatically generated by workflow using github-action-benchmark.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

Comment thread middleware/healthcheck/healthcheck_test.go
Comment thread docs/middleware/healthcheck.md Outdated
@ReneWerner87 ReneWerner87 merged commit 0e2821f into main Apr 5, 2026
21 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in v3 Apr 5, 2026
@welcome
Copy link
Copy Markdown

welcome Bot commented Apr 5, 2026

Congrats on merging your first pull request! 🎉 We here at Fiber are proud of you! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@ReneWerner87 ReneWerner87 deleted the claude/update-healthcheck-endpoints branch April 5, 2026 12:11
@ReneWerner87 ReneWerner87 modified the milestones: v3, v3.2.0 Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

🤗 [Question]: Should middleware/healthcheck support custom JSON responses for /livez and /readyz?

5 participants