Skip to content

Commit

Permalink
discoveryserver: use custom metrics registry
Browse files Browse the repository at this point in the history
Use a custom metrics registry so this program doesn't pickup all of the
random guages registered via init functions in the rest of etcd.
  • Loading branch information
Brandon Philips committed Apr 27, 2019
1 parent 826bf3e commit e280055
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
4 changes: 3 additions & 1 deletion discoveryserver/handlers/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"net/http"

"github.com/prometheus/client_golang/prometheus"

"go.etcd.io/etcd/discoveryserver/handlers/httperror"
"go.etcd.io/etcd/discoveryserver/metrics"
)

var healthCounter *prometheus.CounterVec
Expand All @@ -34,7 +36,7 @@ func init() {
},
[]string{"code", "method"},
)
prometheus.MustRegister(healthCounter)
metrics.Registry.MustRegister(healthCounter)
}

func HealthHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
Expand Down
3 changes: 2 additions & 1 deletion discoveryserver/handlers/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

"go.etcd.io/etcd/clientv3"
"go.etcd.io/etcd/discoveryserver/handlers/httperror"
"go.etcd.io/etcd/discoveryserver/metrics"
"go.etcd.io/etcd/discoveryserver/timeprefix"
"go.etcd.io/etcd/etcdserver/api/v2store"
"go.etcd.io/etcd/etcdserver/api/v2v3"
Expand All @@ -49,7 +50,7 @@ func init() {
},
[]string{"code", "method"},
)
prometheus.MustRegister(newCounter)
metrics.Registry.MustRegister(newCounter)
}

func generateCluster() string {
Expand Down
4 changes: 3 additions & 1 deletion discoveryserver/handlers/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"time"

"go.etcd.io/etcd/discoveryserver/handlers/httperror"
"go.etcd.io/etcd/discoveryserver/metrics"
"go.etcd.io/etcd/etcdserver/api/v2store"

"github.com/prometheus/client_golang/prometheus"
Expand All @@ -39,7 +40,7 @@ func init() {
},
[]string{"code", "method"},
)
prometheus.MustRegister(tokenCounter)
metrics.Registry.MustRegister(tokenCounter)
}

var tokenCounter *prometheus.CounterVec
Expand Down Expand Up @@ -127,6 +128,7 @@ func TokenHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
return
}

tokenCounter.WithLabelValues("200", r.Method).Add(1)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Etcd-Index", fmt.Sprintf("%d", ev.EtcdIndex))
w.WriteHeader(http.StatusOK)
Expand Down
3 changes: 2 additions & 1 deletion discoveryserver/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"

"go.etcd.io/etcd/discoveryserver/handlers"
"go.etcd.io/etcd/discoveryserver/metrics"

gorillaHandlers "github.com/gorilla/handlers"
"github.com/gorilla/mux"
Expand All @@ -31,7 +32,7 @@ func Setup(ctx context.Context, etcdHost, discHost string) *handlers.State {
logH := gorillaHandlers.LoggingHandler(os.Stdout, handler)

http.Handle("/", logH)
http.Handle("/metrics", promhttp.Handler())
http.Handle("/metrics", promhttp.HandlerFor(metrics.Registry, promhttp.HandlerOpts{}))

return st
}
Expand Down
23 changes: 23 additions & 0 deletions discoveryserver/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2019 The etcd Authors
//
// 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 "github.com/prometheus/client_golang/prometheus"

var Registry *prometheus.Registry

func init() {
Registry = prometheus.NewRegistry()
}

0 comments on commit e280055

Please sign in to comment.