Skip to content

feat: add new kube_service_spec_ports metric#2849

Open
Serializator wants to merge 1 commit intokubernetes:mainfrom
Serializator:ISSUE-2819-kube-service-port-metric
Open

feat: add new kube_service_spec_ports metric#2849
Serializator wants to merge 1 commit intokubernetes:mainfrom
Serializator:ISSUE-2819-kube-service-port-metric

Conversation

@Serializator
Copy link
Copy Markdown

What this PR does / why we need it:
Add new metric kube_service_spec_ports to represent ports on the service resource.

The kube_service_spec_ prefix matches with the naming convention for the rest of the metrics.

To stay aligned with other metrics, .spec.ports.appProtocol is not included as a label. The same is true for the kube_endpoint_address metric.

.spec.ports.targetPort is not included as a label due to its inconsistent value. It can be a named port or port number.

How does this change affect the cardinality of KSM:
Increases cardinality with a new time series.

Which issue(s) this PR fixes:
Fixes #2819

@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Jan 18, 2026
@k8s-ci-robot k8s-ci-robot requested a review from mrueg January 18, 2026 01:10
@github-project-automation github-project-automation bot moved this to Needs Triage in SIG Instrumentation Jan 18, 2026
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Jan 18, 2026
@Serializator Serializator changed the title Add new kube_service_spec_ports metric feat: add new kube_service_spec_ports metric Jan 18, 2026
@Serializator Serializator force-pushed the ISSUE-2819-kube-service-port-metric branch from a35c311 to dc2f674 Compare January 18, 2026 01:18
@Serializator Serializator force-pushed the ISSUE-2819-kube-service-port-metric branch from dc2f674 to e9da2b6 Compare January 30, 2026 22:07
@Serializator
Copy link
Copy Markdown
Author

Hey @dgrisonnet & @mrueg! Would one of you have time to approve the workflow so tests can run? Thank you in advance!

Copy link
Copy Markdown
Member

@bhope bhope left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. A few comments:

  1. Stability: new metrics should start at basemetrics.ALPHA, not basemetrics.STABLE (see inline)
  2. node_port_number: should always be present as a label, using "" when no NodePort is set (see inline)

"kube_service_spec_ports",
"Service ports. One series for each port",
metric.Gauge,
basemetrics.STABLE,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this should be basemetrics.ALPHA. We can only promote a metric to alpha->beta->stable after it has gone through multiple releases without any issues. Happy to be corrected if I am missing any context.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review! I updated it to be basemetrics.ALPHA

values := []string{port.Name, string(protocol), strconv.FormatInt(int64(port.Port), 10)}

// 0 means no node port is set. It is also a reserved port (RFC-6335) so there shouldn't be a problem.
if port.NodePort > 0 {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node_port_number should always be present per the project's best practices. Use "" when NodePort == 0 instead of conditionally adding the label.
Check the best practices here: https://github.com/kubernetes/kube-state-metrics/blob/main/docs/design/metrics-best-practices.md#optional-properties

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I've updated it to be an empty string when the node port is not set.

@github-project-automation github-project-automation bot moved this from Needs Triage to In Progress in SIG Instrumentation Mar 1, 2026
@Serializator Serializator force-pushed the ISSUE-2819-kube-service-port-metric branch from e9da2b6 to 12295e8 Compare March 18, 2026 21:31
nodePortNumber = strconv.FormatInt(int64(port.NodePort), 10)
}

keys := []string{"port_name", "port_protocol", "port_number", "node_port_number"}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: probably should sit outside for loop

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point 😉

| 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 |
| kube_service_spec_external_ip | Gauge | Service external ips. One series for each ip | | `service`=&lt;service-name&gt; <br> `namespace`=&lt;service-namespace&gt; <br> `uid`=&lt;service-uid&gt; <br> `external_ip`=&lt;external-ip&gt; | STABLE |
| kube_service_spec_ports | Gauge | Service port. One series for each port | | `service`=&lt;service-name&gt; <br> `namespace`=&lt;service-namespace&gt; <br> `uid`=&lt;service-uid&gt; <br> `port_number`=&lt;port-number&gt; <br> `node_port_number`=&lt;node-port-number&gt; | EXPERIMENTAL |
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you verify this? Your code emits port_name and port_protocol too

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're correct. Good catch! I added port_name and port_protocol to the documentation.

@bhope
Copy link
Copy Markdown
Member

bhope commented Mar 18, 2026

/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Mar 18, 2026
@Serializator Serializator force-pushed the ISSUE-2819-kube-service-port-metric branch from 12295e8 to 33423e9 Compare March 18, 2026 22:29
@bhope
Copy link
Copy Markdown
Member

bhope commented Mar 19, 2026

Overall its in a good shape now. Can you squash the commits?

Signed-off-by: Julian van den Berkmortel <7153670+Serializator@users.noreply.github.com>
@Serializator Serializator force-pushed the ISSUE-2819-kube-service-port-metric branch from 33423e9 to 14684e0 Compare March 19, 2026 20:55
@Serializator
Copy link
Copy Markdown
Author

@bhope squashed the commits and also resolved a linting problem.

@bhope
Copy link
Copy Markdown
Member

bhope commented Mar 19, 2026

Thanks

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 19, 2026
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: bhope, Serializator
Once this PR has been reviewed and has the lgtm label, please assign catherinef-dev for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@mrueg
Copy link
Copy Markdown
Member

mrueg commented Mar 20, 2026

Can we quantify the number of timeseries it generates per port, service, etc.?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

Missing port information in service metrics

4 participants