@@ -39,6 +39,10 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
3939 public let id : ID
4040 /// Logger used by Server
4141 let logger : Logger
42+ #if DistributedTracingSupport
43+ @usableFromInline
44+ let tracer : ( any Tracer ) ?
45+ #endif
4246 @usableFromInline
4347 let channel : any Channel
4448 @usableFromInline
@@ -63,6 +67,9 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
6367 self . configuration = configuration
6468 self . id = connectionID
6569 self . logger = logger
70+ #if DistributedTracingSupport
71+ self . tracer = configuration. tracing. tracer
72+ #endif
6673 switch address? . value {
6774 case let . hostname( host, port) :
6875 self . address = ( host, port)
@@ -177,12 +184,11 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
177184 @inlinable
178185 func _execute< Command: ValkeyCommand > ( command: Command ) async throws -> RESPToken {
179186 #if DistributedTracingSupport
180- let span = startSpan ( Command . name, ofKind: . client)
181- defer { span. end ( ) }
187+ let span = self . tracer ? . startSpan ( Command . name, ofKind: . client)
188+ defer { span? . end ( ) }
182189
183- span. updateAttributes { attributes in
184- attributes [ " db.operation.name " ] = Command . name
185- applyCommonAttributes ( to: & attributes)
190+ span? . updateAttributes { attributes in
191+ self . applyCommonAttributes ( to: & attributes, commandName: Command . name)
186192 }
187193 #endif
188194
@@ -201,21 +207,21 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
201207 }
202208 } catch let error as ValkeyClientError {
203209 #if DistributedTracingSupport
204- span. recordError ( error)
210+ span? . recordError ( error)
205211 if let message = error. message {
206212 var prefixEndIndex = message. startIndex
207213 while prefixEndIndex < message. endIndex, message [ prefixEndIndex] != " " {
208214 message. formIndex ( after: & prefixEndIndex)
209215 }
210216 let prefix = message [ message. startIndex..< prefixEndIndex]
211- span. attributes [ " db.response.status_code " ] = " \( prefix) "
212- span. setStatus ( SpanStatus ( code: . error) )
217+ span? . attributes [ " db.response.status_code " ] = " \( prefix) "
218+ span? . setStatus ( SpanStatus ( code: . error) )
213219 }
214220 #endif
215221 throw error
216222 } catch {
217223 #if DistributedTracingSupport
218- span. recordError ( error)
224+ span? . recordError ( error)
219225 #endif
220226 throw error
221227 }
@@ -236,41 +242,7 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
236242 public func execute< each Command : ValkeyCommand > (
237243 _ commands: repeat each Command
238244 ) async -> sending ( repeat Result < ( each Command ) . Response, Error > ) {
239- #if DistributedTracingSupport
240- let span = startSpan ( " MULTI " , ofKind: . client)
241- defer { span. end ( ) }
242-
243- // We want to suffix the `db.operation.name` if all pipelined commands are of the same type.
244- var commandName : String ?
245- var operationNameSuffix : String ?
246- var commandCount = 0
247-
248- for command in repeat each commands {
249- commandCount += 1
250- if commandName == nil {
251- commandName = Swift . type ( of: command) . name
252- operationNameSuffix = commandName
253- } else if commandName != Swift . type ( of: command) . name {
254- // We should only add a suffix if all commands in the transaction are the same.
255- operationNameSuffix = nil
256- }
257- }
258- let operationName = operationNameSuffix. map { " MULTI \( $0) " } ?? " MULTI "
259-
260- span. updateAttributes { attributes in
261- attributes [ " db.operation.name " ] = operationName
262- attributes [ " db.operation.batch.size " ] = commandCount > 1 ? commandCount : nil
263- applyCommonAttributes ( to: & attributes)
264- }
265- #endif
266-
267245 func convert< Response: RESPTokenDecodable > ( _ result: Result < RESPToken , Error > , to: Response . Type ) -> Result < Response , Error > {
268- #if DistributedTracingSupport
269- if case . failure( let error) = result {
270- span. recordError ( error)
271- }
272- #endif
273-
274246 return result. flatMap {
275247 do {
276248 return try . success( Response ( fromRESP: $0) )
@@ -307,12 +279,13 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
307279 }
308280
309281 @usableFromInline
310- func applyCommonAttributes( to attributes: inout SpanAttributes ) {
311- attributes [ " db.system.name " ] = " valkey "
312- attributes [ " network.peer.address " ] = channel. remoteAddress? . ipAddress
313- attributes [ " network.peer.port " ] = channel. remoteAddress? . port
314- attributes [ " server.address " ] = address? . hostOrSocketPath
315- attributes [ " server.port " ] = address? . port == 6379 ? nil : address? . port
282+ func applyCommonAttributes( to attributes: inout SpanAttributes , commandName: String ) {
283+ attributes [ self . configuration. tracing. attributeNames. databaseOperationName] = commandName
284+ attributes [ self . configuration. tracing. attributeNames. databaseSystemName] = self . configuration. tracing. attributeValue. databaseSystem
285+ attributes [ self . configuration. tracing. attributeNames. networkPeerAddress] = channel. remoteAddress? . ipAddress
286+ attributes [ self . configuration. tracing. attributeNames. networkPeerPort] = channel. remoteAddress? . port
287+ attributes [ self . configuration. tracing. attributeNames. serverAddress] = address? . hostOrSocketPath
288+ attributes [ self . configuration. tracing. attributeNames. serverPort] = address? . port == 6379 ? nil : address? . port
316289 }
317290
318291 @usableFromInline
0 commit comments