istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/networking/telemetry/telemetry.go (about) 1 // Copyright Istio 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 telemetry 16 17 import ( 18 "strconv" 19 "strings" 20 21 "istio.io/istio/pilot/pkg/model" 22 "istio.io/istio/pilot/pkg/networking/util" 23 "istio.io/istio/pilot/pkg/serviceregistry/provider" 24 "istio.io/istio/pkg/config/host" 25 ) 26 27 var ( 28 // StatName patterns 29 serviceStatPattern = "%SERVICE%" 30 serviceFQDNStatPattern = "%SERVICE_FQDN%" 31 servicePortStatPattern = "%SERVICE_PORT%" 32 serviceTargetPortStatPattern = "%TARGET_PORT%" 33 servicePortNameStatPattern = "%SERVICE_PORT_NAME%" 34 subsetNameStatPattern = "%SUBSET_NAME%" 35 ) 36 37 // BuildStatPrefix builds a stat prefix based on the stat pattern. 38 func BuildStatPrefix(statPattern string, host string, subset string, port *model.Port, targetPort int, attributes *model.ServiceAttributes) string { 39 prefix := strings.ReplaceAll(statPattern, serviceStatPattern, shortHostName(host, attributes)) 40 prefix = strings.ReplaceAll(prefix, serviceFQDNStatPattern, host) 41 prefix = strings.ReplaceAll(prefix, subsetNameStatPattern, subset) 42 prefix = strings.ReplaceAll(prefix, serviceTargetPortStatPattern, strconv.Itoa(targetPort)) 43 prefix = strings.ReplaceAll(prefix, servicePortStatPattern, strconv.Itoa(port.Port)) 44 prefix = strings.ReplaceAll(prefix, servicePortNameStatPattern, port.Name) 45 return prefix 46 } 47 48 // BuildInboundStatPrefix builds a stat prefix based on the stat pattern and filter chain telemetry data. 49 func BuildInboundStatPrefix(statPattern string, tm FilterChainMetadata, subset string, port uint32, portName string) string { 50 prefix := strings.ReplaceAll(statPattern, serviceStatPattern, tm.ShortHostname()) 51 prefix = strings.ReplaceAll(prefix, serviceFQDNStatPattern, tm.InstanceHostname.String()) 52 prefix = strings.ReplaceAll(prefix, subsetNameStatPattern, subset) 53 prefix = strings.ReplaceAll(prefix, servicePortStatPattern, strconv.Itoa(int(port))) 54 prefix = strings.ReplaceAll(prefix, servicePortNameStatPattern, portName) 55 return prefix 56 } 57 58 // shortHostName constructs the name from kubernetes hosts based on attributes (name and namespace). 59 // For other hosts like VMs, this method does not do any thing - just returns the passed in host as is. 60 func shortHostName(host string, attributes *model.ServiceAttributes) string { 61 if attributes.ServiceRegistry == provider.Kubernetes { 62 return attributes.Name + "." + attributes.Namespace 63 } 64 return host 65 } 66 67 // TraceOperation builds the string format: "%s:%d/*" for a given host and port 68 func TraceOperation(host string, port int) string { 69 // Format : "%s:%d/*" 70 return util.DomainName(host, port) + "/*" 71 } 72 73 // FilterChainMetadata defines additional metadata for telemetry use for a filter chain. 74 type FilterChainMetadata struct { 75 // InstanceHostname defines the hostname of the service this filter chain is built for. 76 // Note: This is best effort; this may be empty if generated by Sidecar config, and there may be multiple 77 // Services that make up the filter chain. 78 InstanceHostname host.Name 79 // KubernetesServiceNamespace is the namespace the service is defined in, if it is for a Kubernetes Service. 80 // Note: This is best effort; this may be empty if generated by Sidecar config, and there may be multiple 81 // Services that make up the filter chain. 82 KubernetesServiceNamespace string 83 // KubernetesServiceName is the name of service, if it is for a Kubernetes Service. 84 // Note: This is best effort; this may be empty if generated by Sidecar config, and there may be multiple 85 // Services that make up the filter chain. 86 KubernetesServiceName string 87 } 88 89 // ShortHostname constructs the name from kubernetes service name if available or just uses instance host name. 90 func (tm FilterChainMetadata) ShortHostname() string { 91 if tm.KubernetesServiceName != "" { 92 return tm.KubernetesServiceName + "." + tm.KubernetesServiceNamespace 93 } 94 return tm.InstanceHostname.String() 95 }