Skip to content

feat: remove CR meters when they are deleted (after a delay) #1805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 15, 2023

Conversation

metacosm
Copy link
Collaborator

@metacosm metacosm commented Mar 9, 2023

Fixes #1803.

@metacosm metacosm self-assigned this Mar 9, 2023
@metacosm metacosm requested a review from csviri March 9, 2023 19:25
@csviri
Copy link
Collaborator

csviri commented Mar 10, 2023

I guess the target should be the next branch for this PR.

@metacosm
Copy link
Collaborator Author

Yes

@metacosm metacosm changed the base branch from main to next March 10, 2023 12:36
this.registry = registry;
this.cleanUpDelayInSeconds = cleanUpDelayInSeconds;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is there a delay, why cannot be deleted instantly?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because you still might want to get the metrics on the deleted resources for a while before the metrics are removed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

true, would worth to write it as a javadoc so it is obvious from code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, the feature needs to be documented.

@@ -31,9 +31,17 @@ public class MicrometerMetrics implements Metrics {
private static final String RECONCILIATIONS_QUEUE_SIZE = PREFIX + RECONCILIATIONS + "queue.size.";
private final MeterRegistry registry;
private final Map<String, AtomicInteger> gauges = new ConcurrentHashMap<>();
private final Map<ResourceID, Set<Meter.Id>> metersPerResource = new ConcurrentHashMap<>();
private final ScheduledExecutorService metersCleaner = Executors.newScheduledThreadPool(10);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since the clean is a short running task (I assume), wouldn't' be better to use a Timer?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because it's usually better to use ScheduledExecutorService than Timer. See https://stackoverflow.com/a/409993.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, then the number of Threads should be configurable, and we should use 1 as default.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually the whole ScheduledExecutorService should be configurable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Both delay and thread numbers are configurable. What other aspects would need configuration?

Copy link
Collaborator

Choose a reason for hiding this comment

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

For example bursting the threads (like the cached executor), but I think it fine for now now

@@ -116,6 +124,14 @@ public void receivedEvent(Event event, Map<String, Object> metadata) {
@Override
public void cleanupDoneFor(ResourceID resourceID, Map<String, Object> metadata) {
incrementCounter(resourceID, "events.delete", metadata);

// schedule deletion of meters associated with ResourceID
metersCleaner.schedule(() -> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since this is a non trivial logic, we should add unit test.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure a unit test makes sense here, though I certainly could add one. What would make more sense would be an integration test to check that the metrics are actually not there anymore, will see how complex that could be to set up.

Copy link
Collaborator

Choose a reason for hiding this comment

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

IMO we should tests every non trivial logic with unit tests, and see at least that the API there is called for every ID eventually.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The logic is trivial: on delete, call remove on all the meters associated with the resource id. I've added an integration test, anyway.

Copy link
Collaborator

@csviri csviri left a comment

Choose a reason for hiding this comment

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

LGTM

@metacosm metacosm marked this pull request as draft March 13, 2023 20:25
@metacosm metacosm marked this pull request as ready for review March 14, 2023 21:47
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 10 Code Smells

34.1% 34.1% Coverage
0.0% 0.0% Duplication

Copy link
Collaborator

@csviri csviri left a comment

Choose a reason for hiding this comment

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

Made one comment on default otherwise looks good to me

@metacosm metacosm merged commit 9c8ed19 into next Mar 15, 2023
@metacosm metacosm deleted the metrics-cleaning branch March 15, 2023 11:58
csviri pushed a commit that referenced this pull request Mar 15, 2023
* feat: remove CR meters when they are deleted (after a delay)

Fixes #1803.

Also added documentation for the Metrics feature.
metacosm added a commit that referenced this pull request Mar 27, 2023
* feat: remove CR meters when they are deleted (after a delay)

Fixes #1803.

Also added documentation for the Metrics feature.
csviri pushed a commit that referenced this pull request Mar 27, 2023
* feat: remove CR meters when they are deleted (after a delay)

Fixes #1803.

Also added documentation for the Metrics feature.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants