@@ -384,11 +384,6 @@ public static MetricSnapshot escapeMetricSnapshot(MetricSnapshot v, EscapingSche
384384 return v ;
385385 }
386386
387- String outName =
388- isValidLegacyMetricName (v .getMetadata ().getPrometheusName ())
389- ? v .getMetadata ().getPrometheusName ()
390- : escapeName (v .getMetadata ().getPrometheusName (), scheme );
391-
392387 List <DataPointSnapshot > outDataPoints = new ArrayList <>();
393388
394389 for (DataPointSnapshot d : v .getDataPoints ()) {
@@ -401,26 +396,19 @@ public static MetricSnapshot escapeMetricSnapshot(MetricSnapshot v, EscapingSche
401396
402397 for (Label l : d .getLabels ()) {
403398 if (METRIC_NAME_LABEL .equals (l .getName ())) {
404- if (l .getValue () == null || isValidLegacyMetricName (l .getValue ())) {
405- outLabelsBuilder .label (l .getName (), l .getValue ());
406- continue ;
407- }
408399 outLabelsBuilder .label (l .getName (), escapeName (l .getValue (), scheme ));
409- continue ;
410- }
411- if (l .getName () == null || isValidLegacyMetricName (l .getName ())) {
412- outLabelsBuilder .label (l .getName (), l .getValue ());
413- continue ;
400+ } else {
401+ outLabelsBuilder .label (escapeName (l .getName (), scheme ), l .getValue ());
414402 }
415- outLabelsBuilder .label (escapeName (l .getName (), scheme ), l .getValue ());
416403 }
417404
418405 Labels outLabels = outLabelsBuilder .build ();
419406 DataPointSnapshot outDataPointSnapshot = createEscapedDataPointSnapshot (v , d , outLabels );
420407 outDataPoints .add (outDataPointSnapshot );
421408 }
422409
423- return createEscapedMetricSnapshot (v , outName , outDataPoints );
410+ return createEscapedMetricSnapshot (
411+ v , escapeName (v .getMetadata ().getName (), scheme ), outDataPoints );
424412 }
425413
426414 static boolean metricNeedsEscaping (DataPointSnapshot d ) {
@@ -588,9 +576,10 @@ private static MetricSnapshot createEscapedMetricSnapshot(
588576 * which by definition is a noop). This method does not do any validation of the name.
589577 */
590578 public static String escapeName (String name , EscapingScheme scheme ) {
591- if (name .isEmpty ()) {
579+ if (name .isEmpty () || isValidLegacyMetricName ( name ) ) {
592580 return name ;
593581 }
582+
594583 StringBuilder escaped = new StringBuilder ();
595584 switch (scheme ) {
596585 case NO_ESCAPING :
0 commit comments