From a50f92f1bf41e5d0166fecd965244e36a8112297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20D=C3=ADaz?= Date: Mon, 18 Mar 2019 16:19:54 +0100 Subject: [PATCH] Add metrics server to be included on each agent, including methods to stop, start and register metrics --- gossip/metrics.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 gossip/metrics.go diff --git a/gossip/metrics.go b/gossip/metrics.go new file mode 100644 index 000000000..4a281086a --- /dev/null +++ b/gossip/metrics.go @@ -0,0 +1,63 @@ +/* + Copyright 2018 Banco Bilbao Vizcaya Argentaria, S.A. + + 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 gossip + +import ( + "context" + "net/http" + "time" + + "github.com/bbva/qed/api/metricshttp" + "github.com/bbva/qed/log" + "github.com/prometheus/client_golang/prometheus" +) + +type metricsServer struct { + server *http.Server + registry *prometheus.Registry +} + +func newMetricsServer(addr string) *metricsServer { + r := prometheus.NewRegistry() + return &metricsServer{ + server: &http.Server{ + Addr: addr, + Handler: metricshttp.NewMetricsHTTP(r), + }, + registry: r, + } +} + +func (m metricsServer) start() { + if err := m.server.ListenAndServe(); err != http.ErrServerClosed { + log.Errorf("Can't start metrics HTTP server: %s", err) + } +} + +func (m metricsServer) shutdown() { + ctx, _ := context.WithTimeout(context.Background(), 5*time.Second) + m.server.Shutdown(ctx) +} + +func (m metricsServer) register(metric prometheus.Collector) { + if err := m.registry.Register(metric); err != nil { + log.Infof("metric not registered:", err) + } else { + log.Infof("metric registered.") + } +} +