@@ -33,6 +33,10 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
3333 public let id : ID
3434 /// Logger used by Server
3535 let logger : Logger
36+ #if DistributedTracingSupport
37+ @usableFromInline
38+ let tracer : ( any Tracer ) ?
39+ #endif
3640 @usableFromInline
3741 let channel : any Channel
3842 @usableFromInline
@@ -57,6 +61,9 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
5761 self . configuration = configuration
5862 self . id = connectionID
5963 self . logger = logger
64+ #if DistributedTracingSupport
65+ self . tracer = configuration. tracing. tracer
66+ #endif
6067 switch address? . value {
6168 case let . hostname( host, port) :
6269 self . address = ( host, port)
@@ -169,12 +176,11 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
169176 @inlinable
170177 func _execute< Command: ValkeyCommand > ( command: Command ) async throws -> RESPToken {
171178 #if DistributedTracingSupport
172- let span = startSpan ( Command . name, ofKind: . client)
173- defer { span. end ( ) }
179+ let span = self . tracer ? . startSpan ( Command . name, ofKind: . client)
180+ defer { span? . end ( ) }
174181
175- span. updateAttributes { attributes in
176- attributes [ " db.operation.name " ] = Command . name
177- applyCommonAttributes ( to: & attributes)
182+ span? . updateAttributes { attributes in
183+ self . applyCommonAttributes ( to: & attributes, commandName: Command . name)
178184 }
179185 #endif
180186
@@ -193,21 +199,21 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
193199 }
194200 } catch let error as ValkeyClientError {
195201 #if DistributedTracingSupport
196- span. recordError ( error)
202+ span? . recordError ( error)
197203 if let message = error. message {
198204 var prefixEndIndex = message. startIndex
199205 while prefixEndIndex < message. endIndex, message [ prefixEndIndex] != " " {
200206 message. formIndex ( after: & prefixEndIndex)
201207 }
202208 let prefix = message [ message. startIndex..< prefixEndIndex]
203- span. attributes [ " db.response.status_code " ] = " \( prefix) "
204- span. setStatus ( SpanStatus ( code: . error) )
209+ span? . attributes [ " db.response.status_code " ] = " \( prefix) "
210+ span? . setStatus ( SpanStatus ( code: . error) )
205211 }
206212 #endif
207213 throw error
208214 } catch {
209215 #if DistributedTracingSupport
210- span. recordError ( error)
216+ span? . recordError ( error)
211217 #endif
212218 throw error
213219 }
@@ -224,41 +230,7 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
224230 public func execute< each Command : ValkeyCommand > (
225231 _ commands: repeat each Command
226232 ) async -> sending ( repeat Result < ( each Command ) . Response, Error > ) {
227- #if DistributedTracingSupport
228- let span = startSpan ( " MULTI " , ofKind: . client)
229- defer { span. end ( ) }
230-
231- // We want to suffix the `db.operation.name` if all pipelined commands are of the same type.
232- var commandName : String ?
233- var operationNameSuffix : String ?
234- var commandCount = 0
235-
236- for command in repeat each commands {
237- commandCount += 1
238- if commandName == nil {
239- commandName = Swift . type ( of: command) . name
240- operationNameSuffix = commandName
241- } else if commandName != Swift . type ( of: command) . name {
242- // We should only add a suffix if all commands in the transaction are the same.
243- operationNameSuffix = nil
244- }
245- }
246- let operationName = operationNameSuffix. map { " MULTI \( $0) " } ?? " MULTI "
247-
248- span. updateAttributes { attributes in
249- attributes [ " db.operation.name " ] = operationName
250- attributes [ " db.operation.batch.size " ] = commandCount > 1 ? commandCount : nil
251- applyCommonAttributes ( to: & attributes)
252- }
253- #endif
254-
255233 func convert< Response: RESPTokenDecodable > ( _ result: Result < RESPToken , Error > , to: Response . Type ) -> Result < Response , Error > {
256- #if DistributedTracingSupport
257- if case . failure( let error) = result {
258- span. recordError ( error)
259- }
260- #endif
261-
262234 return result. flatMap {
263235 do {
264236 return try . success( Response ( fromRESP: $0) )
@@ -295,12 +267,13 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
295267 }
296268
297269 @usableFromInline
298- func applyCommonAttributes( to attributes: inout SpanAttributes ) {
299- attributes [ " db.system.name " ] = " valkey "
300- attributes [ " network.peer.address " ] = channel. remoteAddress? . ipAddress
301- attributes [ " network.peer.port " ] = channel. remoteAddress? . port
302- attributes [ " server.address " ] = address? . hostOrSocketPath
303- attributes [ " server.port " ] = address? . port == 6379 ? nil : address? . port
270+ func applyCommonAttributes( to attributes: inout SpanAttributes , commandName: String ) {
271+ attributes [ self . configuration. tracing. attributeNames. databaseOperationName] = commandName
272+ attributes [ self . configuration. tracing. attributeNames. databaseSystemName] = self . configuration. tracing. attributeValue. databaseSystem
273+ attributes [ self . configuration. tracing. attributeNames. networkPeerAddress] = channel. remoteAddress? . ipAddress
274+ attributes [ self . configuration. tracing. attributeNames. networkPeerPort] = channel. remoteAddress? . port
275+ attributes [ self . configuration. tracing. attributeNames. serverAddress] = address? . hostOrSocketPath
276+ attributes [ self . configuration. tracing. attributeNames. serverPort] = address? . port == 6379 ? nil : address? . port
304277 }
305278
306279 @usableFromInline
0 commit comments