YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/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
34
          std::chrono::system_clock::now().time_since_epoch()).count()) {}
28
29
34
PrometheusWriter::~PrometheusWriter() {}
30
31
Status PrometheusWriter::FlushAggregatedValues(const uint32_t& max_tables_metrics_breakdowns,
32
31
                                               std::string priority_regex) {
33
31
  uint32_t counter = 0;
34
31
  const auto& p_regex = std::regex(priority_regex);
35
37
  for (const auto& entry : per_table_values_) {
36
37
    const auto& attrs = per_table_attributes_[entry.first];
37
7.03k
    for (const auto& metric_entry : entry.second) {
38
7.03k
      if (counter < max_tables_metrics_breakdowns ||
39
7.03k
          
std::regex_match(metric_entry.first, p_regex)0
) {
40
7.03k
        RETURN_NOT_OK(FlushSingleEntry(attrs, metric_entry.first, metric_entry.second));
41
7.03k
      }
42
7.03k
    }
43
37
    counter += 1;
44
37
  }
45
31
  return Status::OK();
46
31
}
47
48
Status PrometheusWriter::FlushSingleEntry(const MetricEntity::AttributeMap& attr,
49
65.4k
    const std::string& name, const int64_t& value) {
50
65.4k
  *output_ << name;
51
65.4k
  size_t total_elements = attr.size();
52
65.4k
  if (total_elements > 0) {
53
65.4k
    *output_ << "{";
54
235k
    for (const auto& entry : attr) {
55
235k
      *output_ << entry.first << "=\"" << entry.second << "\"";
56
235k
      if (--total_elements > 0) {
57
169k
        *output_ << ",";
58
169k
      }
59
235k
    }
60
65.4k
    *output_ << "}";
61
65.4k
  }
62
65.4k
  *output_ << " " << value;
63
65.4k
  *output_ << " " << timestamp_;
64
65.4k
  *output_ << std::endl;
65
65.4k
  return Status::OK();
66
65.4k
}
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