Skip to content

Commit 77092bc

Browse files
committed
fix prometheus metadata and sorted. Adds in TYPE and HELP
1 parent 6dc259c commit 77092bc

File tree

1 file changed

+105
-27
lines changed

1 file changed

+105
-27
lines changed

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

Lines changed: 105 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -512,20 +512,48 @@ public void updateMetrics() {
512512
public String getMetrics() {
513513
final StringBuilder stringBuilder = new StringBuilder();
514514
stringBuilder.append("# Cloudstack Prometheus Metrics\n");
515-
for (final Item item : metricsItems) {
515+
516+
List<Item> sortedItems = metricsItems.stream()
517+
.sorted((item1, item2) -> item1.name.compareTo(item2.name))
518+
.collect(Collectors.toList());
519+
520+
String currentMetricName = null;
521+
522+
for (Item item : sortedItems) {
523+
if (!item.name.equals(currentMetricName)) {
524+
currentMetricName = item.name;
525+
stringBuilder.append("# HELP ").append(currentMetricName).append(" ")
526+
.append(item.getHelp()).append("\n");
527+
stringBuilder.append("# TYPE ").append(currentMetricName).append(" ")
528+
.append(item.getType()).append("\n");
529+
}
530+
516531
stringBuilder.append(item.toMetricsString()).append("\n");
517532
}
533+
518534
return stringBuilder.toString();
519535
}
520536

521537
private abstract class Item {
522538
String name;
539+
String help;
540+
String type;
523541

524-
public Item(final String nm) {
542+
public Item(final String nm, final String hlp, final String tp) {
525543
name = nm;
544+
help = hlp;
545+
type = tp;
526546
}
527547

528548
public abstract String toMetricsString();
549+
550+
public String getHelp() {
551+
return help;
552+
}
553+
554+
public String getType() {
555+
return type;
556+
}
529557
}
530558

531559
class ItemVM extends Item {
@@ -535,7 +563,9 @@ class ItemVM extends Item {
535563
long total;
536564

537565
public ItemVM(final String zn, final String zu, final String st, long cnt) {
538-
super("cloudstack_vms_total");
566+
super("cloudstack_vms_total",
567+
"Total number of virtual machines",
568+
"gauge");
539569
zoneName = zn;
540570
zoneUuid = zu;
541571
filter = st;
@@ -556,7 +586,9 @@ class ItemVMByTag extends Item {
556586
String hosttags;
557587

558588
public ItemVMByTag(final String zn, final String zu, final String st, long cnt, final String tags) {
559-
super("cloudstack_vms_total_by_tag");
589+
super("cloudstack_vms_total_by_tag",
590+
"Total number of virtual machines grouped by host tags",
591+
"gauge");
560592
zoneName = zn;
561593
zoneUuid = zu;
562594
filter = st;
@@ -577,7 +609,9 @@ class ItemVolume extends Item {
577609
int total;
578610

579611
public ItemVolume(final String zn, final String zu, final String st, int cnt) {
580-
super("cloudstack_volumes_total");
612+
super("cloudstack_volumes_total",
613+
"Total number of volumes",
614+
"gauge");
581615
zoneName = zn;
582616
zoneUuid = zu;
583617
filter = st;
@@ -598,7 +632,9 @@ class ItemHost extends Item {
598632
String hosttags;
599633

600634
public ItemHost(final String zn, final String zu, final String st, int cnt, final String tags) {
601-
super("cloudstack_hosts_total");
635+
super("cloudstack_hosts_total",
636+
"Total number of hosts",
637+
"gauge");
602638
zoneName = zn;
603639
zoneUuid = zu;
604640
state = st;
@@ -610,6 +646,7 @@ public ItemHost(final String zn, final String zu, final String st, int cnt, fina
610646
public String toMetricsString() {
611647
if (StringUtils.isNotEmpty(hosttags)) {
612648
name = "cloudstack_hosts_total_by_tag";
649+
help = "Total number of hosts grouped by tags";
613650
return String.format("%s{zone=\"%s\",filter=\"%s\",tags=\"%s\"} %d", name, zoneName, state, hosttags, total);
614651
}
615652
return String.format("%s{zone=\"%s\",filter=\"%s\"} %d", name, zoneName, state, total);
@@ -627,8 +664,11 @@ class ItemVMCore extends Item {
627664
int isDedicated;
628665
String hosttags;
629666

630-
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) {
631-
super("cloudstack_host_vms_cores_total");
667+
public ItemVMCore(final String zn, final String zu, final String hn, final String hu, final String hip,
668+
final String fl, final Long cr, final int dedicated, final String tags) {
669+
super("cloudstack_host_vms_cores_total",
670+
"Total number of VM cores on hosts",
671+
"gauge");
632672
zoneName = zn;
633673
zoneUuid = zu;
634674
hostName = hn;
@@ -649,6 +689,7 @@ public String toMetricsString() {
649689
return String.format("%s{zone=\"%s\",filter=\"%s\"} %d", name, zoneName, filter, core);
650690
} else {
651691
name = "cloudstack_host_vms_cores_total_by_tag";
692+
help = "Total number of VM cores grouped by host tags";
652693
return String.format("%s{zone=\"%s\",filter=\"%s\",tags=\"%s\"} %d", name, zoneName, filter, hosttags, core);
653694
}
654695
}
@@ -657,13 +698,14 @@ public String toMetricsString() {
657698
}
658699

659700
class MissingHostInfo extends Item {
660-
661701
String zoneName;
662702
String hostName;
663703
MissingInfoFilter filter;
664704

665705
public MissingHostInfo(String zoneName, String hostname, MissingInfoFilter filter) {
666-
super("cloudstack_host_missing_info");
706+
super("cloudstack_host_missing_info",
707+
"Hosts with missing capacity or statistics information",
708+
"gauge");
667709
this.zoneName = zoneName;
668710
this.hostName = hostname;
669711
this.filter = filter;
@@ -688,7 +730,9 @@ class ItemHostCpu extends Item {
688730
String hosttags;
689731

690732
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) {
691-
super("cloudstack_host_cpu_usage_mhz_total");
733+
super("cloudstack_host_cpu_usage_mhz_total",
734+
"Host CPU usage in MHz",
735+
"gauge");
692736
zoneName = zn;
693737
zoneUuid = zu;
694738
hostName = hn;
@@ -708,6 +752,7 @@ public String toMetricsString() {
708752
return String.format("%s{zone=\"%s\",filter=\"%s\"} %.2f", name, zoneName, filter, mhertz);
709753
} else {
710754
name = "cloudstack_host_cpu_usage_mhz_total_by_tag";
755+
help = "Host CPU usage in MHz grouped by host tags";
711756
return String.format("%s{zone=\"%s\",filter=\"%s\",tags=\"%s\"} %.2f", name, zoneName, filter, hosttags, mhertz);
712757
}
713758
}
@@ -728,7 +773,9 @@ class ItemHostMemory extends Item {
728773
String hosttags;
729774

730775
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) {
731-
super("cloudstack_host_memory_usage_mibs_total");
776+
super("cloudstack_host_memory_usage_mibs_total",
777+
"Host memory usage in MiB",
778+
"gauge");
732779
zoneName = zn;
733780
zoneUuid = zu;
734781
hostName = hn;
@@ -748,6 +795,7 @@ public String toMetricsString() {
748795
return String.format("%s{zone=\"%s\",filter=\"%s\"} %.2f", name, zoneName, filter, miBytes);
749796
} else {
750797
name = "cloudstack_host_memory_usage_mibs_total_by_tag";
798+
help = "Host memory usage in MiB grouped by host tags";
751799
return String.format("%s{zone=\"%s\",filter=\"%s\",tags=\"%s\"} %.2f", name, zoneName, filter, hosttags, miBytes);
752800
}
753801
}
@@ -764,7 +812,9 @@ class ItemHostVM extends Item {
764812
int total;
765813

766814
public ItemHostVM(final String zoneName, final String zoneUuid, final String hostName, final String hostUuid, final String hostIp, final int total) {
767-
super("cloudstack_host_vms_total");
815+
super("cloudstack_host_vms_total",
816+
"Total number of VMs per host",
817+
"gauge");
768818
this.zoneName = zoneName;
769819
this.zoneUuid = zoneUuid;
770820
this.hostName = hostName;
@@ -790,7 +840,9 @@ class ItemPool extends Item {
790840
double total;
791841

792842
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) {
793-
super("cloudstack_storage_pool_gibs_total");
843+
super("cloudstack_storage_pool_gibs_total",
844+
"Storage pool capacity in GiB",
845+
"gauge");
794846
zoneName = zn;
795847
zoneUuid = zu;
796848
pname = pn;
@@ -817,7 +869,9 @@ class ItemPrivateIp extends Item {
817869
int total;
818870

819871
public ItemPrivateIp(final String zn, final String zu, final String fl, int cnt) {
820-
super("cloudstack_private_ips_total");
872+
super("cloudstack_private_ips_total",
873+
"Total number of private IP addresses",
874+
"gauge");
821875
zoneName = zn;
822876
zoneUuid = zu;
823877
filter = fl;
@@ -837,7 +891,9 @@ class ItemPublicIp extends Item {
837891
int total;
838892

839893
public ItemPublicIp(final String zn, final String zu, final String fl, int cnt) {
840-
super("cloudstack_public_ips_total");
894+
super("cloudstack_public_ips_total",
895+
"Total number of public IP addresses",
896+
"gauge");
841897
zoneName = zn;
842898
zoneUuid = zu;
843899
filter = fl;
@@ -857,7 +913,9 @@ class ItemSharedNetworkIp extends Item {
857913
int total;
858914

859915
public ItemSharedNetworkIp(final String zn, final String zu, final String fl, int cnt) {
860-
super("cloudstack_shared_network_ips_total");
916+
super("cloudstack_shared_network_ips_total",
917+
"Total number of shared network IP addresses",
918+
"gauge");
861919
zoneName = zn;
862920
zoneUuid = zu;
863921
filter = fl;
@@ -877,7 +935,9 @@ class ItemVlan extends Item {
877935
int total;
878936

879937
public ItemVlan(final String zn, final String zu, final String fl, int cnt) {
880-
super("cloudstack_vlans_total");
938+
super("cloudstack_vlans_total",
939+
"Total number of VLANs",
940+
"gauge");
881941
zoneName = zn;
882942
zoneUuid = zu;
883943
filter = fl;
@@ -894,7 +954,9 @@ class ItemDomainLimitCpu extends Item {
894954
long cores;
895955

896956
public ItemDomainLimitCpu(final long c) {
897-
super("cloudstack_domain_limit_cpu_cores_total");
957+
super("cloudstack_domain_limit_cpu_cores_total",
958+
"Total CPU core limit across all domains",
959+
"gauge");
898960
cores = c;
899961
}
900962

@@ -908,7 +970,9 @@ class ItemDomainLimitMemory extends Item {
908970
long miBytes;
909971

910972
public ItemDomainLimitMemory(final long mb) {
911-
super("cloudstack_domain_limit_memory_mibs_total");
973+
super("cloudstack_domain_limit_memory_mibs_total",
974+
"Total memory limit in MiB across all domains",
975+
"gauge");
912976
miBytes = mb;
913977
}
914978

@@ -927,7 +991,9 @@ class ItemHostIsDedicated extends Item {
927991
int isDedicated;
928992

929993
public ItemHostIsDedicated(final String zoneName, final String zoneUuid, final String hostName, final String hostUuid, final String hostIp, final int isDedicated) {
930-
super("cloudstack_host_is_dedicated");
994+
super("cloudstack_host_is_dedicated",
995+
"Whether a host is dedicated (1) or not (0)",
996+
"gauge");
931997
this.zoneName = zoneName;
932998
this.zoneUuid = zoneUuid;
933999
this.hostName = hostName;
@@ -949,7 +1015,9 @@ class ItemActiveDomains extends Item {
9491015
int total;
9501016

9511017
public ItemActiveDomains(final String zn, final String zu, final int cnt) {
952-
super("cloudstack_active_domains_total");
1018+
super("cloudstack_active_domains_total",
1019+
"Total number of active domains",
1020+
"gauge");
9531021
zoneName = zn;
9541022
zoneUuid = zu;
9551023
total = cnt;
@@ -970,7 +1038,9 @@ class ItemHostDedicatedToAccount extends Item {
9701038

9711039
public ItemHostDedicatedToAccount(final String zoneName, final String hostName,
9721040
final String accountName, final String domainName, int isDedicated) {
973-
super("cloudstack_host_dedicated_to_account");
1041+
super("cloudstack_host_dedicated_to_account",
1042+
"Host dedication to specific account",
1043+
"gauge");
9741044
this.zoneName = zoneName;
9751045
this.hostName = hostName;
9761046
this.accountName = accountName;
@@ -991,7 +1061,9 @@ class ItemPerDomainResourceLimit extends Item {
9911061
String resourceType;
9921062

9931063
public ItemPerDomainResourceLimit(final long c, final String domainName, final String resourceType) {
994-
super("cloudstack_domain_resource_limit");
1064+
super("cloudstack_domain_resource_limit",
1065+
"Resource limits per domain",
1066+
"gauge");
9951067
this.cores = c;
9961068
this.domainName = domainName;
9971069
this.resourceType = resourceType;
@@ -1009,7 +1081,9 @@ class ItemPerDomainResourceCount extends Item {
10091081
String resourceType;
10101082

10111083
public ItemPerDomainResourceCount(final long mb, final String domainName, final String resourceType) {
1012-
super("cloudstack_domain_resource_count");
1084+
super("cloudstack_domain_resource_count",
1085+
"Resource usage count per domain",
1086+
"gauge");
10131087
this.miBytes = mb;
10141088
this.domainName = domainName;
10151089
this.resourceType = resourceType;
@@ -1027,7 +1101,9 @@ class ItemActiveAccounts extends Item {
10271101
int total;
10281102

10291103
public ItemActiveAccounts(final String zn, final String zu, final int cnt) {
1030-
super("cloudstack_active_accounts_total");
1104+
super("cloudstack_active_accounts_total",
1105+
"Total number of active accounts",
1106+
"gauge");
10311107
zoneName = zn;
10321108
zoneUuid = zu;
10331109
total = cnt;
@@ -1047,7 +1123,9 @@ class ItemVMsBySize extends Item {
10471123
int total;
10481124

10491125
public ItemVMsBySize(final String zn, final String zu, final int c, final int m, int cnt) {
1050-
super("cloudstack_vms_total_by_size");
1126+
super("cloudstack_vms_total_by_size",
1127+
"Total number of VMs grouped by CPU and memory size",
1128+
"gauge");
10511129
zoneName = zn;
10521130
zoneUuid = zu;
10531131
cpu = c;

0 commit comments

Comments
 (0)