SOLR-14770 Avoid reregistering JVM Guage as well (#1469)

This commit is contained in:
Mike Drob 2020-09-08 10:18:24 -05:00 committed by GitHub
parent 4c5c8c4ead
commit 984466f31b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 19 deletions

View File

@ -527,7 +527,8 @@ public class SolrMetricManager {
*/
public enum ResolutionStrategy {
/**
* The existing metric will be kept and the new metric will be ignored
* The existing metric will be kept and the new metric will be ignored. If no metric exists, then the new metric
* will be registered.
*/
IGNORE,
/**
@ -556,13 +557,11 @@ public class SolrMetricManager {
Map<String, Metric> existingMetrics = metricRegistry.getMetrics();
for (Map.Entry<String, Metric> entry : metrics.getMetrics().entrySet()) {
String fullName = mkName(entry.getKey(), metricPath);
if (existingMetrics.containsKey(fullName)) {
if (strategy == ResolutionStrategy.REPLACE) {
metricRegistry.remove(fullName);
} else if (strategy == ResolutionStrategy.IGNORE) {
continue;
} // strategy == ERROR will fail when we try to register later
}
if (strategy == ResolutionStrategy.REPLACE) {
metricRegistry.remove(fullName);
} else if (strategy == ResolutionStrategy.IGNORE && existingMetrics.containsKey(fullName)) {
continue;
} // strategy == ERROR will fail when we try to register
metricRegistry.register(fullName, entry.getValue());
}
}
@ -684,28 +683,36 @@ public class SolrMetricManager {
return registry(registry).histogram(name, histogramSupplier);
}
/**
* @deprecated use {@link #registerMetric(SolrMetricsContext, String, Metric, ResolutionStrategy, String, String...)}
*/
@Deprecated
public void registerMetric(SolrMetricsContext context, String registry, Metric metric, boolean force, String metricName, String... metricPath) {
registerMetric(context, registry, metric, force ? ResolutionStrategy.REPLACE : ResolutionStrategy.IGNORE, metricName, metricPath);
}
/**
* Register an instance of {@link Metric}.
*
* @param registry registry name
* @param metric metric instance
* @param force if true then an already existing metric with the same name will be replaced.
* When false and a metric with the same name already exists an exception
* will be thrown.
* @param strategy the conflict resolution strategy to use if the named metric already exists.
* @param metricName metric name, either final name or a fully-qualified name
* using dotted notation
* @param metricPath (optional) additional top-most metric name path elements
*/
public void registerMetric(SolrMetricsContext context, String registry, Metric metric, boolean force, String metricName, String... metricPath) {
public void registerMetric(SolrMetricsContext context, String registry, Metric metric, ResolutionStrategy strategy, String metricName, String... metricPath) {
MetricRegistry metricRegistry = registry(registry);
String fullName = mkName(metricName, metricPath);
if (context != null) {
context.registerMetricName(fullName);
}
synchronized (metricRegistry) { // prevent race; register() throws if metric is already present
if (force) { // must remove any existing one if present
if (strategy == ResolutionStrategy.REPLACE) { // must remove any existing one if present
metricRegistry.remove(fullName);
}
} else if (strategy == ResolutionStrategy.IGNORE && metricRegistry.getMetrics().containsKey(fullName)) {
return;
} // strategy == ERROR will fail when we try to register
metricRegistry.register(fullName, metric);
}
}
@ -740,9 +747,16 @@ public class SolrMetricManager {
}
}
@SuppressWarnings({"unchecked", "rawtypes"})
/**
* @deprecated use {@link #registerGauge(SolrMetricsContext, String, Gauge, String, ResolutionStrategy, String, String...)}
*/
@Deprecated
public void registerGauge(SolrMetricsContext context, String registry, Gauge<?> gauge, String tag, boolean force, String metricName, String... metricPath) {
registerMetric(context, registry, new GaugeWrapper(gauge, tag), force, metricName, metricPath);
registerGauge(context, registry, gauge, tag, force ? ResolutionStrategy.REPLACE : ResolutionStrategy.ERROR, metricName, metricPath);
}
public <T> void registerGauge(SolrMetricsContext context, String registry, Gauge<T> gauge, String tag, ResolutionStrategy strategy, String metricName, String... metricPath) {
registerMetric(context, registry, new GaugeWrapper<>(gauge, tag), strategy, metricName, metricPath);
}
public int unregisterGauges(String registryName, String tagSegment) {

View File

@ -129,10 +129,10 @@ public class SolrMetricsContext {
}
/**
* Convenience method for {@link SolrMetricManager#registerGauge(SolrMetricsContext, String, Gauge, String, boolean, String, String...)}.
* Convenience method for {@link SolrMetricManager#registerGauge(SolrMetricsContext, String, Gauge, String, SolrMetricManager.ResolutionStrategy, String, String...)}.
*/
public void gauge(Gauge<?> gauge, boolean force, String metricName, String... metricPath) {
metricManager.registerGauge(this, registryName, gauge, tag, force, metricName, metricPath);
metricManager.registerGauge(this, registryName, gauge, tag, force ? SolrMetricManager.ResolutionStrategy.REPLACE : SolrMetricManager.ResolutionStrategy.ERROR, metricName, metricPath);
}
/**

View File

@ -228,7 +228,7 @@ public class SolrDispatchFilter extends BaseSolrFilter {
}
});
});
metricManager.registerGauge(null, registryName, sysprops, metricTag, true, "properties", "system");
metricManager.registerGauge(null, registryName, sysprops, metricTag, SolrMetricManager.ResolutionStrategy.IGNORE, "properties", "system");
} catch (Exception e) {
log.warn("Error registering JVM metrics", e);
}