Skip to content

yarlson/chiprom

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Prometheus Middleware for Chi Router

Introduction

The chiprom library provides an elegant integration of Prometheus metrics into the chi router, offering detailed insights into HTTP request handling within a chi-based service. The metrics that can be exposed include request count, latency, and response size, categorized by status code, HTTP method, and path. This integration allows for effortless observability of web services, ensuring that operators can measure, analyze, and troubleshoot their applications' performance with ease.

Installation

To add chiprom to your Go project, you can use go get:

go get -u github.com/yarlson/chiprom

Usage

Simple Usage

The most straightforward usage is to instrument your HTTP handlers with basic request metrics.

Example

package main
import (
    "github.com/go-chi/chi/v5"
    "github.com/yarlson/chiprom"
)

func main() {
    r := chi.NewRouter()
    r.Use(chiprom.NewMiddleware("myservice"))

    // ... your handlers ...

    http.ListenAndServe(":8080", r)
}

This example demonstrates how you can start instrumenting your chi-based service with default Prometheus metrics. The middleware will track the request count, latency, and response size, categorized by the status code, HTTP method, and path.

Using Custom Latency Buckets

You can customize the latency histograms with your desired buckets.

Example

buckets := []float64{100, 500, 2000}
r.Use(chiprom.NewMiddleware("myservice", buckets...))

Monitoring With Routing Patterns

To group requests by chi routing patterns, such as monitoring paths like /users/{firstName} instead of individual instances like /users/bob, you can utilize the NewPatternMiddleware.

Example

r.Use(chiprom.NewPatternMiddleware("myservice"))

Advanced Usage

You may want to combine both pattern monitoring and custom buckets. Here is a more complex example:

buckets := []float64{100, 500, 2000}
r.Use(chiprom.NewPatternMiddleware("myservice", buckets...))

// ... your handlers ...

http.ListenAndServe(":8080", r)

Metrics

The library exposes the following metrics:

  • chi_requests_total: How many HTTP requests processed, partitioned by status code, method, and HTTP path.
  • chi_request_duration_milliseconds: How long it took to process the request, partitioned by status code, method, and HTTP path.
  • chi_pattern_requests_total: Similar to chi_requests_total, but with patterns.
  • chi_pattern_request_duration_milliseconds: Similar to chi_request_duration_milliseconds, but with patterns.

Contribute

Contributions are welcome. Feel free to open a pull request or file an issue on the GitHub repository.

License

This library is distributed under the Apache 2.0 License. See the LICENSE file for details.

About

Prometheus middleware for chi

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%