| 294 | } |
| 295 | |
| 296 | void handleStatItem(struct RedisModuleCtx *ctx, std::string name, StatsRecord &record, const char *pchValue) { |
| 297 | switch (record.type) { |
| 298 | case StatsD_Type::STATSD_GAUGE_LONGLONG: { |
| 299 | long long val = strtoll(pchValue, nullptr, 10); |
| 300 | g_stats->gauge(name, val, record.prefixOnly); |
| 301 | RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, "Emitting metric \"%s\": %lld", name.c_str(), val); |
| 302 | break; |
| 303 | } |
| 304 | |
| 305 | case StatsD_Type::STATSD_GAUGE_FLOAT: { |
| 306 | double val = strtod(pchValue, nullptr); |
| 307 | g_stats->gauge(name, val, record.prefixOnly); |
| 308 | RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, "Emitting metric \"%s\": %f", name.c_str(), val); |
| 309 | break; |
| 310 | } |
| 311 | |
| 312 | case StatsD_Type::STATSD_GAUGE_BYTES: { |
| 313 | long long val = strtoll(pchValue, nullptr, 10); |
| 314 | g_stats->gauge(name + "_MB", val / 1024/1024, record.prefixOnly); |
| 315 | RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, "Emitting metric \"%s\": %llu", (name + "_MB").c_str(), val / 1024/1024); |
| 316 | break; |
| 317 | } |
| 318 | |
| 319 | case StatsD_Type::STATSD_DELTA: { |
| 320 | long long val = strtoll(pchValue, nullptr, 10); |
| 321 | g_stats->count(name, val - record.prevVal, record.prefixOnly); |
| 322 | RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, "Emitting metric count for \"%s\": %lld", name.c_str() , val - record.prevVal); |
| 323 | record.prevVal = val; |
| 324 | break; |
| 325 | } |
| 326 | |
| 327 | case StatsD_Type::STATSD_COUNTER_STRING: { |
| 328 | // parse val string |
| 329 | const char *pNewLine = strchr(pchValue, '\r'); |
| 330 | if (pNewLine == nullptr) { |
| 331 | pNewLine = strchr(pchValue, '\n'); |
| 332 | } |
| 333 | if (pNewLine == nullptr) { |
| 334 | g_stats->increment("STATSD_COUNTER_STRING_failed", 1); |
| 335 | return; |
| 336 | } |
| 337 | std::string val(pchValue, pNewLine - pchValue); |
| 338 | std::replace(val.begin(), val.end(), '.', '-'); |
| 339 | // metrics emit |
| 340 | std::string metricsName = name + g_string_counter_separator + val; |
| 341 | g_stats->increment(metricsName, 1); |
| 342 | RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, "Emitting metric \"%s\"", metricsName.c_str()); |
| 343 | break; |
| 344 | } |
| 345 | |
| 346 | default: |
| 347 | RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_WARNING, "Unknown stats record type for the key \"%s\": %u", name.c_str(), (unsigned)record.type); |
| 348 | break; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | void handleErrorStatItem(struct RedisModuleCtx *ctx, std::string name, std::string rest) { |
| 353 | size_t idx = rest.find('='); |