Skip to content

Feature/prometheus middleware#52

Open
HeerakKashyap wants to merge 17 commits intogoflash:mainfrom
HeerakKashyap:feature/prometheus-middleware
Open

Feature/prometheus middleware#52
HeerakKashyap wants to merge 17 commits intogoflash:mainfrom
HeerakKashyap:feature/prometheus-middleware

Conversation

@HeerakKashyap
Copy link
Copy Markdown

Overview

This PR implements Prometheus instrumentation middleware for the GoFlash framework, addressing issue #37. The middleware provides comprehensive HTTP request metrics collection and exposes them via a configurable metrics endpoint.

Features

Core Metrics Collection

  • Request Count: Total number of HTTP requests by method, path, and status code
  • Request Duration: Histogram of request processing times
  • Request Size: Size of incoming request bodies
  • Response Size: Size of outgoing response bodies
  • In-Flight Requests: Current number of requests being processed

Configuration Options

  • Custom Namespace/Subsystem: Configure metric naming (e.g., myapp_api_requests_total)
  • Configurable Metrics Path: Default /metrics, customizable via config
  • Optional Metrics Endpoint: Can disable HTTP exposure for programmatic access only
  • Custom Labels: Add application-specific labels to all metrics

Usage Examples

Basic Usage

app := flash.New()

// Apply with default configuration
app.Use(middleware.Prometheus())

// Or use the convenient mount function
middleware.MountPrometheus(app)

Implementation Details

Architecture

  • Separate Registries: Each configuration uses its own Prometheus registry to avoid metric conflicts
  • Response Writer Wrapping: Captures response size without affecting performance
  • Thread-Safe: All metrics operations are thread-safe for concurrent requests
  • Memory Efficient: Uses Prometheus's built-in memory management

Performance Considerations

  • Minimal Overhead: Metrics collection adds minimal latency to request processing
  • Efficient Labeling: Uses method and path labels for detailed breakdowns
  • Configurable Buckets: Histogram buckets optimized for typical web request durations

Testing

The implementation includes comprehensive tests covering:

  • Basic metrics collection and exposure
  • Custom namespace/subsystem configuration
  • Metrics endpoint availability and content
  • Middleware behavior without metrics endpoint
  • Request tracking across different HTTP methods and status codes

Security Considerations

  • Metrics Endpoint: Should be protected in production environments
  • Path Exposure: Request paths are included in metrics - consider sanitization for sensitive routes
  • Access Control: Recommend using authentication/authorization for metrics endpoints

Dependencies

Added github.com/prometheus/client_golang/prometheus for metrics collection and exposure.

Closes #37

HeerakKashyap and others added 17 commits August 22, 2025 01:49
- Remove convenience_methods example (should be in separate repo)
- Added tests for edge cases in convenience methods
- Added tests for NoContent, Stream, StreamJSON edge cases
- Added tests for Clone, Finish, SetRequest, SetResponseWriter methods
- Added tests for Get with default values and Set method
- Improve coverage from 95.3% to 95.8%
- Resolve merge conflicts with upstream/main
- Remove duplicate BindJSON declarations (moved to bind.go)
- Add missing strings import to test file
- Keep convenience methods implementation
- Maintain test coverage improvements
- Add Redirect, File, NotFound, InternalServerError convenience methods
- Add BadRequest, Unauthorized, Forbidden, NoContent methods
- Add Stream, StreamJSON methods
- Add SetCookie, GetCookie, ClearCookie methods
- Add missing io import for Stream method
- Fix compilation errors in ratelimit_test.go
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Dmitri Meshin <dmitri.meshin@gmail.com>
- Change from c.Forbidden() to c.Forbidden('file is a directory')
- Provide clear error message when attempting to serve a directory
- Maintain consistent error messaging across convenience methods
Co-authored-by: NewCapital.in <newcapital.in@gmail.com>
Signed-off-by: HootingConjuror <kashyapheerk@gmail.com>
Signed-off-by: HootingConjuror <kashyapheerk@gmail.com>
- Add Pprof middleware that mounts Go's built-in pprof handlers
- Add MountPprof function for direct route mounting (more efficient)
- Support configurable prefix (default: /debug/pprof)
- Include comprehensive tests and examples
- Add security considerations and usage documentation

Closes goflash#38
- Implements Prometheus metrics collection for HTTP requests
- Exposes metrics endpoint at /metrics (configurable)
- Tracks request count, duration, size, and in-flight requests
- Supports custom namespace and subsystem configuration
- Includes comprehensive tests and examples
- Closes goflash#37
@codecov
Copy link
Copy Markdown

codecov Bot commented Aug 31, 2025

Codecov Report

❌ Patch coverage is 78.96104% with 81 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
middleware/pprof.go 40.69% 51 Missing ⚠️
ctx/ctx.go 83.06% 21 Missing ⚠️
middleware/prometheus.go 94.85% 8 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

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.

Prometheus instrumentation (Middlewares — P2)

2 participants