Skip to content
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

Micrometer performance - use Meter.MeterProvider #41295

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Contributor

Choose a reason for hiding this comment

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

There were no tags before... But probably it's better to have them.

Copy link
Contributor Author

@mcruzdev mcruzdev Jun 20, 2024

Choose a reason for hiding this comment

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

Yes, I missed the tags in the first commit. sorry 😢

Copy link
Contributor

Choose a reason for hiding this comment

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

When I say before I mean, before the PR... CC @aureamunoz

} else {// SD failure
serviceDiscoveryFailures.increment();
this.serviceDiscoveryFailures.withTags(tags).increment();
}
}

Expand Down
Loading