Skip to content

Add unit tests for Prometheus (pkg/metrics) package#17

Open
abdultolba wants to merge 2 commits intopmady:mainfrom
abdultolba:chore/add-metrics-unit-tests
Open

Add unit tests for Prometheus (pkg/metrics) package#17
abdultolba wants to merge 2 commits intopmady:mainfrom
abdultolba:chore/add-metrics-unit-tests

Conversation

@abdultolba
Copy link
Contributor

Summary

  • Add comprehensive unit tests for PrometheusClient using httptest to mock the Prometheus HTTP API
  • Previously only MockClient was tested; now the real client implementation has full test coverage

Changes

Helper Functions:

  • makeVectorResponse() - generates successful vector responses
  • makeScalarResponse() - generates scalar responses
  • makeEmptyVectorResponse() - generates empty result responses
  • makeErrorResponse() - generates Prometheus error responses
  • makeMatrixResponse() - generates unexpected matrix type responses
  • makeVectorResponseMultiple() - generates multi-sample responses

Success Tests:

  • TestPrometheusClient_ImplementsInterface - compile-time interface check
  • TestNewPrometheusClient - constructor validation
  • TestPrometheusClient_Query_Success - vector and scalar response handling
  • TestPrometheusClient_GetLatencyP99 - verifies default query and return value
  • TestPrometheusClient_GetLatencyP95 - verifies default query and return value
  • TestPrometheusClient_GetGPUUtilization - verifies default query and return value
  • TestPrometheusClient_GetQueueDepth - verifies int64 conversion and default query

Error Handling Tests:

  • Connection failure
  • HTTP 500/400 errors
  • Prometheus error status responses
  • Empty results
  • Invalid JSON
  • Unexpected result types (matrix)

Edge Case Tests:

  • Context cancellation
  • Custom query passthrough
  • Multiple samples (returns first)
  • NaN values

Test Plan

  • go test ./pkg/metrics/... - all tests pass
  • go test -cover ./pkg/metrics/... - 93.8% coverage (exceeds 80% target)
  • go vet ./pkg/metrics/... - no issues

Closes #2

  - Add helper functions for generating Prometheus API JSON responses
  - Add success tests for Query, GetLatencyP99, GetLatencyP95,
    GetGPUUtilization, and GetQueueDepth methods
  - Add error handling tests covering connection failures, HTTP errors,
    Prometheus errors, empty results, invalid JSON, and unexpected types
  - Add tests for context cancellation, custom queries, multiple samples,
    and NaN values
  - Achieve 93.8% test coverage for the metrics package

  Closes pmady#2
Copilot AI review requested due to automatic review settings January 27, 2026 00:01
Copy link
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

This PR adds comprehensive unit tests for the PrometheusClient in the pkg/metrics package, which previously only had tests for the MockClient implementation. The tests use httptest to mock the Prometheus HTTP API, achieving 93.8% test coverage (exceeding the 80% target).

Changes:

  • Added helper functions to generate various Prometheus API response types (vector, scalar, error, etc.)
  • Added success path tests for all client methods (GetLatencyP99, GetLatencyP95, GetGPUUtilization, GetQueueDepth)
  • Added comprehensive error handling tests (connection failures, HTTP errors, empty results, invalid JSON, unexpected result types)
  • Added edge case tests (context cancellation, multiple samples, custom queries, NaN values)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Rename unused parameters to _ in HTTP handler functions
- Use strings.Builder instead of string concatenation in loop
- Rename TestPrometheusClient_ContextCancellation to
  TestPrometheusClient_ContextTimeout and fix hanging test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add unit tests for pkg/metrics package

2 participants