From 94e4c657cf0d8290c3f415612f19cb0b52e2e224 Mon Sep 17 00:00:00 2001 From: peimingming Date: Thu, 23 May 2019 10:15:09 +0800 Subject: [PATCH 1/3] Support prometheus --- metrics/config.go | 14 +++++++++++--- metrics/metrics.go | 1 + metrics/prometheus.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 metrics/prometheus.go diff --git a/metrics/config.go b/metrics/config.go index b7e7e20f9..e0ea908a8 100644 --- a/metrics/config.go +++ b/metrics/config.go @@ -15,9 +15,10 @@ package metrics // Config defines metrics configuration. type Config struct { - Backend string `yaml:"backend"` - Statsd StatsdConfig `yaml:"statsd"` - M3 M3Config `yaml:"m3"` + Backend string `yaml:"backend"` + Statsd StatsdConfig `yaml:"statsd"` + M3 M3Config `yaml:"m3"` + Prometheus PrometheusConfig `yaml:"prometheus"` } // StatsdConfig defines statsd configuration. @@ -32,3 +33,10 @@ type M3Config struct { Service string `yaml:"service"` Env string `yaml:"env"` } + +// PrometheusConfig defines prometheus configuration. +type PrometheusConfig struct { + ListenAddress string `yaml:"listen_address"` + // HandlerPath if not define use default /metrics. + HandlerPath string `yaml:"handler_path"` +} diff --git a/metrics/metrics.go b/metrics/metrics.go index e675335b8..b33c6d4ef 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -29,6 +29,7 @@ func init() { register("statsd", newStatsdScope) register("disabled", newDisabledScope) register("m3", newM3Scope) + register("prometheus", newPrometheusScope) } var _scopeFactories = make(map[string]scopeFactory) diff --git a/metrics/prometheus.go b/metrics/prometheus.go new file mode 100644 index 000000000..7630d0727 --- /dev/null +++ b/metrics/prometheus.go @@ -0,0 +1,42 @@ +// Copyright (c) 2016-2019 Uber Technologies, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package metrics + +import ( + "fmt" + "io" + "time" + + "github.com/uber-go/tally" + promreporter "github.com/uber-go/tally/prometheus" +) + +func newPrometheusScope(config Config, cluster string) (tally.Scope, io.Closer, error) { + if config.Prometheus.ListenAddress == "" { + return nil, nil, fmt.Errorf("listen_address required for prometheus") + } + + prometheusConfig := promreporter.Configuration{ + HandlerPath: config.Prometheus.HandlerPath, + ListenAddress: config.Prometheus.ListenAddress, + } + r, err := prometheusConfig.NewReporter(promreporter.ConfigurationOptions{}) + if err != nil { + return nil, nil, err + } + s, c := tally.NewRootScope(tally.ScopeOptions{ + CachedReporter: r, + }, time.Second) + return s, c, nil +} From ff6879a15bf8cf0663a63656802b1601721dea30 Mon Sep 17 00:00:00 2001 From: mmpei Date: Tue, 28 May 2019 20:43:55 +0800 Subject: [PATCH 2/3] Fix prometheus metrics name issue --- lib/middleware/middleware.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/middleware/middleware.go b/lib/middleware/middleware.go index 720b4a90c..d947fc903 100644 --- a/lib/middleware/middleware.go +++ b/lib/middleware/middleware.go @@ -95,7 +95,7 @@ func StatusCounter(stats tally.Scope) func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { recordw := &recordStatusWriter{w, false, http.StatusOK} next.ServeHTTP(recordw, r) - tagEndpoint(stats, r).Counter(strconv.Itoa(recordw.code)).Inc(1) + tagEndpoint(stats, r).Counter("http_" + strconv.Itoa(recordw.code)).Inc(1) }) } } From db3ad8d8347202bb02cf226c738b78dd9c3d6d3f Mon Sep 17 00:00:00 2001 From: mmpei Date: Thu, 30 May 2019 12:53:58 +0800 Subject: [PATCH 3/3] Fix unit test error --- lib/middleware/middleware_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/middleware/middleware_test.go b/lib/middleware/middleware_test.go index 3d7f3890f..6ad9b44e3 100644 --- a/lib/middleware/middleware_test.go +++ b/lib/middleware/middleware_test.go @@ -145,7 +145,7 @@ func TestStatusCounter(t *testing.T) { require.Equal(1, len(stats.Snapshot().Counters())) for _, v := range stats.Snapshot().Counters() { - require.Equal(test.expectedStatus, v.Name()) + require.Equal("http_" + test.expectedStatus, v.Name()) require.Equal(int64(5), v.Value()) require.Equal(map[string]string{ "endpoint": "foo",