-
Notifications
You must be signed in to change notification settings - Fork 219
PR for Micrometer Integration - #64 #486
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
metacosm
merged 4 commits into
operator-framework:master
from
laxmikantbpandhare:issue-template
Aug 26, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6c234d9
feature: Micrometer Integration for generating metrics - #64
laxmikantbpandhare 04ad0bd
feature: modified events logic
laxmikantbpandhare 6380615
comment: removed sysout
laxmikantbpandhare 34a5dbe
feature: modified for addition of config in defaulteventhandler
laxmikantbpandhare 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
180 changes: 180 additions & 0 deletions
180
operator-framework-core/src/main/java/io/javaoperatorsdk/operator/Metrics.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,180 @@ | ||
package io.javaoperatorsdk.operator; | ||
|
||
import io.fabric8.kubernetes.client.CustomResource; | ||
import io.javaoperatorsdk.operator.api.Context; | ||
import io.javaoperatorsdk.operator.api.DeleteControl; | ||
import io.javaoperatorsdk.operator.api.ResourceController; | ||
import io.javaoperatorsdk.operator.api.UpdateControl; | ||
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration; | ||
import io.micrometer.core.instrument.*; | ||
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig; | ||
import io.micrometer.core.instrument.distribution.pause.PauseDetector; | ||
import io.micrometer.core.instrument.noop.*; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.ToDoubleFunction; | ||
import java.util.function.ToLongFunction; | ||
|
||
public class Metrics { | ||
public static final Metrics NOOP = new Metrics(new NoopMeterRegistry(Clock.SYSTEM)); | ||
private final MeterRegistry registry; | ||
|
||
public Metrics(MeterRegistry registry) { | ||
this.registry = registry; | ||
} | ||
|
||
public <R extends CustomResource> UpdateControl<R> timeControllerCreateOrUpdate( | ||
ResourceController<R> controller, | ||
ControllerConfiguration<R> configuration, | ||
R resource, | ||
Context<R> context) { | ||
final var name = configuration.getName(); | ||
final var timer = | ||
Timer.builder("operator.sdk.controllers.execution.createorupdate") | ||
.tags("controller", name) | ||
.publishPercentiles(0.3, 0.5, 0.95) | ||
.publishPercentileHistogram() | ||
.register(registry); | ||
try { | ||
final var result = timer.record(() -> controller.createOrUpdateResource(resource, context)); | ||
String successType = "cr"; | ||
if (result.isUpdateStatusSubResource()) { | ||
successType = "status"; | ||
} | ||
if (result.isUpdateCustomResourceAndStatusSubResource()) { | ||
successType = "both"; | ||
} | ||
registry | ||
.counter( | ||
"operator.sdk.controllers.execution.success", "controller", name, "type", successType) | ||
.increment(); | ||
return result; | ||
} catch (Exception e) { | ||
registry | ||
.counter( | ||
"operator.sdk.controllers.execution.failure", | ||
"controller", | ||
name, | ||
"exception", | ||
e.getClass().getSimpleName()) | ||
.increment(); | ||
throw e; | ||
} | ||
} | ||
|
||
public DeleteControl timeControllerDelete( | ||
ResourceController controller, | ||
ControllerConfiguration configuration, | ||
CustomResource resource, | ||
Context context) { | ||
final var name = configuration.getName(); | ||
final var timer = | ||
Timer.builder("operator.sdk.controllers.execution.delete") | ||
.tags("controller", name) | ||
.publishPercentiles(0.3, 0.5, 0.95) | ||
.publishPercentileHistogram() | ||
.register(registry); | ||
try { | ||
final var result = timer.record(() -> controller.deleteResource(resource, context)); | ||
String successType = "notDelete"; | ||
if (result == DeleteControl.DEFAULT_DELETE) { | ||
successType = "delete"; | ||
} | ||
registry | ||
.counter( | ||
"operator.sdk.controllers.execution.success", "controller", name, "type", successType) | ||
.increment(); | ||
return result; | ||
} catch (Exception e) { | ||
registry | ||
.counter( | ||
"operator.sdk.controllers.execution.failure", | ||
"controller", | ||
name, | ||
"exception", | ||
e.getClass().getSimpleName()) | ||
.increment(); | ||
throw e; | ||
} | ||
} | ||
|
||
public void timeControllerRetry() { | ||
|
||
registry | ||
.counter( | ||
"operator.sdk.retry.on.exception", "retry", "retryCounter", "type", | ||
"retryException") | ||
.increment(); | ||
|
||
} | ||
|
||
public void timeControllerEvents() { | ||
|
||
registry | ||
.counter( | ||
"operator.sdk.total.events.received", "events", "totalEvents", "type", | ||
"eventsReceived") | ||
.increment(); | ||
|
||
} | ||
|
||
public static class NoopMeterRegistry extends MeterRegistry { | ||
public NoopMeterRegistry(Clock clock) { | ||
super(clock); | ||
} | ||
|
||
@Override | ||
protected <T> Gauge newGauge(Meter.Id id, T t, ToDoubleFunction<T> toDoubleFunction) { | ||
return new NoopGauge(id); | ||
} | ||
|
||
@Override | ||
protected Counter newCounter(Meter.Id id) { | ||
return new NoopCounter(id); | ||
} | ||
|
||
@Override | ||
protected Timer newTimer( | ||
Meter.Id id, | ||
DistributionStatisticConfig distributionStatisticConfig, | ||
PauseDetector pauseDetector) { | ||
return new NoopTimer(id); | ||
} | ||
|
||
@Override | ||
protected DistributionSummary newDistributionSummary( | ||
Meter.Id id, DistributionStatisticConfig distributionStatisticConfig, double v) { | ||
return new NoopDistributionSummary(id); | ||
} | ||
|
||
@Override | ||
protected Meter newMeter(Meter.Id id, Meter.Type type, Iterable<Measurement> iterable) { | ||
return new NoopMeter(id); | ||
} | ||
|
||
@Override | ||
protected <T> FunctionTimer newFunctionTimer( | ||
Meter.Id id, | ||
T t, | ||
ToLongFunction<T> toLongFunction, | ||
ToDoubleFunction<T> toDoubleFunction, | ||
TimeUnit timeUnit) { | ||
return new NoopFunctionTimer(id); | ||
} | ||
|
||
@Override | ||
protected <T> FunctionCounter newFunctionCounter( | ||
Meter.Id id, T t, ToDoubleFunction<T> toDoubleFunction) { | ||
return new NoopFunctionCounter(id); | ||
} | ||
|
||
@Override | ||
protected TimeUnit getBaseTimeUnit() { | ||
return TimeUnit.SECONDS; | ||
} | ||
|
||
@Override | ||
protected DistributionStatisticConfig defaultHistogramConfig() { | ||
return DistributionStatisticConfig.NONE; | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me this whole construct while wrapping the logic in a Metrics class is little bit smelly, not sure if there is a better way. But will take a look soon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially, I wrote a separate class without wrapping it. @metacosm - Chris asked to have it like above wrapping the logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's wrong with wrapping the logic, @csviri? More than wrapping, it's about measuring things that make sense from the operator's perspective as opposed to calling more metrics-oriented methods. Another aspect is that all metrics code is in a single spot which makes it easier to figure out what's measured and change it if needed.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That the metrics code is in one place that is good. That we are mixing the application logic with metrics is not that nice. Those should be as loosely coupled as possible IMHO. And also seamlessly if possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't say I have a better answer for this now, just mentioning that design principle. So something to think about.