From f4630726181695a19a725c1a5acd1f2867ac1e6a Mon Sep 17 00:00:00 2001 From: Jon Schneider Date: Mon, 29 Jan 2018 13:15:55 -0600 Subject: [PATCH] Remove PropertyMeterFilter --- .../config/PropertyMeterFilter.java | 120 ------------------ .../config/PropertyMeterFilterTest.java | 86 ------------- 2 files changed, 206 deletions(-) delete mode 100644 micrometer-core/src/main/java/io/micrometer/core/instrument/config/PropertyMeterFilter.java delete mode 100644 micrometer-core/src/test/java/io/micrometer/core/instrument/config/PropertyMeterFilterTest.java diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/config/PropertyMeterFilter.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/config/PropertyMeterFilter.java deleted file mode 100644 index f811039435..0000000000 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/config/PropertyMeterFilter.java +++ /dev/null @@ -1,120 +0,0 @@ -/** - * Copyright 2017 Pivotal Software, Inc. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.micrometer.core.instrument.config; - -import io.micrometer.core.instrument.Meter; -import io.micrometer.core.instrument.histogram.HistogramConfig; -import io.micrometer.core.lang.Nullable; - -import java.time.Duration; - -/** - * A meter filter built from a set of properties. - * - * @author Clint Checketts - * @author Jon Schneider - */ -public abstract class PropertyMeterFilter implements MeterFilter { - @Nullable - public abstract V get(String k, Class vClass); - - @Nullable - protected V getMostSpecific(String k, String suffix, Class vClass) { - V v = get(k.isEmpty() ? suffix : k + "." + suffix, vClass); - if (v != null) - return v; - else if (k.isEmpty()) { - return null; - } - - int lastSep = k.lastIndexOf('.'); - if (lastSep == -1) - return getMostSpecific("", suffix, vClass); - - return getMostSpecific(k.substring(0, lastSep), suffix, vClass); - } - - @Override - public MeterFilterReply accept(Meter.Id id) { - Boolean enabled = getMostSpecific(id.getName(), "enabled", Boolean.class); - if (enabled == null) - return MeterFilterReply.NEUTRAL; - return enabled ? MeterFilterReply.ACCEPT : MeterFilterReply.DENY; - } - - @Override - public HistogramConfig configure(Meter.Id id, HistogramConfig histogramConfig) { - if (!id.getType().equals(Meter.Type.Timer) && !id.getType().equals(Meter.Type.DistributionSummary)) - return histogramConfig; - - HistogramConfig.Builder builder = HistogramConfig.builder(); - - Boolean percentileHistogram = getMostSpecific(id.getName(), "percentileHistogram", Boolean.class); - if (percentileHistogram != null) - builder.percentilesHistogram(percentileHistogram); - - double[] percentiles = getMostSpecific(id.getName(), "percentiles", double[].class); - if (percentiles != null) - builder.percentiles(percentiles); - - Integer histogramBufferLength = getMostSpecific(id.getName(), "histogramBufferLength", Integer.class); - if (histogramBufferLength != null) { - builder.histogramBufferLength(histogramBufferLength); - } - - Duration histogramExpiry = getMostSpecific(id.getName(), "histogramExpiry", Duration.class); - if (histogramExpiry != null) { - builder.histogramExpiry(histogramExpiry); - } - - if (id.getType().equals(Meter.Type.Timer)) { - Duration max = getMostSpecific(id.getName(), "maximumExpectedValue", Duration.class); - if (max != null) { - builder.maximumExpectedValue(max.toNanos()); - } - - Duration min = getMostSpecific(id.getName(), "minimumExpectedValue", Duration.class); - if (min != null) { - builder.minimumExpectedValue(min.toNanos()); - } - - Duration[] sla = getMostSpecific(id.getName(), "sla", Duration[].class); - if (sla != null) { - long[] slaNanos = new long[sla.length]; - for (int i = 0; i < sla.length; i++) { - slaNanos[i] = sla[i].toNanos(); - } - builder.sla(slaNanos); - } - } else if (id.getType().equals(Meter.Type.DistributionSummary)) { - Long max = getMostSpecific(id.getName(), "maximumExpectedValue", Long.class); - if (max != null) { - builder.maximumExpectedValue(max); - } - - Long min = getMostSpecific(id.getName(), "minimumExpectedValue", Long.class); - if (min != null) { - builder.minimumExpectedValue(min); - } - - long[] sla = getMostSpecific(id.getName(), "sla", long[].class); - if (sla != null) - builder.sla(sla); - } - - return builder.build().merge(histogramConfig); - } -} diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/config/PropertyMeterFilterTest.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/config/PropertyMeterFilterTest.java deleted file mode 100644 index be1b494e51..0000000000 --- a/micrometer-core/src/test/java/io/micrometer/core/instrument/config/PropertyMeterFilterTest.java +++ /dev/null @@ -1,86 +0,0 @@ -/** - * Copyright 2017 Pivotal Software, Inc. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.micrometer.core.instrument.config; - -import io.micrometer.core.instrument.DistributionSummary; -import io.micrometer.core.instrument.Meter; -import io.micrometer.core.instrument.MeterRegistry; -import io.micrometer.core.instrument.MockClock; -import io.micrometer.core.instrument.histogram.HistogramConfig; -import io.micrometer.core.instrument.simple.SimpleConfig; -import io.micrometer.core.instrument.simple.SimpleMeterRegistry; -import io.micrometer.core.lang.Nullable; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import static java.util.Objects.requireNonNull; -import static org.assertj.core.api.Assertions.assertThat; - -class PropertyMeterFilterTest { - // In a real application, the `get` method would be bound to some property source and - // mapped to the appropriate type with some sort of type conversion service. - private PropertyMeterFilter filter = new PropertyMeterFilter() { - @SuppressWarnings("unchecked") - @Override - public V get(String k, Class vClass) { - if (k.equals("enabled")) - return (V) (Boolean) false; - if (k.equals("my.counter.enabled")) - return (V) (Boolean) false; - if (k.equals("my.timer.enabled")) - return (V) (Boolean) true; - if (k.equals("my.summary.enabled")) - return (V) (Boolean) true; - if (k.equals("my.summary.maximumExpectedValue")) - return (V) (Long) 100L; - return null; - } - }; - - @Nullable - private HistogramConfig histogramConfig; - - private MeterRegistry registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, new MockClock()) { - @Override - protected DistributionSummary newDistributionSummary(Meter.Id id, HistogramConfig conf) { - histogramConfig = conf; - return super.newDistributionSummary(id, conf); - } - }; - - @BeforeEach - void configureRegistry() { - registry.config().meterFilter(filter); - } - - @Test - void disable() { - registry.counter("my.counter"); - assertThat(registry.find("my.counter").counter()).isNull(); - } - - @Test - void enable() { - registry.timer("my.timer"); - registry.get("my.timer").timer(); - } - - @Test - void summaryHistogramConfig() { - registry.summary("my.summary"); - assertThat(requireNonNull(histogramConfig).getMaximumExpectedValue()).isEqualTo(100); - } -}