51
51
import java .util .concurrent .ScheduledExecutorService ;
52
52
import java .util .concurrent .ThreadFactory ;
53
53
import java .util .concurrent .TimeUnit ;
54
- import java .util .function .Supplier ;
55
54
import java .util .function .ToDoubleFunction ;
56
55
import java .util .function .ToLongFunction ;
57
56
@@ -118,7 +117,7 @@ public OtlpMeterRegistry(OtlpConfig config, Clock clock) {
118
117
*/
119
118
public OtlpMeterRegistry (OtlpConfig config , Clock clock , ThreadFactory threadFactory ) {
120
119
this (config , clock , threadFactory , new OtlpHttpMetricsSender (new HttpUrlConnectionSender ()),
121
- OtlpMeterRegistry :: histogramFlavorPerMeter , OtlpMeterRegistry :: maxBucketsPerMeter );
120
+ HistogramFlavorPerMeterLookup . DEFAULT , MaxBucketsPerMeterLookup . DEFAULT );
122
121
}
123
122
124
123
private OtlpMeterRegistry (OtlpConfig config , Clock clock , ThreadFactory threadFactory ,
@@ -440,12 +439,16 @@ private Histogram getHistogram(Meter.Id id, DistributionStatisticConfig distribu
440
439
}
441
440
442
441
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 ;
444
444
}
445
445
446
446
private HistogramFlavor histogramFlavor (Meter .Id id , OtlpConfig otlpConfig ,
447
447
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 ;
449
452
final double [] serviceLevelObjectiveBoundaries = distributionStatisticConfig
450
453
.getServiceLevelObjectiveBoundaries ();
451
454
if (distributionStatisticConfig .isPublishingHistogram ()
@@ -505,37 +508,6 @@ static double[] getSloWithPositiveInf(DistributionStatisticConfig distributionSt
505
508
return sloWithPositiveInf ;
506
509
}
507
510
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
-
539
511
/**
540
512
* Overridable lookup mechanism for {@link HistogramFlavor}.
541
513
*
@@ -544,18 +516,26 @@ private static <T> T doLookup(Map<String, T> values, Meter.Id id, Supplier<T> de
544
516
@ FunctionalInterface
545
517
public interface HistogramFlavorPerMeterLookup {
546
518
519
+ /**
520
+ * Default implementation.
521
+ */
522
+ HistogramFlavorPerMeterLookup DEFAULT = OtlpMeterRegistry ::lookup ;
523
+
547
524
/**
548
525
* 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.
550
527
* {@link OtlpConfig#histogramFlavorPerMeter()} provides the data while this
551
528
* method provides the logic for the lookup, and you can override them
552
529
* independently.
530
+ * @param perMeterMapping configured mapping data
553
531
* @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
555
534
* @see OtlpConfig#histogramFlavorPerMeter()
556
535
* @see OtlpConfig#histogramFlavor()
557
536
*/
558
- HistogramFlavor getHistogramFlavor (OtlpConfig config , Meter .Id id );
537
+ @ Nullable
538
+ HistogramFlavor getHistogramFlavor (Map <String , HistogramFlavor > perMeterMapping , Meter .Id id );
559
539
560
540
}
561
541
@@ -568,20 +548,51 @@ public interface HistogramFlavorPerMeterLookup {
568
548
@ FunctionalInterface
569
549
public interface MaxBucketsPerMeterLookup {
570
550
551
+ /**
552
+ * Default implementation.
553
+ */
554
+ MaxBucketsPerMeterLookup DEFAULT = OtlpMeterRegistry ::lookup ;
555
+
571
556
/**
572
557
* 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.
574
559
* {@link OtlpConfig#maxBucketsPerMeter()} provides the data while this method
575
560
* provides the logic for the lookup, and you can override them independently.
576
561
* This has no effect on a meter if it does not have an exponential bucket
577
562
* histogram configured.
563
+ * @param perMeterMapping configured mapping data
578
564
* @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
580
567
* @see OtlpConfig#maxBucketsPerMeter()
581
568
* @see OtlpConfig#maxBucketCount()
582
569
*/
583
- Integer getMaxBuckets (OtlpConfig config , Meter .Id id );
570
+ @ Nullable
571
+ Integer getMaxBuckets (Map <String , Integer > perMeterMapping , Meter .Id id );
572
+
573
+ }
584
574
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 ;
585
596
}
586
597
587
598
/**
@@ -606,8 +617,8 @@ public static class Builder {
606
617
private Builder (OtlpConfig otlpConfig ) {
607
618
this .otlpConfig = otlpConfig ;
608
619
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 ;
611
622
}
612
623
613
624
/** Override the default clock. */
0 commit comments