mirror of https://github.com/knative/pkg.git
				
				
				
			Expose queue proxy request metrics reporting period (#2362)
* expose qp request metrics reporting period * remove dead link * add comments * add default * fix unit test * add back the correct url * use metrics.reporting-period as default * don't return a pointer
This commit is contained in:
		
							parent
							
								
									bd3cf5174f
								
							
						
					
					
						commit
						696cac83c1
					
				| 
						 | 
				
			
			@ -41,19 +41,21 @@ const (
 | 
			
		|||
	DomainEnv = "METRICS_DOMAIN"
 | 
			
		||||
 | 
			
		||||
	// The following keys are used to configure metrics reporting.
 | 
			
		||||
	// See https://github.com/knative/serving/blob/main/config/config-observability.yaml
 | 
			
		||||
	// See https://github.com/knative/serving/blob/main/config/core/configmaps/observability.yaml
 | 
			
		||||
	// for details.
 | 
			
		||||
	collectorAddressKey = "metrics.opencensus-address"
 | 
			
		||||
	collectorSecureKey  = "metrics.opencensus-require-tls"
 | 
			
		||||
	reportingPeriodKey  = "metrics.reporting-period-seconds"
 | 
			
		||||
 | 
			
		||||
	defaultBackendEnvName = "DEFAULT_METRICS_BACKEND"
 | 
			
		||||
	defaultPrometheusPort = 9090
 | 
			
		||||
	maxPrometheusPort     = 65535
 | 
			
		||||
	minPrometheusPort     = 1024
 | 
			
		||||
	defaultPrometheusHost = "0.0.0.0"
 | 
			
		||||
	prometheusPortEnvName = "METRICS_PROMETHEUS_PORT"
 | 
			
		||||
	prometheusHostEnvName = "METRICS_PROMETHEUS_HOST"
 | 
			
		||||
	defaultBackendEnvName            = "DEFAULT_METRICS_BACKEND"
 | 
			
		||||
	defaultPrometheusPort            = 9090
 | 
			
		||||
	defaultPrometheusReportingPeriod = 5
 | 
			
		||||
	defaultOpenCensusReportingPeriod = 60
 | 
			
		||||
	maxPrometheusPort                = 65535
 | 
			
		||||
	minPrometheusPort                = 1024
 | 
			
		||||
	defaultPrometheusHost            = "0.0.0.0"
 | 
			
		||||
	prometheusPortEnvName            = "METRICS_PROMETHEUS_PORT"
 | 
			
		||||
	prometheusHostEnvName            = "METRICS_PROMETHEUS_HOST"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
| 
						 | 
				
			
			@ -206,9 +208,9 @@ func createMetricsConfig(_ context.Context, ops ExporterOptions) (*metricsConfig
 | 
			
		|||
	} else {
 | 
			
		||||
		switch mc.backendDestination {
 | 
			
		||||
		case openCensus:
 | 
			
		||||
			mc.reportingPeriod = time.Minute
 | 
			
		||||
			mc.reportingPeriod = defaultOpenCensusReportingPeriod * time.Second
 | 
			
		||||
		case prometheus:
 | 
			
		||||
			mc.reportingPeriod = 5 * time.Second
 | 
			
		||||
			mc.reportingPeriod = defaultPrometheusReportingPeriod * time.Second
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return &mc, nil
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -71,6 +71,10 @@ type ObservabilityConfig struct {
 | 
			
		|||
	// OpenCensus. "None" disables all backends.
 | 
			
		||||
	RequestMetricsBackend string
 | 
			
		||||
 | 
			
		||||
	// RequestMetricsReportingPeriodSeconds specifies the request metrics reporting period in sec at queue proxy, eg 1.
 | 
			
		||||
	// If a zero or negative value is passed the default reporting period is used (10 secs).
 | 
			
		||||
	RequestMetricsReportingPeriodSeconds int
 | 
			
		||||
 | 
			
		||||
	// EnableProfiling indicates whether it is allowed to retrieve runtime profiling data from
 | 
			
		||||
	// the pods via an HTTP server in the format expected by the pprof visualization tool.
 | 
			
		||||
	EnableProfiling bool
 | 
			
		||||
| 
						 | 
				
			
			@ -114,6 +118,12 @@ func NewObservabilityConfigFromConfigMap(configMap *corev1.ConfigMap) (*Observab
 | 
			
		|||
		return oc, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	defaultRequestMetricsReportingPeriod, err := getDefaultRequestMetricsReportingPeriod(configMap.Data)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	oc.RequestMetricsReportingPeriodSeconds = defaultRequestMetricsReportingPeriod
 | 
			
		||||
 | 
			
		||||
	if err := cm.Parse(configMap.Data,
 | 
			
		||||
		cm.AsBool("logging.enable-var-log-collection", &oc.EnableVarLogCollection),
 | 
			
		||||
		cm.AsString("logging.revision-url-template", &oc.LoggingURLTemplate),
 | 
			
		||||
| 
						 | 
				
			
			@ -121,6 +131,7 @@ func NewObservabilityConfigFromConfigMap(configMap *corev1.ConfigMap) (*Observab
 | 
			
		|||
		cm.AsBool(EnableReqLogKey, &oc.EnableRequestLog),
 | 
			
		||||
		cm.AsBool(EnableProbeReqLogKey, &oc.EnableProbeRequestLog),
 | 
			
		||||
		cm.AsString("metrics.request-metrics-backend-destination", &oc.RequestMetricsBackend),
 | 
			
		||||
		cm.AsInt("metrics.request-metrics-reporting-period-seconds", &oc.RequestMetricsReportingPeriodSeconds),
 | 
			
		||||
		cm.AsBool("profiling.enable", &oc.EnableProfiling),
 | 
			
		||||
		cm.AsString("metrics.opencensus-address", &oc.MetricsCollectorAddress),
 | 
			
		||||
	); err != nil {
 | 
			
		||||
| 
						 | 
				
			
			@ -163,3 +174,27 @@ func ConfigMapName() string {
 | 
			
		|||
	}
 | 
			
		||||
	return "config-observability"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Use the same as `metrics.reporting-period-seconds` for the default
 | 
			
		||||
// of `metrics.request-metrics-reporting-period-seconds`
 | 
			
		||||
func getDefaultRequestMetricsReportingPeriod(data map[string]string) (int, error) {
 | 
			
		||||
	// Default backend is prometheus
 | 
			
		||||
	period := defaultPrometheusReportingPeriod
 | 
			
		||||
	if repStr := data[reportingPeriodKey]; repStr != "" {
 | 
			
		||||
		repInt, err := strconv.Atoi(repStr)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return -1, fmt.Errorf("invalid %s value %q", reportingPeriodKey, repStr)
 | 
			
		||||
		}
 | 
			
		||||
		period = repInt
 | 
			
		||||
	} else {
 | 
			
		||||
		if raw, ok := data["metrics.request-metrics-backend-destination"]; ok {
 | 
			
		||||
			switch metricsBackend(raw) {
 | 
			
		||||
			case prometheus:
 | 
			
		||||
				period = defaultPrometheusReportingPeriod
 | 
			
		||||
			case openCensus:
 | 
			
		||||
				period = defaultOpenCensusReportingPeriod
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return period, nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -34,13 +34,14 @@ func TestObservabilityConfiguration(t *testing.T) {
 | 
			
		|||
	}{{
 | 
			
		||||
		name: "observability configuration with all inputs",
 | 
			
		||||
		wantConfig: &ObservabilityConfig{
 | 
			
		||||
			EnableProbeRequestLog:  true,
 | 
			
		||||
			EnableProfiling:        true,
 | 
			
		||||
			EnableVarLogCollection: true,
 | 
			
		||||
			EnableRequestLog:       true,
 | 
			
		||||
			LoggingURLTemplate:     "https://logging.io",
 | 
			
		||||
			RequestLogTemplate:     `{"requestMethod": "{{.Request.Method}}"}`,
 | 
			
		||||
			RequestMetricsBackend:  "opencensus",
 | 
			
		||||
			EnableProbeRequestLog:                true,
 | 
			
		||||
			EnableProfiling:                      true,
 | 
			
		||||
			EnableVarLogCollection:               true,
 | 
			
		||||
			EnableRequestLog:                     true,
 | 
			
		||||
			LoggingURLTemplate:                   "https://logging.io",
 | 
			
		||||
			RequestLogTemplate:                   `{"requestMethod": "{{.Request.Method}}"}`,
 | 
			
		||||
			RequestMetricsBackend:                "opencensus",
 | 
			
		||||
			RequestMetricsReportingPeriodSeconds: defaultOpenCensusReportingPeriod,
 | 
			
		||||
		},
 | 
			
		||||
		data: map[string]string{
 | 
			
		||||
			EnableProbeReqLogKey:                          "true",
 | 
			
		||||
| 
						 | 
				
			
			@ -52,8 +53,12 @@ func TestObservabilityConfiguration(t *testing.T) {
 | 
			
		|||
			"profiling.enable":                            "true",
 | 
			
		||||
		},
 | 
			
		||||
	}, {
 | 
			
		||||
		name:       "observability config with no map",
 | 
			
		||||
		wantConfig: defaultConfig(),
 | 
			
		||||
		name: "observability config with no map",
 | 
			
		||||
		wantConfig: func() *ObservabilityConfig {
 | 
			
		||||
			oc := defaultConfig()
 | 
			
		||||
			oc.RequestMetricsReportingPeriodSeconds = defaultPrometheusReportingPeriod
 | 
			
		||||
			return oc
 | 
			
		||||
		}(),
 | 
			
		||||
	}, {
 | 
			
		||||
		name:    "invalid request log template",
 | 
			
		||||
		wantErr: true,
 | 
			
		||||
| 
						 | 
				
			
			@ -72,6 +77,7 @@ func TestObservabilityConfiguration(t *testing.T) {
 | 
			
		|||
			oc.EnableProbeRequestLog = true
 | 
			
		||||
			oc.EnableRequestLog = true
 | 
			
		||||
			oc.LoggingURLTemplate = "https://logging.io"
 | 
			
		||||
			oc.RequestMetricsReportingPeriodSeconds = defaultPrometheusReportingPeriod
 | 
			
		||||
			return oc
 | 
			
		||||
		}(),
 | 
			
		||||
	}, {
 | 
			
		||||
| 
						 | 
				
			
			@ -80,6 +86,7 @@ func TestObservabilityConfiguration(t *testing.T) {
 | 
			
		|||
			oc := defaultConfig()
 | 
			
		||||
			oc.RequestLogTemplate = ""
 | 
			
		||||
			oc.EnableProbeRequestLog = true
 | 
			
		||||
			oc.RequestMetricsReportingPeriodSeconds = defaultPrometheusReportingPeriod
 | 
			
		||||
			return oc
 | 
			
		||||
		}(),
 | 
			
		||||
		data: map[string]string{
 | 
			
		||||
| 
						 | 
				
			
			@ -103,6 +110,7 @@ func TestObservabilityConfiguration(t *testing.T) {
 | 
			
		|||
			oc.EnableProbeRequestLog = true
 | 
			
		||||
			oc.EnableVarLogCollection = true
 | 
			
		||||
			oc.RequestLogTemplate = `{"requestMethod": "{{.Request.Method}}"}`
 | 
			
		||||
			oc.RequestMetricsReportingPeriodSeconds = defaultPrometheusReportingPeriod
 | 
			
		||||
			return oc
 | 
			
		||||
		}(),
 | 
			
		||||
		data: map[string]string{
 | 
			
		||||
| 
						 | 
				
			
			@ -116,12 +124,27 @@ func TestObservabilityConfiguration(t *testing.T) {
 | 
			
		|||
			oc := defaultConfig()
 | 
			
		||||
			oc.RequestMetricsBackend = "opencensus"
 | 
			
		||||
			oc.MetricsCollectorAddress = "otel:55678"
 | 
			
		||||
			oc.RequestMetricsReportingPeriodSeconds = defaultOpenCensusReportingPeriod
 | 
			
		||||
			return oc
 | 
			
		||||
		}(),
 | 
			
		||||
		data: map[string]string{
 | 
			
		||||
			"metrics.request-metrics-backend-destination": "opencensus",
 | 
			
		||||
			"metrics.opencensus-address":                  "otel:55678",
 | 
			
		||||
		},
 | 
			
		||||
	}, {
 | 
			
		||||
		name: "observability configuration with collector address and reporting period",
 | 
			
		||||
		wantConfig: func() *ObservabilityConfig {
 | 
			
		||||
			oc := defaultConfig()
 | 
			
		||||
			oc.RequestMetricsBackend = "opencensus"
 | 
			
		||||
			oc.MetricsCollectorAddress = "otel:55678"
 | 
			
		||||
			oc.RequestMetricsReportingPeriodSeconds = 10
 | 
			
		||||
			return oc
 | 
			
		||||
		}(),
 | 
			
		||||
		data: map[string]string{
 | 
			
		||||
			"metrics.request-metrics-backend-destination":      "opencensus",
 | 
			
		||||
			"metrics.opencensus-address":                       "otel:55678",
 | 
			
		||||
			"metrics.request-metrics-reporting-period-seconds": "10",
 | 
			
		||||
		},
 | 
			
		||||
	}}
 | 
			
		||||
 | 
			
		||||
	for _, tt := range observabilityConfigTests {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue