This is an automated email from the ASF dual-hosted git repository.
hulk pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/kvrocks.git
The following commit(s) were added to refs/heads/unstable by this push:
new 15b4d8f0 Add the client name and ipport to the slowlog output (#1740)
15b4d8f0 is described below
commit 15b4d8f0ff92d9329b32cfe5703b80d87aea34d4
Author: Yuhui Liu <[email protected]>
AuthorDate: Thu Sep 7 00:12:59 2023 +0800
Add the client name and ipport to the slowlog output (#1740)
---
src/server/redis_connection.cc | 2 +-
src/server/redis_connection.h | 4 ++--
src/server/server.cc | 6 +++++-
src/server/server.h | 2 +-
src/stats/log_collector.cc | 5 ++++-
src/stats/log_collector.h | 5 ++++-
src/storage/scripting.cc | 2 +-
tests/gocase/unit/slowlog/slowlog_test.go | 11 +++++++++++
8 files changed, 29 insertions(+), 8 deletions(-)
diff --git a/src/server/redis_connection.cc b/src/server/redis_connection.cc
index a83b4d59..64b8aa03 100644
--- a/src/server/redis_connection.cc
+++ b/src/server/redis_connection.cc
@@ -416,7 +416,7 @@ void Connection::ExecuteCommands(std::deque<CommandTokens>
*to_process_cmds) {
uint64_t duration =
std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
if (is_profiling) RecordProfilingSampleIfNeed(cmd_name, duration);
- svr_->SlowlogPushEntryIfNeeded(&cmd_tokens, duration);
+ svr_->SlowlogPushEntryIfNeeded(&cmd_tokens, duration, this);
svr_->stats.IncrLatency(static_cast<uint64_t>(duration), cmd_name);
svr_->FeedMonitorConns(this, cmd_tokens);
diff --git a/src/server/redis_connection.h b/src/server/redis_connection.h
index ccad9a9a..8e92042c 100644
--- a/src/server/redis_connection.h
+++ b/src/server/redis_connection.h
@@ -82,12 +82,12 @@ class Connection : public EvbufCallbackBase<Connection> {
uint64_t GetID() const { return id_; }
void SetID(uint64_t id) { id_ = id; }
- std::string GetName() { return name_; }
+ std::string GetName() const { return name_; }
void SetName(std::string name) { name_ = std::move(name); }
std::string GetAddr() { return addr_; }
void SetAddr(std::string ip, uint32_t port);
void SetLastCmd(std::string cmd) { last_cmd_ = std::move(cmd); }
- std::string GetIP() { return ip_; }
+ std::string GetIP() const { return ip_; }
uint32_t GetPort() const { return port_; }
void SetListeningPort(int port) { listening_port_ = port; }
int GetListeningPort() const { return listening_port_; }
diff --git a/src/server/server.cc b/src/server/server.cc
index 90e9505b..9a2bc10c 100644
--- a/src/server/server.cc
+++ b/src/server/server.cc
@@ -1407,7 +1407,8 @@ time_t Server::GetLastScanTime(const std::string &ns) {
return 0;
}
-void Server::SlowlogPushEntryIfNeeded(const std::vector<std::string> *args,
uint64_t duration) {
+void Server::SlowlogPushEntryIfNeeded(const std::vector<std::string> *args,
uint64_t duration,
+ const redis::Connection *conn) {
int64_t threshold = config_->slowlog_log_slower_than;
if (threshold < 0 || static_cast<int64_t>(duration) < threshold) return;
@@ -1428,6 +1429,9 @@ void Server::SlowlogPushEntryIfNeeded(const
std::vector<std::string> *args, uint
}
entry->duration = duration;
+ entry->client_name = conn->GetName();
+ entry->ip = conn->GetIP();
+ entry->port = conn->GetPort();
slow_log_.PushEntry(std::move(entry));
}
diff --git a/src/server/server.h b/src/server/server.h
index 953cbbbc..28db9333 100644
--- a/src/server/server.h
+++ b/src/server/server.h
@@ -264,7 +264,7 @@ class Server {
LogCollector<PerfEntry> *GetPerfLog() { return &perf_log_; }
LogCollector<SlowEntry> *GetSlowLog() { return &slow_log_; }
- void SlowlogPushEntryIfNeeded(const std::vector<std::string> *args, uint64_t
duration);
+ void SlowlogPushEntryIfNeeded(const std::vector<std::string> *args, uint64_t
duration, const redis::Connection *conn);
std::shared_lock<std::shared_mutex> WorkConcurrencyGuard();
std::unique_lock<std::shared_mutex> WorkExclusivityGuard();
diff --git a/src/stats/log_collector.cc b/src/stats/log_collector.cc
index 519f5277..1d3f9f40 100644
--- a/src/stats/log_collector.cc
+++ b/src/stats/log_collector.cc
@@ -21,17 +21,20 @@
#include "log_collector.h"
#include <algorithm>
+#include <string>
#include "server/redis_reply.h"
#include "time_util.h"
std::string SlowEntry::ToRedisString() const {
std::string output;
- output.append(redis::MultiLen(4));
+ output.append(redis::MultiLen(6));
output.append(redis::Integer(id));
output.append(redis::Integer(time));
output.append(redis::Integer(duration));
output.append(redis::MultiBulkString(args));
+ output.append(redis::BulkString(ip + ":" + std::to_string(port)));
+ output.append(redis::BulkString(client_name));
return output;
}
diff --git a/src/stats/log_collector.h b/src/stats/log_collector.h
index 14bcea48..d67893d3 100644
--- a/src/stats/log_collector.h
+++ b/src/stats/log_collector.h
@@ -20,6 +20,7 @@
#pragma once
+#include <sys/types.h>
#include <time.h>
#include <cstdint>
@@ -36,7 +37,9 @@ class SlowEntry {
time_t time;
uint64_t duration;
std::vector<std::string> args;
-
+ std::string client_name;
+ std::string ip;
+ uint32_t port;
std::string ToRedisString() const;
};
diff --git a/src/storage/scripting.cc b/src/storage/scripting.cc
index 3f787f47..acfe0680 100644
--- a/src/storage/scripting.cc
+++ b/src/storage/scripting.cc
@@ -406,7 +406,7 @@ int RedisGenericCommand(lua_State *lua, int raise_error) {
auto end = std::chrono::high_resolution_clock::now();
uint64_t duration =
std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
if (is_profiling) conn->RecordProfilingSampleIfNeed(cmd_name, duration);
- srv->SlowlogPushEntryIfNeeded(&args, duration);
+ srv->SlowlogPushEntryIfNeeded(&args, duration, conn);
srv->stats.IncrLatency(static_cast<uint64_t>(duration), cmd_name);
srv->FeedMonitorConns(conn, args);
if (!s) {
diff --git a/tests/gocase/unit/slowlog/slowlog_test.go
b/tests/gocase/unit/slowlog/slowlog_test.go
index d2f6f8de..eb7782db 100644
--- a/tests/gocase/unit/slowlog/slowlog_test.go
+++ b/tests/gocase/unit/slowlog/slowlog_test.go
@@ -161,4 +161,15 @@ func TestSlowlog(t *testing.T) {
require.EqualValues(t, 0, len(rdb.SlowLogGet(ctx, -1).Val()))
})
+ t.Run("SLOWLOG - slowlog get output client name and ipport", func(t
*testing.T) {
+ require.NoError(t, rdb.ConfigSet(ctx,
"slowlog-log-slower-than", "0").Err())
+ require.NoError(t, rdb.Do(ctx, "client", "setname",
"foobar").Err())
+ require.NoError(t, rdb.SAdd(ctx, "set", "foo", "bar").Err())
+
+ val, err := rdb.SlowLogGet(ctx, -1).Result()
+ require.NoError(t, err)
+ require.EqualValues(t, "foobar", val[0].ClientName)
+ require.NotEmpty(t, val[0].ClientAddr)
+ })
+
}