prom_to_otlp.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver/internal"
  4. import (
  5. "net"
  6. "github.com/prometheus/common/model"
  7. "github.com/prometheus/prometheus/model/labels"
  8. "go.opentelemetry.io/collector/pdata/pcommon"
  9. conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
  10. )
  11. // isDiscernibleHost checks if a host can be used as a value for the 'host.name' key.
  12. // localhost-like hosts and unspecified (0.0.0.0) hosts are not discernible.
  13. func isDiscernibleHost(host string) bool {
  14. ip := net.ParseIP(host)
  15. if ip != nil {
  16. // An IP is discernible if
  17. // - it's not local (e.g. belongs to 127.0.0.0/8 or ::1/128) and
  18. // - it's not unspecified (e.g. the 0.0.0.0 address).
  19. return !ip.IsLoopback() && !ip.IsUnspecified()
  20. }
  21. if host == "localhost" {
  22. return false
  23. }
  24. // not an IP, not 'localhost', assume it is discernible.
  25. return true
  26. }
  27. // CreateResource creates the resource data added to OTLP payloads.
  28. func CreateResource(job, instance string, serviceDiscoveryLabels labels.Labels) pcommon.Resource {
  29. host, port, err := net.SplitHostPort(instance)
  30. if err != nil {
  31. host = instance
  32. }
  33. resource := pcommon.NewResource()
  34. attrs := resource.Attributes()
  35. attrs.PutStr(conventions.AttributeServiceName, job)
  36. if isDiscernibleHost(host) {
  37. attrs.PutStr(conventions.AttributeNetHostName, host)
  38. }
  39. attrs.PutStr(conventions.AttributeServiceInstanceID, instance)
  40. attrs.PutStr(conventions.AttributeNetHostPort, port)
  41. attrs.PutStr(conventions.AttributeHTTPScheme, serviceDiscoveryLabels.Get(model.SchemeLabel))
  42. addKubernetesResource(attrs, serviceDiscoveryLabels)
  43. return resource
  44. }
  45. // kubernetesDiscoveryToResourceAttributes maps from metadata labels discovered
  46. // through the kubernetes implementation of service discovery to opentelemetry
  47. // resource attribute keys.
  48. var kubernetesDiscoveryToResourceAttributes = map[string]string{
  49. "__meta_kubernetes_pod_name": conventions.AttributeK8SPodName,
  50. "__meta_kubernetes_pod_uid": conventions.AttributeK8SPodUID,
  51. "__meta_kubernetes_pod_container_name": conventions.AttributeK8SContainerName,
  52. "__meta_kubernetes_namespace": conventions.AttributeK8SNamespaceName,
  53. // Only one of the node name service discovery labels will be present
  54. "__meta_kubernetes_pod_node_name": conventions.AttributeK8SNodeName,
  55. "__meta_kubernetes_node_name": conventions.AttributeK8SNodeName,
  56. "__meta_kubernetes_endpoint_node_name": conventions.AttributeK8SNodeName,
  57. }
  58. // addKubernetesResource adds resource information detected by prometheus'
  59. // kubernetes service discovery.
  60. func addKubernetesResource(attrs pcommon.Map, serviceDiscoveryLabels labels.Labels) {
  61. for sdKey, attributeKey := range kubernetesDiscoveryToResourceAttributes {
  62. if attr := serviceDiscoveryLabels.Get(sdKey); attr != "" {
  63. attrs.PutStr(attributeKey, attr)
  64. }
  65. }
  66. controllerName := serviceDiscoveryLabels.Get("__meta_kubernetes_pod_controller_name")
  67. controllerKind := serviceDiscoveryLabels.Get("__meta_kubernetes_pod_controller_kind")
  68. if controllerKind != "" && controllerName != "" {
  69. switch controllerKind {
  70. case "ReplicaSet":
  71. attrs.PutStr(conventions.AttributeK8SReplicaSetName, controllerName)
  72. case "DaemonSet":
  73. attrs.PutStr(conventions.AttributeK8SDaemonSetName, controllerName)
  74. case "StatefulSet":
  75. attrs.PutStr(conventions.AttributeK8SStatefulSetName, controllerName)
  76. case "Job":
  77. attrs.PutStr(conventions.AttributeK8SJobName, controllerName)
  78. case "CronJob":
  79. attrs.PutStr(conventions.AttributeK8SCronJobName, controllerName)
  80. }
  81. }
  82. }