Skip to content

Commit

Permalink
feat: Add External traffic policy to kube_service_info
Browse files Browse the repository at this point in the history
  • Loading branch information
jahantech committed Jan 2, 2025
1 parent 279f84b commit 0591d0b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/metrics/service/service-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
| Metric name | Metric type | Description | Unit (where applicable) | Labels/tags | Status |
| ----------------------------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------- | ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ |
| kube_service_annotations | Gauge | Kubernetes annotations converted to Prometheus labels controlled via [--metric-annotations-allowlist](../../developer/cli-arguments.md) | | `service`=&lt;service-name&gt; <br> `namespace`=&lt;service-namespace&gt; <br> `uid`=&lt;service-uid&gt; <br> `annotation_SERVICE_ANNOTATION`=&lt;SERVICE_ANNOTATION&gt; | EXPERIMENTAL |
| kube_service_info | Gauge | Information about service | | `service`=&lt;service-name&gt; <br> `namespace`=&lt;service-namespace&gt; <br> `uid`=&lt;service-uid&gt; <br> `cluster_ip`=&lt;service cluster ip&gt; <br> `external_name`=&lt;service external name&gt; <br> `load_balancer_ip`=&lt;service load balancer ip&gt; | STABLE |
| kube_service_info | Gauge | Information about service | | `service`=&lt;service-name&gt; <br> `namespace`=&lt;service-namespace&gt; <br> `uid`=&lt;service-uid&gt; <br> `cluster_ip`=&lt;service cluster ip&gt; <br> `external_name`=&lt;service external name&gt; <br> `external_traffic_policy`=&lt;service external traffic policy&gt; <br> `load_balancer_ip`=&lt;service load balancer ip&gt; | STABLE |
| kube_service_labels | Gauge | Kubernetes labels converted to Prometheus labels controlled via [--metric-labels-allowlist](../../developer/cli-arguments.md) | | `service`=&lt;service-name&gt; <br> `namespace`=&lt;service-namespace&gt; <br> `uid`=&lt;service-uid&gt; <br> `label_SERVICE_LABEL`=&lt;SERVICE_LABEL&gt; | STABLE |
| kube_service_created | Gauge | Unix creation timestamp | seconds | `service`=&lt;service-name&gt; <br> `namespace`=&lt;service-namespace&gt; <br> `uid`=&lt;service-uid&gt; | STABLE |
| kube_service_spec_type | Gauge | Type about service | | `service`=&lt;service-name&gt; <br> `namespace`=&lt;service-namespace&gt; <br> `uid`=&lt;service-uid&gt; <br> `type`=&lt;ClusterIP\|NodePort\|LoadBalancer\|ExternalName&gt; | STABLE |
Expand Down
4 changes: 2 additions & 2 deletions internal/store/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func serviceMetricFamilies(allowAnnotationsList, allowLabelsList []string) []gen
"",
wrapSvcFunc(func(s *v1.Service) *metric.Family {
m := metric.Metric{
LabelKeys: []string{"cluster_ip", "external_name", "load_balancer_ip"},
LabelValues: []string{s.Spec.ClusterIP, s.Spec.ExternalName, s.Spec.LoadBalancerIP},
LabelKeys: []string{"cluster_ip", "external_name", "load_balancer_ip", "external_traffic_policy"},
LabelValues: []string{s.Spec.ClusterIP, s.Spec.ExternalName, s.Spec.LoadBalancerIP, string(s.Spec.ExternalTrafficPolicy)},
Value: 1,
}
return &metric.Family{Metrics: []*metric.Metric{&m}}
Expand Down
59 changes: 53 additions & 6 deletions internal/store/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestServiceStore(t *testing.T) {
# TYPE kube_service_labels gauge
# TYPE kube_service_spec_type gauge
kube_service_created{namespace="default",service="test-service1",uid="uid1"} 1.5e+09
kube_service_info{cluster_ip="1.2.3.4",external_name="",load_balancer_ip="",namespace="default",service="test-service1",uid="uid1"} 1
kube_service_info{cluster_ip="1.2.3.4",external_name="",external_traffic_policy="",load_balancer_ip="",namespace="default",service="test-service1",uid="uid1"} 1
kube_service_spec_type{namespace="default",service="test-service1",type="ClusterIP",uid="uid1"} 1
`,
MetricNames: []string{
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestServiceStore(t *testing.T) {
},
Want: metadata + `
kube_service_created{namespace="default",service="test-service2",uid="uid2"} 1.5e+09
kube_service_info{cluster_ip="1.2.3.5",external_name="",load_balancer_ip="",namespace="default",service="test-service2",uid="uid2"} 1
kube_service_info{cluster_ip="1.2.3.5",external_name="",external_traffic_policy="",load_balancer_ip="",namespace="default",service="test-service2",uid="uid2"} 1
kube_service_spec_type{namespace="default",service="test-service2",uid="uid2",type="NodePort"} 1
`,
},
Expand All @@ -127,7 +127,7 @@ func TestServiceStore(t *testing.T) {
},
Want: metadata + `
kube_service_created{namespace="default",service="test-service3",uid="uid3"} 1.5e+09
kube_service_info{cluster_ip="1.2.3.6",external_name="",load_balancer_ip="1.2.3.7",namespace="default",service="test-service3",uid="uid3"} 1
kube_service_info{cluster_ip="1.2.3.6",external_name="",external_traffic_policy="",load_balancer_ip="1.2.3.7",namespace="default",service="test-service3",uid="uid3"} 1
kube_service_spec_type{namespace="default",service="test-service3",type="LoadBalancer",uid="uid3"} 1
`,
},
Expand All @@ -149,7 +149,7 @@ func TestServiceStore(t *testing.T) {
},
Want: metadata + `
kube_service_created{namespace="default",service="test-service4",uid="uid4"} 1.5e+09
kube_service_info{cluster_ip="",external_name="www.example.com",load_balancer_ip="",namespace="default",service="test-service4",uid="uid4"} 1
kube_service_info{cluster_ip="",external_name="www.example.com",external_traffic_policy="",load_balancer_ip="",namespace="default",service="test-service4",uid="uid4"} 1
kube_service_spec_type{namespace="default",service="test-service4",uid="uid4",type="ExternalName"} 1
`,
},
Expand Down Expand Up @@ -180,7 +180,7 @@ func TestServiceStore(t *testing.T) {
},
Want: metadata + `
kube_service_created{namespace="default",service="test-service5",uid="uid5"} 1.5e+09
kube_service_info{cluster_ip="",external_name="",load_balancer_ip="",namespace="default",service="test-service5",uid="uid5"} 1
kube_service_info{cluster_ip="",external_name="",external_traffic_policy="",load_balancer_ip="",namespace="default",service="test-service5",uid="uid5"} 1
kube_service_spec_type{namespace="default",service="test-service5",type="LoadBalancer",uid="uid5"} 1
kube_service_status_load_balancer_ingress{hostname="www.example.com",ip="1.2.3.8",namespace="default",service="test-service5",uid="uid5"} 1
`,
Expand All @@ -206,12 +206,59 @@ func TestServiceStore(t *testing.T) {
},
Want: metadata + `
kube_service_created{namespace="default",service="test-service6",uid="uid6"} 1.5e+09
kube_service_info{cluster_ip="",external_name="",load_balancer_ip="",namespace="default",service="test-service6",uid="uid6"} 1
kube_service_info{cluster_ip="",external_name="",external_traffic_policy="",load_balancer_ip="",namespace="default",service="test-service6",uid="uid6"} 1
kube_service_spec_type{namespace="default",service="test-service6",uid="uid6",type="ClusterIP"} 1
kube_service_spec_external_ip{external_ip="1.2.3.9",namespace="default",service="test-service6",uid="uid6"} 1
kube_service_spec_external_ip{external_ip="1.2.3.10",namespace="default",service="test-service6",uid="uid6"} 1
`,
},
{
Obj: &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "test-service7",
CreationTimestamp: metav1.Time{Time: time.Unix(1500000000, 0)},
Namespace: "default",
UID: "uid7",
Labels: map[string]string{
"app": "example7",
},
},
Spec: v1.ServiceSpec{
ClusterIP: "1.2.3.11",
Type: v1.ServiceTypeClusterIP,
ExternalTrafficPolicy: "Cluster",
},
},
Want: metadata + `
kube_service_created{namespace="default",service="test-service7",uid="uid7"} 1.5e+09
kube_service_info{cluster_ip="1.2.3.11",external_name="",external_traffic_policy="Cluster",load_balancer_ip="",namespace="default",service="test-service7",uid="uid7"} 1
kube_service_spec_type{namespace="default",service="test-service7",uid="uid7",type="ClusterIP"} 1
`,
},
{
Obj: &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "test-service8",
CreationTimestamp: metav1.Time{Time: time.Unix(1500000000, 0)},
Namespace: "default",
UID: "uid8",
Labels: map[string]string{
"app": "example8",
},
},
Spec: v1.ServiceSpec{
ClusterIP: "1.2.3.12",
LoadBalancerIP: "1.2.3.13",
Type: v1.ServiceTypeLoadBalancer,
ExternalTrafficPolicy: "Local",
},
},
Want: metadata + `
kube_service_created{namespace="default",service="test-service8",uid="uid8"} 1.5e+09
kube_service_info{cluster_ip="1.2.3.12",external_name="",external_traffic_policy="Local",load_balancer_ip="1.2.3.13",namespace="default",service="test-service8",uid="uid8"} 1
kube_service_spec_type{namespace="default",service="test-service8",uid="uid8",type="LoadBalancer"} 1
`,
},
}
for i, c := range cases {
c.Func = generator.ComposeMetricGenFuncs(serviceMetricFamilies(nil, nil))
Expand Down

0 comments on commit 0591d0b

Please sign in to comment.