Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the client name and ipport to the slowlog output #1740

Merged
merged 5 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/server/redis_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions src/server/redis_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_; }
Expand Down
6 changes: 5 additions & 1 deletion src/server/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 4 additions & 1 deletion src/stats/log_collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 4 additions & 1 deletion src/stats/log_collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#pragma once

#include <sys/types.h>
#include <time.h>

#include <cstdint>
Expand All @@ -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;
};

Expand Down
2 changes: 1 addition & 1 deletion src/storage/scripting.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions tests/gocase/unit/slowlog/slowlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

}
Loading