Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[metric][feat]improve metrics #692

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions include/ylt/metric.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2024, Alibaba Group Holding Limited;
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#define CINATRA_ENABLE_METRIC_JSON
#include "metric/gauge.hpp"
#include "metric/histogram.hpp"
#include "metric/metric.hpp"
#include "metric/summary.hpp"
#include "ylt/struct_json/json_writer.h"
70 changes: 57 additions & 13 deletions include/ylt/metric/counter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@

#include "metric.hpp"

namespace ylt {
namespace ylt::metric {
enum class op_type_t { INC, DEC, SET };
struct counter_sample {
op_type_t op_type;
std::vector<std::string> labels_value;
double value;

#ifdef CINATRA_ENABLE_METRIC_JSON
struct json_counter_metric_t {
std::unordered_multimap<std::string, std::string> labels;
int64_t value;
};
REFLECTION(json_counter_metric_t, labels, value);
struct json_counter_t {
std::string name;
std::string help;
std::string type;
std::vector<json_counter_metric_t> metrics;
};
REFLECTION(json_counter_t, name, help, type, metrics);
#endif

class counter_t : public metric_t {
public:
Expand All @@ -23,12 +33,8 @@ class counter_t : public metric_t {
// static labels value, contains a map with atomic value.
counter_t(std::string name, std::string help,
std::map<std::string, std::string> labels)
: metric_t(MetricType::Counter, std::move(name), std::move(help)) {
for (auto &[k, v] : labels) {
labels_name_.push_back(k);
labels_value_.push_back(v);
}

: metric_t(MetricType::Counter, std::move(name), std::move(help),
std::move(labels)) {
atomic_value_map_.emplace(labels_value_, 0);
use_atomic_ = true;
}
Expand Down Expand Up @@ -56,7 +62,7 @@ class counter_t : public metric_t {

std::map<std::vector<std::string>, double,
std::less<std::vector<std::string>>>
value_map() {
value_map() override {
std::map<std::vector<std::string>, double,
std::less<std::vector<std::string>>>
map;
Expand Down Expand Up @@ -97,6 +103,44 @@ class counter_t : public metric_t {
}
}

#ifdef CINATRA_ENABLE_METRIC_JSON
void serialize_to_json(std::string &str) override {
std::string s;
if (labels_name_.empty()) {
if (default_lable_value_ == 0) {
return;
}
json_counter_t counter{name_, help_, std::string(metric_name())};
int64_t value = default_lable_value_;
counter.metrics.push_back({{}, value});
iguana::to_json(counter, str);
return;
}

json_counter_t counter{name_, help_, std::string(metric_name())};
if (use_atomic_) {
to_json(counter, atomic_value_map_, str);
}
else {
to_json(counter, value_map_, str);
}
}

template <typename T>
void to_json(json_counter_t &counter, T &map, std::string &str) {
for (auto &[k, v] : map) {
json_counter_metric_t metric;
size_t index = 0;
for (auto &label_value : k) {
metric.labels.emplace(labels_name_[index++], label_value);
}
metric.value = (int64_t)v;
counter.metrics.push_back(std::move(metric));
}
iguana::to_json(counter, str);
}
#endif

void inc(double val = 1) {
if (val < 0) {
throw std::invalid_argument("the value is less than zero");
Expand Down Expand Up @@ -255,4 +299,4 @@ class counter_t : public metric_t {
std::less<std::vector<std::string>>>
value_map_;
};
} // namespace ylt
} // namespace ylt::metric
4 changes: 2 additions & 2 deletions include/ylt/metric/detail/ckms_quantiles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

// https://github.com/jupp0r/prometheus-cpp/blob/master/core/include/prometheus/detail/ckms_quantiles.h

namespace ylt {
namespace ylt::metric {
class CKMSQuantiles {
public:
struct Quantile {
Expand Down Expand Up @@ -172,4 +172,4 @@ class CKMSQuantiles {
std::array<double, 500> buffer_;
std::size_t buffer_count_;
};
} // namespace ylt
} // namespace ylt::metric
4 changes: 2 additions & 2 deletions include/ylt/metric/detail/time_window_quantiles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "ckms_quantiles.hpp"
// https://github.com/jupp0r/prometheus-cpp/blob/master/core/include/prometheus/detail/time_window_quantiles.h

namespace ylt {
namespace ylt::metric {
class TimeWindowQuantiles {
using Clock = std::chrono::steady_clock;

Expand Down Expand Up @@ -49,4 +49,4 @@ class TimeWindowQuantiles {
mutable Clock::time_point last_rotation_;
const Clock::duration rotation_interval_;
};
} // namespace ylt
} // namespace ylt::metric
4 changes: 2 additions & 2 deletions include/ylt/metric/gauge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "counter.hpp"

namespace ylt {
namespace ylt::metric {
class gauge_t : public counter_t {
public:
gauge_t(std::string name, std::string help)
Expand Down Expand Up @@ -49,4 +49,4 @@ class gauge_t : public counter_t {
}
}
};
} // namespace ylt
} // namespace ylt::metric
Loading
Loading