1717
1818import io .micrometer .common .util .internal .logging .WarnThenDebugLogger ;
1919import io .micrometer .core .instrument .*;
20+ import io .micrometer .core .instrument .config .NamingConvention ;
2021import io .micrometer .core .instrument .cumulative .CumulativeFunctionCounter ;
2122import io .micrometer .core .instrument .cumulative .CumulativeFunctionTimer ;
2223import io .micrometer .core .instrument .distribution .*;
@@ -114,6 +115,10 @@ public PrometheusMeterRegistry(PrometheusConfig config, PrometheusRegistry regis
114115 config ().onMeterRemoved (this ::onMeterRemoved );
115116 }
116117
118+ private static List <String > tagKeys (Meter .Id id , NamingConvention convention ) {
119+ return id .getConventionTags (convention ).stream ().map (Tag ::getKey ).collect (toList ());
120+ }
121+
117122 private static List <String > tagValues (Meter .Id id ) {
118123 return stream (id .getTagsAsIterable ().spliterator (), false ).map (Tag ::getValue ).collect (toList ());
119124 }
@@ -212,11 +217,11 @@ public Counter newCounter(Meter.Id id) {
212217 long createdTimestampMillis = clock .wallTime ();
213218 applyToCollector (id , (collector ) -> {
214219 List <String > tagValues = tagValues (id );
215- collector
216- .add (tagValues , (conventionName , tagKeys ) -> Stream .of (new MicrometerCollector .Family <>(conventionName ,
217- family -> new CounterSnapshot (family .metadata , family .dataPointSnapshots ),
218- getMetadata (conventionName , id .getDescription ()), new CounterDataPointSnapshot (counter .count (),
219- Labels .of (tagKeys , tagValues ), counter .exemplar (), createdTimestampMillis ))));
220+ List < String > tagKeys = tagKeys ( id , config (). namingConvention ());
221+ collector .add (id , (conventionName ) -> Stream .of (new MicrometerCollector .Family <>(conventionName ,
222+ family -> new CounterSnapshot (family .metadata , family .dataPointSnapshots ),
223+ getMetadata (conventionName , id .getDescription ()), new CounterDataPointSnapshot (counter .count (),
224+ Labels .of (tagKeys , tagValues ), counter .exemplar (), createdTimestampMillis ))));
220225 });
221226 return counter ;
222227 }
@@ -229,7 +234,8 @@ public DistributionSummary newDistributionSummary(Meter.Id id,
229234 long createdTimestampMillis = clock .wallTime ();
230235 applyToCollector (id , (collector ) -> {
231236 List <String > tagValues = tagValues (id );
232- collector .add (tagValues , (conventionName , tagKeys ) -> {
237+ List <String > tagKeys = tagKeys (id , config ().namingConvention ());
238+ collector .add (id , (conventionName ) -> {
233239 Stream .Builder <MicrometerCollector .Family <?>> families = Stream .builder ();
234240
235241 final ValueAtPercentile [] percentileValues = summary .takeSnapshot ().percentileValues ();
@@ -322,17 +328,17 @@ protected <T> io.micrometer.core.instrument.Gauge newGauge(Meter.Id id, @Nullabl
322328 Gauge gauge = new DefaultGauge <>(id , obj , valueFunction );
323329 applyToCollector (id , (collector ) -> {
324330 List <String > tagValues = tagValues (id );
331+ List <String > tagKeys = tagKeys (id , config ().namingConvention ());
325332 if (id .getName ().endsWith (".info" )) {
326- collector .add (tagValues ,
327- (conventionName ,
328- tagKeys ) -> Stream .of (new MicrometerCollector .Family <>(conventionName ,
329- family -> new InfoSnapshot (family .metadata , family .dataPointSnapshots ),
330- getMetadata (conventionName , id .getDescription ()),
331- new InfoDataPointSnapshot (Labels .of (tagKeys , tagValues )))));
333+ collector .add (id ,
334+ (conventionName ) -> Stream .of (new MicrometerCollector .Family <>(conventionName ,
335+ family -> new InfoSnapshot (family .metadata , family .dataPointSnapshots ),
336+ getMetadata (conventionName , id .getDescription ()),
337+ new InfoDataPointSnapshot (Labels .of (tagKeys , tagValues )))));
332338 }
333339 else {
334- collector .add (tagValues ,
335- (conventionName , tagKeys ) -> Stream .of (new MicrometerCollector .Family <>(conventionName ,
340+ collector .add (id ,
341+ (conventionName ) -> Stream .of (new MicrometerCollector .Family <>(conventionName ,
336342 family -> new GaugeSnapshot (family .metadata , family .dataPointSnapshots ),
337343 getMetadata (conventionName , id .getDescription ()),
338344 new GaugeDataPointSnapshot (gauge .value (), Labels .of (tagKeys , tagValues ), null ))));
@@ -357,14 +363,13 @@ protected <T> FunctionTimer newFunctionTimer(Meter.Id id, T obj, ToLongFunction<
357363 long createdTimestampMillis = clock .wallTime ();
358364 applyToCollector (id , (collector ) -> {
359365 List <String > tagValues = tagValues (id );
360- collector .add (tagValues ,
361- (conventionName ,
362- tagKeys ) -> Stream .of (new MicrometerCollector .Family <>(conventionName ,
363- family -> new SummarySnapshot (family .metadata , family .dataPointSnapshots ),
364- getMetadata (conventionName , id .getDescription ()),
365- new SummaryDataPointSnapshot ((long ) ft .count (), ft .totalTime (getBaseTimeUnit ()),
366- Quantiles .EMPTY , Labels .of (tagKeys , tagValues ), null ,
367- createdTimestampMillis ))));
366+ List <String > tagKeys = tagKeys (id , config ().namingConvention ());
367+ collector .add (id ,
368+ (conventionName ) -> Stream .of (new MicrometerCollector .Family <>(conventionName ,
369+ family -> new SummarySnapshot (family .metadata , family .dataPointSnapshots ),
370+ getMetadata (conventionName , id .getDescription ()),
371+ new SummaryDataPointSnapshot ((long ) ft .count (), ft .totalTime (getBaseTimeUnit ()),
372+ Quantiles .EMPTY , Labels .of (tagKeys , tagValues ), null , createdTimestampMillis ))));
368373 });
369374 return ft ;
370375 }
@@ -375,8 +380,9 @@ protected <T> FunctionCounter newFunctionCounter(Meter.Id id, T obj, ToDoubleFun
375380 long createdTimestampMillis = clock .wallTime ();
376381 applyToCollector (id , (collector ) -> {
377382 List <String > tagValues = tagValues (id );
378- collector .add (tagValues , (conventionName ,
379- tagKeys ) -> Stream .of (new MicrometerCollector .Family <>(conventionName ,
383+ List <String > tagKeys = tagKeys (id , config ().namingConvention ());
384+ collector .add (id ,
385+ (conventionName ) -> Stream .of (new MicrometerCollector .Family <>(conventionName ,
380386 family -> new CounterSnapshot (family .metadata , family .dataPointSnapshots ),
381387 getMetadata (conventionName , id .getDescription ()), new CounterDataPointSnapshot (fc .count (),
382388 Labels .of (tagKeys , tagValues ), null , createdTimestampMillis ))));
@@ -388,7 +394,8 @@ protected <T> FunctionCounter newFunctionCounter(Meter.Id id, T obj, ToDoubleFun
388394 protected Meter newMeter (Meter .Id id , Meter .Type type , Iterable <Measurement > measurements ) {
389395 applyToCollector (id , (collector ) -> {
390396 List <String > tagValues = tagValues (id );
391- collector .add (tagValues , (conventionName , tagKeys ) -> {
397+ List <String > tagKeys = tagKeys (id , config ().namingConvention ());
398+ collector .add (id , (conventionName ) -> {
392399 Stream .Builder <MicrometerCollector .Family <?>> families = Stream .builder ();
393400 List <String > statKeys = new ArrayList <>(tagKeys );
394401 statKeys .add ("statistic" );
@@ -464,7 +471,8 @@ private void addDistributionStatisticSamples(Meter.Id id, MicrometerCollector co
464471 HistogramSupport histogramSupport , Supplier <Exemplars > exemplarsSupplier , List <String > tagValues ,
465472 boolean forLongTaskTimer ) {
466473 long createdTimestampMillis = clock .wallTime ();
467- collector .add (tagValues , (conventionName , tagKeys ) -> {
474+ List <String > tagKeys = tagKeys (id , config ().namingConvention ());
475+ collector .add (id , (conventionName ) -> {
468476 Stream .Builder <MicrometerCollector .Family <?>> families = Stream .builder ();
469477
470478 HistogramSnapshot histogramSnapshot = histogramSupport .takeSnapshot ();
@@ -557,7 +565,7 @@ private Exemplar createExemplarWithNewValue(double newValue, Exemplar exemplar)
557565 private void onMeterRemoved (Meter meter ) {
558566 MicrometerCollector collector = collectorMap .get (getConventionName (meter .getId ()));
559567 if (collector != null ) {
560- collector .remove (tagValues ( meter .getId () ));
568+ collector .remove (meter .getId ());
561569 if (collector .isEmpty ()) {
562570 collectorMap .remove (getConventionName (meter .getId ()));
563571 getPrometheusRegistry ().unregister (collector );
@@ -591,16 +599,7 @@ private void applyToCollector(Meter.Id id, Consumer<MicrometerCollector> consume
591599 return existingCollector ;
592600 }
593601
594- List <String > tagKeys = getConventionTags (id ).stream ().map (Tag ::getKey ).collect (toList ());
595- if (existingCollector .getTagKeys ().equals (tagKeys )) {
596- consumer .accept (existingCollector );
597- return existingCollector ;
598- }
599-
600- meterRegistrationFailed (id , "Prometheus requires that all meters with the same name have the same"
601- + " set of tag keys. There is already an existing meter named '" + name + "' containing tag keys ["
602- + String .join (", " , existingCollector .getTagKeys ()) + "]. The meter you are attempting to register"
603- + " has keys [" + String .join (", " , tagKeys ) + "]." );
602+ consumer .accept (existingCollector );
604603 return existingCollector ;
605604 });
606605 }
0 commit comments