@@ -39,6 +39,10 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
39
39
public let id : ID
40
40
/// Logger used by Server
41
41
let logger : Logger
42
+ #if DistributedTracingSupport
43
+ @usableFromInline
44
+ let tracer : ( any Tracer ) ?
45
+ #endif
42
46
@usableFromInline
43
47
let channel : any Channel
44
48
@usableFromInline
@@ -63,6 +67,9 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
63
67
self . configuration = configuration
64
68
self . id = connectionID
65
69
self . logger = logger
70
+ #if DistributedTracingSupport
71
+ self . tracer = configuration. tracing. tracer
72
+ #endif
66
73
switch address? . value {
67
74
case let . hostname( host, port) :
68
75
self . address = ( host, port)
@@ -175,12 +182,11 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
175
182
@inlinable
176
183
func _execute< Command: ValkeyCommand > ( command: Command ) async throws -> RESPToken {
177
184
#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 ( ) }
180
187
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)
184
190
}
185
191
#endif
186
192
@@ -199,21 +205,21 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
199
205
}
200
206
} catch let error as ValkeyClientError {
201
207
#if DistributedTracingSupport
202
- span. recordError ( error)
208
+ span? . recordError ( error)
203
209
if let message = error. message {
204
210
var prefixEndIndex = message. startIndex
205
211
while prefixEndIndex < message. endIndex, message [ prefixEndIndex] != " " {
206
212
message. formIndex ( after: & prefixEndIndex)
207
213
}
208
214
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) )
211
217
}
212
218
#endif
213
219
throw error
214
220
} catch {
215
221
#if DistributedTracingSupport
216
- span. recordError ( error)
222
+ span? . recordError ( error)
217
223
#endif
218
224
throw error
219
225
}
@@ -230,41 +236,7 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
230
236
public func execute< each Command : ValkeyCommand > (
231
237
_ commands: repeat each Command
232
238
) 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
-
261
239
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
-
268
240
return result. flatMap {
269
241
do {
270
242
return try . success( Response ( fromRESP: $0) )
@@ -301,12 +273,13 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
301
273
}
302
274
303
275
@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
310
283
}
311
284
312
285
@usableFromInline
0 commit comments