From 23e08bffae04d8cc5e55430239582685b75a56a1 Mon Sep 17 00:00:00 2001 From: Robert Lankford Date: Mon, 8 Aug 2022 12:28:31 -0500 Subject: [PATCH] expose metrics handler for agent integration (#36) * expose metrics handler for agent integration * set default telemetry path --- vsphere/collector.go | 2 ++ vsphere/exporter.go | 11 +++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/vsphere/collector.go b/vsphere/collector.go index c529ead..1818f74 100644 --- a/vsphere/collector.go +++ b/vsphere/collector.go @@ -214,6 +214,8 @@ func (c *vsphereCollector) collect(ctx context.Context, cli *client, spec types. constLabels) // send metric, using v.Value[0] since we're only requesting a single sample at this time. + // TODO: need to make sure that this is what we want to do here -- in some cases vsphere is returning + // multiple samples for a counter because there are multiple instances of the resource e.g. cpu cores m, err := prometheus.NewConstMetric(desc, prometheus.GaugeValue, float64(v.Value[0])) if err != nil { level.Error(c.logger).Log("err", err) diff --git a/vsphere/exporter.go b/vsphere/exporter.go index 9a1c0d5..3c23285 100644 --- a/vsphere/exporter.go +++ b/vsphere/exporter.go @@ -19,6 +19,8 @@ type Exporter struct { cfg *Config logger log.Logger server *http.Server + + metricsHandlerFunc http.HandlerFunc } // NewExporter creates a new vSphere exporter from the given config @@ -73,12 +75,17 @@ func NewExporter(logger log.Logger, cfg *Config) (*Exporter, error) { topMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) topMux.HandleFunc("/debug/pprof/trace", pprof.Trace) + if cfg.TelemetryPath == "" { + cfg.TelemetryPath = defaultConfig.TelemetryPath + } topMux.Handle(cfg.TelemetryPath, h) + x.metricsHandlerFunc = func(w http.ResponseWriter, r *http.Request) { + h.ServeHTTP(w, r) + } x.server = &http.Server{ Addr: cfg.ListenAddr, Handler: topMux, } - return x, nil } @@ -90,7 +97,7 @@ func (e *Exporter) Start() error { } func (e *Exporter) ServeHTTP(w http.ResponseWriter, r *http.Request) { - e.server.Handler.ServeHTTP(w, r) + e.metricsHandlerFunc(w, r) } var _ http.Handler = (*Exporter)(nil)