Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Apr 29, 2024
1 parent 4880f21 commit 2731ff7
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
42 changes: 42 additions & 0 deletions include/cinatra/metric/counter.hpp
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
53 changes: 53 additions & 0 deletions include/cinatra/metric/guage.hpp
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
40 changes: 40 additions & 0 deletions include/cinatra/metric/metric.hpp
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

0 comments on commit 2731ff7

Please sign in to comment.