Skip to content
Merged
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@
* [Official website](https://echo.labstack.com)
* [All middleware docs](https://echo.labstack.com/docs/category/middleware)

## Versioning

This repository does not use semantic versioning. MAJOR version tracks which Echo version should be used. MINOR version
tracks API changes (possibly backwards incompatible, which is very rare occasion), and a PATCH version is incremented for fixes.

> **Always add at least one integration test in your project.**

For Echo `v5` use `v5.x.y` releases.
Minimal needed Echo versions:

* `v5.x.y` needs Echo `v5.0.0+`, use `go get github.com/labstack/echo-contrib@v5`
* `v0.18.0` needs Echo `v4.15.0+`, use `go get github.com/labstack/[email protected]`

For `v0.x.y` releases the code is located in `v4` branch.

# Supported Go version

Each major Go release is supported until there are two newer major releases. https://golang.org/doc/devel/release.html#policy
Expand Down
63 changes: 38 additions & 25 deletions casbin/README.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,58 @@
# Usage
Simple example:
```go
package main
package main

import (
"github.com/casbin/casbin/v2"
"github.com/labstack/echo/v4"
casbin_mw "github.com/labstack/echo-contrib/casbin"
)
import (
"log/slog"

func main() {
e := echo.New()
"github.com/casbin/casbin/v2"
casbin_mw "github.com/labstack/echo-contrib/v5/casbin"
"github.com/labstack/echo/v5"
)

// Mediate the access for every request
e.Use(casbin_mw.Middleware(casbin.NewEnforcer("auth_model.conf", "auth_policy.csv")))
func main() {
e := echo.New()

e.Logger.Fatal(e.Start(":1323"))
// Mediate the access for every request
enforcer, err := casbin.NewEnforcer("auth_model.conf", "auth_policy.csv")
if err != nil {
slog.Error("failed to load casbin enforcer", "error", err)
}
e.Use(casbin_mw.Middleware(enforcer))

if err := e.Start(":1323"); err != nil {
slog.Error("failed to start server", "error", err)
}
}

```

Advanced example:
```go
package main
package main

import (
"log/slog"

"github.com/casbin/casbin/v2"
casbin_mw "github.com/labstack/echo-contrib/v5/casbin"
"github.com/labstack/echo/v5"
)

import (
"github.com/casbin/casbin/v2"
"github.com/labstack/echo/v4"
casbin_mw "github.com/labstack/echo-contrib/casbin"
)
func main() {
ce, _ := casbin.NewEnforcer("auth_model.conf", "")
ce.AddRoleForUser("alice", "admin")
ce.AddPolicy(...)

func main() {
ce, _ := casbin.NewEnforcer("auth_model.conf", "")
ce.AddRoleForUser("alice", "admin")
ce.AddPolicy("added_user", "data1", "read")

e := echo.New()
e := echo.New()

e.Use(casbin_mw.Middleware(ce))
e.Use(casbin_mw.Middleware(ce))

e.Logger.Fatal(e.Start(":1323"))
if err := e.Start(":1323"); err != nil {
slog.Error("failed to start server", "error", err)
}
}
```

# API Reference
Expand Down
4 changes: 2 additions & 2 deletions casbin/casbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Simple example:
import (
"github.com/casbin/casbin/v2"
"github.com/labstack/echo/v5"
casbin_mw "github.com/labstack/echo-contrib/casbin"
casbin_mw "github.com/labstack/echo-contrib/v5/casbin"
)

func main() {
Expand All @@ -29,7 +29,7 @@ Advanced example:
import (
"github.com/casbin/casbin/v2"
"github.com/labstack/echo/v5"
casbin_mw "github.com/labstack/echo-contrib/casbin"
casbin_mw "github.com/labstack/echo-contrib/v5/casbin"
)

func main() {
Expand Down
152 changes: 11 additions & 141 deletions echoprometheus/README.md
Original file line number Diff line number Diff line change
@@ -1,154 +1,24 @@
# Usage

```
```go
package main

import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo-contrib/echoprometheus"
"log/slog"

"github.com/labstack/echo-contrib/v5/echoprometheus"
"github.com/labstack/echo/v5"
)

func main() {
e := echo.New()
// Enable metrics middleware
e.Use(echoprometheus.NewMiddleware("myapp"))
e.GET("/metrics", echoprometheus.NewHandler())

e.Logger.Fatal(e.Start(":1323"))
}
```


# How to migrate

## Creating and adding middleware to the application

Older `prometheus` middleware
```go
e := echo.New()
p := prometheus.NewPrometheus("echo", nil)
p.Use(e)
```

With the new `echoprometheus` middleware
```go
e := echo.New()
e.Use(echoprometheus.NewMiddleware("myapp")) // register middleware to gather metrics from requests
e.GET("/metrics", echoprometheus.NewHandler()) // register route to serve gathered metrics in Prometheus format
```

## Replacement for `Prometheus.MetricsList` field, `NewMetric(m *Metric, subsystem string)` function and `prometheus.Metric` struct

The `NewMetric` function allowed to create custom metrics with the old `prometheus` middleware. This helper is no longer available
to avoid the added complexity. It is recommended to use native Prometheus metrics and register those yourself.

This can be done now as follows:
```go
e := echo.New()

// Enable metrics middleware
e.Use(echoprometheus.NewMiddleware("myapp"))
e.GET("/metrics", echoprometheus.NewHandler())

customRegistry := prometheus.NewRegistry() // create custom registry for your custom metrics
customCounter := prometheus.NewCounter( // create new counter metric. This is replacement for `prometheus.Metric` struct
prometheus.CounterOpts{
Name: "custom_requests_total",
Help: "How many HTTP requests processed, partitioned by status code and HTTP method.",
},
)
if err := customRegistry.Register(customCounter); err != nil { // register your new counter metric with metrics registry
log.Fatal(err)
if err := e.Start(":1323"); err != nil {
slog.Error("failed to start server", "error", err)
}

e.Use(NewMiddlewareWithConfig(MiddlewareConfig{
AfterNext: func(c echo.Context, err error) {
customCounter.Inc() // use our custom metric in middleware. after every request increment the counter
},
Registerer: customRegistry, // use our custom registry instead of default Prometheus registry
}))
e.GET("/metrics", NewHandlerWithConfig(HandlerConfig{Gatherer: customRegistry})) // register route for getting gathered metrics data from our custom Registry
```

## Replacement for `Prometheus.MetricsPath`

`MetricsPath` was used to skip metrics own route from Prometheus metrics. Skipping is no longer done and requests to Prometheus
route will be included in gathered metrics.

To restore the old behaviour the `/metrics` path needs to be excluded from counting using the Skipper function:
```go
conf := echoprometheus.MiddlewareConfig{
Skipper: func(c echo.Context) bool {
return c.Path() == "/metrics"
},
}
e.Use(echoprometheus.NewMiddlewareWithConfig(conf))
```

## Replacement for `Prometheus.RequestCounterURLLabelMappingFunc` and `Prometheus.RequestCounterHostLabelMappingFunc`

These function fields were used to define how "URL" or "Host" attribute in Prometheus metric lines are created.

These can now be substituted by using `LabelFuncs`:
```go
e.Use(echoprometheus.NewMiddlewareWithConfig(echoprometheus.MiddlewareConfig{
LabelFuncs: map[string]echoprometheus.LabelValueFunc{
"scheme": func(c echo.Context, err error) string { // additional custom label
return c.Scheme()
},
"url": func(c echo.Context, err error) string { // overrides default 'url' label value
return "x_" + c.Request().URL.Path
},
"host": func(c echo.Context, err error) string { // overrides default 'host' label value
return "y_" + c.Request().Host
},
},
}))
```

Will produce Prometheus line as
`echo_request_duration_seconds_count{code="200",host="y_example.com",method="GET",scheme="http",url="x_/ok",scheme="http"} 1`


## Replacement for `Metric.Buckets` and modifying default metrics

The `echoprometheus` middleware registers the following metrics by default:

* Counter `requests_total`
* Histogram `request_duration_seconds`
* Histogram `response_size_bytes`
* Histogram `request_size_bytes`

You can modify their definition before these metrics are registed with `CounterOptsFunc` and `HistogramOptsFunc` callbacks

Example:
```go
e.Use(NewMiddlewareWithConfig(MiddlewareConfig{
HistogramOptsFunc: func(opts prometheus.HistogramOpts) prometheus.HistogramOpts {
if opts.Name == "request_duration_seconds" {
opts.Buckets = []float64{1.0 * bKB, 2.0 * bKB, 5.0 * bKB, 10.0 * bKB, 100 * bKB, 500 * bKB, 1.0 * bMB, 2.5 * bMB, 5.0 * bMB, 10.0 * bMB}
}
return opts
},
CounterOptsFunc: func(opts prometheus.CounterOpts) prometheus.CounterOpts {
if opts.Name == "requests_total" {
opts.ConstLabels = prometheus.Labels{"my_const": "123"}
}
return opts
},
}))
```

## Replacement for `PushGateway` struct and related methods

Function `RunPushGatewayGatherer` starts pushing collected metrics and block until context completes or ErrorHandler returns an error.
This function should be run in separate goroutine.

Example:
```go
go func() {
config := echoprometheus.PushGatewayConfig{
PushGatewayURL: "https://host:9080",
PushInterval: 10 * time.Millisecond,
}
if err := echoprometheus.RunPushGatewayGatherer(context.Background(), config); !errors.Is(err, context.Canceled) {
log.Fatal(err)
}
}()
```
2 changes: 1 addition & 1 deletion echoprometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"strings"
"time"

"github.com/labstack/echo-contrib/internal/helpers"
"github.com/labstack/echo-contrib/v5/internal/helpers"
"github.com/labstack/echo/v5"
"github.com/labstack/echo/v5/middleware"
"github.com/prometheus/client_golang/prometheus"
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module github.com/labstack/echo-contrib
module github.com/labstack/echo-contrib/v5

go 1.25.0

require (
github.com/casbin/casbin/v2 v2.135.0
github.com/gorilla/context v1.1.2
github.com/gorilla/sessions v1.4.0
github.com/labstack/echo/v5 v5.0.0-20260118161441-9500f2745481
github.com/labstack/echo/v5 v5.0.1
github.com/opentracing/opentracing-go v1.2.0
github.com/openzipkin/zipkin-go v0.4.3
github.com/prometheus/client_golang v1.23.2
Expand All @@ -16,9 +16,9 @@ require (
)

require (
github.com/HdrHistogram/hdrhistogram-go v1.1.2 // indirect
github.com/HdrHistogram/hdrhistogram-go v1.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bmatcuk/doublestar/v4 v4.9.2 // indirect
github.com/bmatcuk/doublestar/v4 v4.10.0 // indirect
github.com/casbin/govaluate v1.10.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
Loading
Loading