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

Add k8s service discovery resource attributes #9416

Merged
merged 2 commits into from
Apr 29, 2022

Conversation

dashpole
Copy link
Contributor

@dashpole dashpole commented Apr 23, 2022

Description:

I've found myself writing relabel rules to take labels from prometheus service discovery and then wondering how to convert from metric labels to resource labels. In GKE, we even wrote a custom processor to do this for us. I realized the other day that we actually have all of these labels available to us in the prometheus receiver.

This PR converts some of the service discovery __meta labels from prometheus' kubernetes service discovery into otel semantic conventions. This could be extended to include other service discovery implementations (e.g. gce, aws, azure, docker swarm, etc.), or we could even add configuration to allow users to define their own map for prom service discovery labels to prometheus resource attributes.

This is most useful when specifying - role: pod (Used by the PodMonitor prom-operator resource) or - role: node in kubernetes_sd_configs, as OpenTelemetry k8s attributes map well to the set of prometheus service discovery labels. In particular, - role: endpoints (used by ServiceMonitor resource) and -role: service (used by Probe resource) doesn't add any particularly useful attributes.

Example

With this collector config:

receivers:
  prometheus:
    config:
      scrape_configs:
      - job_name: 'kubernetes-pods'
        kubernetes_sd_configs:
        - role: pod
        relabel_configs:
        - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
          action: keep
          regex: true
        - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
          action: replace
          target_label: __metrics_path__
          regex: (.+)
        - source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
          action: replace
          regex: (.+):(?:\d+);(\d+)
          replacement: $$1:$$2
          target_label: __address__
exporters:
  logging:
    logLevel: debug
service:
  pipelines:
    metrics:
      receivers: [prometheus]
      exporters: [logging]

You get the following resource labels in the output:

DEBUG	loggingexporter/logging_exporter.go:66	ResourceMetrics #0
Resource SchemaURL:
Resource labels:
     -> service.name: STRING(kubernetes-pods)
     -> net.host.name: STRING(xx.xx.x.x)
     -> service.instance.id: STRING(xx.xx.x.x:80)
     -> net.host.port: STRING(80)
     -> http.scheme: STRING(http)
     -> k8s.pod.name: STRING(kube-dns-697dc8fc8b-zl95s)
     -> k8s.pod.uid: STRING(adc8575a-2d85-49bb-907b-16626182ec3d)
     -> k8s.container.name: STRING(prometheus-to-sd)
     -> k8s.node.name: STRING(gke-dashpole-test-default-pool-61f071a2-1re0)
     -> k8s.replicaset.name: STRING(kube-dns-697dc8fc8b)
ScopeMetrics #0
ScopeMetrics SchemaURL:
InstrumentationScope
Metric #0
Descriptor:
     -> Name: up
     -> Description: The scraping was successful
     -> Unit:
     -> DataType: Gauge

@dashpole dashpole force-pushed the kubernetes_resource_discovery branch 2 times, most recently from 126b9c2 to c181f1b Compare April 24, 2022 17:45
@dashpole dashpole changed the title WIP Add k8s service discovery resource attributes Add k8s service discovery resource attributes Apr 24, 2022
@dashpole dashpole marked this pull request as ready for review April 24, 2022 17:51
@dashpole dashpole requested a review from a team April 24, 2022 17:51
@dashpole
Copy link
Contributor Author

cc @open-telemetry/wg-prometheus

@dashpole dashpole added the comp:prometheus Prometheus related issues label Apr 25, 2022
@jpkrohling
Copy link
Member

Changelog conflict resolved.

@dashpole dashpole force-pushed the kubernetes_resource_discovery branch from 88ccd34 to c85830d Compare April 28, 2022 14:04
@codeboten codeboten merged commit 814c3a9 into open-telemetry:main Apr 29, 2022
@dashpole dashpole deleted the kubernetes_resource_discovery branch April 29, 2022 19:19
djaglowski pushed a commit to djaglowski/opentelemetry-collector-contrib that referenced this pull request May 10, 2022
* Add k8s service discovery resource attributes

* address feedback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:prometheus Prometheus related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants