From 2731ff7195f45615a17f212424825bf7f1dea3c2 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 29 Apr 2024 16:50:30 +0800 Subject: [PATCH] init --- include/cinatra/metric/counter.hpp | 42 +++++++++++++++++++++++ include/cinatra/metric/guage.hpp | 53 ++++++++++++++++++++++++++++++ include/cinatra/metric/metric.hpp | 40 ++++++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 include/cinatra/metric/counter.hpp create mode 100644 include/cinatra/metric/guage.hpp create mode 100644 include/cinatra/metric/metric.hpp diff --git a/include/cinatra/metric/counter.hpp b/include/cinatra/metric/counter.hpp new file mode 100644 index 00000000..c60249e6 --- /dev/null +++ b/include/cinatra/metric/counter.hpp @@ -0,0 +1,42 @@ +#pragma once +#include + +#include "metric.hpp" + +namespace cinatra { +class counter_t : public metric_t { + public: + counter_t(std::string name, std::string help, + std::pair labels = {}) + : metric_t(MetricType::Counter, std::move(name), std::move(help), + std::move(labels)) {} + + void inc() { + std::lock_guard guard(mtx_); + value_map_[{}]++; + } + + void inc(const std::pair &label, double value) { + assert(value > 0); + std::lock_guard guard(mtx_); + value_map_[label] += value; + } + + void update(const std::pair &label, double value) { + assert(value > 0); + std::lock_guard guard(mtx_); + value_map_[label] = value; + } + + void reset() { + std::lock_guard guard(mtx_); + for (auto &pair : value_map_) { + pair.second = 0; + } + } + + private: + std::mutex mtx_; + std::map, double> value_map_; +}; +} // namespace cinatra \ No newline at end of file diff --git a/include/cinatra/metric/guage.hpp b/include/cinatra/metric/guage.hpp new file mode 100644 index 00000000..2cf61fe6 --- /dev/null +++ b/include/cinatra/metric/guage.hpp @@ -0,0 +1,53 @@ +#pragma once +#include + +#include "metric.hpp" + +namespace cinatra { +class guage_t : public metric_t { + public: + guage_t(std::string name, std::string help, + std::pair labels = {}) + : metric_t(MetricType::Counter, std::move(name), std::move(help), + std::move(labels)) {} + + void inc() { + std::lock_guard guard(mtx_); + value_map_[{}]++; + } + + void inc(const std::pair &label, double value) { + assert(value > 0); + std::lock_guard guard(mtx_); + value_map_[label] += value; + } + + void dec() { + std::lock_guard guard(mtx_); + value_map_[{}]--; + } + + void dec(const std::pair &label, double value) { + assert(value > 0); + std::lock_guard guard(mtx_); + value_map_[{}] -= value; + } + + void update(const std::pair &label, double value) { + assert(value > 0); + std::lock_guard guard(mtx_); + value_map_[label] = value; + } + + void reset() { + std::lock_guard guard(mtx_); + for (auto &pair : value_map_) { + pair.second = 0; + } + } + + private: + std::mutex mtx_; + std::map, double> value_map_; +}; +} // namespace cinatra \ No newline at end of file diff --git a/include/cinatra/metric/metric.hpp b/include/cinatra/metric/metric.hpp new file mode 100644 index 00000000..ab373f46 --- /dev/null +++ b/include/cinatra/metric/metric.hpp @@ -0,0 +1,40 @@ +#pragma once +#include +#include +#include +#include +#include + +namespace cinatra { +enum class MetricType { + Counter, + Guage, + Histogram, + Summary, + Nil, +}; + +class metric_t { + public: + metric_t(MetricType type, std::string name, std::string help, + std::pair labels = {}) + : type_(type), + name_(std::move(name)), + help_(std::move(help)), + label_(std::move(labels)) {} + std::string_view name() { return name_; } + + std::string_view help() { return help_; } + + MetricType metric_type() { return type_; } + + const std::pair &label() { return label_; } + + protected: + MetricType type_ = MetricType::Nil; + std::string name_; + std::string help_; + std::pair label_; + std::map> metric_map_; +}; +} // namespace cinatra \ No newline at end of file