}
string Metrics::serialize_name(const string &name, const vector<pair<string, string>> &labels)
+{
+ return "nageru_" + name + serialize_labels(labels);
+}
+
+string Metrics::serialize_labels(const vector<pair<string, string>> &labels)
{
if (labels.empty()) {
- return "nageru_" + name;
+ return "";
}
string label_str;
}
label_str += label.first + "=\"" + label.second + "\"";
}
- return "nageru_" + name + "{" + label_str + "}";
+ return "{" + label_str + "}";
}
void Metrics::add(const string &name, const vector<pair<string, string>> &labels, atomic<int64_t> *location, Metrics::Type type)
lock_guard<mutex> lock(mu);
auto type_it = types.cbegin();
for (const auto &key_and_metric : metrics) {
- const string &name = key_and_metric.first.serialized;
+ string name = "nageru_" + key_and_metric.first.name + key_and_metric.first.serialized_labels;
const Metric &metric = key_and_metric.second;
if (type_it != types.cend() &&
private:
static std::string serialize_name(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels);
+ static std::string serialize_labels(const std::vector<std::pair<std::string, std::string>> &labels);
enum DataType {
DATA_TYPE_INT64,
};
struct MetricKey {
MetricKey(const std::string &name, const std::vector<std::pair<std::string, std::string>> labels)
- : name(name), labels(labels), serialized(serialize_name(name, labels))
+ : name(name), labels(labels), serialized_labels(serialize_labels(labels))
{
}
bool operator< (const MetricKey &other) const
{
- return serialized < other.serialized;
+ if (name != other.name)
+ return name < other.name;
+ return serialized_labels < other.serialized_labels;
}
const std::string name;
const std::vector<std::pair<std::string, std::string>> labels;
- const std::string serialized;
+ const std::string serialized_labels;
};
struct Metric {
DataType data_type;
};
mutable std::mutex mu;
- std::map<std::string, Type> types; // Ordered the same as metrics, assuming no { signs in names (which are illegal).
+ std::map<std::string, Type> types; // Ordered the same as metrics.
std::map<MetricKey, Metric> metrics;
std::vector<Histogram> histograms;