|
| 1 | +/* |
| 2 | + * Copyright 2025, gRPC Authors All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +internal import GRPCCore |
| 18 | +internal import Tracing |
| 19 | + |
| 20 | +public enum GRPCTracingKeys { |
| 21 | + static let rpcSystem = "rpc.system" |
| 22 | + static let rpcMethod = "rpc.method" |
| 23 | + static let rpcService = "rpc.service" |
| 24 | + static let rpcMessageID = "rpc.message.id" |
| 25 | + static let rpcMessageType = "rpc.message.type" |
| 26 | + static let grpcStatusCode = "rpc.grpc.status_code" |
| 27 | + |
| 28 | + static let serverAddress = "server.address" |
| 29 | + static let serverPort = "server.port" |
| 30 | + |
| 31 | + static let clientAddress = "client.address" |
| 32 | + static let clientPort = "client.port" |
| 33 | + |
| 34 | + static let networkTransport = "network.transport" |
| 35 | + static let networkType = "network.type" |
| 36 | + static let networkPeerAddress = "network.peer.address" |
| 37 | + static let networkPeerPort = "network.peer.port" |
| 38 | +} |
| 39 | + |
| 40 | +extension Span { |
| 41 | + // See: https://opentelemetry.io/docs/specs/semconv/rpc/rpc-spans/ |
| 42 | + func setOTelClientSpanGRPCAttributes( |
| 43 | + context: ClientContext, |
| 44 | + serverHostname: String, |
| 45 | + networkTransportMethod: String |
| 46 | + ) { |
| 47 | + self.attributes[GRPCTracingKeys.rpcSystem] = "grpc" |
| 48 | + self.attributes[GRPCTracingKeys.serverAddress] = serverHostname |
| 49 | + self.attributes[GRPCTracingKeys.networkTransport] = networkTransportMethod |
| 50 | + self.attributes[GRPCTracingKeys.rpcService] = context.descriptor.service.fullyQualifiedService |
| 51 | + self.attributes[GRPCTracingKeys.rpcMethod] = context.descriptor.method |
| 52 | + |
| 53 | + // Set server address information |
| 54 | + switch PeerAddress(context.remotePeer) { |
| 55 | + case .ipv4(let address, let port): |
| 56 | + self.attributes[GRPCTracingKeys.networkType] = "ipv4" |
| 57 | + self.attributes[GRPCTracingKeys.networkPeerAddress] = address |
| 58 | + self.attributes[GRPCTracingKeys.networkPeerPort] = port |
| 59 | + self.attributes[GRPCTracingKeys.serverPort] = port |
| 60 | + |
| 61 | + case .ipv6(let address, let port): |
| 62 | + self.attributes[GRPCTracingKeys.networkType] = "ipv6" |
| 63 | + self.attributes[GRPCTracingKeys.networkPeerAddress] = address |
| 64 | + self.attributes[GRPCTracingKeys.networkPeerPort] = port |
| 65 | + self.attributes[GRPCTracingKeys.serverPort] = port |
| 66 | + |
| 67 | + case .unixDomainSocket(let path): |
| 68 | + self.attributes[GRPCTracingKeys.networkType] = "unix" |
| 69 | + self.attributes[GRPCTracingKeys.networkPeerAddress] = path |
| 70 | + |
| 71 | + case .other(let address): |
| 72 | + // We can't nicely format the span attributes to contain the appropriate information here, |
| 73 | + // so include the whole thing as part of the peer address. |
| 74 | + self.attributes[GRPCTracingKeys.networkPeerAddress] = address |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +package enum PeerAddress: Equatable { |
| 80 | + case ipv4(address: String, port: Int?) |
| 81 | + case ipv6(address: String, port: Int?) |
| 82 | + case unixDomainSocket(path: String) |
| 83 | + case other(String) |
| 84 | + |
| 85 | + package init(_ address: String) { |
| 86 | + // We expect this address to be of one of these formats: |
| 87 | + // - ipv4:<host>:<port> for ipv4 addresses |
| 88 | + // - ipv6:[<host>]:<port> for ipv6 addresses |
| 89 | + // - unix:<uds-pathname> for UNIX domain sockets |
| 90 | + let addressComponents = address.split(separator: ":", omittingEmptySubsequences: false) |
| 91 | + |
| 92 | + guard addressComponents.count > 1 else { |
| 93 | + // This is some unexpected/unknown format, so we have no way of splitting it up nicely. |
| 94 | + self = .other(address) |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + // Check what type the transport is... |
| 99 | + switch addressComponents[0] { |
| 100 | + case "ipv4": |
| 101 | + guard addressComponents.count == 3, let port = Int(addressComponents[2]) else { |
| 102 | + // This is some unexpected/unknown format, so we have no way of splitting it up nicely. |
| 103 | + self = .other(address) |
| 104 | + return |
| 105 | + } |
| 106 | + self = .ipv4(address: String(addressComponents[1]), port: port) |
| 107 | + |
| 108 | + case "ipv6": |
| 109 | + guard addressComponents.count > 2, let port = Int(addressComponents.last!) else { |
| 110 | + // This is some unexpected/unknown format, so we have no way of splitting it up nicely. |
| 111 | + self = .other(address) |
| 112 | + return |
| 113 | + } |
| 114 | + self = .ipv6( |
| 115 | + address: String( |
| 116 | + addressComponents[1 ..< addressComponents.count - 1].joined(separator: ":") |
| 117 | + ), |
| 118 | + port: port |
| 119 | + ) |
| 120 | + |
| 121 | + case "unix": |
| 122 | + guard addressComponents.count == 2 else { |
| 123 | + // This is some unexpected/unknown format, so we have no way of splitting it up nicely. |
| 124 | + self = .other(address) |
| 125 | + return |
| 126 | + } |
| 127 | + self = .unixDomainSocket(path: String(addressComponents[1])) |
| 128 | + |
| 129 | + default: |
| 130 | + // This is some unexpected/unknown format, so we have no way of splitting it up nicely. |
| 131 | + self = .other(address) |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments