Skip to content

Commit

Permalink
expose metrics handler for agent integration (#36)
Browse files Browse the repository at this point in the history
* expose metrics handler for agent integration

* set default telemetry path
  • Loading branch information
rlankfo authored Aug 8, 2022
1 parent e118acc commit 23e08bf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 2 additions & 0 deletions vsphere/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions vsphere/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
Expand Down

0 comments on commit 23e08bf

Please sign in to comment.