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 1 commit
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->name_, this->ip_, this->port_);
svr_->stats.IncrLatency(static_cast<uint64_t>(duration), cmd_name);
svr_->FeedMonitorConns(this, cmd_tokens);

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, std::string client_name,
git-hulk marked this conversation as resolved.
Show resolved Hide resolved
std::string ip, uint32_t port) {
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 = std::move(client_name);
entry->ip = std::move(ip);
entry->port = port;
slow_log_.PushEntry(std::move(entry));
}

Expand Down
3 changes: 2 additions & 1 deletion src/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ 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, std::string client_name,
std::string ip, uint32_t port);

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->GetName(), conn->GetIP(), conn->GetPort());
srv->stats.IncrLatency(static_cast<uint64_t>(duration), cmd_name);
srv->FeedMonitorConns(conn, args);
if (!s) {
Expand Down
Loading