Skip to content

Commit

Permalink
Added type annotations to prometheus output (#159)
Browse files Browse the repository at this point in the history
* Added type annotations to prometheus output

* Updated with patch from Li Shuang
  • Loading branch information
naqvis authored Aug 30, 2023
1 parent 60a956c commit c54a5f8
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/api/stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -862,12 +862,24 @@ void MetricDataSum::serialize(Data::Builder &db, bool initial) {
}

void MetricDataSum::to_prometheus(const std::function<void(const void *, size_t)> &out) const {
static const std::string s_prefix_TYPE("# TYPE ");
static const std::string s_type_counter(" counter\n");
static const std::string s_type_gauge(" gauge\n");
static const std::string s_type_histogram(" histogram\n");
auto print = [&](const std::string &str) { out(str.c_str(), str.length()); };
for (const auto &p : m_entry_map) {
auto ent = p.second;
if (auto root = ent->root.get()) {
print(s_prefix_TYPE);
print(ent->name->str());
const char *le_str = nullptr;
if (utils::starts_with(ent->type->str(), s_prefix_histogram)) {
le_str = ent->type->c_str() + s_prefix_histogram.length();
print(s_type_histogram);
} else if (ent->type->str() == "Gauge") {
print(s_type_gauge);
} else {
print(s_type_counter);
}
if (ent->shape->size() > 0 && ent->labels.empty()) {
auto labels = utils::split(ent->shape->str(), '/');
Expand Down

0 comments on commit c54a5f8

Please sign in to comment.