Path: blob/main/component/otelcol/receiver/prometheus/internal/prom_to_otlp.go
5460 views
// Copyright The OpenTelemetry Authors1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314package internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver/internal"1516import (17"net"1819"github.com/prometheus/common/model"20"github.com/prometheus/prometheus/model/labels"21"go.opentelemetry.io/collector/pdata/pcommon"22conventions "go.opentelemetry.io/collector/semconv/v1.6.1"23)2425// isDiscernibleHost checks if a host can be used as a value for the 'host.name' key.26// localhost-like hosts and unspecified (0.0.0.0) hosts are not discernible.27func isDiscernibleHost(host string) bool {28ip := net.ParseIP(host)29if ip != nil {30// An IP is discernible if31// - it's not local (e.g. belongs to 127.0.0.0/8 or ::1/128) and32// - it's not unspecified (e.g. the 0.0.0.0 address).33return !ip.IsLoopback() && !ip.IsUnspecified()34}3536if host == "localhost" {37return false38}3940// not an IP, not 'localhost', assume it is discernible.41return true42}4344// CreateResource creates the resource data added to OTLP payloads.45func CreateResource(job, instance string, serviceDiscoveryLabels labels.Labels) pcommon.Resource {46host, port, err := net.SplitHostPort(instance)47if err != nil {48host = instance49}50resource := pcommon.NewResource()51attrs := resource.Attributes()52attrs.PutStr(conventions.AttributeServiceName, job)53if isDiscernibleHost(host) {54attrs.PutStr(conventions.AttributeNetHostName, host)55}56attrs.PutStr(conventions.AttributeServiceInstanceID, instance)57attrs.PutStr(conventions.AttributeNetHostPort, port)58attrs.PutStr(conventions.AttributeHTTPScheme, serviceDiscoveryLabels.Get(model.SchemeLabel))5960addKubernetesResource(attrs, serviceDiscoveryLabels)6162return resource63}6465// kubernetesDiscoveryToResourceAttributes maps from metadata labels discovered66// through the kubernetes implementation of service discovery to opentelemetry67// resource attribute keys.68var kubernetesDiscoveryToResourceAttributes = map[string]string{69"__meta_kubernetes_pod_name": conventions.AttributeK8SPodName,70"__meta_kubernetes_pod_uid": conventions.AttributeK8SPodUID,71"__meta_kubernetes_pod_container_name": conventions.AttributeK8SContainerName,72"__meta_kubernetes_namespace": conventions.AttributeK8SNamespaceName,73// Only one of the node name service discovery labels will be present74"__meta_kubernetes_pod_node_name": conventions.AttributeK8SNodeName,75"__meta_kubernetes_node_name": conventions.AttributeK8SNodeName,76"__meta_kubernetes_endpoint_node_name": conventions.AttributeK8SNodeName,77}7879// addKubernetesResource adds resource information detected by prometheus'80// kubernetes service discovery.81func addKubernetesResource(attrs pcommon.Map, serviceDiscoveryLabels labels.Labels) {82for sdKey, attributeKey := range kubernetesDiscoveryToResourceAttributes {83if attr := serviceDiscoveryLabels.Get(sdKey); attr != "" {84attrs.PutStr(attributeKey, attr)85}86}87controllerName := serviceDiscoveryLabels.Get("__meta_kubernetes_pod_controller_name")88controllerKind := serviceDiscoveryLabels.Get("__meta_kubernetes_pod_controller_kind")89if controllerKind != "" && controllerName != "" {90switch controllerKind {91case "ReplicaSet":92attrs.PutStr(conventions.AttributeK8SReplicaSetName, controllerName)93case "DaemonSet":94attrs.PutStr(conventions.AttributeK8SDaemonSetName, controllerName)95case "StatefulSet":96attrs.PutStr(conventions.AttributeK8SStatefulSetName, controllerName)97case "Job":98attrs.PutStr(conventions.AttributeK8SJobName, controllerName)99case "CronJob":100attrs.PutStr(conventions.AttributeK8SCronJobName, controllerName)101}102}103}104105106