Skip to content

Commit 31e7386

Browse files
authored
add kphp_slow_curl_response metric (#1377)
1 parent 7feeb11 commit 31e7386

File tree

4 files changed

+80
-31
lines changed

4 files changed

+80
-31
lines changed

runtime/curl.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <cstdio>
88
#include <cstring>
9+
#include <string_view>
910

1011
#include "curl/curl.h"
1112
#include "curl/easy.h"
@@ -25,7 +26,9 @@
2526
#include "net/net-events.h"
2627
#include "net/net-reactor.h"
2728
#include "server/curl-adaptor.h"
29+
#include "server/php-queries.h"
2830
#include "server/slot-ids-factory.h"
31+
#include "server/statshouse/statshouse-manager.h"
2932

3033
static_assert(LIBCURL_VERSION_NUM >= 0x071c00, "Outdated libcurl");
3134
static_assert(CURL_MAX_WRITE_SIZE <= (1 << 30), "CURL_MAX_WRITE_SIZE expected to be less than (1 << 30)");
@@ -667,8 +670,11 @@ mixed f$curl_exec(curl_easy easy_id) noexcept {
667670
easy_context->error_num = dl::critical_section_call(curl_easy_perform, easy_context->easy_handle);
668671
double request_finish_time = dl_time();
669672
if (request_finish_time - request_start_time >= long_curl_query) {
670-
kprintf("LONG curl query : %f. Curl id = %d, url = %.100s\n", request_finish_time - request_start_time, easy_context->uniq_id,
671-
easy_context->get_info(CURLINFO_EFFECTIVE_URL).as_string().c_str());
673+
string curl_url = easy_context->get_info(CURLINFO_EFFECTIVE_URL).as_string();
674+
StatsHouseManager::get().add_slow_net_event_stats(
675+
slow_net_event_stats::slow_curl_response_stats{slow_net_event_stats::slow_curl_response_stats::curl_kind::sync,
676+
std::string_view{curl_url.c_str(), curl_url.size()}, request_finish_time - request_start_time});
677+
kprintf("LONG curl query : %f. Curl id = %d, url = %.100s\n", request_finish_time - request_start_time, easy_context->uniq_id, curl_url.c_str());
672678
}
673679
if (easy_context->error_num != CURLE_OK && easy_context->error_num != CURLE_PARTIAL_FILE) {
674680
if (kphp_tracing::is_turned_on()) {

server/php-queries.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#include <cstddef>
88
#include <cstdint>
99
#include <memory>
10+
#include <optional>
11+
#include <string_view>
12+
#include <utility>
1013
#include <variant>
1114

1215
#include "common/sanitizer.h"
@@ -91,7 +94,20 @@ struct slow_job_worker_response_stats final {
9194
double response_time{};
9295
};
9396

94-
using stats_t = std::variant<slow_rpc_response_stats, slow_job_worker_response_stats>;
97+
struct slow_curl_response_stats final {
98+
enum class curl_kind : uint8_t { sync, async };
99+
100+
curl_kind kind;
101+
std::optional<std::string_view> opt_url;
102+
double response_time;
103+
104+
slow_curl_response_stats(curl_kind kind_, std::optional<std::string_view> url_, double response_time_) noexcept
105+
: kind(kind_),
106+
opt_url(url_),
107+
response_time(response_time_) {}
108+
};
109+
110+
using stats_t = std::variant<slow_rpc_response_stats, slow_job_worker_response_stats, slow_curl_response_stats>;
95111

96112
}; // namespace slow_net_event_stats
97113

server/php-runner.cpp

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <cerrno>
1010
#include <cstdlib>
1111
#include <cstring>
12+
#include <optional>
1213
#include <sys/mman.h>
1314
#include <sys/time.h>
1415
#include <unistd.h>
@@ -60,26 +61,31 @@ namespace {
6061
[[maybe_unused]] const void *main_thread_stack = nullptr;
6162
[[maybe_unused]] size_t main_thread_stacksize = 0;
6263

63-
void send_slow_net_event_stats(const net_event_t &event, double time_sec) noexcept {
64+
void send_slow_net_event_stats(const net_event_t& event, double time_sec) noexcept {
6465
std::visit(overloaded{
65-
[&event, time_sec](const net_events_data::rpc_answer &) noexcept {
66-
const auto *rpc_req = get_rpc_request(event.slot_id);
67-
StatsHouseManager::get().add_slow_net_event_stats(
68-
slow_net_event_stats::slow_rpc_response_stats{tl_magic_convert_to_name(rpc_req->function_magic), rpc_req->actor_or_port, time_sec, false});
69-
},
70-
[&event, time_sec](const net_events_data::rpc_error &) noexcept {
71-
const auto *rpc_req = get_rpc_request(event.slot_id);
72-
StatsHouseManager::get().add_slow_net_event_stats(
73-
slow_net_event_stats::slow_rpc_response_stats{tl_magic_convert_to_name(rpc_req->function_magic), rpc_req->actor_or_port, time_sec, true});
74-
},
75-
[time_sec](const net_events_data::job_worker_answer &jw_answer) noexcept {
76-
if (jw_answer.job_result != nullptr) {
77-
StatsHouseManager::get().add_slow_net_event_stats(
78-
slow_net_event_stats::slow_job_worker_response_stats{jw_answer.job_result->response.get_class(), time_sec});
79-
}
80-
},
81-
[](const database_drivers::Response *) {},
82-
[](const curl_async::CurlResponse *) {},
66+
[&event, time_sec](const net_events_data::rpc_answer&) noexcept {
67+
const auto* rpc_req = get_rpc_request(event.slot_id);
68+
StatsHouseManager::get().add_slow_net_event_stats(slow_net_event_stats::slow_rpc_response_stats{
69+
tl_magic_convert_to_name(rpc_req->function_magic), rpc_req->actor_or_port, time_sec, false});
70+
},
71+
[&event, time_sec](const net_events_data::rpc_error&) noexcept {
72+
const auto* rpc_req = get_rpc_request(event.slot_id);
73+
StatsHouseManager::get().add_slow_net_event_stats(slow_net_event_stats::slow_rpc_response_stats{
74+
tl_magic_convert_to_name(rpc_req->function_magic), rpc_req->actor_or_port, time_sec, true});
75+
},
76+
[time_sec](const net_events_data::job_worker_answer& jw_answer) noexcept {
77+
if (jw_answer.job_result != nullptr) {
78+
StatsHouseManager::get().add_slow_net_event_stats(
79+
slow_net_event_stats::slow_job_worker_response_stats{jw_answer.job_result->response.get_class(), time_sec});
80+
}
81+
},
82+
[](const database_drivers::Response*) {},
83+
[time_sec](const curl_async::CurlResponse* curl_response) noexcept {
84+
if (curl_response != nullptr) {
85+
StatsHouseManager::get().add_slow_net_event_stats(slow_net_event_stats::slow_curl_response_stats{
86+
slow_net_event_stats::slow_curl_response_stats::curl_kind::async, std::nullopt, time_sec});
87+
}
88+
},
8389

8490
},
8591
event.data);

server/statshouse/statshouse-manager.cpp

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
#include "server/statshouse/statshouse-manager.h"
66

7+
#include <algorithm>
78
#include <array>
89
#include <charconv>
910
#include <chrono>
1011
#include <cstddef>
1112
#include <string>
13+
#include <string_view>
1214
#include <variant>
1315

1416
#include "common/precise-time.h"
@@ -380,22 +382,41 @@ void StatsHouseManager::add_confdata_binlog_reader_stats(const binlog_reader_sta
380382
client.metric("kphp_confdata_next_binlog_wait_time").tag("binlog_name", confdata_stats.next_binlog_expectator_name).write_value(confdata_stats.next_binlog_wait_time.count());
381383
}
382384

383-
void StatsHouseManager::add_slow_net_event_stats(const slow_net_event_stats::stats_t &stats) noexcept {
384-
std::visit(overloaded{[this](const slow_net_event_stats::slow_rpc_response_stats &rpc_query_stat) noexcept {
385+
void StatsHouseManager::add_slow_net_event_stats(const slow_net_event_stats::stats_t& stats) noexcept {
386+
std::visit(overloaded{[this](const slow_net_event_stats::slow_rpc_response_stats& rpc_query_stat) noexcept {
385387
// FIXME: it's enough to have it equal 10, but due to bug in GCC we are forced to use a length > 253
386388
constexpr auto MAX_INT_STRING_LENGTH = 254;
387389
std::array<char, MAX_INT_STRING_LENGTH> buf{};
388390
const auto chars{std::to_chars(buf.data(), buf.data() + buf.size(), rpc_query_stat.actor_or_port)};
389391
client.metric("kphp_slow_rpc_response")
390-
.tag(rpc_query_stat.tl_function_name != nullptr ? rpc_query_stat.tl_function_name : "unknown")
391-
.tag({buf.data(), static_cast<size_t>(chars.ptr - buf.data())})
392-
.tag(rpc_query_stat.is_error ? "error" : "success")
393-
.write_value(rpc_query_stat.response_time);
392+
.tag(rpc_query_stat.tl_function_name != nullptr ? rpc_query_stat.tl_function_name : "unknown")
393+
.tag({buf.data(), static_cast<size_t>(chars.ptr - buf.data())})
394+
.tag(rpc_query_stat.is_error ? "error" : "success")
395+
.write_value(rpc_query_stat.response_time);
394396
},
395-
[this](const slow_net_event_stats::slow_job_worker_response_stats &jw_response_stat) noexcept {
397+
[this](const slow_net_event_stats::slow_job_worker_response_stats& jw_response_stat) noexcept {
396398
client.metric("kphp_slow_job_worker_response")
397-
.tag(jw_response_stat.class_name != nullptr ? jw_response_stat.class_name : "unknown")
398-
.write_value(jw_response_stat.response_time);
399+
.tag(jw_response_stat.class_name != nullptr ? jw_response_stat.class_name : "unknown")
400+
.write_value(jw_response_stat.response_time);
401+
},
402+
[this](const slow_net_event_stats::slow_curl_response_stats& curl_response_stat) noexcept {
403+
std::string_view curl_kind = "unknown";
404+
switch (curl_response_stat.kind) {
405+
case slow_net_event_stats::slow_curl_response_stats::curl_kind::sync:
406+
curl_kind = "sync";
407+
break;
408+
case slow_net_event_stats::slow_curl_response_stats::curl_kind::async:
409+
curl_kind = "async";
410+
break;
411+
}
412+
413+
static constexpr size_t CURL_URL_MAX_LEN = 100;
414+
std::string_view curl_url = curl_response_stat.opt_url.value_or(std::string_view{"unknown"});
415+
416+
client.metric("kphp_slow_curl_response")
417+
.tag(curl_kind)
418+
.tag(std::string_view{curl_url.data(), std::min(curl_url.size(), CURL_URL_MAX_LEN)})
419+
.write_value(curl_response_stat.response_time);
399420
}},
400421
stats);
401422
}

0 commit comments

Comments
 (0)