Skip to content

Commit

Permalink
Increase Micrometer performance using Meter.MeterProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
mcruzdev authored and holly-cummins committed Jul 31, 2024
1 parent 69e73a9 commit a079ab2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void shouldGetStorkMetricsWhenServiceSelectorFails() {

private static void assertStorkMetrics(String serviceName) {
StorkObservation metrics = StorkObservationCollectorBean.STORK_METRICS
.get(serviceName + StorkObservationCollectorBean.METRICS_SUFIX);
.get(serviceName + StorkObservationCollectorBean.METRICS_SUFFIX);
Assertions.assertThat(metrics.getDiscoveredInstancesCount()).isEqualTo(1);
Assertions.assertThat(metrics.getServiceName()).isEqualTo(serviceName);
Assertions.assertThat(metrics.isDone()).isTrue();
Expand All @@ -94,10 +94,8 @@ private void assertStorkMetricsInMicrometerRegistry(String serviceName) {
Counter instanceCounter = registry.counter("stork.service-discovery.instances.count", "service-name", serviceName);
Timer serviceDiscoveryDuration = registry.timer("stork.service-discovery.duration", "service-name", serviceName);
Timer serviceSelectionDuration = registry.timer("stork.service-selection.duration", "service-name", serviceName);
Counter serviceDiscoveryFailures = registry.get("stork.service-discovery.failures")
.tags("service-name", serviceName).counter();
Counter loadBalancerFailures = registry.get("stork.service-selection.failures").tags("service-name", serviceName)
.counter();
Counter serviceDiscoveryFailures = registry.counter("stork.service-discovery.failures", "service-name", serviceName);
Counter loadBalancerFailures = registry.counter("stork.service-selection.failures", "service-name", serviceName);

Util.assertTags(Tag.of("service-name", serviceName), instanceCounter, serviceDiscoveryDuration,
serviceSelectionDuration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,8 @@ private void assertStorkMetricsInMicrometerRegistry(String serviceName) {
Counter instanceCounter = registry.counter("stork.service-discovery.instances.count", "service-name", serviceName);
Timer serviceDiscoveryDuration = registry.timer("stork.service-discovery.duration", "service-name", serviceName);
Timer serviceSelectionDuration = registry.timer("stork.service-selection.duration", "service-name", serviceName);
Counter serviceDiscoveryFailures = registry.get("stork.service-discovery.failures")
.tags("service-name", serviceName).counter();
Counter loadBalancerFailures = registry.get("stork.service-selection.failures").tags("service-name", serviceName)
.counter();
Counter serviceDiscoveryFailures = registry.counter("stork.service-discovery.failures", "service-name", serviceName);
Counter loadBalancerFailures = registry.counter("stork.service-selection.failures", "service-name", serviceName);

Util.assertTags(Tag.of("service-name", serviceName), instanceCounter, serviceDiscoveryDuration,
serviceSelectionDuration);
Expand All @@ -93,7 +91,7 @@ private void assertStorkMetricsInMicrometerRegistry(String serviceName) {

private static void assertStorkMetrics(String serviceName) {
StorkObservation metrics = StorkObservationCollectorBean.STORK_METRICS
.get(serviceName + StorkObservationCollectorBean.METRICS_SUFIX);
.get(serviceName + StorkObservationCollectorBean.METRICS_SUFFIX);
Assertions.assertThat(metrics.getDiscoveredInstancesCount()).isNegative();
Assertions.assertThat(metrics.getServiceName()).isEqualTo(serviceName);
Assertions.assertThat(metrics.isDone()).isTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,12 @@ public void shouldGetStorkMetricsForTwoServicesWhenEverythingSucceded() {
}

private void assertStorkMetricsInMicrometerRegistry(String serviceName) {

Counter instanceCounter = registry.counter("stork.service-discovery.instances.count", "service-name", serviceName);
Timer serviceDiscoveryDuration = registry.timer("stork.service-discovery.duration", "service-name", serviceName);
Timer serviceSelectionDuration = registry.timer("stork.service-selection.duration", "service-name", serviceName);
Counter serviceDiscoveryFailures = registry.get("stork.service-discovery.failures")
.tags("service-name", serviceName).counter();
Counter loadBalancerFailures = registry.get("stork.service-selection.failures").tags("service-name", serviceName)
.counter();
Counter serviceDiscoveryFailures = registry.counter("stork.service-discovery.failures", "service-name", serviceName);
Counter loadBalancerFailures = registry.counter("stork.service-selection.failures", "service-name", serviceName);

Util.assertTags(Tag.of("service-name", serviceName), instanceCounter, serviceDiscoveryDuration,
serviceSelectionDuration);
Expand All @@ -80,7 +79,8 @@ private void assertStorkMetricsInMicrometerRegistry(String serviceName) {

public static void assertStorkMetrics(String serviceName) {
StorkObservation metrics = StorkObservationCollectorBean.STORK_METRICS
.get(serviceName + StorkObservationCollectorBean.METRICS_SUFIX);
.get(serviceName + StorkObservationCollectorBean.METRICS_SUFFIX);

Assertions.assertThat(metrics.getDiscoveredInstancesCount()).isEqualTo(1);
Assertions.assertThat(metrics.getServiceName()).isEqualTo(serviceName);
Assertions.assertThat(metrics.isDone()).isTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import jakarta.enterprise.inject.Typed;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Tag;
Expand All @@ -21,59 +22,66 @@
@Typed(ObservationCollector.class)
public class StorkObservationCollectorBean implements ObservationCollector, StorkEventHandler {

public static final String METRICS_SUFIX = "-metrics";
public static final String METRICS_SUFFIX = "-metrics";
final MeterRegistry registry = Metrics.globalRegistry;

public final static Map<String, StorkObservation> STORK_METRICS = new ConcurrentHashMap<>();
private final Meter.MeterProvider<Counter> instanceCounter;
private final Meter.MeterProvider<Timer> serviceDiscoveryTimer;
private final Meter.MeterProvider<Timer> serviceSelectionTimer;
private final Meter.MeterProvider<Counter> serviceDiscoveryFailures;
private final Meter.MeterProvider<Counter> serviceSelectionFailures;

@Override
public StorkObservation create(String serviceName, String serviceDiscoveryType,
String serviceSelectionType) {
return STORK_METRICS.computeIfAbsent(serviceName + METRICS_SUFIX,
key -> new StorkObservation(serviceName, serviceDiscoveryType, serviceSelectionType,
this));
}

@Override
public void complete(StorkObservation observation) {
Tags tags = Tags.of(Tag.of("service-name", observation.getServiceName()));
public StorkObservationCollectorBean() {

Counter instanceCounter = Counter.builder("stork.service-discovery.instances.count")
this.instanceCounter = Counter
.builder("stork.service-discovery.instances.count")
.description("The number of service instances discovered")
.tags(tags)
.register(registry);
.withRegistry(registry);

Timer serviceDiscoveryTimer = Timer
this.serviceDiscoveryTimer = Timer
.builder("stork.service-discovery.duration")
.description("The duration of the discovery operation")
.tags(tags)
.register(registry);
.withRegistry(registry);

Timer serviceSelectionTimer = Timer
this.serviceSelectionTimer = Timer
.builder("stork.service-selection.duration")
.description("The duration of the selection operation ")
.tags(tags)
.register(registry);
.withRegistry(registry);

Counter serviceDiscoveryFailures = Counter
this.serviceDiscoveryFailures = Counter
.builder("stork.service-discovery.failures")
.description("The number of failures during service discovery").tags(tags)
.register(registry);
.description("The number of failures during service discovery")
.withRegistry(registry);

Counter serviceSelectionFailures = Counter
this.serviceSelectionFailures = Counter
.builder("stork.service-selection.failures")
.description("The number of failures during service selection.").tags(tags)
.register(registry);
.description("The number of failures during service selection.")
.withRegistry(registry);
}

@Override
public StorkObservation create(String serviceName, String serviceDiscoveryType,
String serviceSelectionType) {
return STORK_METRICS.computeIfAbsent(serviceName + METRICS_SUFFIX,
key -> new StorkObservation(serviceName, serviceDiscoveryType, serviceSelectionType,
this));
}

@Override
public void complete(StorkObservation observation) {
Tags tags = Tags.of(Tag.of("service-name", observation.getServiceName()));

instanceCounter.increment(observation.getDiscoveredInstancesCount());
serviceDiscoveryTimer.record(observation.getServiceDiscoveryDuration().getNano(), TimeUnit.NANOSECONDS);
serviceSelectionTimer.record(observation.getServiceSelectionDuration().getNano(), TimeUnit.NANOSECONDS);
this.instanceCounter.withTags(tags).increment(observation.getDiscoveredInstancesCount());
this.serviceDiscoveryTimer.withTags(tags).record(observation.getServiceDiscoveryDuration().getNano(),
TimeUnit.NANOSECONDS);
this.serviceSelectionTimer.withTags(tags).record(observation.getServiceSelectionDuration().getNano(),
TimeUnit.NANOSECONDS);

if (observation.failure() != null) {
if (observation.isServiceDiscoverySuccessful()) {
serviceSelectionFailures.increment();
this.serviceSelectionFailures.withTags(tags).increment();
} else {// SD failure
serviceDiscoveryFailures.increment();
this.serviceDiscoveryFailures.withTags(tags).increment();
}
}

Expand Down

0 comments on commit a079ab2

Please sign in to comment.