• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jupp0r / prometheus-cpp / 4192383612

pending completion
4192383612

Pull #634

github

GitHub
Merge 039bc354a into 1833c1848
Pull Request #634: Update deps

773 of 802 relevant lines covered (96.38%)

109256.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.55
/core/src/check_names.cc
1

2
#include "prometheus/check_names.h"
3

4
#include <algorithm>
5
#include <iterator>
6

7
namespace prometheus {
8

9
namespace {
10
bool isLocaleIndependentAlphaNumeric(char c) {
2,672✔
11
  return ('0' <= c && c <= '9') || ('a' <= c && c <= 'z') ||
2,856✔
12
         ('A' <= c && c <= 'Z');
2,856✔
13
}
14

15
bool isLocaleIndependentDigit(char c) { return '0' <= c && c <= '9'; }
286✔
16

17
bool nameStartsValid(const std::string& name) {
302✔
18
  // must not be empty
19
  if (name.empty()) {
302✔
20
    return false;
16✔
21
  }
22

23
  // must not start with a digit
24
  if (isLocaleIndependentDigit(name.front())) {
286✔
25
    return false;
×
26
  }
27

28
  // must not start with "__"
29
  auto reserved_for_internal_purposes = name.compare(0, 2, "__") == 0;
286✔
30
  if (reserved_for_internal_purposes) return false;
286✔
31

32
  return true;
268✔
33
}
34
}  // anonymous namespace
35

36
/// \brief Check if the metric name is valid
37
///
38
/// The metric name regex is "[a-zA-Z_:][a-zA-Z0-9_:]*"
39
///
40
/// \see https://prometheus.io/docs/concepts/data_model/
41
///
42
/// \param name metric name
43
/// \return true is valid, false otherwise
44
bool CheckMetricName(const std::string& name) {
146✔
45
  if (!nameStartsValid(name)) {
146✔
46
    return false;
6✔
47
  }
48

49
  auto validMetricCharacters = [](char c) {
2,194✔
50
    return isLocaleIndependentAlphaNumeric(c) || c == '_' || c == ':';
2,194✔
51
  };
52

53
  auto mismatch =
54
      std::find_if_not(std::begin(name), std::end(name), validMetricCharacters);
140✔
55
  return mismatch == std::end(name);
140✔
56
}
57

58
/// \brief Check if the label name is valid
59
///
60
/// The label name regex is "[a-zA-Z_][a-zA-Z0-9_]*"
61
///
62
/// \see https://prometheus.io/docs/concepts/data_model/
63
///
64
/// \param name label name
65
/// \param type metric type
66
/// \return true is valid, false otherwise
67
bool CheckLabelName(const std::string& name, MetricType type) {
156✔
68
  if (!nameStartsValid(name)) {
156✔
69
    return false;
28✔
70
  }
71

72
  auto validLabelCharacters = [](char c) {
478✔
73
    return isLocaleIndependentAlphaNumeric(c) || c == '_';
478✔
74
  };
75

76
  if ((type == MetricType::Histogram && name == "le") ||
148✔
77
      (type == MetricType::Summary && name == "quantile")) {
20✔
78
    return false;
12✔
79
  }
80

81
  auto mismatch =
82
      std::find_if_not(std::begin(name), std::end(name), validLabelCharacters);
116✔
83
  return mismatch == std::end(name);
116✔
84
}
85
}  // namespace prometheus
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc