From e349418ba406f8861650c9b2fc769e6b846a8c12 Mon Sep 17 00:00:00 2001 From: Stavros Kontopoulos Date: Wed, 16 Dec 2020 15:49:32 +0200 Subject: [PATCH] add prom host --- go.mod | 2 +- go.sum | 4 ++-- vendor/knative.dev/pkg/metrics/config.go | 18 ++++++++++++++++++ vendor/knative.dev/pkg/metrics/exporter.go | 7 ++++++- .../pkg/metrics/prometheus_exporter.go | 8 ++++---- vendor/modules.txt | 2 +- 6 files changed, 32 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 6ccca329817..7835462f823 100644 --- a/go.mod +++ b/go.mod @@ -43,7 +43,7 @@ require ( k8s.io/client-go v11.0.1-0.20190805182717-6502b5e7b1b5+incompatible k8s.io/utils v0.0.0-20200603063816-c1c6865ac451 knative.dev/hack v0.0.0-20201103151104-3d5abc3a0075 - knative.dev/pkg v0.0.0-20201103163404-5514ab0c1fdf + knative.dev/pkg v0.0.0-20201215150143-89a9cc3e03a5 sigs.k8s.io/yaml v1.2.0 ) diff --git a/go.sum b/go.sum index 94197dea322..75365608dc1 100644 --- a/go.sum +++ b/go.sum @@ -1101,8 +1101,8 @@ k8s.io/utils v0.0.0-20200603063816-c1c6865ac451 h1:v8ud2Up6QK1lNOKFgiIVrZdMg7Mpm k8s.io/utils v0.0.0-20200603063816-c1c6865ac451/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= knative.dev/hack v0.0.0-20201103151104-3d5abc3a0075 h1:YAgWplKIy4O5e3F5vUUECmXAAyZ0M5ymo6fCt1jeZhs= knative.dev/hack v0.0.0-20201103151104-3d5abc3a0075/go.mod h1:PHt8x8yX5Z9pPquBEfIj0X66f8iWkWfR0S/sarACJrI= -knative.dev/pkg v0.0.0-20201103163404-5514ab0c1fdf h1:QwULgRwcv6R3Ya1GZlf/E1atcaGUNw4DKjxSQUfcR6U= -knative.dev/pkg v0.0.0-20201103163404-5514ab0c1fdf/go.mod h1:cuKOgUvJvnWHIps/apCXX8wZuMlT0dyMZLqRQfsENbQ= +knative.dev/pkg v0.0.0-20201215150143-89a9cc3e03a5 h1:VRRec+QKr9MuDktQGHD1UDFNpDNIXYu0NuhoGeQsHiE= +knative.dev/pkg v0.0.0-20201215150143-89a9cc3e03a5/go.mod h1:cuKOgUvJvnWHIps/apCXX8wZuMlT0dyMZLqRQfsENbQ= pgregory.net/rapid v0.3.3/go.mod h1:UYpPVyjFHzYBGHIxLFoupi8vwk6rXNzRY9OMvVxFIOU= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= diff --git a/vendor/knative.dev/pkg/metrics/config.go b/vendor/knative.dev/pkg/metrics/config.go index 38ac02e65fd..d4ae24ad543 100644 --- a/vendor/knative.dev/pkg/metrics/config.go +++ b/vendor/knative.dev/pkg/metrics/config.go @@ -60,7 +60,9 @@ const ( defaultPrometheusPort = 9090 maxPrometheusPort = 65535 minPrometheusPort = 1024 + defaultPrometheusHost = "0.0.0.0" prometheusPortEnvName = "METRICS_PROMETHEUS_PORT" + prometheusHostEnvName = "METRICS_PROMETHEUS_HOST" ) // Metrics backend "enum". @@ -105,6 +107,10 @@ type metricsConfig struct { // format. It defaults to 9090. prometheusPort int + // prometheusHost is the host where the metrics are exposed in Prometheus + // format. It defaults to "0.0.0.0" + prometheusHost string + // ---- Stackdriver specific below ---- // True if backendDestination equals to "stackdriver". Store this in a variable // to reduce string comparison operations. @@ -240,6 +246,7 @@ func createMetricsConfig(ctx context.Context, ops ExporterOptions) (*metricsConf } mc.prometheusPort = pp + mc.prometheusHost = prometheusHost() case stackdriver: // If stackdriverClientConfig is not provided for stackdriver backend destination, OpenCensus will try to // use the application default credentials. If that is not available, Opencensus would fail to create the @@ -341,6 +348,17 @@ func prometheusPort() (int, error) { return int(pp), nil } +// prometheusHost returns the host configured via the environment +// for the Prometheus metrics exporter if it's set, a default value otherwise. +// No validation is done here. +func prometheusHost() string { + phStr := os.Getenv(prometheusHostEnvName) + if phStr == "" { + return defaultPrometheusHost + } + return phStr +} + // JSONToOptions converts a json string to ExporterOptions. func JSONToOptions(jsonOpts string) (*ExporterOptions, error) { var opts ExporterOptions diff --git a/vendor/knative.dev/pkg/metrics/exporter.go b/vendor/knative.dev/pkg/metrics/exporter.go index a7411fccb0f..0c680106eec 100644 --- a/vendor/knative.dev/pkg/metrics/exporter.go +++ b/vendor/knative.dev/pkg/metrics/exporter.go @@ -66,10 +66,15 @@ type ExporterOptions struct { // PrometheusPort is the port to expose metrics if metrics backend is Prometheus. // It should be between maxPrometheusPort and maxPrometheusPort. 0 value means - // using the default 9090 value. If is ignored if metrics backend is not + // using the default 9090 value. It is ignored if metrics backend is not // Prometheus. PrometheusPort int + // PrometheusHost is the host to expose metrics on if metrics backend is Prometheus. + // The default value is "0.0.0.0". It is ignored if metrics backend is not + // Prometheus. + PrometheusHost string + // ConfigMap is the data from config map config-observability. Must be present. // See https://github.com/knative/serving/blob/master/config/config-observability.yaml // for details. diff --git a/vendor/knative.dev/pkg/metrics/prometheus_exporter.go b/vendor/knative.dev/pkg/metrics/prometheus_exporter.go index 7f49302cc41..29b9206338a 100644 --- a/vendor/knative.dev/pkg/metrics/prometheus_exporter.go +++ b/vendor/knative.dev/pkg/metrics/prometheus_exporter.go @@ -17,8 +17,8 @@ limitations under the License. package metrics import ( - "fmt" "net/http" + "strconv" "sync" prom "contrib.go.opencensus.io/exporter/prometheus" @@ -50,7 +50,7 @@ func newPrometheusExporter(config *metricsConfig, logger *zap.SugaredLogger) (vi logger.Infof("Created Opencensus Prometheus exporter with config: %v. Start the server for Prometheus exporter.", config) // Start the server for Prometheus scraping go func() { - srv := startNewPromSrv(e, config.prometheusPort) + srv := startNewPromSrv(e, config.prometheusHost, config.prometheusPort) srv.ListenAndServe() }() return e, @@ -73,7 +73,7 @@ func resetCurPromSrv() { } } -func startNewPromSrv(e *prom.Exporter, port int) *http.Server { +func startNewPromSrv(e *prom.Exporter, host string, port int) *http.Server { sm := http.NewServeMux() sm.Handle("/metrics", e) curPromSrvMux.Lock() @@ -82,7 +82,7 @@ func startNewPromSrv(e *prom.Exporter, port int) *http.Server { curPromSrv.Close() } curPromSrv = &http.Server{ - Addr: fmt.Sprint(":", port), + Addr: host + ":" + strconv.Itoa(port), Handler: sm, } return curPromSrv diff --git a/vendor/modules.txt b/vendor/modules.txt index aa20d7d8bdf..1ec7a92f111 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -963,7 +963,7 @@ k8s.io/utils/trace # knative.dev/hack v0.0.0-20201103151104-3d5abc3a0075 ## explicit knative.dev/hack -# knative.dev/pkg v0.0.0-20201103163404-5514ab0c1fdf +# knative.dev/pkg v0.0.0-20201215150143-89a9cc3e03a5 ## explicit knative.dev/pkg/apiextensions/storageversion knative.dev/pkg/apiextensions/storageversion/cmd/migrate