Skip to content

Commit 7923884

Browse files
committed
feat mongo: add heartbeat_time log tag (attempt 2)
commit_hash:e9e26e72d850ce29c2c18e92d49ffd8fc29c2bb8
1 parent ddcdfad commit 7923884

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

mongo/src/storages/mongo/cdriver/pool_impl.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ USERVER_NAMESPACE_BEGIN
2929
namespace storages::mongo::impl::cdriver {
3030
namespace {
3131

32+
using RealMilliseconds = std::chrono::duration<double, std::milli>;
33+
3234
[[maybe_unused]] void MongocCoroFrieldlyUsleep(int64_t usec, void*) noexcept {
3335
UASSERT(usec >= 0);
3436
if (engine::current_task::IsTaskProcessorThread()) {
@@ -171,19 +173,32 @@ void HeartbeatStarted(const mongoc_apm_server_heartbeat_started_t* event) {
171173
auto& stats = GetStats(mongoc_apm_server_heartbeat_started_get_context(event));
172174
++stats.apm_stats_->heartbeats.start;
173175
LOG_LIMITED_DEBUG() << mongoc_apm_server_heartbeat_started_get_host(event)->host_and_port << " heartbeat started";
176+
stats.apm_stats_->heartbeats.hb_started = std::chrono::steady_clock::now();
177+
}
178+
179+
void HeartbeatFinished(stats::ConnStats& stats) {
180+
auto* span = tracing::Span::CurrentSpanUnchecked();
181+
if (span) {
182+
auto diff = std::chrono::duration_cast<RealMilliseconds>(
183+
std::chrono::steady_clock::now() - stats.apm_stats_->heartbeats.hb_started
184+
);
185+
span->AddTag("heartbeat_time", diff.count());
186+
}
174187
}
175188

176189
void HeartbeatSuccess(const mongoc_apm_server_heartbeat_succeeded_t* event) {
177190
auto& stats = GetStats(mongoc_apm_server_heartbeat_succeeded_get_context(event));
178191
++stats.apm_stats_->heartbeats.success;
179192
LOG_LIMITED_DEBUG() << mongoc_apm_server_heartbeat_succeeded_get_host(event)->host_and_port
180193
<< " heartbeat succeeded";
194+
HeartbeatFinished(stats);
181195
}
182196

183197
void HeartbeatFailed(const mongoc_apm_server_heartbeat_failed_t* event) {
184198
auto& stats = GetStats(mongoc_apm_server_heartbeat_failed_get_context(event));
185199
++stats.apm_stats_->heartbeats.failed;
186200
LOG_LIMITED_WARNING() << mongoc_apm_server_heartbeat_failed_get_host(event)->host_and_port << " heartbeat failed";
201+
HeartbeatFinished(stats);
187202
}
188203

189204
std::string CreateTopologyChangeMessage(const mongoc_apm_topology_changed_t* event) {

mongo/src/storages/mongo/stats.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ struct HeartbeatsStatistics final {
123123
Counter success;
124124
Counter failed;
125125
Counter start;
126+
127+
std::chrono::steady_clock::time_point hb_started{};
126128
};
127129

128130
// See

0 commit comments

Comments
 (0)