From 0350bc7bc4aaffd8f3a3779664fd38cee1d379b4 Mon Sep 17 00:00:00 2001 From: smith Date: Sun, 28 Jan 2024 22:38:06 +0800 Subject: [PATCH] add code --- plog/reporter.go | 93 ++++++++++++++++++++---------------------------- 1 file changed, 39 insertions(+), 54 deletions(-) diff --git a/plog/reporter.go b/plog/reporter.go index 175ac60..1e4490a 100644 --- a/plog/reporter.go +++ b/plog/reporter.go @@ -3,20 +3,15 @@ package plog import ( "context" "fmt" - "github.com/golang/protobuf/proto" - "github.com/jinzhu/copier" "github.com/prometheus/client_golang/prometheus" dto "github.com/prometheus/client_model/go" "google.golang.org/grpc/codes" - "sync" "time" ) type Reporter struct { - mux sync.Mutex reg *prometheus.Registry config *ReporterConfig - globalLabels []*dto.LabelPair serverEventTotal *prometheus.CounterVec clientEventTotal *prometheus.CounterVec serverEventDurationSeconds *prometheus.HistogramVec @@ -24,10 +19,12 @@ type Reporter struct { } type ReporterConfig struct { - ReportSvr string - ReportInst string - Component string - GlobalLabels prometheus.Labels + ReportSvr string + ReportInst string + Component string + GlobalLabels prometheus.Labels + ExtraLabels []string + ExtraExtractor func(c context.Context) []string } func NewReporter() *Reporter { @@ -55,12 +52,12 @@ func (r *Reporter) init() { reg := r.reg r.serverEventTotal = prometheus.NewCounterVec(prometheus.CounterOpts{ Name: r.metricName("server_event_total"), - }, []string{"lvl", "method", "event"}) + }, r.requestLabels("lvl", "method", "event")) reg.MustRegister(r.serverEventTotal) r.clientEventTotal = prometheus.NewCounterVec(prometheus.CounterOpts{ Name: r.metricName("client_event_total"), - }, []string{"lvl", "svr", "method", "event"}) + }, r.requestLabels("lvl", "svr", "method", "event")) reg.MustRegister(r.clientEventTotal) r.serverEventDurationSeconds = prometheus.NewHistogramVec(prometheus.HistogramOpts{ @@ -74,16 +71,21 @@ func (r *Reporter) init() { reg.MustRegister(r.clientEventDurationSeconds) } +func (r *Reporter) requestLabels(labels ...string) []string { + result := append([]string{}, r.config.ExtraLabels...) + return append(result, labels...) +} + func (r *Reporter) metricName(name string) string { return fmt.Sprintf("%s_%s", "evo", name) } -func (r *Reporter) ReportEvent(c context.Context, event string) { - r.ReportEventWithLevel(c, MonitorLevelInfo, event) +func (r *Reporter) ReportEvent(c context.Context, event string, extra ...string) { + r.ReportEventWithLevel(c, MonitorLevelInfo, event, extra...) } -func (r *Reporter) ReportErrEvent(c context.Context, event string) { - r.ReportEventWithLevel(c, MonitorLevelErr, event) +func (r *Reporter) ReportErrEvent(c context.Context, event string, extra ...string) { + r.ReportEventWithLevel(c, MonitorLevelErr, event, extra...) } func (r *Reporter) ReportRequest(c context.Context, code codes.Code, event string, extra ...string) { @@ -94,10 +96,29 @@ func (r *Reporter) ReportRequest(c context.Context, code codes.Code, event strin func (r *Reporter) ReportEventWithLevel( c context.Context, lvl MonitorLevel, event string, extra ...string) { lc := GetLogContext(c) + extra = r.extractExtra(c, extra...) lvs := append(extra, lvl.String(), lc.GetMethod(), event) r.serverEventTotal.WithLabelValues(lvs...).Inc() } +func (r *Reporter) extractExtra(c context.Context, extra ...string) []string { + if len(extra) == len(r.config.ExtraLabels) { + return extra + } + if r.config.ExtraExtractor != nil { + extra = r.config.ExtraExtractor(c) + } + if len(extra) == len(r.config.ExtraLabels) { + return extra + } + + extra = []string{} + for i := 0; i < len(r.config.ExtraLabels); i++ { + extra = append(extra, "") + } + return extra +} + func (r *Reporter) ReportDuration(c context.Context, duration time.Duration) { lc := GetLogContext(c) r.serverEventDurationSeconds.WithLabelValues(lc.method).Observe(duration.Seconds()) @@ -111,7 +132,8 @@ func (r *Reporter) ReportClientRequest(c context.Context, code codes.Code, svr, func (r *Reporter) ReportClientEventWithLevel(c context.Context, lvl MonitorLevel, svr, event string, extra ...string) { lc := GetLogContext(c) - lvs := append(extra, lvl.String(), lc.GetMethod(), event) + extra = r.extractExtra(c, extra...) + lvs := append(extra, lvl.String(), svr, lc.GetMethod(), event) r.clientEventTotal.WithLabelValues(lvs...).Inc() } @@ -119,48 +141,11 @@ func (r *Reporter) ReportClientDuration(c context.Context, svr, method string, d r.clientEventDurationSeconds.WithLabelValues(svr, method).Observe(duration.Seconds()) } -func (r *Reporter) UpdateConfig(f func(conf *ReporterConfig)) error { - r.mux.Lock() - defer r.mux.Lock() - var newConfig ReporterConfig - copier.Copy(&newConfig, r.config) - f(&newConfig) - var globalLabels []*dto.LabelPair - globalLabels = append(globalLabels, &dto.LabelPair{ - Name: proto.String("report_inst"), - Value: proto.String(newConfig.ReportInst), - }) - globalLabels = append(globalLabels, &dto.LabelPair{ - Name: proto.String("report_svr"), - Value: proto.String(newConfig.ReportSvr), - }) - globalLabels = append(globalLabels, &dto.LabelPair{ - Name: proto.String("component"), - Value: proto.String(newConfig.Component), - }) - for k, v := range newConfig.GlobalLabels { - globalLabels = append(globalLabels, &dto.LabelPair{ - Name: &k, - Value: &v, - }) - } - r.config = &newConfig - r.globalLabels = globalLabels - return nil -} - // https://prometheus.io/docs/instrumenting/writing_clientlibs/ // https://prometheus.io/docs/instrumenting/exposition_formats/ func (r *Reporter) Gather() ([]*dto.MetricFamily, error) { - globalLabels := r.globalLabels - families, err := r.reg.Gather() - for _, family := range families { - for _, metric := range family.Metric { - metric.Label = append(metric.Label, globalLabels...) - } - } - return families, err + return r.reg.Gather() } func GetMonitorLevel(code codes.Code) MonitorLevel {