Skip to content

Commit 6375e91

Browse files
committed
add metric emit for non-empty primary with less than 2 connected replicas
1 parent bf29926 commit 6375e91

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

src/modules/keydb_modstatsd/modmain.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,31 @@ void emit_system_free_memory() {
544544
}
545545
}
546546

547+
void emit_non_empty_primary_with_less_than_2_connected_replicas_error_metrics(struct RedisModuleCtx *ctx, long long keys) {
548+
// non-empty
549+
if (keys <= 0) {
550+
return;
551+
}
552+
RedisModuleCallReply *reply = RedisModule_Call(ctx, "ROLE", "");
553+
if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_ARRAY) {
554+
return;
555+
}
556+
RedisModuleCallReply *roleReply = RedisModule_CallReplyArrayElement(reply, 0);
557+
if (RedisModule_CallReplyType(roleReply) != REDISMODULE_REPLY_STRING) {
558+
return;
559+
}
560+
size_t len;
561+
const char *role = RedisModule_CallReplyStringPtr(roleReply, &len);
562+
// check if the current node is a primary
563+
if (strncmp(role, "master", len) == 0) {
564+
RedisModuleCallReply *replicasReply = RedisModule_CallReplyArrayElement(reply, 2);
565+
// check if there are less than 2 connected replicas
566+
if (RedisModule_CallReplyLength(replicasReply) < 2) {
567+
g_stats->increment("hasLessThan2ConnectedReplicas_error", 1);
568+
}
569+
}
570+
}
571+
547572
void event_cron_handler(struct RedisModuleCtx *ctx, RedisModuleEvent eid, uint64_t subevent, void *data) {
548573
static time_t lastTime = 0;
549574
time_t curTime = time(nullptr);
@@ -619,11 +644,16 @@ void event_cron_handler(struct RedisModuleCtx *ctx, RedisModuleEvent eid, uint64
619644
g_stats->timing("emit_free_system_memory_time_taken_us", ustime() - commandStartTime);
620645

621646
/* Log Keys */
647+
commandStartTime = ustime();
622648
reply = RedisModule_Call(ctx, "dbsize", "");
623649
long long keys = RedisModule_CallReplyInteger(reply);
624650
RedisModule_FreeCallReply(reply);
625651
g_stats->gauge("keys", keys);
626652
RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, "Emitting metric \"keys\": %llu", keys);
653+
g_stats->timing("emit_keys_metric_time_taken_us", ustime() - commandStartTime);
654+
655+
emit_non_empty_primary_with_less_than_2_connected_replicas_error_metrics(ctx, keys);
656+
627657
g_stats->timing("metrics_time_taken_us", ustime() - startTime);
628658

629659
lastTime = curTime;

0 commit comments

Comments
 (0)