|
| 1 | +/* |
| 2 | + * Copyright 2019 IBM All Rights Reserved. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +package org.hyperledger.fabric.traces.impl; |
| 7 | + |
| 8 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
| 9 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; |
| 10 | + |
| 11 | +import javax.annotation.Nullable; |
| 12 | +import java.time.Duration; |
| 13 | +import java.util.*; |
| 14 | +import java.util.concurrent.TimeUnit; |
| 15 | +import java.util.stream.Collectors; |
| 16 | + |
| 17 | +public final class OpenTelemetryProperties implements ConfigProperties { |
| 18 | + private final Map<String, String> config; |
| 19 | + |
| 20 | + public OpenTelemetryProperties(Properties props) { |
| 21 | + Map<String, String> config = new HashMap<>(); |
| 22 | + System.getenv().forEach( |
| 23 | + (name, value) -> config.put(name.toLowerCase(Locale.ROOT).replace('_', '.'), value)); |
| 24 | + System.getProperties().forEach( |
| 25 | + (key, value) -> |
| 26 | + config.put(((String) key).toLowerCase(Locale.ROOT).replace('-', '.'), (String) value)); |
| 27 | + props.forEach((key, value) -> |
| 28 | + config.put(((String) key).toLowerCase(Locale.ROOT).replace('-', '.'), (String) value)); |
| 29 | + |
| 30 | + this.config = config; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + @Nullable |
| 35 | + public String getString(String name) { |
| 36 | + return config.get(name); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + @Nullable |
| 41 | + public Boolean getBoolean(String name) { |
| 42 | + String value = config.get(name); |
| 43 | + if (value == null || value.isEmpty()) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + return Boolean.parseBoolean(value); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + @Nullable |
| 51 | + @SuppressWarnings("UnusedException") |
| 52 | + public Integer getInt(String name) { |
| 53 | + String value = config.get(name); |
| 54 | + if (value == null || value.isEmpty()) { |
| 55 | + return null; |
| 56 | + } |
| 57 | + try { |
| 58 | + return Integer.parseInt(value); |
| 59 | + } catch (NumberFormatException ex) { |
| 60 | + throw newInvalidPropertyException(name, value, "integer"); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + @Nullable |
| 66 | + @SuppressWarnings("UnusedException") |
| 67 | + public Long getLong(String name) { |
| 68 | + String value = config.get(name); |
| 69 | + if (value == null || value.isEmpty()) { |
| 70 | + return null; |
| 71 | + } |
| 72 | + try { |
| 73 | + return Long.parseLong(value); |
| 74 | + } catch (NumberFormatException ex) { |
| 75 | + throw newInvalidPropertyException(name, value, "long"); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + @Nullable |
| 81 | + @SuppressWarnings("UnusedException") |
| 82 | + public Double getDouble(String name) { |
| 83 | + String value = config.get(name); |
| 84 | + if (value == null || value.isEmpty()) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + try { |
| 88 | + return Double.parseDouble(value); |
| 89 | + } catch (NumberFormatException ex) { |
| 90 | + throw newInvalidPropertyException(name, value, "double"); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + @Nullable |
| 96 | + @SuppressWarnings("UnusedException") |
| 97 | + public Duration getDuration(String name) { |
| 98 | + String value = config.get(name); |
| 99 | + if (value == null || value.isEmpty()) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + String unitString = getUnitString(value); |
| 103 | + // TODO: Environment variables have unknown encoding. `trim()` may cut codepoints oddly |
| 104 | + // but likely we'll fail for malformed unit string either way. |
| 105 | + String numberString = value.substring(0, value.length() - unitString.length()); |
| 106 | + try { |
| 107 | + long rawNumber = Long.parseLong(numberString.trim()); |
| 108 | + TimeUnit unit = getDurationUnit(unitString.trim()); |
| 109 | + return Duration.ofMillis(TimeUnit.MILLISECONDS.convert(rawNumber, unit)); |
| 110 | + } catch (NumberFormatException ex) { |
| 111 | + throw new ConfigurationException( |
| 112 | + "Invalid duration property " |
| 113 | + + name |
| 114 | + + "=" |
| 115 | + + value |
| 116 | + + ". Expected number, found: " |
| 117 | + + numberString); |
| 118 | + } catch (ConfigurationException ex) { |
| 119 | + throw new ConfigurationException( |
| 120 | + "Invalid duration property " + name + "=" + value + ". " + ex.getMessage()); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public List<String> getList(String name) { |
| 126 | + String value = config.get(name); |
| 127 | + if (value == null) { |
| 128 | + return Collections.emptyList(); |
| 129 | + } |
| 130 | + return filterBlanksAndNulls(value.split(",")); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public Map<String, String> getMap(String name) { |
| 135 | + return getList(name).stream() |
| 136 | + .map(keyValuePair -> filterBlanksAndNulls(keyValuePair.split("=", 2))) |
| 137 | + .map( |
| 138 | + splitKeyValuePairs -> { |
| 139 | + if (splitKeyValuePairs.size() != 2) { |
| 140 | + throw new ConfigurationException( |
| 141 | + "Invalid map property: " + name + "=" + config.get(name)); |
| 142 | + } |
| 143 | + return new AbstractMap.SimpleImmutableEntry<>( |
| 144 | + splitKeyValuePairs.get(0), splitKeyValuePairs.get(1)); |
| 145 | + }) |
| 146 | + // If duplicate keys, prioritize later ones similar to duplicate system properties on a |
| 147 | + // Java command line. |
| 148 | + .collect( |
| 149 | + Collectors.toMap( |
| 150 | + Map.Entry::getKey, Map.Entry::getValue, (first, next) -> next, LinkedHashMap::new)); |
| 151 | + } |
| 152 | + |
| 153 | + private static ConfigurationException newInvalidPropertyException( |
| 154 | + String name, String value, String type) { |
| 155 | + throw new ConfigurationException( |
| 156 | + "Invalid value for property " + name + "=" + value + ". Must be a " + type + "."); |
| 157 | + } |
| 158 | + |
| 159 | + private static List<String> filterBlanksAndNulls(String[] values) { |
| 160 | + return Arrays.stream(values) |
| 161 | + .map(String::trim) |
| 162 | + .filter(s -> !s.isEmpty()) |
| 163 | + .collect(Collectors.toList()); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Returns the TimeUnit associated with a unit string. Defaults to milliseconds. |
| 168 | + */ |
| 169 | + private static TimeUnit getDurationUnit(String unitString) { |
| 170 | + switch (unitString) { |
| 171 | + case "": // Fallthrough expected |
| 172 | + case "ms": |
| 173 | + return TimeUnit.MILLISECONDS; |
| 174 | + case "s": |
| 175 | + return TimeUnit.SECONDS; |
| 176 | + case "m": |
| 177 | + return TimeUnit.MINUTES; |
| 178 | + case "h": |
| 179 | + return TimeUnit.HOURS; |
| 180 | + case "d": |
| 181 | + return TimeUnit.DAYS; |
| 182 | + default: |
| 183 | + throw new ConfigurationException("Invalid duration string, found: " + unitString); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Fragments the 'units' portion of a config value from the 'value' portion. |
| 189 | + * |
| 190 | + * <p>E.g. "1ms" would return the string "ms". |
| 191 | + */ |
| 192 | + private static String getUnitString(String rawValue) { |
| 193 | + int lastDigitIndex = rawValue.length() - 1; |
| 194 | + while (lastDigitIndex >= 0) { |
| 195 | + char c = rawValue.charAt(lastDigitIndex); |
| 196 | + if (Character.isDigit(c)) { |
| 197 | + break; |
| 198 | + } |
| 199 | + lastDigitIndex -= 1; |
| 200 | + } |
| 201 | + // Pull everything after the last digit. |
| 202 | + return rawValue.substring(lastDigitIndex + 1); |
| 203 | + } |
| 204 | +} |
0 commit comments