- for (const auto &name_and_type : types) {
- if (name_and_type.second == TYPE_GAUGE) {
- ss << "# TYPE nageru_" << name_and_type.first << " gauge\n";
- } else if (name_and_type.second == TYPE_HISTOGRAM) {
- ss << "# TYPE nageru_" << name_and_type.first << " histogram\n";
+ auto type_it = types.cbegin();
+ for (const auto &key_and_metric : metrics) {
+ const string &name = key_and_metric.first.serialized;
+ const Metric &metric = key_and_metric.second;
+
+ if (type_it != types.cend() &&
+ key_and_metric.first.name == type_it->first) {
+ // It's the first time we print out any metric with this name,
+ // so add the type header.
+ if (type_it->second == TYPE_GAUGE) {
+ ss << "# TYPE nageru_" << type_it->first << " gauge\n";
+ } else if (type_it->second == TYPE_HISTOGRAM) {
+ ss << "# TYPE nageru_" << type_it->first << " histogram\n";
+ }
+ ++type_it;