mirror of
https://github.com/pomerium/pomerium.git
synced 2025-08-02 08:19:23 +02:00
Proxy envoy metrics through control plane prometheus endpoint (#709)
* Proxy metrics requests to envoy control plane
This commit is contained in:
parent
5ea1f719a7
commit
d514ec2ecf
3 changed files with 97 additions and 8 deletions
|
@ -2,13 +2,21 @@ package metrics
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
ocprom "contrib.go.opencensus.io/exporter/prometheus"
|
||||
prom "github.com/prometheus/client_golang/prometheus"
|
||||
"go.opencensus.io/stats/view"
|
||||
|
||||
"github.com/pomerium/pomerium/internal/envoy"
|
||||
log "github.com/pomerium/pomerium/internal/log"
|
||||
"github.com/pomerium/pomerium/internal/urlutil"
|
||||
)
|
||||
|
||||
var envoyURL = envoy.EnvoyAdminURL
|
||||
|
||||
// PrometheusHandler creates an exporter that exports stats to Prometheus
|
||||
// and returns a handler suitable for exporting metrics.
|
||||
func PrometheusHandler() (http.Handler, error) {
|
||||
|
@ -26,7 +34,13 @@ func PrometheusHandler() (http.Handler, error) {
|
|||
}
|
||||
view.RegisterExporter(exporter)
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/metrics", exporter)
|
||||
|
||||
envoyMetricsURL, err := urlutil.ParseAndValidateURL(fmt.Sprintf("%s/stats/prometheus", envoyURL))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("telemetry/metrics: invalid proxy URL: %w", err)
|
||||
}
|
||||
|
||||
mux.Handle("/metrics", newProxyMetricsHandler(exporter, *envoyMetricsURL))
|
||||
return mux, nil
|
||||
}
|
||||
|
||||
|
@ -37,3 +51,33 @@ func registerDefaultViews() error {
|
|||
}
|
||||
return view.Register(views...)
|
||||
}
|
||||
|
||||
// newProxyMetricsHandler creates a subrequest to the envoy control plane for metrics and
|
||||
// combines them with our own
|
||||
func newProxyMetricsHandler(promHandler http.Handler, envoyURL url.URL) http.HandlerFunc {
|
||||
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
defer promHandler.ServeHTTP(w, r)
|
||||
|
||||
r, err := http.NewRequestWithContext(r.Context(), "GET", envoyURL.String(), nil)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("telemetry/metrics: failed to create request for envoy")
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(r)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("telemetry/metrics: fail to fetch proxy metrics")
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
envoyBody, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("telemetry/metric: failed to read proxy metrics")
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(envoyBody)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue