Skip to content

Commit 7e48fcb

Browse files
Sinscerlyowsferraro
authored andcommitted
Fix/prometheus metadata sorting (apache#12112)
* fix prometheus metadata and sorted. Adds in TYPE and HELP * fix random linting issue
1 parent 2f59860 commit 7e48fcb

File tree

1 file changed

+103
-26
lines changed

1 file changed

+103
-26
lines changed

plugins/integrations/prometheus/src/main/java/org/apache/cloudstack/metrics/PrometheusExporterImpl.java

Lines changed: 103 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -531,20 +531,48 @@ public void updateMetrics() {
531531
public String getMetrics() {
532532
final StringBuilder stringBuilder = new StringBuilder();
533533
stringBuilder.append("# Cloudstack Prometheus Metrics\n");
534-
for (final Item item : metricsItems) {
534+
535+
List<Item> sortedItems = metricsItems.stream()
536+
.sorted((item1, item2) -> item1.name.compareTo(item2.name))
537+
.collect(Collectors.toList());
538+
539+
String currentMetricName = null;
540+
541+
for (Item item : sortedItems) {
542+
if (!item.name.equals(currentMetricName)) {
543+
currentMetricName = item.name;
544+
stringBuilder.append("# HELP ").append(currentMetricName).append(" ")
545+
.append(item.getHelp()).append("\n");
546+
stringBuilder.append("# TYPE ").append(currentMetricName).append(" ")
547+
.append(item.getType()).append("\n");
548+
}
549+
535550
stringBuilder.append(item.toMetricsString()).append("\n");
536551
}
552+
537553
return stringBuilder.toString();
538554
}
539555

540556
private abstract class Item {
541557
String name;
558+
String help;
559+
String type;
542560

543-
public Item(final String nm) {
561+
public Item(final String nm, final String hlp, final String tp) {
544562
name = nm;
563+
help = hlp;
564+
type = tp;
545565
}
546566

547567
public abstract String toMetricsString();
568+
569+
public String getHelp() {
570+
return help;
571+
}
572+
573+
public String getType() {
574+
return type;
575+
}
548576
}
549577

550578
class ItemVM extends Item {
@@ -554,7 +582,9 @@ class ItemVM extends Item {
554582
long total;
555583

556584
public ItemVM(final String zn, final String zu, final String st, long cnt) {
557-
super("cloudstack_vms_total");
585+
super("cloudstack_vms_total",
586+
"Total number of virtual machines",
587+
"gauge");
558588
zoneName = zn;
559589
zoneUuid = zu;
560590
filter = st;
@@ -575,7 +605,9 @@ class ItemVMByTag extends Item {
575605
String hosttags;
576606

577607
public ItemVMByTag(final String zn, final String zu, final String st, long cnt, final String tags) {
578-
super("cloudstack_vms_total_by_tag");
608+
super("cloudstack_vms_total_by_tag",
609+
"Total number of virtual machines grouped by host tags",
610+
"gauge");
579611
zoneName = zn;
580612
zoneUuid = zu;
581613
filter = st;
@@ -596,7 +628,9 @@ class ItemVolume extends Item {
596628
int total;
597629

598630
public ItemVolume(final String zn, final String zu, final String st, int cnt) {
599-
super("cloudstack_volumes_total");
631+
super("cloudstack_volumes_total",
632+
"Total number of volumes",
633+
"gauge");
600634
zoneName = zn;
601635
zoneUuid = zu;
602636
filter = st;
@@ -617,7 +651,9 @@ class ItemHost extends Item {
617651
String hosttags;
618652

619653
public ItemHost(final String zn, final String zu, final String st, int cnt, final String tags) {
620-
super("cloudstack_hosts_total");
654+
super("cloudstack_hosts_total",
655+
"Total number of hosts",
656+
"gauge");
621657
zoneName = zn;
622658
zoneUuid = zu;
623659
state = st;
@@ -629,6 +665,7 @@ public ItemHost(final String zn, final String zu, final String st, int cnt, fina
629665
public String toMetricsString() {
630666
if (StringUtils.isNotEmpty(hosttags)) {
631667
name = "cloudstack_hosts_total_by_tag";
668+
help = "Total number of hosts grouped by tags";
632669
return String.format("%s{zone=\"%s\",filter=\"%s\",tags=\"%s\"} %d", name, zoneName, state, hosttags, total);
633670
}
634671
return String.format("%s{zone=\"%s\",filter=\"%s\"} %d", name, zoneName, state, total);
@@ -647,7 +684,9 @@ class ItemVMCore extends Item {
647684
String hosttags;
648685

649686
public ItemVMCore(final String zn, final String zu, final String hn, final String hu, final String hip, final String fl, final Long cr, final int dedicated, final String tags) {
650-
super("cloudstack_host_vms_cores_total");
687+
super("cloudstack_host_vms_cores_total",
688+
"Total number of VM cores on hosts",
689+
"gauge");
651690
zoneName = zn;
652691
zoneUuid = zu;
653692
hostName = hn;
@@ -668,6 +707,7 @@ public String toMetricsString() {
668707
return String.format("%s{zone=\"%s\",filter=\"%s\"} %d", name, zoneName, filter, core);
669708
} else {
670709
name = "cloudstack_host_vms_cores_total_by_tag";
710+
help = "Total number of VM cores grouped by host tags";
671711
return String.format("%s{zone=\"%s\",filter=\"%s\",tags=\"%s\"} %d", name, zoneName, filter, hosttags, core);
672712
}
673713
}
@@ -676,13 +716,14 @@ public String toMetricsString() {
676716
}
677717

678718
class MissingHostInfo extends Item {
679-
680719
String zoneName;
681720
String hostName;
682721
MissingInfoFilter filter;
683722

684723
public MissingHostInfo(String zoneName, String hostname, MissingInfoFilter filter) {
685-
super("cloudstack_host_missing_info");
724+
super("cloudstack_host_missing_info",
725+
"Hosts with missing capacity or statistics information",
726+
"gauge");
686727
this.zoneName = zoneName;
687728
this.hostName = hostname;
688729
this.filter = filter;
@@ -707,7 +748,9 @@ class ItemHostCpu extends Item {
707748
String hosttags;
708749

709750
public ItemHostCpu(final String zn, final String zu, final String hn, final String hu, final String hip, final String of, final String fl, final double mh, final int dedicated, final String tags) {
710-
super("cloudstack_host_cpu_usage_mhz_total");
751+
super("cloudstack_host_cpu_usage_mhz_total",
752+
"Host CPU usage in MHz",
753+
"gauge");
711754
zoneName = zn;
712755
zoneUuid = zu;
713756
hostName = hn;
@@ -727,6 +770,7 @@ public String toMetricsString() {
727770
return String.format("%s{zone=\"%s\",filter=\"%s\"} %.2f", name, zoneName, filter, mhertz);
728771
} else {
729772
name = "cloudstack_host_cpu_usage_mhz_total_by_tag";
773+
help = "Host CPU usage in MHz grouped by host tags";
730774
return String.format("%s{zone=\"%s\",filter=\"%s\",tags=\"%s\"} %.2f", name, zoneName, filter, hosttags, mhertz);
731775
}
732776
}
@@ -747,7 +791,9 @@ class ItemHostMemory extends Item {
747791
String hosttags;
748792

749793
public ItemHostMemory(final String zn, final String zu, final String hn, final String hu, final String hip, final String of, final String fl, final double membytes, final int dedicated, final String tags) {
750-
super("cloudstack_host_memory_usage_mibs_total");
794+
super("cloudstack_host_memory_usage_mibs_total",
795+
"Host memory usage in MiB",
796+
"gauge");
751797
zoneName = zn;
752798
zoneUuid = zu;
753799
hostName = hn;
@@ -767,6 +813,7 @@ public String toMetricsString() {
767813
return String.format("%s{zone=\"%s\",filter=\"%s\"} %.2f", name, zoneName, filter, miBytes);
768814
} else {
769815
name = "cloudstack_host_memory_usage_mibs_total_by_tag";
816+
help = "Host memory usage in MiB grouped by host tags";
770817
return String.format("%s{zone=\"%s\",filter=\"%s\",tags=\"%s\"} %.2f", name, zoneName, filter, hosttags, miBytes);
771818
}
772819
}
@@ -783,7 +830,9 @@ class ItemHostVM extends Item {
783830
int total;
784831

785832
public ItemHostVM(final String zoneName, final String zoneUuid, final String hostName, final String hostUuid, final String hostIp, final int total) {
786-
super("cloudstack_host_vms_total");
833+
super("cloudstack_host_vms_total",
834+
"Total number of VMs per host",
835+
"gauge");
787836
this.zoneName = zoneName;
788837
this.zoneUuid = zoneUuid;
789838
this.hostName = hostName;
@@ -809,7 +858,9 @@ class ItemPool extends Item {
809858
double total;
810859

811860
public ItemPool(final String zn, final String zu, final String pn, final String pa, final String typ, final String of, final String fl, double cnt) {
812-
super("cloudstack_storage_pool_gibs_total");
861+
super("cloudstack_storage_pool_gibs_total",
862+
"Storage pool capacity in GiB",
863+
"gauge");
813864
zoneName = zn;
814865
zoneUuid = zu;
815866
pname = pn;
@@ -836,7 +887,9 @@ class ItemPrivateIp extends Item {
836887
int total;
837888

838889
public ItemPrivateIp(final String zn, final String zu, final String fl, int cnt) {
839-
super("cloudstack_private_ips_total");
890+
super("cloudstack_private_ips_total",
891+
"Total number of private IP addresses",
892+
"gauge");
840893
zoneName = zn;
841894
zoneUuid = zu;
842895
filter = fl;
@@ -856,7 +909,9 @@ class ItemPublicIp extends Item {
856909
int total;
857910

858911
public ItemPublicIp(final String zn, final String zu, final String fl, int cnt) {
859-
super("cloudstack_public_ips_total");
912+
super("cloudstack_public_ips_total",
913+
"Total number of public IP addresses",
914+
"gauge");
860915
zoneName = zn;
861916
zoneUuid = zu;
862917
filter = fl;
@@ -876,7 +931,9 @@ class ItemSharedNetworkIp extends Item {
876931
int total;
877932

878933
public ItemSharedNetworkIp(final String zn, final String zu, final String fl, int cnt) {
879-
super("cloudstack_shared_network_ips_total");
934+
super("cloudstack_shared_network_ips_total",
935+
"Total number of shared network IP addresses",
936+
"gauge");
880937
zoneName = zn;
881938
zoneUuid = zu;
882939
filter = fl;
@@ -896,7 +953,9 @@ class ItemVlan extends Item {
896953
int total;
897954

898955
public ItemVlan(final String zn, final String zu, final String fl, int cnt) {
899-
super("cloudstack_vlans_total");
956+
super("cloudstack_vlans_total",
957+
"Total number of VLANs",
958+
"gauge");
900959
zoneName = zn;
901960
zoneUuid = zu;
902961
filter = fl;
@@ -913,7 +972,9 @@ class ItemDomainLimitCpu extends Item {
913972
long cores;
914973

915974
public ItemDomainLimitCpu(final long c) {
916-
super("cloudstack_domain_limit_cpu_cores_total");
975+
super("cloudstack_domain_limit_cpu_cores_total",
976+
"Total CPU core limit across all domains",
977+
"gauge");
917978
cores = c;
918979
}
919980

@@ -927,7 +988,9 @@ class ItemDomainLimitMemory extends Item {
927988
long miBytes;
928989

929990
public ItemDomainLimitMemory(final long mb) {
930-
super("cloudstack_domain_limit_memory_mibs_total");
991+
super("cloudstack_domain_limit_memory_mibs_total",
992+
"Total memory limit in MiB across all domains",
993+
"gauge");
931994
miBytes = mb;
932995
}
933996

@@ -946,7 +1009,9 @@ class ItemHostIsDedicated extends Item {
9461009
int isDedicated;
9471010

9481011
public ItemHostIsDedicated(final String zoneName, final String zoneUuid, final String hostName, final String hostUuid, final String hostIp, final int isDedicated) {
949-
super("cloudstack_host_is_dedicated");
1012+
super("cloudstack_host_is_dedicated",
1013+
"Whether a host is dedicated (1) or not (0)",
1014+
"gauge");
9501015
this.zoneName = zoneName;
9511016
this.zoneUuid = zoneUuid;
9521017
this.hostName = hostName;
@@ -968,7 +1033,9 @@ class ItemActiveDomains extends Item {
9681033
int total;
9691034

9701035
public ItemActiveDomains(final String zn, final String zu, final int cnt) {
971-
super("cloudstack_active_domains_total");
1036+
super("cloudstack_active_domains_total",
1037+
"Total number of active domains",
1038+
"gauge");
9721039
zoneName = zn;
9731040
zoneUuid = zu;
9741041
total = cnt;
@@ -989,7 +1056,9 @@ class ItemHostDedicatedToAccount extends Item {
9891056

9901057
public ItemHostDedicatedToAccount(final String zoneName, final String hostName,
9911058
final String accountName, final String domainName, int isDedicated) {
992-
super("cloudstack_host_dedicated_to_account");
1059+
super("cloudstack_host_dedicated_to_account",
1060+
"Host dedication to specific account",
1061+
"gauge");
9931062
this.zoneName = zoneName;
9941063
this.hostName = hostName;
9951064
this.accountName = accountName;
@@ -1010,7 +1079,9 @@ class ItemPerDomainResourceLimit extends Item {
10101079
String resourceType;
10111080

10121081
public ItemPerDomainResourceLimit(final long c, final String domainName, final String resourceType) {
1013-
super("cloudstack_domain_resource_limit");
1082+
super("cloudstack_domain_resource_limit",
1083+
"Resource limits per domain",
1084+
"gauge");
10141085
this.cores = c;
10151086
this.domainName = domainName;
10161087
this.resourceType = resourceType;
@@ -1028,7 +1099,9 @@ class ItemPerDomainResourceCount extends Item {
10281099
String resourceType;
10291100

10301101
public ItemPerDomainResourceCount(final long mb, final String domainName, final String resourceType) {
1031-
super("cloudstack_domain_resource_count");
1102+
super("cloudstack_domain_resource_count",
1103+
"Resource usage count per domain",
1104+
"gauge");
10321105
this.miBytes = mb;
10331106
this.domainName = domainName;
10341107
this.resourceType = resourceType;
@@ -1046,7 +1119,9 @@ class ItemActiveAccounts extends Item {
10461119
int total;
10471120

10481121
public ItemActiveAccounts(final String zn, final String zu, final int cnt) {
1049-
super("cloudstack_active_accounts_total");
1122+
super("cloudstack_active_accounts_total",
1123+
"Total number of active accounts",
1124+
"gauge");
10501125
zoneName = zn;
10511126
zoneUuid = zu;
10521127
total = cnt;
@@ -1066,7 +1141,9 @@ class ItemVMsBySize extends Item {
10661141
int total;
10671142

10681143
public ItemVMsBySize(final String zn, final String zu, final int c, final int m, int cnt) {
1069-
super("cloudstack_vms_total_by_size");
1144+
super("cloudstack_vms_total_by_size",
1145+
"Total number of VMs grouped by CPU and memory size",
1146+
"gauge");
10701147
zoneName = zn;
10711148
zoneUuid = zu;
10721149
cpu = c;

0 commit comments

Comments
 (0)