Metrics: Add --metrics-per-undefined-host argument. (#11818)
Signed-off-by: Jon Carl <grounded042@joncarl.com>
This commit is contained in:
parent
93f9f9fbb3
commit
034c3ccad4
11 changed files with 172 additions and 28 deletions
|
|
@ -105,13 +105,14 @@ type Configuration struct {
|
|||
|
||||
EnableProfiling bool
|
||||
|
||||
EnableMetrics bool
|
||||
MetricsPerHost bool
|
||||
MetricsBuckets *collectors.HistogramBuckets
|
||||
MetricsBucketFactor float64
|
||||
MetricsMaxBuckets uint32
|
||||
ReportStatusClasses bool
|
||||
ExcludeSocketMetrics []string
|
||||
EnableMetrics bool
|
||||
MetricsPerHost bool
|
||||
MetricsPerUndefinedHost bool
|
||||
MetricsBuckets *collectors.HistogramBuckets
|
||||
MetricsBucketFactor float64
|
||||
MetricsMaxBuckets uint32
|
||||
ReportStatusClasses bool
|
||||
ExcludeSocketMetrics []string
|
||||
|
||||
FakeCertificate *ingress.SSLCert
|
||||
|
||||
|
|
|
|||
|
|
@ -81,8 +81,9 @@ type SocketCollector struct {
|
|||
|
||||
hosts sets.Set[string]
|
||||
|
||||
metricsPerHost bool
|
||||
reportStatusClasses bool
|
||||
metricsPerHost bool
|
||||
metricsPerUndefinedHost bool
|
||||
reportStatusClasses bool
|
||||
}
|
||||
|
||||
var requestTags = []string{
|
||||
|
|
@ -99,7 +100,7 @@ var requestTags = []string{
|
|||
|
||||
// NewSocketCollector creates a new SocketCollector instance using
|
||||
// the ingress watch namespace and class used by the controller
|
||||
func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStatusClasses bool, buckets HistogramBuckets, bucketFactor float64, maxBuckets uint32, excludeMetrics []string) (*SocketCollector, error) {
|
||||
func NewSocketCollector(pod, namespace, class string, metricsPerHost, metricsPerUndefinedHost, reportStatusClasses bool, buckets HistogramBuckets, bucketFactor float64, maxBuckets uint32, excludeMetrics []string) (*SocketCollector, error) {
|
||||
socket := "/tmp/nginx/prometheus-nginx.socket"
|
||||
// unix sockets must be unlink()ed before being used
|
||||
//nolint:errcheck // Ignore unlink error
|
||||
|
|
@ -139,8 +140,9 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, reportStat
|
|||
sc := &SocketCollector{
|
||||
listener: listener,
|
||||
|
||||
metricsPerHost: metricsPerHost,
|
||||
reportStatusClasses: reportStatusClasses,
|
||||
metricsPerHost: metricsPerHost,
|
||||
metricsPerUndefinedHost: metricsPerUndefinedHost,
|
||||
reportStatusClasses: reportStatusClasses,
|
||||
|
||||
connectTime: histogramMetric(
|
||||
&prometheus.HistogramOpts{
|
||||
|
|
@ -306,8 +308,8 @@ func (sc *SocketCollector) handleMessage(msg []byte) {
|
|||
|
||||
for i := range statsBatch {
|
||||
stats := &statsBatch[i]
|
||||
if sc.metricsPerHost && !sc.hosts.Has(stats.Host) {
|
||||
klog.V(3).InfoS("Skipping metric for host not being served", "host", stats.Host)
|
||||
if sc.metricsPerHost && !sc.hosts.Has(stats.Host) && !sc.metricsPerUndefinedHost {
|
||||
klog.V(3).InfoS("Skipping metric for host not explicitly defined in an ingress", "host", stats.Host)
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -87,14 +87,15 @@ func TestCollector(t *testing.T) {
|
|||
maxBuckets := uint32(100)
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
data []string
|
||||
metrics []string
|
||||
useStatusClasses bool
|
||||
excludeMetrics []string
|
||||
wantBefore string
|
||||
removeIngresses []string
|
||||
wantAfter string
|
||||
name string
|
||||
data []string
|
||||
metrics []string
|
||||
metricsPerUndefinedHost bool
|
||||
useStatusClasses bool
|
||||
excludeMetrics []string
|
||||
wantBefore string
|
||||
removeIngresses []string
|
||||
wantAfter string
|
||||
}{
|
||||
{
|
||||
name: "invalid metric object should not increase prometheus metrics",
|
||||
|
|
@ -591,13 +592,69 @@ func TestCollector(t *testing.T) {
|
|||
nginx_ingress_controller_response_duration_seconds_count{canary="",controller_class="ingress",controller_namespace="default",controller_pod="pod",host="testshop.com",ingress="web-yml",method="GET",namespace="test-app-production",path="/admin",service="test-app",status="2xx"} 1
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "metrics with a host should not be dropped when the host is not in the hosts slice but metricsPerUndefinedHost is true",
|
||||
data: []string{`[{
|
||||
"host":"wildcard.testshop.com",
|
||||
"status":"200",
|
||||
"bytesSent":150.0,
|
||||
"method":"GET",
|
||||
"path":"/admin",
|
||||
"requestLength":300.0,
|
||||
"requestTime":60.0,
|
||||
"upstreamLatency":1.0,
|
||||
"upstreamHeaderTime":5.0,
|
||||
"upstreamName":"test-upstream",
|
||||
"upstreamIP":"1.1.1.1:8080",
|
||||
"upstreamResponseTime":200,
|
||||
"upstreamStatus":"220",
|
||||
"namespace":"test-app-production",
|
||||
"ingress":"web-yml",
|
||||
"service":"test-app",
|
||||
"canary":""
|
||||
}]`},
|
||||
excludeMetrics: []string{"response_duration_seconds2", "test.*", "nginx_ingress_.*", "response_duration_secon"},
|
||||
metrics: []string{"nginx_ingress_controller_requests"},
|
||||
metricsPerUndefinedHost: true,
|
||||
useStatusClasses: true,
|
||||
wantBefore: `
|
||||
# HELP nginx_ingress_controller_requests The total number of client requests
|
||||
# TYPE nginx_ingress_controller_requests counter
|
||||
nginx_ingress_controller_requests{canary="",controller_class="ingress",controller_namespace="default",controller_pod="pod",host="wildcard.testshop.com",ingress="web-yml",method="GET",namespace="test-app-production",path="/admin",service="test-app",status="2xx"} 1
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "metrics with a host should be dropped when the host is not in the hosts slice",
|
||||
data: []string{`[{
|
||||
"host":"wildcard.testshop.com",
|
||||
"status":"200",
|
||||
"bytesSent":150.0,
|
||||
"method":"GET",
|
||||
"path":"/admin",
|
||||
"requestLength":300.0,
|
||||
"requestTime":60.0,
|
||||
"upstreamLatency":1.0,
|
||||
"upstreamHeaderTime":5.0,
|
||||
"upstreamName":"test-upstream",
|
||||
"upstreamIP":"1.1.1.1:8080",
|
||||
"upstreamResponseTime":200,
|
||||
"upstreamStatus":"220",
|
||||
"namespace":"test-app-production",
|
||||
"ingress":"web-yml",
|
||||
"service":"test-app",
|
||||
"canary":""
|
||||
}]`},
|
||||
excludeMetrics: []string{"response_duration_seconds2", "test.*", "nginx_ingress_.*", "response_duration_secon"},
|
||||
metrics: []string{"nginx_ingress_controller_requests"},
|
||||
useStatusClasses: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
registry := prometheus.NewPedanticRegistry()
|
||||
|
||||
sc, err := NewSocketCollector("pod", "default", "ingress", true, c.useStatusClasses, buckets, bucketFactor, maxBuckets, c.excludeMetrics)
|
||||
sc, err := NewSocketCollector("pod", "default", "ingress", true, c.metricsPerUndefinedHost, c.useStatusClasses, buckets, bucketFactor, maxBuckets, c.excludeMetrics)
|
||||
if err != nil {
|
||||
t.Errorf("%v: unexpected error creating new SocketCollector: %v", c.name, err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ type collector struct {
|
|||
}
|
||||
|
||||
// NewCollector creates a new metric collector the for ingress controller
|
||||
func NewCollector(metricsPerHost, reportStatusClasses bool, registry *prometheus.Registry, ingressclass string, buckets collectors.HistogramBuckets, bucketFactor float64, maxBuckets uint32, excludedSocketMetrics []string) (Collector, error) {
|
||||
func NewCollector(metricsPerHost, metricsPerUndefinedHost, reportStatusClasses bool, registry *prometheus.Registry, ingressclass string, buckets collectors.HistogramBuckets, bucketFactor float64, maxBuckets uint32, excludedSocketMetrics []string) (Collector, error) {
|
||||
podNamespace := os.Getenv("POD_NAMESPACE")
|
||||
if podNamespace == "" {
|
||||
podNamespace = "default"
|
||||
|
|
@ -89,7 +89,7 @@ func NewCollector(metricsPerHost, reportStatusClasses bool, registry *prometheus
|
|||
return nil, err
|
||||
}
|
||||
|
||||
s, err := collectors.NewSocketCollector(podName, podNamespace, ingressclass, metricsPerHost, reportStatusClasses, buckets, bucketFactor, maxBuckets, excludedSocketMetrics)
|
||||
s, err := collectors.NewSocketCollector(podName, podNamespace, ingressclass, metricsPerHost, metricsPerUndefinedHost, reportStatusClasses, buckets, bucketFactor, maxBuckets, excludedSocketMetrics)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue