Repository: open-telemetry/opentelemetry-java
File: api/incubator/src/main/java/io/opentelemetry/api/incubator/propagation/EnvironmentGetter.java (Java)

Code before:
    return INSTANCE;
  }

  @Override
  public Iterable<String> keys(Map<String, String> carrier) {
    if (carrier == null) {
      return Collections.emptyList();
    }
    return carrier.keySet();
  }

  @Nullable
  @Override
  public String get(@Nullable Map<String, String> carrier, String key) {
    if (carrier == null || key == null) {
      return null;
    }
    // Spec recommends using uppercase and underscores for environment variable
    // names for maximum
    // cross-platform compatibility.
    String sanitizedKey = key.replace('.', '_').replace('-', '_').toUpperCase(Locale.ROOT);
    String value = carrier.get(sanitizedKey);
    if (value != null && !isValidHttpHeaderValue(value)) {
      logger.log(
          Level.FINE,
          "Ignoring environment variable ''{0}'': "
              + "value contains characters not valid in HTTP header fields per RFC 9110.",
          sanitizedKey);
      return null;
    }
    return value;
  }

  /**
   * Checks whether a string contains only characters valid in HTTP header field values per <a
   * href="https://datatracker.ietf.org/doc/html/rfc9110#section-5.5">RFC 9110 Section 5.5</a>.
   * Valid characters are: visible ASCII (0x21-0x7E), space (0x20), and horizontal tab (0x09).
   */
  private static boolean isValidHttpHeaderValue(String value) {
    for (int i = 0; i < value.length(); i++) {
      char ch = value.charAt(i);
      // VCHAR (0x21-0x7E), SP (0x20), HTAB (0x09)
      if (ch != '\t' && (ch < ' ' || ch > '~')) {
        return false;
      }
    }
    return true;
  }

  @Override
  public String toString() {

Review comment (suggestion):
```suggestion
          "Ignoring environment variable '{0}': "
```

Code after:
  /** Returns the singleton instance of {@link EnvironmentGetter}. */
  public static EnvironmentGetter getInstance() {
    return INSTANCE;
  }

  @Override
  public Iterable<String> keys(Map<String, String> carrier) {
    if (carrier == null) {
      return Collections.emptyList();
    }
    List<String> result = new ArrayList<>(carrier.size());
    for (String key : carrier.keySet()) {
      result.add(key.toLowerCase(Locale.ROOT));
    }
    return result;
  }

  @Nullable
  @Override
  public String get(@Nullable Map<String, String> carrier, String key) {
    if (carrier == null || key == null) {
      return null;
    }
    // Spec recommends using uppercase and underscores for environment variable
    // names for maximum
    // cross-platform compatibility.
    String sanitizedKey = key.replace('.', '_').replace('-', '_').toUpperCase(Locale.ROOT);
    String value = carrier.get(sanitizedKey);
    if (value != null && !EnvironmentSetter.isValidHttpHeaderValue(value)) {
      logger.log(
          Level.FINE,
          "Ignoring environment variable '{0}': "
              + "value contains characters not valid in HTTP header fields per RFC 9110.",
          sanitizedKey);
      return null;
    }
    return value;
  }

  @Override
  public String toString() {
    return "EnvironmentGetter";
  }
}