Skip to content
Open
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
30 changes: 12 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ package main
import (
"net/http"

echoPrometheus "github.com/globocom/echo-prometheus/v2"
"github.com/labstack/echo/v4"
"github.com/prometheus/client_golang/prometheus/promhttp"
echoPrometheus "github.com/globocom/echo-prometheus"
)

func main() {
e := echo.New()

e.Use(echoPrometheus.MetricsMiddleware())
ec := echoPrometheus.MetricsMiddleware()
e.Use(ec.MetricsMiddlewareFunc())
e.GET("/metrics", echo.WrapHandler(promhttp.Handler()))

e.GET("/", func(c echo.Context) error {
Expand All @@ -36,29 +37,22 @@ package main
import (
"net/http"

echoPrometheus "github.com/globocom/echo-prometheus/v2"
"github.com/labstack/echo/v4"
"github.com/prometheus/client_golang/prometheus/promhttp"
echoPrometheus "github.com/globocom/echo-prometheus"
)

func main() {
e := echo.New()

var configMetrics = echoPrometheus.NewConfig()
configMetrics.Namespace = "namespace"
configMetrics.Buckets = []float64{
0.0005, // 0.5ms
0.001, // 1ms
0.005, // 5ms
0.01, // 10ms
0.05, // 50ms
0.1, // 100ms
0.5, // 500ms
1, // 1s
2, // 2s
}

e.Use(echoPrometheus.MetricsMiddlewareWithConfig(configMetrics))
ec := echoPrometheus.MetricsMiddleware()
ec.
WithBuckets([]float64{0.01, 0.1, 0.5, 1.0, 5.0, 10.0}).
WithNormalizeHTTPStatus(true).
WithNamespace("my_custom_namespace").
WithSubsystem("my_custom_subsystem")

e.Use(ec.MetricsMiddlewareFunc())
e.GET("/metrics", echo.WrapHandler(promhttp.Handler()))

e.GET("/", func(c echo.Context) error {
Expand Down
24 changes: 8 additions & 16 deletions example/customConfig/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,22 @@ package main
import (
"net/http"

echoPrometheus "github.com/globocom/echo-prometheus"
echoPrometheus "github.com/globocom/echo-prometheus/v2"
"github.com/labstack/echo/v4"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func main() {
e := echo.New()

var configMetrics = echoPrometheus.NewConfig()
configMetrics.Namespace = "namespace"
configMetrics.NormalizeHTTPStatus = false
configMetrics.Buckets = []float64{
0.0005, // 0.5ms
0.001, // 1ms
0.005, // 5ms
0.01, // 10ms
0.05, // 50ms
0.1, // 100ms
0.5, // 500ms
1, // 1s
2, // 2s
}
ec := echoPrometheus.MetricsMiddleware()
ec.
WithBuckets([]float64{0.01, 0.1, 0.5, 1.0, 5.0, 10.0}).
WithNormalizeHTTPStatus(true).
WithNamespace("MyCustomNamespace").
WithSubsystem("MyCustomSubsystem")

e.Use(echoPrometheus.MetricsMiddlewareWithConfig(configMetrics))
e.Use(ec.MetricsMiddlewareFunc())
e.GET("/metrics", echo.WrapHandler(promhttp.Handler()))

e.GET("/", func(c echo.Context) error {
Expand Down
5 changes: 3 additions & 2 deletions example/defaultConfig/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package main
import (
"net/http"

echoPrometheus "github.com/globocom/echo-prometheus"
echoPrometheus "github.com/globocom/echo-prometheus/v2"
"github.com/labstack/echo/v4"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func main() {
e := echo.New()

e.Use(echoPrometheus.MetricsMiddleware())
ec := echoPrometheus.MetricsMiddleware()
e.Use(ec.MetricsMiddlewareFunc())
e.GET("/metrics", echo.WrapHandler(promhttp.Handler()))

e.GET("/", func(c echo.Context) error {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/globocom/echo-prometheus
module github.com/globocom/echo-prometheus/v2

go 1.12

Expand Down
60 changes: 42 additions & 18 deletions middleware.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package echoprometheus

import (
"strconv"
"reflect"
"strconv"

echo "github.com/labstack/echo/v4"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -11,9 +11,9 @@ import (

// Config responsible to configure middleware
type Config struct {
Namespace string
Buckets []float64
Subsystem string
Namespace string
Buckets []float64
Subsystem string
NormalizeHTTPStatus bool
}

Expand All @@ -23,6 +23,10 @@ const (
notFoundPath = "/not-found"
)

type EchoPrometheus struct {
config Config
}

// DefaultConfig has the default instrumentation config
var DefaultConfig = Config{
Namespace: "echo",
Expand Down Expand Up @@ -66,32 +70,45 @@ func isNotFoundHandler(handler echo.HandlerFunc) bool {
return reflect.ValueOf(handler).Pointer() == reflect.ValueOf(echo.NotFoundHandler).Pointer()
}

// NewConfig returns a new config with default values
func NewConfig() Config {
return DefaultConfig
// WithNamespace change the namespace
func (ep *EchoPrometheus) WithNamespace(namespace string) *EchoPrometheus {
ep.config.Namespace = namespace
return ep
}

// MetricsMiddleware returns an echo middleware with default config for instrumentation.
func MetricsMiddleware() echo.MiddlewareFunc {
return MetricsMiddlewareWithConfig(DefaultConfig)
// WithBuckets change the buckets
func (ep *EchoPrometheus) WithBuckets(buckets []float64) *EchoPrometheus {
ep.config.Buckets = buckets
return ep
}

// MetricsMiddlewareWithConfig returns an echo middleware for instrumentation.
func MetricsMiddlewareWithConfig(config Config) echo.MiddlewareFunc {
// WithSubsystem change the subsystem
func (ep *EchoPrometheus) WithSubsystem(subsystem string) *EchoPrometheus {
ep.config.Subsystem = subsystem
return ep
}

// WithNormalizeHTTPStatus change the normalizeHTTPStatus flag
func (ep *EchoPrometheus) WithNormalizeHTTPStatus(normalizeHTTPStatus bool) *EchoPrometheus {
ep.config.NormalizeHTTPStatus = normalizeHTTPStatus
return ep
}

// MetricsMiddlewareWithConfig returns an echo middleware for instrumentation.
func (ep EchoPrometheus) MetricsMiddlewareFunc() echo.MiddlewareFunc {
httpRequests := promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: config.Namespace,
Subsystem: config.Subsystem,
Namespace: ep.config.Namespace,
Subsystem: ep.config.Subsystem,
Name: httpRequestsCount,
Help: "Number of HTTP operations",
}, []string{"status", "method", "handler"})

httpDuration := promauto.NewHistogramVec(prometheus.HistogramOpts{
Namespace: config.Namespace,
Subsystem: config.Subsystem,
Namespace: ep.config.Namespace,
Subsystem: ep.config.Subsystem,
Name: httpRequestsDuration,
Help: "Spend time by processing a route",
Buckets: config.Buckets,
Buckets: ep.config.Buckets,
}, []string{"method", "handler"})

return func(next echo.HandlerFunc) echo.HandlerFunc {
Expand All @@ -113,7 +130,7 @@ func MetricsMiddlewareWithConfig(config Config) echo.MiddlewareFunc {
}

status := ""
if config.NormalizeHTTPStatus {
if ep.config.NormalizeHTTPStatus {
status = normalizeHTTPStatus(c.Response().Status)
} else {
status = strconv.Itoa(c.Response().Status)
Expand All @@ -125,3 +142,10 @@ func MetricsMiddlewareWithConfig(config Config) echo.MiddlewareFunc {
}
}
}

// MetricsMiddleware returns an echo middleware with default config for instrumentation.
func MetricsMiddleware() *EchoPrometheus {
return &EchoPrometheus{
config: DefaultConfig,
}
}