Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/otelcol/receiver/prometheus/internal/prom_to_otlp.go
5460 views
1
// Copyright The OpenTelemetry Authors
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
package internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver/internal"
16
17
import (
18
"net"
19
20
"github.com/prometheus/common/model"
21
"github.com/prometheus/prometheus/model/labels"
22
"go.opentelemetry.io/collector/pdata/pcommon"
23
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
24
)
25
26
// isDiscernibleHost checks if a host can be used as a value for the 'host.name' key.
27
// localhost-like hosts and unspecified (0.0.0.0) hosts are not discernible.
28
func isDiscernibleHost(host string) bool {
29
ip := net.ParseIP(host)
30
if ip != nil {
31
// An IP is discernible if
32
// - it's not local (e.g. belongs to 127.0.0.0/8 or ::1/128) and
33
// - it's not unspecified (e.g. the 0.0.0.0 address).
34
return !ip.IsLoopback() && !ip.IsUnspecified()
35
}
36
37
if host == "localhost" {
38
return false
39
}
40
41
// not an IP, not 'localhost', assume it is discernible.
42
return true
43
}
44
45
// CreateResource creates the resource data added to OTLP payloads.
46
func CreateResource(job, instance string, serviceDiscoveryLabels labels.Labels) pcommon.Resource {
47
host, port, err := net.SplitHostPort(instance)
48
if err != nil {
49
host = instance
50
}
51
resource := pcommon.NewResource()
52
attrs := resource.Attributes()
53
attrs.PutStr(conventions.AttributeServiceName, job)
54
if isDiscernibleHost(host) {
55
attrs.PutStr(conventions.AttributeNetHostName, host)
56
}
57
attrs.PutStr(conventions.AttributeServiceInstanceID, instance)
58
attrs.PutStr(conventions.AttributeNetHostPort, port)
59
attrs.PutStr(conventions.AttributeHTTPScheme, serviceDiscoveryLabels.Get(model.SchemeLabel))
60
61
addKubernetesResource(attrs, serviceDiscoveryLabels)
62
63
return resource
64
}
65
66
// kubernetesDiscoveryToResourceAttributes maps from metadata labels discovered
67
// through the kubernetes implementation of service discovery to opentelemetry
68
// resource attribute keys.
69
var kubernetesDiscoveryToResourceAttributes = map[string]string{
70
"__meta_kubernetes_pod_name": conventions.AttributeK8SPodName,
71
"__meta_kubernetes_pod_uid": conventions.AttributeK8SPodUID,
72
"__meta_kubernetes_pod_container_name": conventions.AttributeK8SContainerName,
73
"__meta_kubernetes_namespace": conventions.AttributeK8SNamespaceName,
74
// Only one of the node name service discovery labels will be present
75
"__meta_kubernetes_pod_node_name": conventions.AttributeK8SNodeName,
76
"__meta_kubernetes_node_name": conventions.AttributeK8SNodeName,
77
"__meta_kubernetes_endpoint_node_name": conventions.AttributeK8SNodeName,
78
}
79
80
// addKubernetesResource adds resource information detected by prometheus'
81
// kubernetes service discovery.
82
func addKubernetesResource(attrs pcommon.Map, serviceDiscoveryLabels labels.Labels) {
83
for sdKey, attributeKey := range kubernetesDiscoveryToResourceAttributes {
84
if attr := serviceDiscoveryLabels.Get(sdKey); attr != "" {
85
attrs.PutStr(attributeKey, attr)
86
}
87
}
88
controllerName := serviceDiscoveryLabels.Get("__meta_kubernetes_pod_controller_name")
89
controllerKind := serviceDiscoveryLabels.Get("__meta_kubernetes_pod_controller_kind")
90
if controllerKind != "" && controllerName != "" {
91
switch controllerKind {
92
case "ReplicaSet":
93
attrs.PutStr(conventions.AttributeK8SReplicaSetName, controllerName)
94
case "DaemonSet":
95
attrs.PutStr(conventions.AttributeK8SDaemonSetName, controllerName)
96
case "StatefulSet":
97
attrs.PutStr(conventions.AttributeK8SStatefulSetName, controllerName)
98
case "Job":
99
attrs.PutStr(conventions.AttributeK8SJobName, controllerName)
100
case "CronJob":
101
attrs.PutStr(conventions.AttributeK8SCronJobName, controllerName)
102
}
103
}
104
}
105
106