Skip to content

[WIP] Refactor Prometheus plugin - #5773

Closed
soreana wants to merge 0 commit into
apache:mainfrom
soreana:refactor-prometheus
Closed

soreana wants to merge 0 commit into
apache:mainfrom
soreana:refactor-prometheus

Conversation

@soreana

@soreana soreana commented Dec 14, 2021

Copy link
Copy Markdown
Member

Description

For a long time, I've worked with Prometheus. I've always wanted to clean its code. Now I have enough time to work on it.

My idea is to use a lambda expression rather than an object creation for Prometheus output.
Add documentation and UI support to it.

Let me know if you have any ideas.

P.S: This pr is based on the #4438. I will rebase it when it gets merged.

Types of changes

  • Breaking change (fix or feature that would cause existing functionality to change)
  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (improves an existing feature and functionality)
  • Cleanup (Code refactoring and cleanup, that may add test cases)

Feature/Enhancement Scale or Bug Severity

Feature/Enhancement Scale

  • Major
  • Minor

How Has This Been Tested?

@soreana soreana mentioned this pull request Dec 14, 2021
1 of 5 tasks

@DaanHoogland DaanHoogland left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems to look good generally (no prometheus expert, but seems like useful functionality)

Comment on lines +145 to +162
List<String> hostTags = _hostTagsDao.gethostTags(host.getId());
String hosttags = StringUtils.join(hostTags, ",");
for (String tag : hostTags) {
Integer current = totalHosts.get(tag) != null ? totalHosts.get(tag) : 0;
totalHosts.put(tag, current + 1);
}
if (host.getStatus() == Status.Up) {
for (String tag : hostTags) {
Integer current = upHosts.get(tag) != null ? upHosts.get(tag) : 0;
upHosts.put(tag, current + 1);
}
} else if (host.getStatus() == Status.Disconnected || host.getStatus() == Status.Down) {
for (String tag : hostTags) {
Integer current = downHosts.get(tag) != null ? downHosts.get(tag) : 0;
downHosts.put(tag, current + 1);
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addHostMetrics() is already way to long, and the new functionality shouldn't be added to it, so as an example
this functionality seems like it could be extracted and there is some repetition in it that can be generalised as well.

Suggested change
List<String> hostTags = _hostTagsDao.gethostTags(host.getId());
String hosttags = StringUtils.join(hostTags, ",");
for (String tag : hostTags) {
Integer current = totalHosts.get(tag) != null ? totalHosts.get(tag) : 0;
totalHosts.put(tag, current + 1);
}
if (host.getStatus() == Status.Up) {
for (String tag : hostTags) {
Integer current = upHosts.get(tag) != null ? upHosts.get(tag) : 0;
upHosts.put(tag, current + 1);
}
} else if (host.getStatus() == Status.Disconnected || host.getStatus() == Status.Down) {
for (String tag : hostTags) {
Integer current = downHosts.get(tag) != null ? downHosts.get(tag) : 0;
downHosts.put(tag, current + 1);
}
}
String hosttags = markTagMaps(totalHosts, upHosts, downHosts);

and then add

        private String markTagMaps(Map<String, Integer> totalHosts, Map<String, Integer> upHosts, Map<String, Integer> downHosts) {
            List<String> hostTags = _hostTagsDao.gethostTags(host.getId());
            markTags(hostTags,totalHosts);
            if (host.getStatus() == Status.Up) {
                markTags(hostTags, upHosts);
            } else if (host.getStatus() == Status.Disconnected || host.getStatus() == Status.Down) {
                markTags(hostTags, downHosts);
            }
            return StringUtils.join(hostTags, ",");
        }

and

  private void markTags(List<String tags, Map<String, Integer> tagMap) {
            for (String tag : tags) {
                Integer current = tagMap.get(tag) != null ? tagMap.get(tag) : 0;
                tagMap.put(tag, current + 1);
            }
        }

Comment on lines +218 to +243
}

metricsList.add(new ItemHost(zoneName, zoneUuid, ONLINE, up, null));
metricsList.add(new ItemHost(zoneName, zoneUuid, OFFLINE, down, null));
metricsList.add(new ItemHost(zoneName, zoneUuid, TOTAL, total, null));
for (Map.Entry<String, Integer> entry : totalHosts.entrySet()) {
String tag = entry.getKey();
Integer count = entry.getValue();
metricsList.add(new ItemHost(zoneName, zoneUuid, TOTAL, count, tag));
if (upHosts.get(tag) != null) {
metricsList.add(new ItemHost(zoneName, zoneUuid, ONLINE, upHosts.get(tag), tag));
} else {
metricsList.add(new ItemHost(zoneName, zoneUuid, ONLINE, 0, tag));
}
if (downHosts.get(tag) != null) {
metricsList.add(new ItemHost(zoneName, zoneUuid, OFFLINE, downHosts.get(tag), tag));
} else {
metricsList.add(new ItemHost(zoneName, zoneUuid, OFFLINE, 0, tag));
}
}
for (Map.Entry<String, Integer> entry : totalHosts.entrySet()) {
String tag = entry.getKey();
Ternary<Long, Long, Long> allocatedCapacityByTag = capacityDao.findCapacityByZoneAndHostTag(dcId, tag);
metricsList.add(new ItemVMCore(zoneName, zoneUuid, null, null, null, ALLOCATED, allocatedCapacityByTag.first(), 0, tag));
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, null, null, null, null, ALLOCATED, allocatedCapacityByTag.second(),0, tag));
metricsList.add(new ItemHostMemory(zoneName, zoneUuid, null, null, null, null, ALLOCATED, allocatedCapacityByTag.third(), 0, tag));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please extract this

Comment on lines +255 to +266

List<String> allHostTags = hostDao.listAll().stream()
.flatMap( h -> _hostTagsDao.gethostTags(h.getId()).stream())
.distinct()
.collect(Collectors.toList());

for (final State state : State.values()) {
for (final String hosttag : allHostTags) {
final Long count = vmDao.countByZoneAndStateAndHostTag(dcId, state, hosttag);
metricsList.add(new ItemVMByTag(zoneName, zoneUuid, state.name().toLowerCase(), count, hosttag));
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please extract this

@github-actions

Copy link
Copy Markdown

This pull request has merge conflicts. Dear author, please fix the conflicts and sync your branch with the base branch.

@DaanHoogland

Copy link
Copy Markdown
Contributor

@soreana another conflict...

@DaanHoogland

Copy link
Copy Markdown
Contributor

@soreana will you still work on this?

@soreana

soreana commented Dec 27, 2022

Copy link
Copy Markdown
Member Author

@DaanHoogland Yes, I'm still work on this but not actively. My plan is to remove object creation and use functions to export entries. I didn't find a time to do so yet.

@soreana soreana closed this Jun 6, 2023
@soreana
soreana force-pushed the refactor-prometheus branch from 34c1525 to 51d4e54 Compare June 6, 2023 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants