Skip to content

Commit 5f829c1

Browse files
author
Sina Kashipazha
committed
Extract repeated codes to new methods.
1 parent 0689f97 commit 5f829c1

File tree

1 file changed

+56
-47
lines changed

1 file changed

+56
-47
lines changed

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

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -142,23 +142,7 @@ private void addHostMetrics(final List<Item> metricsList, final long dcId, final
142142
int isDedicated = (dr != null) ? 1 : 0;
143143
metricsList.add(new ItemHostIsDedicated(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), isDedicated));
144144

145-
List<String> hostTags = _hostTagsDao.getHostTags(host.getId());
146-
String hosttags = StringUtils.join(hostTags, ",");
147-
for (String tag : hostTags) {
148-
Integer current = totalHosts.get(tag) != null ? totalHosts.get(tag) : 0;
149-
totalHosts.put(tag, current + 1);
150-
}
151-
if (host.getStatus() == Status.Up) {
152-
for (String tag : hostTags) {
153-
Integer current = upHosts.get(tag) != null ? upHosts.get(tag) : 0;
154-
upHosts.put(tag, current + 1);
155-
}
156-
} else if (host.getStatus() == Status.Disconnected || host.getStatus() == Status.Down) {
157-
for (String tag : hostTags) {
158-
Integer current = downHosts.get(tag) != null ? downHosts.get(tag) : 0;
159-
downHosts.put(tag, current + 1);
160-
}
161-
}
145+
String hostTags = markTagMaps(host, totalHosts, upHosts, downHosts);
162146

163147
// Get account, domain details for dedicated hosts
164148
if (isDedicated == 1) {
@@ -173,32 +157,32 @@ private void addHostMetrics(final List<Item> metricsList, final long dcId, final
173157
final String cpuFactor = String.valueOf(CapacityManager.CpuOverprovisioningFactor.valueIn(host.getClusterId()));
174158
final CapacityVO cpuCapacity = capacityDao.findByHostIdType(host.getId(), Capacity.CAPACITY_TYPE_CPU);
175159
if (cpuCapacity != null) {
176-
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, USED, cpuCapacity.getUsedCapacity(), isDedicated, hosttags));
177-
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, TOTAL, cpuCapacity.getTotalCapacity(), isDedicated, hosttags));
160+
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, USED, cpuCapacity.getUsedCapacity(), isDedicated, hostTags));
161+
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, TOTAL, cpuCapacity.getTotalCapacity(), isDedicated, hostTags));
178162
} else {
179-
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, USED, 0L, isDedicated, hosttags));
180-
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, TOTAL, 0L, isDedicated, hosttags));
163+
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, USED, 0L, isDedicated, hostTags));
164+
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, TOTAL, 0L, isDedicated, hostTags));
181165
}
182166

183167
final String memoryFactor = String.valueOf(CapacityManager.MemOverprovisioningFactor.valueIn(host.getClusterId()));
184168
final CapacityVO memCapacity = capacityDao.findByHostIdType(host.getId(), Capacity.CAPACITY_TYPE_MEMORY);
185169
if (memCapacity != null) {
186-
metricsList.add(new ItemHostMemory(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), memoryFactor, USED, memCapacity.getUsedCapacity(), isDedicated, hosttags));
187-
metricsList.add(new ItemHostMemory(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), memoryFactor, TOTAL, memCapacity.getTotalCapacity(), isDedicated, hosttags));
170+
metricsList.add(new ItemHostMemory(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), memoryFactor, USED, memCapacity.getUsedCapacity(), isDedicated, hostTags));
171+
metricsList.add(new ItemHostMemory(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), memoryFactor, TOTAL, memCapacity.getTotalCapacity(), isDedicated, hostTags));
188172
} else {
189-
metricsList.add(new ItemHostMemory(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), memoryFactor, USED, 0L, isDedicated, hosttags));
190-
metricsList.add(new ItemHostMemory(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), memoryFactor, TOTAL, 0L, isDedicated, hosttags));
173+
metricsList.add(new ItemHostMemory(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), memoryFactor, USED, 0L, isDedicated, hostTags));
174+
metricsList.add(new ItemHostMemory(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), memoryFactor, TOTAL, 0L, isDedicated, hostTags));
191175
}
192176

193177
metricsList.add(new ItemHostVM(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), vmDao.listByHostId(host.getId()).size()));
194178

195179
final CapacityVO coreCapacity = capacityDao.findByHostIdType(host.getId(), Capacity.CAPACITY_TYPE_CPU_CORE);
196180
if (coreCapacity != null) {
197-
metricsList.add(new ItemVMCore(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), USED, coreCapacity.getUsedCapacity(), isDedicated, hosttags));
198-
metricsList.add(new ItemVMCore(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), TOTAL, coreCapacity.getTotalCapacity(), isDedicated, hosttags));
181+
metricsList.add(new ItemVMCore(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), USED, coreCapacity.getUsedCapacity(), isDedicated, hostTags));
182+
metricsList.add(new ItemVMCore(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), TOTAL, coreCapacity.getTotalCapacity(), isDedicated, hostTags));
199183
} else {
200-
metricsList.add(new ItemVMCore(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), USED, 0L, isDedicated, hosttags));
201-
metricsList.add(new ItemVMCore(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), TOTAL, 0L, isDedicated, hosttags));
184+
metricsList.add(new ItemVMCore(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), USED, 0L, isDedicated, hostTags));
185+
metricsList.add(new ItemVMCore(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), TOTAL, 0L, isDedicated, hostTags));
202186
}
203187
}
204188

