@@ -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)
@@ -177,12 +184,11 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
177
184
@inlinable
178
185
func _execute< Command: ValkeyCommand > ( command: Command ) async throws -> RESPToken {
179
186
#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 ( ) }
182
189
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)
186
192
}
187
193
#endif
188
194
@@ -201,21 +207,21 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
201
207
}
202
208
} catch let error as ValkeyClientError {
203
209
#if DistributedTracingSupport
204
- span. recordError ( error)
210
+ span? . recordError ( error)
205
211
if let message = error. message {
206
212
var prefixEndIndex = message. startIndex
207
213
while prefixEndIndex < message. endIndex, message [ prefixEndIndex] != " " {
208
214
message. formIndex ( after: & prefixEndIndex)
209
215
}
210
216
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) )
213
219
}
214
220
#endif
215
221
throw error
216
222
} catch {
217
223
#if DistributedTracingSupport
218
- span. recordError ( error)
224
+ span? . recordError ( error)
219
225
#endif
220
226
throw error
221
227
}
@@ -236,41 +242,7 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
236
242
public func execute< each Command : ValkeyCommand > (
237
243
_ commands: repeat each Command
238
244
) 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
-
267
245
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
-
274
246
return result. flatMap {
275
247
do {
276
248
return try . success( Response ( fromRESP: $0) )
@@ -307,12 +279,13 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
307
279
}
308
280
309
281
@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
316
289
}
317
290
318
291
@usableFromInline
0 commit comments