Skip to content

Commit

Permalink
ApplyConfig for integrations (#486)
Browse files Browse the repository at this point in the history
* dynamic metrics endpoint

* add applyconfig

* finish writing code, updating tests

* use common function for converting integration name to key

* no early returns
  • Loading branch information
rfratto authored Mar 25, 2021
1 parent 5c1b14b commit 7727df2
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 200 deletions.
13 changes: 4 additions & 9 deletions pkg/integrations/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ package agent

import (
"context"
"net/http"

"github.com/go-kit/kit/log"
"github.com/gorilla/mux"
"github.com/grafana/agent/pkg/integrations"
"github.com/grafana/agent/pkg/integrations/config"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand Down Expand Up @@ -48,14 +48,9 @@ func New(c *Config) *Integration {
return &Integration{c: c}
}

// RegisterRoutes satisfies Integration.RegisterRoutes.
func (i *Integration) RegisterRoutes(r *mux.Router) error {
// Note that if the weaveworks common server is set to not register
// instrumentation endpoints, this lets the agent integration still be able
// to scrape itself, just at /integrations/agent/metrics.
r.Handle("/metrics", promhttp.Handler())

return nil
// MetricsHandler satisfies Integration.RegisterRoutes.
func (i *Integration) MetricsHandler() (http.Handler, error) {
return promhttp.Handler(), nil
}

// ScrapeConfigs satisfies Integration.ScrapeConfigs.
Expand Down
17 changes: 2 additions & 15 deletions pkg/integrations/collector_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"net/http"

"github.com/gorilla/mux"
"github.com/grafana/agent/pkg/integrations/config"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand Down Expand Up @@ -61,20 +60,8 @@ func WithExporterMetricsIncluded(included bool) CollectorIntegrationConfig {
}
}

// RegisterRoutes satisfies Integration.RegisterRoutes. The mux.Router provided
// here is expected to be a subrouter, where all registered paths will be
// registered within that subroute.
func (i *CollectorIntegration) RegisterRoutes(r *mux.Router) error {
handler, err := i.handler()
if err != nil {
return err
}

r.Handle("/metrics", handler)
return nil
}

func (i *CollectorIntegration) handler() (http.Handler, error) {
// MetricsHandler returns the HTTP handler for the integration.
func (i *CollectorIntegration) MetricsHandler() (http.Handler, error) {
r := prometheus.NewRegistry()
for _, c := range i.cs {
if err := r.Register(c); err != nil {
Expand Down
9 changes: 3 additions & 6 deletions pkg/integrations/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package integrations

import (
"context"
"net/http"

"github.com/go-kit/kit/log"
"github.com/gorilla/mux"
"github.com/grafana/agent/pkg/integrations/config"
)

Expand All @@ -25,11 +25,8 @@ type Config interface {
// An Integration is a process that integrates with some external system and
// pulls telemetry data.
type Integration interface {
// RegisterRoutes should register any HTTP handlers needed for the
// integrations. The mux router provided will be a subrouter for the path
// /integrations/<integration name>, where the integration name is retrieved
// by the config that created this integration.
RegisterRoutes(r *mux.Router) error
// MetricsHandler returns an http.Handler that will return metrics.
MetricsHandler() (http.Handler, error)

// ScrapeConfigs returns a set of scrape configs that determine where metrics
// can be scraped.
Expand Down
Loading

0 comments on commit 7727df2

Please sign in to comment.