Skip to content

Commit e36f6e9

Browse files
committed
Revise API to not pass OtlpConfig
1 parent 1873ac2 commit e36f6e9

File tree

2 files changed

+64
-73
lines changed

2 files changed

+64
-73
lines changed

Diff for: implementations/micrometer-registry-otlp/src/main/java/io/micrometer/registry/otlp/OtlpMeterRegistry.java

+54-43
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import java.util.concurrent.ScheduledExecutorService;
5252
import java.util.concurrent.ThreadFactory;
5353
import java.util.concurrent.TimeUnit;
54-
import java.util.function.Supplier;
5554
import java.util.function.ToDoubleFunction;
5655
import java.util.function.ToLongFunction;
5756

@@ -118,7 +117,7 @@ public OtlpMeterRegistry(OtlpConfig config, Clock clock) {
118117
*/
119118
public OtlpMeterRegistry(OtlpConfig config, Clock clock, ThreadFactory threadFactory) {
120119
this(config, clock, threadFactory, new OtlpHttpMetricsSender(new HttpUrlConnectionSender()),
121-
OtlpMeterRegistry::histogramFlavorPerMeter, OtlpMeterRegistry::maxBucketsPerMeter);
120+
HistogramFlavorPerMeterLookup.DEFAULT, MaxBucketsPerMeterLookup.DEFAULT);
122121
}
123122

