-
Notifications
You must be signed in to change notification settings - Fork 219
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
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fa6a3cf
feat: remove CR meters when they are deleted (after a delay)
metacosm d8becd1
tests: add integration test for meter cleaning
metacosm 712d258
fix: add http client implementation
metacosm b66712a
feat: make meter cleaning thread number configurable
metacosm a60cd31
tests: verify that remove is indeed called on the registry
metacosm c414db7
fix: add missing annotation
metacosm c91fbc2
fix: remove local associations once the meters have been removed
metacosm 7465f3c
fix: tests
metacosm f2eb591
feat: support having no delay before deleting associated meters
metacosm 1fa7805
feat: add action name to ResourceEvent metrics tags
metacosm d9c27e2
docs: add Metrics feature documentation
metacosm ddd0d16
feat: make 1 the default thread count, added javadoc
metacosm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...est/java/io/javaoperatorsdk/operator/monitoring/micrometer/MetricsCleaningOnDeleteIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package io.javaoperatorsdk.operator.monitoring.micrometer; | ||
|
||
import java.time.Duration; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.fabric8.kubernetes.api.model.ConfigMap; | ||
import io.fabric8.kubernetes.api.model.ConfigMapBuilder; | ||
import io.javaoperatorsdk.operator.api.config.ConfigurationServiceProvider; | ||
import io.javaoperatorsdk.operator.api.reconciler.*; | ||
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; | ||
import io.javaoperatorsdk.operator.processing.event.ResourceID; | ||
import io.micrometer.core.instrument.Meter; | ||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
public class MetricsCleaningOnDeleteIT { | ||
@RegisterExtension | ||
static LocallyRunOperatorExtension operator = | ||
LocallyRunOperatorExtension.builder().withReconciler(new MetricsCleaningTestReconciler()) | ||
.build(); | ||
|
||
private static final TestSimpleMeterRegistry registry = new TestSimpleMeterRegistry(); | ||
private static final int testDelay = 1; | ||
private static final MicrometerMetrics metrics = new MicrometerMetrics(registry, testDelay, 2); | ||
private static final String testResourceName = "cleaning-metrics-cr"; | ||
|
||
@BeforeAll | ||
static void setup() { | ||
ConfigurationServiceProvider.overrideCurrent(overrider -> overrider.withMetrics(metrics)); | ||
} | ||
|
||
@AfterAll | ||
static void reset() { | ||
ConfigurationServiceProvider.reset(); | ||
} | ||
|
||
@Test | ||
void removesMetersAssociatedWithResourceAfterItsDeletion() throws InterruptedException { | ||
var testResource = new ConfigMapBuilder() | ||
.withNewMetadata() | ||
.withName(testResourceName) | ||
.endMetadata() | ||
.build(); | ||
final var created = operator.create(testResource); | ||
|
||
// make sure the resource is created | ||
await().until(() -> !operator.get(ConfigMap.class, testResourceName) | ||
.getMetadata().getFinalizers().isEmpty()); | ||
|
||
// check that we properly recorded meters associated with the resource | ||
final var meters = metrics.recordedMeterIdsFor(ResourceID.fromResource(created)); | ||
assertThat(meters).isNotNull(); | ||
assertThat(meters).isNotEmpty(); | ||
|
||
// delete the resource and wait for it to be deleted | ||
operator.delete(testResource); | ||
await().until(() -> operator.get(ConfigMap.class, testResourceName) == null); | ||
|
||
// check that the meters are properly removed after the specified delay | ||
Thread.sleep(Duration.ofSeconds(testDelay).toMillis()); | ||
assertThat(registry.removed).isEqualTo(meters); | ||
assertThat(metrics.recordedMeterIdsFor(ResourceID.fromResource(created))).isNull(); | ||
} | ||
|
||
@ControllerConfiguration | ||
private static class MetricsCleaningTestReconciler | ||
implements Reconciler<ConfigMap>, Cleaner<ConfigMap> { | ||
@Override | ||
public UpdateControl<ConfigMap> reconcile(ConfigMap resource, Context<ConfigMap> context) { | ||
return UpdateControl.noUpdate(); | ||
} | ||
|
||
@Override | ||
public DeleteControl cleanup(ConfigMap resource, Context<ConfigMap> context) { | ||
return DeleteControl.defaultDelete(); | ||
} | ||
} | ||
|
||
private static class TestSimpleMeterRegistry extends SimpleMeterRegistry { | ||
private final Set<Meter.Id> removed = new HashSet<>(); | ||
|
||
@Override | ||
public Meter remove(Meter.Id mappedId) { | ||
final var removed = super.remove(mappedId); | ||
this.removed.add(removed.getId()); | ||
return removed; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.