improve creation of metric mapping

This commit is contained in:
Marco Cadetg 2023-04-01 09:03:12 +02:00
parent 84c661d7e7
commit 2cfb7d002a
3 changed files with 30 additions and 30 deletions

View file

@ -25,6 +25,7 @@ They are set in the container spec of the `ingress-nginx-controller` Deployment
| `--enable-ssl-chain-completion` | Autocomplete SSL certificate chains with missing intermediate CA certificates. Certificates uploaded to Kubernetes must have the "Authority Information Access" X.509 v3 extension for this to succeed. (default false)|
| `--enable-ssl-passthrough` | Enable SSL Passthrough. (default false) |
| `--enable-topology-aware-routing` | Enable topology aware hints feature, needs service object annotation service.kubernetes.io/topology-aware-hints sets to auto. (default false) |
| `--exclude-socket-metrics` | Set of socket request metrics to exclude which won't be exported nor being calculated. The possible socket request metrics to exclude are documented in the monitoring guide e.g. 'nginx_ingress_controller_request_duration_seconds,nginx_ingress_controller_response_size'|
| `--health-check-path` | URL path of the health check endpoint. Configured inside the NGINX status server. All requests received on the port defined by the healthz-port parameter are forwarded internally to this path. (default "/healthz") |
| `--health-check-timeout` | Time limit, in seconds, for a probe to health-check-path to succeed. (default 10) |
| `--healthz-port` | Port to use for the healthz endpoint. (default 10254) |
@ -53,7 +54,6 @@ They are set in the container spec of the `ingress-nginx-controller` Deployment
| `--ssl-passthrough-proxy-port` | Port to use internally for SSL Passthrough. (default 442) |
| `--status-port` | Port to use for the lua HTTP endpoint configuration. (default 10246) |
| `--status-update-interval` | Time interval in seconds in which the status should check if an update is required. Default is 60 seconds. (default 60) |
| `--exclude-socket-metrics` | Set of socket request metrics to exclude which won't be exported nor being calculated. E.g. 'nginx_ingress_controller_request_size,nginx_ingress_controller_header_duration_seconds'. |
| `--stream-port` | Port to use for the lua TCP/UDP endpoint configuration. (default 10247) |
| `--sync-period` | Period at which the controller forces the repopulation of its local object stores. Disabled by default. |
| `--sync-rate-limit` | Define the sync frequency upper limit. (default 0.3) |

View file

@ -61,6 +61,8 @@ type HistogramBuckets struct {
SizeBuckets []float64
}
type metricMapping map[string]prometheus.Collector
// SocketCollector stores prometheus metrics and ingress meta-data
type SocketCollector struct {
prometheus.Collector
@ -79,7 +81,7 @@ type SocketCollector struct {
listener net.Listener
metricMapping map[string]prometheus.Collector
metricMapping metricMapping
hosts sets.Set[string]
@ -140,6 +142,9 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStat
em[strings.TrimPrefix(m, "nginx_ingress_controller_")] = struct{}{}
}
// create metric mapping with only the metrics that are not excluded
mm := make(metricMapping)
sc := &SocketCollector{
listener: listener,
@ -156,6 +161,7 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStat
},
requestTags,
em,
mm,
),
headerTime: histogramMetric(
@ -168,6 +174,7 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStat
},
requestTags,
em,
mm,
),
responseTime: histogramMetric(
prometheus.HistogramOpts{
@ -179,6 +186,7 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStat
},
requestTags,
em,
mm,
),
requestTime: histogramMetric(
@ -191,6 +199,7 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStat
},
requestTags,
em,
mm,
),
responseLength: histogramMetric(
@ -203,6 +212,7 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStat
},
requestTags,
em,
mm,
),
requestLength: histogramMetric(
@ -215,6 +225,7 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStat
},
requestTags,
em,
mm,
),
requests: counterMetric(
@ -226,6 +237,7 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStat
},
requestTags,
em,
mm,
),
bytesSent: histogramMetric(
@ -238,6 +250,7 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStat
},
requestTags,
em,
mm,
),
upstreamLatency: summaryMetric(
@ -250,30 +263,11 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStat
},
[]string{"ingress", "namespace", "service", "canary"},
em,
mm,
),
}
sc.metricMapping = map[string]prometheus.Collector{
prometheus.BuildFQName(PrometheusNamespace, "", "requests"): sc.requests,
prometheus.BuildFQName(PrometheusNamespace, "", "connect_duration_seconds"): sc.connectTime,
prometheus.BuildFQName(PrometheusNamespace, "", "header_duration_seconds"): sc.headerTime,
prometheus.BuildFQName(PrometheusNamespace, "", "response_duration_seconds"): sc.responseTime,
prometheus.BuildFQName(PrometheusNamespace, "", "request_duration_seconds"): sc.requestTime,
prometheus.BuildFQName(PrometheusNamespace, "", "request_size"): sc.requestLength,
prometheus.BuildFQName(PrometheusNamespace, "", "response_size"): sc.responseLength,
prometheus.BuildFQName(PrometheusNamespace, "", "bytes_sent"): sc.bytesSent,
prometheus.BuildFQName(PrometheusNamespace, "", "ingress_upstream_latency_seconds"): sc.upstreamLatency,
}
for m := range em {
// remove excluded metrics from the metricMapping
delete(sc.metricMapping, prometheus.BuildFQName(PrometheusNamespace, "", m))
}
sc.metricMapping = mm
return sc, nil
}
@ -285,34 +279,40 @@ func containsMetric(excludeMetrics map[string]struct{}, name string) bool {
return false
}
func summaryMetric(opts prometheus.SummaryOpts, requestTags []string, excludeMetrics map[string]struct{}) *prometheus.SummaryVec {
func summaryMetric(opts prometheus.SummaryOpts, requestTags []string, excludeMetrics map[string]struct{}, metricMapping metricMapping) *prometheus.SummaryVec {
if containsMetric(excludeMetrics, opts.Name) {
return nil
}
return prometheus.NewSummaryVec(
m := prometheus.NewSummaryVec(
opts,
requestTags,
)
metricMapping[prometheus.BuildFQName(PrometheusNamespace, "", opts.Name)] = m
return m
}
func counterMetric(opts prometheus.CounterOpts, requestTags []string, excludeMetrics map[string]struct{}) *prometheus.CounterVec {
func counterMetric(opts prometheus.CounterOpts, requestTags []string, excludeMetrics map[string]struct{}, metricMapping metricMapping) *prometheus.CounterVec {
if containsMetric(excludeMetrics, opts.Name) {
return nil
}
return prometheus.NewCounterVec(
m := prometheus.NewCounterVec(
opts,
requestTags,
)
metricMapping[prometheus.BuildFQName(PrometheusNamespace, "", opts.Name)] = m
return m
}
func histogramMetric(opts prometheus.HistogramOpts, requestTags []string, excludeMetrics map[string]struct{}) *prometheus.HistogramVec {
func histogramMetric(opts prometheus.HistogramOpts, requestTags []string, excludeMetrics map[string]struct{}, metricMapping metricMapping) *prometheus.HistogramVec {
if containsMetric(excludeMetrics, opts.Name) {
return nil
}
return prometheus.NewHistogramVec(
m := prometheus.NewHistogramVec(
opts,
requestTags,
)
metricMapping[prometheus.BuildFQName(PrometheusNamespace, "", opts.Name)] = m
return m
}
func (sc *SocketCollector) handleMessage(msg []byte) {

View file

@ -174,7 +174,7 @@ Requires the update-status parameter.`)
timeBuckets = flags.Float64Slice("time-buckets", prometheus.DefBuckets, "Set of buckets which will be used for prometheus histogram metrics such as RequestTime, ResponseTime.")
lengthBuckets = flags.Float64Slice("length-buckets", prometheus.LinearBuckets(10, 10, 10), "Set of buckets which will be used for prometheus histogram metrics such as RequestLength, ResponseLength.")
sizeBuckets = flags.Float64Slice("size-buckets", prometheus.ExponentialBuckets(10, 10, 7), "Set of buckets which will be used for prometheus histogram metrics such as BytesSent.")
excludeSocketMetrics = flags.StringSlice("exclude-socket-metrics", []string{}, "Set of socket metrics to exclude which won't be exported nor being calculated. E.g. 'nginx_ingress_controller_success,nginx_ingress_controller_header_duration_seconds'.")
excludeSocketMetrics = flags.StringSlice("exclude-socket-metrics", []string{}, "et of socket request metrics to exclude which won't be exported nor being calculated. E.g. 'nginx_ingress_controller_success,nginx_ingress_controller_header_duration_seconds'.")
monitorMaxBatchSize = flags.Int("monitor-max-batch-size", 10000, "Max batch size of NGINX metrics.")
httpPort = flags.Int("http-port", 80, `Port to use for servicing HTTP traffic.`)