Skip to content

Commit 22651ce

Browse files
authored
Add logger trace calls with command names (#252)
1 parent 19cd708 commit 22651ce

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

Sources/Valkey/Connection/ValkeyConnection.swift

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
3131
static let requestIDGenerator: IDGenerator = .init()
3232
/// Connection ID, used by connection pool
3333
public let id: ID
34-
/// Logger used by Server
34+
/// Logger used by connection
35+
@usableFromInline
3536
let logger: Logger
3637
#if DistributedTracingSupport
3738
@usableFromInline
@@ -178,6 +179,8 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
178179
}
179180
#endif
180181

182+
self.logger.trace("execute", metadata: ["command": "\(Command.name)"])
183+
181184
let requestID = Self.requestIDGenerator.next()
182185

183186
do {
@@ -225,6 +228,8 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
225228
public func execute<each Command: ValkeyCommand>(
226229
_ commands: repeat each Command
227230
) async -> sending (repeat Result<(each Command).Response, any Error>) {
231+
self.logger.trace("execute", metadata: ["commands": .string(Self.concatenateCommandNames(repeat each commands))])
232+
228233
// this currently allocates a promise for every command. We could collapse this down to one promise
229234
var promises: [EventLoopPromise<RESPToken>] = []
230235
var encoder = ValkeyCommandEncoder()
@@ -259,6 +264,7 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
259264
public func transaction<each Command: ValkeyCommand>(
260265
_ commands: repeat each Command
261266
) async throws -> sending (repeat Result<(each Command).Response, Error>) {
267+
self.logger.trace("transaction", metadata: ["commands": .string(Self.concatenateCommandNames(repeat each commands))])
262268
// Construct encoded commands and promise array
263269
var encoder = ValkeyCommandEncoder()
264270
var promises: [EventLoopPromise<RESPToken>] = []
@@ -318,6 +324,8 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
318324
public func execute(
319325
_ commands: some Collection<any ValkeyCommand>
320326
) async -> [Result<RESPToken, any Error>] {
327+
self.logger.trace("execute", metadata: ["commands": .string(Self.concatenateCommandNames(commands))])
328+
321329
// this currently allocates a promise for every command. We could collapse this down to one promise
322330
var promises: [EventLoopPromise<RESPToken>] = []
323331
promises.reserveCapacity(commands.count)
@@ -362,6 +370,7 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
362370
public func transaction(
363371
_ commands: some Collection<any ValkeyCommand>
364372
) async throws -> [Result<RESPToken, Error>] {
373+
self.logger.trace("transaction", metadata: ["commands": .string(Self.concatenateCommandNames(commands))])
365374
// Construct encoded commands and promise array
366375
var encoder = ValkeyCommandEncoder()
367376
var promises: [EventLoopPromise<RESPToken>] = []
@@ -428,6 +437,7 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
428437
func executeWithAsk(
429438
_ commands: some Collection<any ValkeyCommand>
430439
) async -> [Result<RESPToken, any Error>] {
440+
self.logger.trace("asking", metadata: ["commands": .string(Self.concatenateCommandNames(commands))])
431441
// this currently allocates a promise for every command. We could collapse this down to one promise
432442
var promises: [EventLoopPromise<RESPToken>] = []
433443
promises.reserveCapacity(commands.count)
@@ -509,6 +519,48 @@ public final actor ValkeyConnection: ValkeyClientProtocol, Sendable {
509519
}
510520
}
511521

522+
/// Concatenate names from parameter pack of commands together
523+
@usableFromInline
524+
static func concatenateCommandNames<each Command: ValkeyCommand>(
525+
_ commands: repeat each Command
526+
) -> String {
527+
var string: String = ""
528+
var count = 0
529+
for command in repeat each commands {
530+
if count == 0 {
531+
string += "\(Swift.type(of: command).name)"
532+
} else if count == 16 {
533+
string += "..."
534+
break
535+
} else {
536+
string += ",\(Swift.type(of: command).name)"
537+
}
538+
count += 1
539+
}
540+
return string
541+
}
542+
543+
/// Concatenate names from collection of command together
544+
@usableFromInline
545+
static func concatenateCommandNames<Commands: Collection>(
546+
_ commands: Commands
547+
) -> String where Commands.Element == any ValkeyCommand {
548+
var string: String = ""
549+
guard let firstCommand = commands.first else { return "" }
550+
string = "\(Swift.type(of: firstCommand).name)"
551+
var count = 1
552+
for command in commands.dropFirst() {
553+
if count == 16 {
554+
string += "..."
555+
break
556+
} else {
557+
string += ",\(Swift.type(of: command).name)"
558+
}
559+
count += 1
560+
}
561+
return string
562+
}
563+
512564
/// Create Valkey connection and return channel connection is running on and the Valkey channel handler
513565
private static func _makeConnection(
514566
address: ValkeyServerAddress,

Tests/IntegrationTests/ClientIntegrationTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ struct ClientIntegratedTests {
276276
@available(valkeySwift 1.0, *)
277277
func testTransactionSetIncrGet() async throws {
278278
var logger = Logger(label: "Valkey")
279-
logger.logLevel = .debug
279+
logger.logLevel = .trace
280280
try await withValkeyClient(.hostname(valkeyHostname, port: 6379), logger: logger) { client in
281281
try await withKey(connection: client) { key in
282282
let responses = try await client.transaction(

0 commit comments

Comments
 (0)