Skip to content

Commit

Permalink
feat redis: add common statistic of states for instances at cluster l…
Browse files Browse the repository at this point in the history
…evel

Tests: протестировано в тестинге
  • Loading branch information
leonidlazarev committed Dec 4, 2023
1 parent 5994315 commit 976245d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
13 changes: 13 additions & 0 deletions redis/functional_tests/metrics/tests/static/metrics_values.txt
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,19 @@ redis.state: redis_database=metrics_test, redis_instance=127.0.0.1:00000, redis_
redis.state: redis_database=metrics_test, redis_instance=127.0.0.1:00000, redis_instance_state=init_error, redis_instance_type=masters, redis_shard=test_master0 GAUGE 0
redis.state: redis_database=metrics_test, redis_instance=127.0.0.1:00000, redis_instance_state=init_error, redis_instance_type=sentinels GAUGE 0

redis.cluster_states: redis_database=metrics_test, redis_instance_state=connected, redis_instance_type=masters GAUGE 0
redis.cluster_states: redis_database=metrics_test, redis_instance_state=connected, redis_instance_type=sentinels GAUGE 0
redis.cluster_states: redis_database=metrics_test, redis_instance_state=disconnect_error, redis_instance_type=masters GAUGE 0
redis.cluster_states: redis_database=metrics_test, redis_instance_state=disconnect_error, redis_instance_type=sentinels GAUGE 0
redis.cluster_states: redis_database=metrics_test, redis_instance_state=disconnected, redis_instance_type=masters GAUGE 0
redis.cluster_states: redis_database=metrics_test, redis_instance_state=disconnected, redis_instance_type=sentinels GAUGE 0
redis.cluster_states: redis_database=metrics_test, redis_instance_state=disconnecting, redis_instance_type=masters GAUGE 0
redis.cluster_states: redis_database=metrics_test, redis_instance_state=disconnecting, redis_instance_type=sentinels GAUGE 0
redis.cluster_states: redis_database=metrics_test, redis_instance_state=init, redis_instance_type=masters GAUGE 0
redis.cluster_states: redis_database=metrics_test, redis_instance_state=init, redis_instance_type=sentinels GAUGE 0
redis.cluster_states: redis_database=metrics_test, redis_instance_state=init_error, redis_instance_type=masters GAUGE 0
redis.cluster_states: redis_database=metrics_test, redis_instance_state=init_error, redis_instance_type=sentinels GAUGE 0

redis.timings: percentile=p0, redis_database=metrics_test GAUGE 0
redis.timings: percentile=p0, redis_database=metrics_test, redis_instance=127.0.0.1:00000, redis_instance_type=masters, redis_shard=test_master0 GAUGE 0
redis.timings: percentile=p0, redis_database=metrics_test, redis_instance=127.0.0.1:00000, redis_instance_type=sentinels GAUGE 0
Expand Down
51 changes: 48 additions & 3 deletions redis/src/storages/redis/impl/redis_stats.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <storages/redis/impl/redis_stats.hpp>

#include <array>

#include <storages/redis/impl/command.hpp>
#include <storages/redis/impl/redis.hpp>
#include <userver/logging/log.hpp>
Expand Down Expand Up @@ -102,6 +104,34 @@ const std::string_view kCommandTypes[] = {
"zscore",
};

struct ConnStateStatistic {
std::array<size_t, static_cast<size_t>(Redis::State::kDisconnectError) + 1>
statistic{};

void Add(const ShardStatistics& shard_stats) {
for (const auto& [_, stats] : shard_stats.instances) {
UASSERT(stats.state <= Redis::State::kDisconnectError);
statistic.at(static_cast<size_t>(stats.state))++;
}
}

size_t Get(Redis::State state) const {
UASSERT(state <= Redis::State::kDisconnectError);
return statistic.at(static_cast<size_t>(state));
}
};

void DumpMetric(utils::statistics::Writer& writer,
const ConnStateStatistic& stats) {
for (size_t i = 0; i <= static_cast<int>(Redis::State::kDisconnectError);
++i) {
const auto state = static_cast<Redis::State>(i);
writer["cluster_states"].ValueWithLabels(
stats.Get(state),
{"redis_instance_state", redis::Redis::StateToString(state)});
}
}

} // namespace

std::chrono::milliseconds MillisecondsSinceEpoch() {
Expand Down Expand Up @@ -239,19 +269,34 @@ void DumpMetric(utils::statistics::Writer& writer,
DumpMetric(writer, stats.shard_group_total, false);
writer["errors"].ValueWithLabels(stats.internal.redis_not_ready.load(),
{"redis_error", "redis_not_ready"});
if (settings.GetMetricsLevel() >= MetricsSettings::Level::kShard) {
for (const auto& [shard_name, shard_stats] : stats.masters) {

ConnStateStatistic conn_stat_masters;
for (const auto& [shard_name, shard_stats] : stats.masters) {
if (settings.GetMetricsLevel() >= MetricsSettings::Level::kShard) {
writer.ValueWithLabels(shard_stats, {{"redis_instance_type", "masters"},
{"redis_shard", shard_name}});
}
for (const auto& [shard_name, shard_stats] : stats.slaves) {
conn_stat_masters.Add(shard_stats);
}
writer.ValueWithLabels(conn_stat_masters,
{{"redis_instance_type", "masters"}});

ConnStateStatistic conn_stat_slaves;
for (const auto& [shard_name, shard_stats] : stats.slaves) {
if (settings.GetMetricsLevel() >= MetricsSettings::Level::kShard) {
writer.ValueWithLabels(shard_stats, {{"redis_instance_type", "slaves"},
{"redis_shard", shard_name}});
}
conn_stat_slaves.Add(shard_stats);
}
writer.ValueWithLabels(conn_stat_slaves, {{"redis_instance_type", "slaves"}});

if (stats.sentinel) {
writer.ValueWithLabels(*stats.sentinel,
{"redis_instance_type", "sentinels"});
ConnStateStatistic conn_stat;
conn_stat.Add(stats.sentinel.value());
writer.ValueWithLabels(conn_stat, {{"redis_instance_type", "sentinels"}});
}
}

Expand Down

0 comments on commit 976245d

Please sign in to comment.