diff --git a/go.mod b/go.mod index 361ce2f..5235b9b 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,5 @@ go 1.16 require ( github.com/VictoriaMetrics/metrics v1.17.2 - github.com/unistack-org/micro/v3 v3.3.17 + github.com/unistack-org/micro/v3 v3.4.11 ) diff --git a/go.sum b/go.sum index 1812389..895b377 100644 --- a/go.sum +++ b/go.sum @@ -2,19 +2,19 @@ github.com/VictoriaMetrics/metrics v1.17.2 h1:9zPJ7DPfxdJWshOGLPLpAtPL0ZZ9AeUyQC github.com/VictoriaMetrics/metrics v1.17.2/go.mod h1:Z1tSfPfngDn12bTfZSCqArT3OPY3u88J12hSoOhuiRE= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/ef-ds/deque v1.0.4/go.mod h1:gXDnTC3yqvBcHbq2lcExjtAcVrOnJCbMcZXmuj8Z4tg= -github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs= -github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I= -github.com/unistack-org/micro/v3 v3.3.17 h1:WcyS7InP0DlS/JpRQGLh5sG6VstkdHJbgpMp+gmHmwg= -github.com/unistack-org/micro/v3 v3.3.17/go.mod h1:022EOEZZ789hZY3yB5ZSMXU6jLiadBgcNB/cpediV3c= +github.com/unistack-org/micro/v3 v3.4.11 h1:WIqUeRN+awVMzTFNeCPx67sSMP3YuXugLmO9qkn8e7g= +github.com/unistack-org/micro/v3 v3.4.11/go.mod h1:1ZkwpEqpiHiVhM2hiF9DamtpsF04oFybFhEQ4zEMcro= github.com/valyala/fastrand v1.0.0 h1:LUKT9aKer2dVQNUi3waewTbKV+7H17kvWFNKs2ObdkI= github.com/valyala/fastrand v1.0.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ= github.com/valyala/histogram v1.1.2 h1:vOk5VrGjMBIoPR5k6wA8vBaC8toeJ8XO0yfRjFEc1h8= github.com/valyala/histogram v1.1.2/go.mod h1:CZAr6gK9dbD7hYx2s8WSPh0p5x5wETjC+2b3PJVtEdg= -golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= +golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= diff --git a/victoriametrics.go b/victoriametrics.go index efa51cf..fc7b938 100644 --- a/victoriametrics.go +++ b/victoriametrics.go @@ -2,7 +2,6 @@ package victoriametrics import ( "io" - "strings" "time" "github.com/VictoriaMetrics/metrics" @@ -23,36 +22,19 @@ func (r *victoriametricsMeter) Name() string { } func (r *victoriametricsMeter) buildName(name string, labels ...string) string { - var b strings.Builder - - labels = append(r.opts.Labels, labels...) if len(r.opts.MetricPrefix) > 0 { - _, _ = b.WriteString(r.opts.MetricPrefix) + name = r.opts.MetricPrefix + name } - labelPrefix := false - if len(r.opts.LabelPrefix) > 0 { - labelPrefix = true - } - _, _ = b.WriteString(name) - if len(labels) > 0 { - meter.Sort(&labels) - _, _ = b.WriteRune('{') - for idx := 0; idx < len(labels); idx += 2 { - if idx > 0 { - _, _ = b.WriteRune(',') - } - if labelPrefix { - _, _ = b.WriteString(r.opts.LabelPrefix) - } - _, _ = b.WriteString(labels[idx]) - _, _ = b.WriteString(`="`) - _, _ = b.WriteString(labels[idx+1]) - _, _ = b.WriteString(`"`) - } - _, _ = b.WriteRune('}') + if len(r.opts.LabelPrefix) == 0 { + return meter.BuildName(name, labels...) } - return b.String() + nlabels := make([]string, len(labels)) + copy(nlabels, labels) + for idx := 0; idx <= len(nlabels)/2; idx += 2 { + nlabels[idx] = r.opts.LabelPrefix + nlabels[idx] + } + return meter.BuildName(name, nlabels...) } func (r *victoriametricsMeter) Counter(name string, labels ...string) meter.Counter { diff --git a/victoriametrics_test.go b/victoriametrics_test.go index ba8cf87..d5fea14 100644 --- a/victoriametrics_test.go +++ b/victoriametrics_test.go @@ -1,11 +1,13 @@ package victoriametrics import ( + "bytes" "context" "testing" "github.com/unistack-org/micro/v3/client" "github.com/unistack-org/micro/v3/codec" + "github.com/unistack-org/micro/v3/meter" "github.com/unistack-org/micro/v3/meter/wrapper" ) @@ -20,7 +22,6 @@ func TestBuildName(t *testing.T) { cnt := m.Counter("counter", "key", "val") cnt.Inc() - //m.Write(os.Stdout, meter.WriteProcessMetrics(true), meter.WriteFDMetrics(true)) } func TestWrapper(t *testing.T) { @@ -43,5 +44,9 @@ func TestWrapper(t *testing.T) { req := &codec.Frame{} err := c.Call(ctx, c.NewRequest("svc2", "Service.Method", req), rsp) _, _ = rsp, err - //m.Write(os.Stdout, meter.WriteProcessMetrics(true), meter.WriteFDMetrics(true)) + buf := bytes.NewBuffer(nil) + m.Write(buf, meter.WriteProcessMetrics(false), meter.WriteFDMetrics(false)) + if !bytes.Contains(buf.Bytes(), []byte(`micro_client_request_inflight{micro_endpoint="svc2.Service.Method"} 0`)) { + t.Fatalf("invalid metrics output: %s", buf.Bytes()) + } }