Skip to content

Commit

Permalink
add prom host (#4661)
Browse files Browse the repository at this point in the history
  • Loading branch information
skonto authored Dec 16, 2020
1 parent 0e55a6a commit c98493d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
18 changes: 18 additions & 0 deletions vendor/knative.dev/pkg/metrics/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion vendor/knative.dev/pkg/metrics/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions vendor/knative.dev/pkg/metrics/prometheus_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ limitations under the License.
package metrics

import (
"fmt"
"net/http"
"strconv"
"sync"

prom "contrib.go.opencensus.io/exporter/prometheus"
Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c98493d

Please sign in to comment.