Skip to content

Commit

Permalink
prometheus support custom collector
Browse files Browse the repository at this point in the history
  • Loading branch information
why444216978 committed Dec 4, 2024
1 parent 821214f commit 5dcb835
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
25 changes: 17 additions & 8 deletions library/prometheus/http/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"

lp "github.com/air-go/rpc/library/prometheus"
)
Expand All @@ -20,11 +21,13 @@ type httpMetrics struct {
errorSummary *prometheus.SummaryVec
panicCounter prometheus.Counter
working prometheus.Gauge
process prometheus.Collector
gc prometheus.Collector
registerer prometheus.Registerer
}

var _ lp.Metrics = (*httpMetrics)(nil)

// opts ...OptionFunc)
func NewHTTPMetrics(opts ...OptionFunc) *httpMetrics {
opt := defaultOptions()
for _, o := range opts {
Expand All @@ -36,7 +39,7 @@ func NewHTTPMetrics(opts ...OptionFunc) *httpMetrics {
labels = append(labels, l.Name())
}

return &httpMetrics{
m := &httpMetrics{
opts: opt,
successCounter: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "http_server",
Expand Down Expand Up @@ -81,23 +84,29 @@ func NewHTTPMetrics(opts ...OptionFunc) *httpMetrics {
Name: "working_count",
Help: "http_server working_count",
}),
process: collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
gc: collectors.NewGoCollector(),
registerer: prometheus.DefaultRegisterer,
}
}

func (m *httpMetrics) Register(r prometheus.Registerer) {
m.once.Do(func() {
if r == nil {
r = prometheus.DefaultRegisterer
}
r.MustRegister(
m.registerer.MustRegister(
m.successCounter,
m.successSummary,
m.errorCounter,
m.errorSummary,
m.panicCounter,
m.working,
m.process,
m.gc,
)
})

return m
}

func (m *httpMetrics) Register(c ...prometheus.Collector) {
m.registerer.MustRegister(c...)
}

func (m *httpMetrics) withLabelValues(c *gin.Context, cost time.Duration) {
Expand Down
1 change: 0 additions & 1 deletion library/prometheus/http/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
// So must register after PanicMiddleware and LoggerMiddleware.
func HTTPMetricsMiddleware(opts ...OptionFunc) gin.HandlerFunc {
metrics := NewHTTPMetrics(opts...)
metrics.Register(nil)
return func(c *gin.Context) {
// handle filters
for _, filter := range metrics.getFilters() {
Expand Down
41 changes: 40 additions & 1 deletion library/prometheus/metrics.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,48 @@
package prometheus

import (
"context"

"github.com/prometheus/client_golang/prometheus"

lc "github.com/air-go/rpc/library/context"
)

type Metrics interface {
Register(prometheus.Registerer)
Register(...prometheus.Collector)
}

var CustomCollector = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "custom_error",
Name: "error_count",
Help: "error",
},
[]string{"message", "logid", "traceid", "error"},
)

type options struct{}

type CustomCollectorOption func(o *options)

// ReportCustomError
// Registration is required before use.
// metrics.Register(CustomCollector)
func ReportCustomError(ctx context.Context, msg string, err error, opts ...CustomCollectorOption) {
if msg == "" {
return
}

option := &options{}
for _, o := range opts {
o(option)
}

e := ""
if err != nil {
e = err.Error()
}

values := []string{msg, lc.ValueLogID(ctx), lc.ValueTraceID(ctx), e}
CustomCollector.WithLabelValues(values...).Inc()
}

0 comments on commit 5dcb835

Please sign in to comment.