Skip to content

Commit

Permalink
Add logic to clear data structures between same-JVM TCK tests (essent…
Browse files Browse the repository at this point in the history
…ially no-ops during normal usage)
  • Loading branch information
tjquinno committed Jul 1, 2023
1 parent eefdf9b commit 396c276
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ public static Builder builder() {

private final String scopeTagName;
private final Iterable<String> scopeSelection;
private final Iterable<String> meterSelection;
private final Iterable<String> meterNameSelection;
private final MediaType resultMediaType;

private MicrometerPrometheusFormatter(Builder builder) {
scopeTagName = builder.scopeTagName;
scopeSelection = builder.scopeSelection;
meterSelection = builder.meterNameSelection;
meterNameSelection = builder.meterNameSelection;
resultMediaType = builder.resultMediaType;
}

Expand All @@ -82,28 +82,7 @@ private MicrometerPrometheusFormatter(Builder builder) {
* @return filtered Prometheus output
*/
public Optional<String> filteredOutput() {
return formattedOutput(prometheusMeterRegistry(),
resultMediaType,
scopeTagName,
scopeSelection,
meterSelection);
}

/**
* Retrieves the Prometheus-format report from the specified registry, according to the specified media type,
* filtered by the specified scope and meter name, and returns the filtered Prometheus-format output.
*
* @param prometheusMeterRegistry registry to query
* @param resultMediaType media type which controls the exact output format
* @param scopeSelection scope to select; null if no scope selection required
* @param meterNameSelection meter name to select; null if no meter name selection required
* @return filtered output
*/
Optional<String> formattedOutput(PrometheusMeterRegistry prometheusMeterRegistry,
MediaType resultMediaType,
String scopeTagName,
Iterable<String> scopeSelection,
Iterable<String> meterNameSelection) {
PrometheusMeterRegistry prometheusMeterRegistry = prometheusMeterRegistry();
Set<String> meterNamesOfInterest = meterNamesOfInterest(prometheusMeterRegistry,
scopeSelection,
meterNameSelection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

Expand Down Expand Up @@ -80,6 +81,17 @@ private void accessMetricsSettings(Runnable operation) {
}
}

private <T> T accessMetricsSettings(Callable<T> callable) {
metricsSettingsAccess.lock();
try {
return callable.call();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
metricsSettingsAccess.unlock();
}
}


/**
* Create a new factory with default configuration, with pre-filled
Expand Down Expand Up @@ -137,9 +149,6 @@ public static RegistryFactory getInstance(Config config) {
}

Registry getARegistry(String scope) {
if (Registry.BASE_SCOPE.equals(scope)) {
ensureBase();
}
return registries.get(scope);
}

Expand All @@ -151,10 +160,10 @@ Registry getARegistry(String scope) {
*/
@Override
public io.helidon.metrics.api.Registry getRegistry(String scope) {
if (Registry.BASE_SCOPE.equals(scope)) {
ensureBase();
}
return registries.computeIfAbsent(scope, s -> Registry.create(s, metricsSettings.registrySettings(s)));
return accessMetricsSettings(() -> registries.computeIfAbsent(scope, s ->
s.equals(Registry.BASE_SCOPE)
? BaseRegistry.create(metricsSettings)
: Registry.create(s, metricsSettings.registrySettings(s))));
}

@Override
Expand Down Expand Up @@ -204,21 +213,20 @@ public Iterable<String> scopes() {

@Override
public void start() {
/*
Primarily for successive tests (e.g., in the TCK) which might share the same VM, delete each metric individually
(which will trickle down into the delegate meter registry) and also clear out the collection of registries.
*/
registries.values()
.forEach(r -> r.getMetrics()
.forEach((id, m) -> r.remove(id)));
registries.clear();
PeriodicExecutor.start();
}

@Override
public void stop() {
PeriodicExecutor.stop();
}

private void ensureBase() {
if (null == registries.get(Registry.BASE_SCOPE)) {
accessMetricsSettings(() -> {
Registry registry = BaseRegistry.create(metricsSettings);
registries.put(Registry.BASE_SCOPE, registry);
});
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
*/
package io.helidon.microprofile.metrics.tck;

import io.helidon.metrics.api.RegistryFactory;

import jakarta.enterprise.event.Observes;
import jakarta.enterprise.inject.spi.BeforeBeanDiscovery;
import jakarta.enterprise.inject.spi.Extension;

public class MetricsTckCdiExtension implements Extension {

void before(@Observes BeforeBeanDiscovery discovery) {
RegistryFactory.getInstance().start();
discovery.addAnnotatedType(ArrayParamConverterProvider.class, ArrayParamConverterProvider.class.getSimpleName());
}
}

0 comments on commit 396c276

Please sign in to comment.