diff --git a/library/prometheus/http/metrics.go b/library/prometheus/http/metrics.go index 7c0f8eb..f530fcf 100644 --- a/library/prometheus/http/metrics.go +++ b/library/prometheus/http/metrics.go @@ -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" ) @@ -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 { @@ -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", @@ -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) { diff --git a/library/prometheus/http/middleware.go b/library/prometheus/http/middleware.go index c62d916..a8f5dbb 100644 --- a/library/prometheus/http/middleware.go +++ b/library/prometheus/http/middleware.go @@ -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() { diff --git a/library/prometheus/metrics.go b/library/prometheus/metrics.go index 9ab8603..004fe80 100644 --- a/library/prometheus/metrics.go +++ b/library/prometheus/metrics.go @@ -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() }