@@ -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)
@@ -175,12 +182,11 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
175182 @inlinable
176183 func _execute< Command: ValkeyCommand > ( command: Command ) async throws -> RESPToken {
177184 #if DistributedTracingSupport
178- let span = startSpan ( Command . name, ofKind: . client)
179- defer { span. end ( ) }
185+ let span = self . tracer ? . startSpan ( Command . name, ofKind: . client)
186+ defer { span? . end ( ) }
180187
181- span. updateAttributes { attributes in
182- attributes [ " db.operation.name " ] = Command . name
183- applyCommonAttributes ( to: & attributes)
188+ span? . updateAttributes { attributes in
189+ self . applyCommonAttributes ( to: & attributes, commandName: Command . name)
184190 }
185191 #endif
186192
@@ -199,21 +205,21 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
199205 }
200206 } catch let error as ValkeyClientError {
201207 #if DistributedTracingSupport
202- span. recordError ( error)
208+ span? . recordError ( error)
203209 if let message = error. message {
204210 var prefixEndIndex = message. startIndex
205211 while prefixEndIndex < message. endIndex, message [ prefixEndIndex] != " " {
206212 message. formIndex ( after: & prefixEndIndex)
207213 }
208214 let prefix = message [ message. startIndex..< prefixEndIndex]
209- span. attributes [ " db.response.status_code " ] = " \( prefix) "
210- span. setStatus ( SpanStatus ( code: . error) )
215+ span? . attributes [ " db.response.status_code " ] = " \( prefix) "
216+ span? . setStatus ( SpanStatus ( code: . error) )
211217 }
212218 #endif
213219 throw error
214220 } catch {
215221 #if DistributedTracingSupport
216- span. recordError ( error)
222+ span? . recordError ( error)
217223 #endif
218224 throw error
219225 }
@@ -230,41 +236,7 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
230236 public func execute< each Command : ValkeyCommand > (
231237 _ commands: repeat each Command
232238 ) async -> sending ( repeat Result < ( each Command ) . Response, Error > ) {
233- #if DistributedTracingSupport
234- let span = startSpan ( " MULTI " , ofKind: . client)
235- defer { span. end ( ) }
236-
237- // We want to suffix the `db.operation.name` if all pipelined commands are of the same type.
238- var commandName : String ?
239- var operationNameSuffix : String ?
240- var commandCount = 0
241-
242- for command in repeat each commands {
243- commandCount += 1
244- if commandName == nil {
245- commandName = Swift . type ( of: command) . name
246- operationNameSuffix = commandName
247- } else if commandName != Swift . type ( of: command) . name {
248- // We should only add a suffix if all commands in the transaction are the same.
249- operationNameSuffix = nil
250- }
251- }
252- let operationName = operationNameSuffix. map { " MULTI \( $0) " } ?? " MULTI "
253-
254- span. updateAttributes { attributes in
255- attributes [ " db.operation.name " ] = operationName
256- attributes [ " db.operation.batch.size " ] = commandCount > 1 ? commandCount : nil
257- applyCommonAttributes ( to: & attributes)
258- }
259- #endif
260-
261239 func convert< Response: RESPTokenDecodable > ( _ result: Result < RESPToken , Error > , to: Response . Type ) -> Result < Response , Error > {
262- #if DistributedTracingSupport
263- if case . failure( let error) = result {
264- span. recordError ( error)
265- }
266- #endif
267-
268240 return result. flatMap {
269241 do {
270242 return try . success( Response ( fromRESP: $0) )
@@ -301,12 +273,13 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
301273 }
302274
303275 @usableFromInline
304- func applyCommonAttributes( to attributes: inout SpanAttributes ) {
305- attributes [ " db.system.name " ] = " valkey "
306- attributes [ " network.peer.address " ] = channel. remoteAddress? . ipAddress
307- attributes [ " network.peer.port " ] = channel. remoteAddress? . port
308- attributes [ " server.address " ] = address? . hostOrSocketPath
309- attributes [ " server.port " ] = address? . port == 6379 ? nil : address? . port
276+ func applyCommonAttributes( to attributes: inout SpanAttributes , commandName: String ) {
277+ attributes [ self . configuration. tracing. attributeNames. databaseOperationName] = commandName
278+ attributes [ self . configuration. tracing. attributeNames. databaseSystemName] = self . configuration. tracing. attributeValue. databaseSystem
279+ attributes [ self . configuration. tracing. attributeNames. networkPeerAddress] = channel. remoteAddress? . ipAddress
280+ attributes [ self . configuration. tracing. attributeNames. networkPeerPort] = channel. remoteAddress? . port
281+ attributes [ self . configuration. tracing. attributeNames. serverAddress] = address? . hostOrSocketPath
282+ attributes [ self . configuration. tracing. attributeNames. serverPort] = address? . port == 6379 ? nil : address? . port
310283 }
311284
312285 @usableFromInline
0 commit comments