Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set service_name label for podLogs #1175

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

rlankfo
Copy link
Member

@rlankfo rlankfo commented Jan 28, 2025

This PR sets service_name, service_namespace, deployment_environment, and deployment_environment_name labels on Loki pod logs. This works when sent to loki destinations. I'll follow this PR up with updates for pod logs sent to OTLP destinations.

The labels are set according to the following rules:

  • service_name label set by taking the first non-empty value from these Kubernetes metadata fields (in order): the pod annotation resource.opentelemetry.io/service.name, the pod label app.kubernetes.io/name, the pod’s name, or the container’s name.
  • service_namespace set by resource.opentelemetry.io/service.namespace pod annotation.
  • deployment_environment set by resource.opentelemetry.io/deployment.environment pod annotation.
  • deployment_environment_name set by resource.opentelemetry.io/deployment.environment.name pod annotation.

If not set, Loki will attempt to set a default value according to these rules: https://grafana.com/docs/loki/latest/get-started/labels/#default-labels-for-all-users

@rlankfo rlankfo requested a review from petewall as a code owner January 28, 2025 02:44
Copy link
Collaborator

@petewall petewall left a comment

Choose a reason for hiding this comment

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

LGTM. Run make build inside charts/k8s-monitoring to update the generated files.

@rlankfo rlankfo requested a review from a team as a code owner January 29, 2025 01:46
@rlankfo rlankfo changed the title add podLogs to otlp-gateway test set service_name label for podLogs Jan 29, 2025
@rlankfo rlankfo marked this pull request as draft January 29, 2025 01:49
@rlankfo rlankfo changed the title set service_name label for podLogs WIP: set service_name label for podLogs Jan 29, 2025
@rlankfo rlankfo force-pushed the rlankfo/otlp-gateway-logs-service-name branch from 2f134cb to 6dc7369 Compare January 29, 2025 22:08
@rlankfo rlankfo self-assigned this Jan 29, 2025
@rlankfo rlankfo force-pushed the rlankfo/otlp-gateway-logs-service-name branch 3 times, most recently from de5ea21 to f248799 Compare January 30, 2025 23:30
@rlankfo rlankfo marked this pull request as ready for review January 30, 2025 23:32
Comment on lines +64 to +84
// explicitly set service_name. if not set, loki will automatically try to populate a default.
// see https://grafana.com/docs/loki/latest/get-started/labels/#default-labels-for-all-users
//
// choose the first value found from the following ordered list:
// - pod.annotation[resource.opentelemetry.io/service.name]
// - pod.label[app.kubernetes.io/name]
// - k8s.pod.name
// - k8s.container.name
rule {
action = "replace"
source_labels = [
"__meta_kubernetes_pod_annotation_resource_opentelemetry_io_service_name",
"__meta_kubernetes_pod_label_app_kubernetes_io_name",
"__meta_kubernetes_pod_name",
"__meta_kubernetes_pod_container_name",
]
separator = ";"
regex = "^(?:;*)?([^;]+).*$"
replacement = "$1"
target_label = "service_name"
}
Copy link
Member Author

Choose a reason for hiding this comment

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

@cyrille-leclerc do you mind taking a look at this?

I can't include other values used by the opentelemetry operator (statefulset, deployment, etc.) because they are not currently supported by prometheus kubernetes service discovery.

@rlankfo rlankfo changed the title WIP: set service_name label for podLogs set service_name label for podLogs Jan 30, 2025
@rlankfo rlankfo force-pushed the rlankfo/otlp-gateway-logs-service-name branch from f248799 to 310a260 Compare January 30, 2025 23:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants