Repository: open-telemetry/opentelemetry-java
File: exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Otel2PrometheusConverter.java (Java)

Code before:
              + b.getUnit()
              + ".");
      return null;
    }
    return new MetricMetadata(name, help, unit);
  }

  private static String typeString(MetricSnapshot snapshot) {
    // Simple helper for a log message.
    return snapshot.getClass().getSimpleName().replace("Snapshot", "").toLowerCase(Locale.ENGLISH);
  }

  private static String toLabelValue(AttributeType type, Object attributeValue) {
    if (AttributeType.STRING.equals(type)) {
      return attributeValue.toString();
    } else if (AttributeType.BOOLEAN.equals(type)) {
      return attributeValue.toString();
    } else if (AttributeType.LONG.equals(type)) {
      return attributeValue.toString();
    } else if (AttributeType.DOUBLE.equals(type)) {
      return attributeValue.toString();
    } else if (AttributeType.STRING_ARRAY.equals(type)) {
      if (attributeValue instanceof List) {
        return ((List<?>) attributeValue)
            .stream()
                .map(String.class::cast)
                .map(Otel2PrometheusConverter::toJsonValidStr)
                .collect(Collectors.toList())
                .toString();
      } else {
        LOGGER.log(
            Level.WARNING,
            "Unexpected label value for AttributeType.STRING_ARRAY, toString() is being used as fallback value...");
        return attributeValue.toString();
      }
    } else if (AttributeType.BOOLEAN_ARRAY.equals(type)) {
      return attributeValue.toString();
    } else if (AttributeType.LONG_ARRAY.equals(type)) {
      return attributeValue.toString();
    } else if (AttributeType.DOUBLE_ARRAY.equals(type)) {
      return attributeValue.toString();
    } else {
      throw new IllegalStateException(("Unrecognized AttributeType: " + type));
    }
  }

  public static String toJsonValidStr(String str) {
    StringBuilder sb = new StringBuilder();
    sb.append('"');
    for (int i = 0; i < str.length(); i++) {
      char c = str.charAt(i);

Review comment (bug):
How about calling `toString` here instead? Just to avoid the class cast error in case something goes wrong

Code after:
              + " but different units found. Dropping the one with unit "
              + b.getUnit()
              + ".");
      return null;
    }
    return new MetricMetadata(name, help, unit);
  }

  private static String typeString(MetricSnapshot snapshot) {
    // Simple helper for a log message.
    return snapshot.getClass().getSimpleName().replace("Snapshot", "").toLowerCase(Locale.ENGLISH);
  }

  private static String toLabelValue(AttributeType type, Object attributeValue) {
    switch (type) {
      case STRING:
      case BOOLEAN:
      case LONG:
      case DOUBLE:
        return attributeValue.toString();
      case BOOLEAN_ARRAY:
      case LONG_ARRAY:
      case DOUBLE_ARRAY:
      case STRING_ARRAY:
        if (attributeValue instanceof List) {
          return toJsonStr((List<?>) attributeValue);
        } else {
          throw new IllegalStateException(
              String.format(
                  "Unexpected label value of %s for %s",
                  attributeValue.getClass().getName(), type.name()));
        }
    }
    throw new IllegalStateException("Unrecognized AttributeType: " + type);
  }

  public static String toJsonStr(List<?> attributeValue) {
    StringJoiner joiner = new StringJoiner(",", "[", "]");
    for (int i = 0; i < attributeValue.size(); i++) {
      Object value = attributeValue.get(i);
      joiner.add(value instanceof String ? toJsonValidStr((String) value) : String.valueOf(value));
    }
    return joiner.toString();
  }

  public static String toJsonValidStr(String str) {
    StringBuilder sb = new StringBuilder();
    sb.append('"');
    for (int i = 0; i < str.length(); i++) {
      char c = str.charAt(i);