2121import io .prometheus .metrics .model .snapshots .MetricSnapshot ;
2222import io .prometheus .metrics .model .snapshots .MetricSnapshots ;
2323import io .prometheus .metrics .model .snapshots .NativeHistogramBuckets ;
24- import io .prometheus .metrics .model .snapshots .PrometheusNaming ;
2524import io .prometheus .metrics .model .snapshots .Quantiles ;
2625import io .prometheus .metrics .model .snapshots .SnapshotEscaper ;
2726import io .prometheus .metrics .model .snapshots .StateSetSnapshot ;
@@ -51,7 +50,10 @@ public String toDebugString(MetricSnapshots metricSnapshots, EscapingScheme esca
5150 for (MetricSnapshot s : merged ) {
5251 MetricSnapshot snapshot = SnapshotEscaper .escapeMetricSnapshot (s , escapingScheme );
5352 if (!snapshot .getDataPoints ().isEmpty ()) {
54- stringBuilder .append (TextFormat .printer ().printToString (convert (snapshot , escapingScheme )));
53+ stringBuilder .append (
54+ TextFormat .printer ()
55+ .printToString (
56+ convert (snapshot , s .getMetadata ().getOriginalName (), escapingScheme )));
5557 }
5658 }
5759 return stringBuilder .toString ();
@@ -65,12 +67,17 @@ public void write(
6567 for (MetricSnapshot s : merged ) {
6668 MetricSnapshot snapshot = SnapshotEscaper .escapeMetricSnapshot (s , escapingScheme );
6769 if (!snapshot .getDataPoints ().isEmpty ()) {
68- convert (snapshot , escapingScheme ).writeDelimitedTo (out );
70+ convert (snapshot , s . getMetadata (). getOriginalName (), escapingScheme ).writeDelimitedTo (out );
6971 }
7072 }
7173 }
7274
7375 public Metrics .MetricFamily convert (MetricSnapshot snapshot , EscapingScheme scheme ) {
76+ return convert (snapshot , snapshot .getMetadata ().getOriginalName (), scheme );
77+ }
78+
79+ private Metrics .MetricFamily convert (
80+ MetricSnapshot snapshot , String rawOriginalName , EscapingScheme scheme ) {
7481 Metrics .MetricFamily .Builder builder = Metrics .MetricFamily .newBuilder ();
7582 if (snapshot instanceof CounterSnapshot ) {
7683 for (CounterDataPointSnapshot data : ((CounterSnapshot ) snapshot ).getDataPoints ()) {
@@ -83,7 +90,13 @@ public Metrics.MetricFamily convert(MetricSnapshot snapshot, EscapingScheme sche
8390 builder .addMetric (convert (data , scheme ));
8491 }
8592 setMetadataUnlessEmpty (
86- builder , snapshot .getMetadata (), null , Metrics .MetricType .GAUGE , scheme , true );
93+ builder ,
94+ snapshot .getMetadata (),
95+ rawOriginalName ,
96+ null ,
97+ Metrics .MetricType .GAUGE ,
98+ scheme ,
99+ true );
87100 } else if (snapshot instanceof HistogramSnapshot ) {
88101 HistogramSnapshot histogram = (HistogramSnapshot ) snapshot ;
89102 for (HistogramSnapshot .HistogramDataPointSnapshot data : histogram .getDataPoints ()) {
@@ -291,12 +304,14 @@ private void setMetadataUnlessEmpty(
291304 @ Nullable String nameSuffix ,
292305 Metrics .MetricType type ,
293306 EscapingScheme scheme ) {
294- setMetadataUnlessEmpty (builder , metadata , nameSuffix , type , scheme , false );
307+ setMetadataUnlessEmpty (
308+ builder , metadata , metadata .getOriginalName (), nameSuffix , type , scheme , false );
295309 }
296310
297311 private void setMetadataUnlessEmpty (
298312 Metrics .MetricFamily .Builder builder ,
299313 MetricMetadata metadata ,
314+ String rawOriginalName ,
300315 @ Nullable String nameSuffix ,
301316 Metrics .MetricType type ,
302317 EscapingScheme scheme ,
@@ -305,7 +320,8 @@ private void setMetadataUnlessEmpty(
305320 return ;
306321 }
307322 builder .setName (
308- resolveMetricFamilyName (metadata , nameSuffix , scheme , normalizeLegacyGaugeName ));
323+ resolveMetricFamilyName (
324+ metadata , rawOriginalName , nameSuffix , scheme , normalizeLegacyGaugeName ));
309325 if (metadata .getHelp () != null ) {
310326 builder .setHelp (metadata .getHelp ());
311327 }
@@ -314,19 +330,12 @@ private void setMetadataUnlessEmpty(
314330
315331 private String resolveMetricFamilyName (
316332 MetricMetadata metadata ,
333+ String rawOriginalName ,
317334 @ Nullable String nameSuffix ,
318335 EscapingScheme scheme ,
319336 boolean normalizeLegacyGaugeName ) {
320337 if (normalizeLegacyGaugeName ) {
321- String originalName = metadata .getOriginalName ();
322- if (originalName .endsWith (".created" )) {
323- return PrometheusNaming .escapeName (
324- originalName .substring (0 , originalName .length () - ".created" .length ()), scheme );
325- }
326- if (originalName .endsWith (".total" )) {
327- return PrometheusNaming .escapeName (
328- originalName .substring (0 , originalName .length () - ".total" .length ()), scheme );
329- }
338+ return SnapshotEscaper .getLegacyGaugeName (metadata , rawOriginalName , scheme );
330339 }
331340 if (nameSuffix == null ) {
332341 return SnapshotEscaper .getMetadataName (metadata , scheme );
0 commit comments