-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
root
committed
Apr 29, 2024
1 parent
4880f21
commit 2731ff7
Showing
3 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#pragma once | ||
#include <mutex> | ||
|
||
#include "metric.hpp" | ||
|
||
namespace cinatra { | ||
class counter_t : public metric_t { | ||
public: | ||
counter_t(std::string name, std::string help, | ||
std::pair<std::string, std::string> 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<std::string, std::string> &label, double value) { | ||
assert(value > 0); | ||
std::lock_guard guard(mtx_); | ||
value_map_[label] += value; | ||
} | ||
|
||
void update(const std::pair<std::string, std::string> &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<std::pair<std::string, std::string>, double> value_map_; | ||
}; | ||
} // namespace cinatra |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#pragma once | ||
#include <mutex> | ||
|
||
#include "metric.hpp" | ||
|
||
namespace cinatra { | ||
class guage_t : public metric_t { | ||
public: | ||
guage_t(std::string name, std::string help, | ||
std::pair<std::string, std::string> 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<std::string, std::string> &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<std::string, std::string> &label, double value) { | ||
assert(value > 0); | ||
std::lock_guard guard(mtx_); | ||
value_map_[{}] -= value; | ||
} | ||
|
||
void update(const std::pair<std::string, std::string> &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<std::pair<std::string, std::string>, double> value_map_; | ||
}; | ||
} // namespace cinatra |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#pragma once | ||
#include <cassert> | ||
#include <map> | ||
#include <memory> | ||
#include <set> | ||
#include <string> | ||
|
||
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<std::string, std::string> 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<std::string, std::string> &label() { return label_; } | ||
|
||
protected: | ||
MetricType type_ = MetricType::Nil; | ||
std::string name_; | ||
std::string help_; | ||
std::pair<std::string, std::string> label_; | ||
std::map<std::string, std::shared_ptr<metric_t>> metric_map_; | ||
}; | ||
} // namespace cinatra |