@@ -220,6 +204,30 @@ private void addHostMetrics(final List<Item> metricsList, final long dcId, final
220204
metricsList.add(new ItemHost(zoneName, zoneUuid, ONLINE, up, null));
221205
metricsList.add(new ItemHost(zoneName, zoneUuid, OFFLINE, down, null));
222206
metricsList.add(new ItemHost(zoneName, zoneUuid, TOTAL, total, null));
207+
208+
addHostTagsMetrics(metricsList, dcId, zoneName, zoneUuid, totalHosts, upHosts, downHosts, total, up, down);
209+
}
210+
211+
private String markTagMaps(HostVO host, Map<String, Integer> totalHosts, Map<String, Integer> upHosts, Map<String, Integer> downHosts) {
212+
List<String> hostTags = _hostTagsDao.getHostTags(host.getId());
213+
markTags(hostTags,totalHosts);
214+
if (host.getStatus() == Status.Up) {
215+
markTags(hostTags, upHosts);
216+
} else if (host.getStatus() == Status.Disconnected || host.getStatus() == Status.Down) {
217+
markTags(hostTags, downHosts);
218+
}
219+
return StringUtils.join(hostTags, ",");
220+
}
221+
222+
private void markTags(List<String> tags, Map<String, Integer> tagMap) {
223+
tags.forEach(tag -> {
224+
int current = tagMap.get(tag) != null ? tagMap.get(tag) : 0;
225+
tagMap.put(tag, current + 1);
226+
});
227+
}
228+
229+
private void addHostTagsMetrics(final List<Item> metricsList, final long dcId, final String zoneName, final String zoneUuid, Map<String, Integer> totalHosts, Map<String, Integer> upHosts, Map<String, Integer> downHosts, int total, int up, int down) {
230+
223231
for (Map.Entry<String, Integer> entry : totalHosts.entrySet()) {
224232
String tag = entry.getKey();
225233
Integer count = entry.getValue();
@@ -235,12 +243,25 @@ private void addHostMetrics(final List<Item> metricsList, final long dcId, final
235243
metricsList.add(new ItemHost(zoneName, zoneUuid, OFFLINE, 0, tag));
236244
}
237245
}
238-
for (Map.Entry<String, Integer> entry : totalHosts.entrySet()) {
239-
String tag = entry.getKey();
240-
Ternary<Long, Long, Long> allocatedCapacityByTag = capacityDao.findCapacityByZoneAndHostTag(dcId, tag);
241-
metricsList.add(new ItemVMCore(zoneName, zoneUuid, null, null, null, ALLOCATED, allocatedCapacityByTag.first(), 0, tag));
242-
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, null, null, null, null, ALLOCATED, allocatedCapacityByTag.second(),0, tag));
243-
metricsList.add(new ItemHostMemory(zoneName, zoneUuid, null, null, null, null, ALLOCATED, allocatedCapacityByTag.third(), 0, tag));
246+
247+
totalHosts.keySet()
248+
.forEach( tag -> {
249+
Ternary<Long, Long, Long> allocatedCapacityByTag = capacityDao.findCapacityByZoneAndHostTag(dcId, tag);
250+
metricsList.add(new ItemVMCore(zoneName, zoneUuid, null, null, null, ALLOCATED, allocatedCapacityByTag.first(), 0, tag));
251+
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, null, null, null, null, ALLOCATED, allocatedCapacityByTag.second(), 0, tag));
252+
metricsList.add(new ItemHostMemory(zoneName, zoneUuid, null, null, null, null, ALLOCATED, allocatedCapacityByTag.third(), 0, tag));
253+
});
254+
255+
List<String> allHostTags = hostDao.listAll().stream()
256+
.flatMap( h -> _hostTagsDao.getHostTags(h.getId()).stream())
257+
.distinct()
258+
.collect(Collectors.toList());
259+
260+
for (final State state : State.values()) {
261+
for (final String hostTag : allHostTags) {
262+
final Long count = vmDao.countByZoneAndStateAndHostTag(dcId, state, hostTag);
263+
metricsList.add(new ItemVMByTag(zoneName, zoneUuid, state.name().toLowerCase(), count, hostTag));
264+
}
244265
}
245266
}
246267

@@ -252,18 +273,6 @@ private void addVMMetrics(final List<Item> metricsList, final long dcId, final S
252273
}
253274
metricsList.add(new ItemVM(zoneName, zoneUuid, state.name().toLowerCase(), count));
254275
}
255-
256-
List<String> allHostTags = hostDao.listAll().stream()
257-
.flatMap( h -> _hostTagsDao.getHostTags(h.getId()).stream())
258-
.distinct()
259-
.collect(Collectors.toList());
260-
261-
for (final State state : State.values()) {
262-
for (final String hosttag : allHostTags) {
263-
final Long count = vmDao.countByZoneAndStateAndHostTag(dcId, state, hosttag);
264-
metricsList.add(new ItemVMByTag(zoneName, zoneUuid, state.name().toLowerCase(), count, hosttag));
265-
}
266-
}
267276
}
268277

269278
private void addVolumeMetrics(final List<Item> metricsList, final long dcId, final String zoneName, final String zoneUuid) {

0 commit comments

Comments
 (0)