YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/metrics_writer.cc
Line
Count
Source (jump to first uncovered line)
1
//
2
// Copyright (c) YugaByte, Inc.
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
// in compliance with the License.  You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software distributed under the License
10
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
// or implied.  See the License for the specific language governing permissions and limitations
12
// under the License.
13
//
14
//
15
16
#include "yb/util/metrics_writer.h"
17
18
#include <regex>
19
20
#include "yb/util/enums.h"
21
22
namespace yb {
23
24
PrometheusWriter::PrometheusWriter(std::stringstream* output)
25
    : output_(output),
26
      timestamp_(std::chrono::duration_cast<std::chrono::milliseconds>(
27
22
          std::chrono::system_clock::now().time_since_epoch()).count()) {}
28
29
22
PrometheusWriter::~PrometheusWriter() {}
30
31
Status PrometheusWriter::FlushAggregatedValues(const uint32_t& max_tables_metrics_breakdowns,
32
19
                                               std::string priority_regex) {
33
19
  uint32_t counter = 0;
34
19
  const auto& p_regex = std::regex(priority_regex);
35
25
  for (const auto& entry : per_table_values_) {
36
25
    const auto& attrs = per_table_attributes_[entry.first];
37
4.74k
    for (const auto& metric_entry : entry.second) {
38
4.74k
      if (counter < max_tables_metrics_breakdowns ||
39
4.74k
          std::regex_match(metric_entry.first, p_regex)) {
40
4.74k
        RETURN_NOT_OK(FlushSingleEntry(attrs, metric_entry.first, metric_entry.second));
41
4.74k
      }
42
4.74k
    }
43
25
    counter += 1;
44
25
  }
45
19
  return Status::OK();
46
19
}
47
48
Status PrometheusWriter::FlushSingleEntry(const MetricEntity::AttributeMap& attr,
49
37.5k
    const std::string& name, const int64_t& value) {
50
37.5k
  *output_ << name;
51
37.5k
  size_t total_elements = attr.size();
52
37.5k
  if (total_elements > 0) {
53
37.5k
    *output_ << "{";
54
136k
    for (const auto& entry : attr) {
55
136k
      *output_ << entry.first << "=\"" << entry.second << "\"";
56
136k
      if (--total_elements > 0) {
57
98.4k
        *output_ << ",";
58
98.4k
      }
59
136k
    }
60
37.5k
    *output_ << "}";
61
37.5k
  }
62
37.5k
  *output_ << " " << value;
63
37.5k
  *output_ << " " << timestamp_;
64
37.5k
  *output_ << std::endl;
65
37.5k
  return Status::OK();
66
37.5k
}
67
68
0
void PrometheusWriter::InvalidAggregationFunction(AggregationFunction aggregation_function) {
69
0
  FATAL_INVALID_ENUM_VALUE(AggregationFunction, aggregation_function);
70
0
}
71
72
NMSWriter::NMSWriter(EntityMetricsMap* table_metrics, MetricsMap* server_metrics)
73
    : PrometheusWriter(nullptr), table_metrics_(table_metrics),
74
0
      server_metrics_(server_metrics) {}
75
76
Status NMSWriter::FlushSingleEntry(
77
    const MetricEntity::AttributeMap& attr, const std::string& name,
78
0
    const int64_t& value) {
79
0
  auto it = attr.find("metric_type");
80
0
  if (it == attr.end()) {
81
    // ignore.
82
0
  } else if (it->second == "server") {
83
0
    (*server_metrics_)[name] = (int64_t)value;
84
0
  } else if (it->second == "tablet") {
85
0
    auto it2 = attr.find("table_id");
86
0
    if (it2 == attr.end()) {
87
      // ignore.
88
0
    } else {
89
0
      (*table_metrics_)[it2->second][name] = (int64_t)value;
90
0
    }
91
0
  }
92
0
  return Status::OK();
93
0
}
94
95
} // namespace yb