124123
private OtlpMeterRegistry(OtlpConfig config, Clock clock, ThreadFactory threadFactory,
@@ -440,12 +439,16 @@ private Histogram getHistogram(Meter.Id id, DistributionStatisticConfig distribu
440439
}
441440

442441
private int getMaxBuckets(Meter.Id id) {
443-
return maxBucketsPerMeterLookup.getMaxBuckets(config, id);
442+
Integer maxBuckets = maxBucketsPerMeterLookup.getMaxBuckets(config.maxBucketsPerMeter(), id);
443+
return (maxBuckets == null) ? config.maxBucketCount() : maxBuckets;
444444
}
445445

446446
private HistogramFlavor histogramFlavor(Meter.Id id, OtlpConfig otlpConfig,
447447
DistributionStatisticConfig distributionStatisticConfig) {
448-
HistogramFlavor preferredHistogramFlavor = histogramFlavorPerMeterLookup.getHistogramFlavor(otlpConfig, id);
448+
HistogramFlavor preferredHistogramFlavor = histogramFlavorPerMeterLookup
449+
.getHistogramFlavor(otlpConfig.histogramFlavorPerMeter(), id);
450+
preferredHistogramFlavor = preferredHistogramFlavor == null ? otlpConfig.histogramFlavor()
451+
: preferredHistogramFlavor;
449452
final double[] serviceLevelObjectiveBoundaries = distributionStatisticConfig
450453
.getServiceLevelObjectiveBoundaries();
451454
if (distributionStatisticConfig.isPublishingHistogram()
@@ -505,37 +508,6 @@ static double[] getSloWithPositiveInf(DistributionStatisticConfig distributionSt
505508
return sloWithPositiveInf;
506509
}
507510

508-
// VisibleForTesting
509-
static HistogramFlavor histogramFlavorPerMeter(OtlpConfig config, Meter.Id id) {
510-
return lookup(config.histogramFlavorPerMeter(), id, config.histogramFlavor());
511-
}
512-
513-
// VisibleForTesting
514-
static Integer maxBucketsPerMeter(OtlpConfig config, Meter.Id id) {
515-
return lookup(config.maxBucketsPerMeter(), id, config.maxBucketCount());
516-
}
517-
518-
private static <T> T lookup(Map<String, T> values, Meter.Id id, T defaultValue) {
519-
if (values.isEmpty()) {
520-
return defaultValue;
521-
}
522-
return doLookup(values, id, () -> defaultValue);
523-
}
524-
525-
private static <T> T doLookup(Map<String, T> values, Meter.Id id, Supplier<T> defaultValue) {
526-
String name = id.getName();
527-
while (StringUtils.isNotEmpty(name)) {
528-
T result = values.get(name);
529-
if (result != null) {
530-
return result;
531-
}
532-
int lastDot = name.lastIndexOf('.');
533-
name = (lastDot != -1) ? name.substring(0, lastDot) : "";
534-
}
535-
536-
return defaultValue.get();
537-
}
538-
539511
/**
540512
* Overridable lookup mechanism for {@link HistogramFlavor}.
541513
*
@@ -544,18 +516,26 @@ private static <T> T doLookup(Map<String, T> values, Meter.Id id, Supplier<T> de
544516
@FunctionalInterface
545517
public interface HistogramFlavorPerMeterLookup {
546518

519+
/**
520+
* Default implementation.
521+
*/
522+
HistogramFlavorPerMeterLookup DEFAULT = OtlpMeterRegistry::lookup;
523+
547524
/**
548525
* Looks up the histogram flavor to use on a per-meter level. This will override
549-
* the {@link OtlpConfig#histogramFlavor()} for matching Meters.
526+
* the default {@link OtlpConfig#histogramFlavor()} for matching Meters.
550527
* {@link OtlpConfig#histogramFlavorPerMeter()} provides the data while this
551528
* method provides the logic for the lookup, and you can override them
552529
* independently.
530+
* @param perMeterMapping configured mapping data
553531
* @param id the {@link Meter.Id} the {@link HistogramFlavor} is configured for
554-
* @return the histogram flavor mapped to the {@link Meter.Id}
532+
* @return the histogram flavor mapped to the {@link Meter.Id} or {@code null} if
533+
* mapping is undefined
555534
* @see OtlpConfig#histogramFlavorPerMeter()
556535
* @see OtlpConfig#histogramFlavor()
557536
*/
558-
HistogramFlavor getHistogramFlavor(OtlpConfig config, Meter.Id id);
537+
@Nullable
538+
HistogramFlavor getHistogramFlavor(Map<String, HistogramFlavor> perMeterMapping, Meter.Id id);
559539

560540
}
561541

@@ -568,20 +548,51 @@ public interface HistogramFlavorPerMeterLookup {
568548
@FunctionalInterface
569549
public interface MaxBucketsPerMeterLookup {
570550

551+
/**
552+
* Default implementation.
553+
*/
554+
MaxBucketsPerMeterLookup DEFAULT = OtlpMeterRegistry::lookup;
555+
571556
/**
572557
* Looks up the max bucket count to use on a per-meter level. This will override
573-
* the {@link OtlpConfig#maxBucketCount()} for matching Meters.
558+
* the default {@link OtlpConfig#maxBucketCount()} for matching Meters.
574559
* {@link OtlpConfig#maxBucketsPerMeter()} provides the data while this method
575560
* provides the logic for the lookup, and you can override them independently.
576561
* This has no effect on a meter if it does not have an exponential bucket
577562
* histogram configured.
563+
* @param perMeterMapping configured mapping data
578564
* @param id the {@link Meter.Id} the max bucket count is configured for
579-
* @return the max bucket count mapped to the {@link Meter.Id}
565+
* @return the max bucket count mapped to the {@link Meter.Id} or {@code null} if
566+
* the mapping is undefined
580567
* @see OtlpConfig#maxBucketsPerMeter()
581568
* @see OtlpConfig#maxBucketCount()
582569
*/
583-
Integer getMaxBuckets(OtlpConfig config, Meter.Id id);
570+
@Nullable
571+
Integer getMaxBuckets(Map<String, Integer> perMeterMapping, Meter.Id id);
572+
573+
}
584574

575+
@Nullable
576+
private static <T> T lookup(Map<String, T> values, Meter.Id id) {
577+
if (values.isEmpty()) {
578+
return null;
579+
}
580+
return doLookup(values, id);
581+
}
582+
583+
@Nullable
584+
private static <T> T doLookup(Map<String, T> values, Meter.Id id) {
585+
String name = id.getName();
586+
while (StringUtils.isNotEmpty(name)) {
587+
T result = values.get(name);
588+
if (result != null) {
589+
return result;
590+
}
591+
int lastDot = name.lastIndexOf('.');
592+
name = (lastDot != -1) ? name.substring(0, lastDot) : "";
593+
}
594+
595+
return null;
585596
}
586597

587598
/**
@@ -606,8 +617,8 @@ public static class Builder {
606617
private Builder(OtlpConfig otlpConfig) {
607618
this.otlpConfig = otlpConfig;
608619
this.metricsSender = new OtlpHttpMetricsSender(new HttpUrlConnectionSender());
609-
this.histogramFlavorPerMeterLookup = OtlpMeterRegistry::histogramFlavorPerMeter;
610-
this.maxBucketsPerMeterLookup = OtlpMeterRegistry::maxBucketsPerMeter;
620+
this.histogramFlavorPerMeterLookup = HistogramFlavorPerMeterLookup.DEFAULT;
621+
this.maxBucketsPerMeterLookup = MaxBucketsPerMeterLookup.DEFAULT;
611622
}
612623

613624
/** Override the default clock. */

Diff for: implementations/micrometer-registry-otlp/src/test/java/io/micrometer/registry/otlp/OtlpMeterRegistryTest.java

+10-30
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,8 @@ public HistogramFlavor histogramFlavor() {
542542
};
543543
OtlpMeterRegistry meterRegistry = OtlpMeterRegistry.builder(config)
544544
.clock(clock)
545-
.histogramFlavorPerMeterLookup((cfg, id) -> id.getName().startsWith("e")
546-
? BASE2_EXPONENTIAL_BUCKET_HISTOGRAM : cfg.histogramFlavor())
545+
.histogramFlavorPerMeterLookup((pmp, id) -> id.getName().startsWith("e")
546+
? BASE2_EXPONENTIAL_BUCKET_HISTOGRAM : config.histogramFlavor())
547547
.build();
548548

549549
Timer expo = Timer.builder("expo").publishPercentileHistogram().register(meterRegistry);
@@ -600,8 +600,8 @@ public Map<String, HistogramFlavor> histogramFlavorPerMeter() {
600600
};
601601
OtlpMeterRegistry meterRegistry = OtlpMeterRegistry.builder(config)
602602
.clock(clock)
603-
.histogramFlavorPerMeterLookup((cfg, id) -> id.getName().startsWith("e")
604-
? BASE2_EXPONENTIAL_BUCKET_HISTOGRAM : cfg.histogramFlavor())
603+
.histogramFlavorPerMeterLookup((pmp, id) -> id.getName().startsWith("e")
604+
? BASE2_EXPONENTIAL_BUCKET_HISTOGRAM : config.histogramFlavor())
605605
.build();
606606

607607
Timer expo = Timer.builder("expo").publishPercentileHistogram().register(meterRegistry);
@@ -781,7 +781,7 @@ public int maxBucketCount() {
781781
};
782782
OtlpMeterRegistry meterRegistry = OtlpMeterRegistry.builder(config)
783783
.clock(clock)
784-
.maxBucketsPerMeterLookup((cfg, id) -> id.getName().equals("low.variation") ? 15 : cfg.maxBucketCount())
784+
.maxBucketsPerMeterLookup((pmp, id) -> id.getName().equals("low.variation") ? 15 : config.maxBucketCount())
785785
.build();
786786

787787
Timer lowVariation = Timer.builder("low.variation").publishPercentileHistogram().register(meterRegistry);
@@ -864,7 +864,7 @@ public Map<String, Integer> maxBucketsPerMeter() {
864864
};
865865
OtlpMeterRegistry meterRegistry = OtlpMeterRegistry.builder(config)
866866
.clock(clock)
867-
.maxBucketsPerMeterLookup((cfg, id) -> id.getName().equals("low.variation") ? 15 : cfg.maxBucketCount())
867+
.maxBucketsPerMeterLookup((pmp, id) -> id.getName().equals("low.variation") ? 15 : config.maxBucketCount())
868868
.build();
869869
Timer lowVariation = Timer.builder("low.variation").publishPercentileHistogram().register(meterRegistry);
870870
Timer lowVariationOther = Timer.builder("low.variation.other")
@@ -919,19 +919,9 @@ void longestMatchWinsByDefaultHistogramFlavorPerMeter() {
919919
Map<String, HistogramFlavor> histogramFlavorPerMeter = new HashMap<>();
920920
histogramFlavorPerMeter.put("http", EXPLICIT_BUCKET_HISTOGRAM);
921921
histogramFlavorPerMeter.put("http.server", BASE2_EXPONENTIAL_BUCKET_HISTOGRAM);
922-
OtlpConfig config = new OtlpConfig() {
923-
@Override
924-
public String get(String key) {
925-
return null;
926-
}
927-
928-
@Override
929-
public Map<String, HistogramFlavor> histogramFlavorPerMeter() {
930-
return histogramFlavorPerMeter;
931-
}
932-
};
933922

934-
assertThat(OtlpMeterRegistry.histogramFlavorPerMeter(config, createIdWithName("http.server.requests")))
923+
assertThat(OtlpMeterRegistry.HistogramFlavorPerMeterLookup.DEFAULT.getHistogramFlavor(histogramFlavorPerMeter,
924+
createIdWithName("http.server.requests")))
935925
.isEqualTo(BASE2_EXPONENTIAL_BUCKET_HISTOGRAM);
936926
}
937927

@@ -940,19 +930,9 @@ void longestMatchWinsByDefaultMaxBucketsPerMeter() {
940930
Map<String, Integer> maxBucketsPerMeter = new HashMap<>();
941931
maxBucketsPerMeter.put("http", 10);
942932
maxBucketsPerMeter.put("http.server", 20);
943-
OtlpConfig config = new OtlpConfig() {
944-
@Override
945-
public String get(String key) {
946-
return null;
947-
}
948-
949-
@Override
950-
public Map<String, Integer> maxBucketsPerMeter() {
951-
return maxBucketsPerMeter;
952-
}
953-
};
954933

955-
assertThat(OtlpMeterRegistry.maxBucketsPerMeter(config, createIdWithName("http.server.requests")))
934+
assertThat(OtlpMeterRegistry.MaxBucketsPerMeterLookup.DEFAULT.getMaxBuckets(maxBucketsPerMeter,
935+
createIdWithName("http.server.requests")))
956936
.isEqualTo(20);
957937
}
958938

0 commit comments

Comments
 (0)