|
| 1 | +# Prometheus Wrapper |
| 2 | + |
| 3 | +The `prometheus` wrapper package exposes standard request metrics (request |
| 4 | +count, latency, errors) for go-micro services and clients, so they can be |
| 5 | +scraped by a Prometheus server with zero extra boilerplate. |
| 6 | + |
| 7 | +Resolves [micro/go-micro#2893](https://github.com/micro/go-micro/issues/2893). |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +```go |
| 12 | +import prom "go-micro.dev/v5/wrapper/monitoring/prometheus" |
| 13 | +``` |
| 14 | + |
| 15 | +## Exported Metrics |
| 16 | + |
| 17 | +All metrics are labelled with `service`, `endpoint` and `status` |
| 18 | +(`"success"` or `"fail"`). Labels are kept small on purpose to avoid |
| 19 | +blowing up Prometheus memory. |
| 20 | + |
| 21 | +| Metric | Type | Description | |
| 22 | +|---------------------------------|-----------|---------------------------------------------| |
| 23 | +| `micro_request_total` | Counter | Total number of requests handled. | |
| 24 | +| `micro_request_duration_seconds`| Histogram | Request latency distribution (seconds). | |
| 25 | + |
| 26 | +The `micro` prefix can be overridden with `prom.ServiceName("myapp")`. |
| 27 | + |
| 28 | +## Basic Usage |
| 29 | + |
| 30 | +```go |
| 31 | +import ( |
| 32 | + "go-micro.dev/v5" |
| 33 | + prom "go-micro.dev/v5/wrapper/monitoring/prometheus" |
| 34 | +) |
| 35 | + |
| 36 | +func main() { |
| 37 | + service := micro.NewService( |
| 38 | + micro.Name("example.service"), |
| 39 | + micro.WrapHandler(prom.NewHandlerWrapper()), |
| 40 | + micro.WrapClient(prom.NewClientWrapper()), |
| 41 | + micro.WrapSubscriber(prom.NewSubscriberWrapper()), |
| 42 | + ) |
| 43 | + |
| 44 | + service.Init() |
| 45 | + |
| 46 | + if err := service.Run(); err != nil { |
| 47 | + panic(err) |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +To expose the metrics to Prometheus, serve the default `promhttp` handler |
| 53 | +on a side HTTP endpoint: |
| 54 | + |
| 55 | +```go |
| 56 | +import ( |
| 57 | + "net/http" |
| 58 | + |
| 59 | + "github.com/prometheus/client_golang/prometheus/promhttp" |
| 60 | +) |
| 61 | + |
| 62 | +go func() { |
| 63 | + http.Handle("/metrics", promhttp.Handler()) |
| 64 | + _ = http.ListenAndServe(":9100", nil) |
| 65 | +}() |
| 66 | +``` |
| 67 | + |
| 68 | +Then point Prometheus at it: |
| 69 | + |
| 70 | +```yaml |
| 71 | +scrape_configs: |
| 72 | + - job_name: 'example.service' |
| 73 | + static_configs: |
| 74 | + - targets: ['localhost:9100'] |
| 75 | +``` |
| 76 | +
|
| 77 | +## Wrappers |
| 78 | +
|
| 79 | +| Constructor | Wraps | Notes | |
| 80 | +|---------------------------|-------------------------|--------------------------------------------| |
| 81 | +| `NewHandlerWrapper` | `server.HandlerWrapper` | Incoming RPC handlers. | |
| 82 | +| `NewSubscriberWrapper` | `server.SubscriberWrapper` | Event subscribers (uses topic as endpoint). | |
| 83 | +| `NewCallWrapper` | `client.CallWrapper` | Outgoing unary RPC calls only. | |
| 84 | +| `NewClientWrapper` | `client.Wrapper` | Outgoing `Call` **and** `Publish`. | |
| 85 | + |
| 86 | +`NewClientWrapper` is the right choice when you want metrics for both |
| 87 | +`Call` and `Publish`; use `NewCallWrapper` if you only care about unary |
| 88 | +calls and want lower overhead. |
| 89 | + |
| 90 | +## Configuration |
| 91 | + |
| 92 | +All constructors accept functional options: |
| 93 | + |
| 94 | +```go |
| 95 | +prom.NewHandlerWrapper( |
| 96 | + prom.ServiceName("myapp"), // metric name prefix |
| 97 | + prom.Namespace("prod"), // Prometheus namespace |
| 98 | + prom.Subsystem("api"), // Prometheus subsystem |
| 99 | + prom.ConstLabels(prometheus.Labels{"dc": "eu-1"}), // labels on every metric |
| 100 | + prom.Buckets([]float64{0.005, 0.05, 0.5, 1, 5}), // latency buckets |
| 101 | + prom.Registerer(myRegistry), // custom registerer |
| 102 | +) |
| 103 | +``` |
| 104 | + |
| 105 | +Defaults: |
| 106 | + |
| 107 | +- `ServiceName`: `"micro"` |
| 108 | +- `Buckets`: `prometheus.DefBuckets` |
| 109 | +- `Registerer`: `prometheus.DefaultRegisterer` |
| 110 | + |
| 111 | +## Reusing Collectors |
| 112 | + |
| 113 | +Creating multiple wrappers with the same options (e.g. `NewHandlerWrapper` |
| 114 | +and `NewClientWrapper` together) is safe: the collectors are cached per |
| 115 | +`(name, namespace, subsystem)` triple and `AlreadyRegisteredError` from |
| 116 | +Prometheus is handled transparently, so the existing collector is reused. |
| 117 | + |
| 118 | +## Testing |
| 119 | + |
| 120 | +The package ships with unit tests that use a fresh `prometheus.Registry` |
| 121 | +per test to keep assertions isolated: |
| 122 | + |
| 123 | +```bash |
| 124 | +go test ./wrapper/monitoring/prometheus/... |
| 125 | +``` |
| 126 | + |
| 127 | +## License |
| 128 | + |
| 129 | +Apache 2.0 |
0 commit comments