|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.contrib.sdk.autoconfigure; |
| 7 | + |
| 8 | +import static io.opentelemetry.api.incubator.config.DeclarativeConfigProperties.empty; |
| 9 | + |
| 10 | +import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties; |
| 11 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
| 12 | +import java.time.Duration; |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.Objects; |
| 18 | +import java.util.function.BiFunction; |
| 19 | +import javax.annotation.Nullable; |
| 20 | + |
| 21 | +/** |
| 22 | + * A {@link ConfigProperties} which resolves properties based on {@link |
| 23 | + * DeclarativeConfigProperties}. |
| 24 | + * |
| 25 | + * <p>Only properties starting with "otel.instrumentation." are resolved. Others return null (or |
| 26 | + * default value if provided). |
| 27 | + * |
| 28 | + * <p>To resolve: |
| 29 | + * |
| 30 | + * <ul> |
| 31 | + * <li>"otel.instrumentation" refers to the ".instrumentation.java" node |
| 32 | + * <li>The portion of the property after "otel.instrumentation." is split into segments based on |
| 33 | + * ".". |
| 34 | + * <li>For each N-1 segment, we walk down the tree to find the relevant leaf {@link |
| 35 | + * DeclarativeConfigProperties}. |
| 36 | + * <li>We extract the property from the resolved {@link DeclarativeConfigProperties} using the |
| 37 | + * last segment as the property key. |
| 38 | + * </ul> |
| 39 | + * |
| 40 | + * <p>For example, given the following YAML, asking for {@code |
| 41 | + * ConfigProperties#getString("otel.instrumentation.common.string_key")} yields "value": |
| 42 | + * |
| 43 | + * <pre> |
| 44 | + * instrumentation: |
| 45 | + * java: |
| 46 | + * common: |
| 47 | + * string_key: value |
| 48 | + * </pre> |
| 49 | + */ |
| 50 | +final class DeclarativeConfigPropertiesBridge implements ConfigProperties { |
| 51 | + |
| 52 | + private static final String OTEL_INSTRUMENTATION_PREFIX = "otel.instrumentation."; |
| 53 | + |
| 54 | + private final DeclarativeConfigProperties baseNode; |
| 55 | + |
| 56 | + // lookup order matters - we choose the first match |
| 57 | + private final Map<String, String> mappings; |
| 58 | + private final Map<String, Object> overrideValues; |
| 59 | + |
| 60 | + DeclarativeConfigPropertiesBridge( |
| 61 | + DeclarativeConfigProperties baseNode, |
| 62 | + Map<String, String> mappings, |
| 63 | + Map<String, Object> overrideValues) { |
| 64 | + this.baseNode = Objects.requireNonNull(baseNode); |
| 65 | + this.mappings = mappings; |
| 66 | + this.overrideValues = overrideValues; |
| 67 | + } |
| 68 | + |
| 69 | + @Nullable |
| 70 | + @Override |
| 71 | + public String getString(String propertyName) { |
| 72 | + return getPropertyValue(propertyName, String.class, DeclarativeConfigProperties::getString); |
| 73 | + } |
| 74 | + |
| 75 | + @Nullable |
| 76 | + @Override |
| 77 | + public Boolean getBoolean(String propertyName) { |
| 78 | + return getPropertyValue(propertyName, Boolean.class, DeclarativeConfigProperties::getBoolean); |
| 79 | + } |
| 80 | + |
| 81 | + @Nullable |
| 82 | + @Override |
| 83 | + public Integer getInt(String propertyName) { |
| 84 | + return getPropertyValue(propertyName, Integer.class, DeclarativeConfigProperties::getInt); |
| 85 | + } |
| 86 | + |
| 87 | + @Nullable |
| 88 | + @Override |
| 89 | + public Long getLong(String propertyName) { |
| 90 | + return getPropertyValue(propertyName, Long.class, DeclarativeConfigProperties::getLong); |
| 91 | + } |
| 92 | + |
| 93 | + @Nullable |
| 94 | + @Override |
| 95 | + public Double getDouble(String propertyName) { |
| 96 | + return getPropertyValue(propertyName, Double.class, DeclarativeConfigProperties::getDouble); |
| 97 | + } |
| 98 | + |
| 99 | + @Nullable |
| 100 | + @Override |
| 101 | + public Duration getDuration(String propertyName) { |
| 102 | + Long millis = getPropertyValue(propertyName, Long.class, DeclarativeConfigProperties::getLong); |
| 103 | + if (millis == null) { |
| 104 | + return null; |
| 105 | + } |
| 106 | + return Duration.ofMillis(millis); |
| 107 | + } |
| 108 | + |
| 109 | + @SuppressWarnings("unchecked") |
| 110 | + @Override |
| 111 | + public List<String> getList(String propertyName) { |
| 112 | + List<String> propertyValue = |
| 113 | + getPropertyValue( |
| 114 | + propertyName, |
| 115 | + List.class, |
| 116 | + (properties, lastPart) -> properties.getScalarList(lastPart, String.class)); |
| 117 | + return propertyValue == null ? Collections.emptyList() : propertyValue; |
| 118 | + } |
| 119 | + |
| 120 | + @SuppressWarnings("unchecked") |
| 121 | + @Override |
| 122 | + public Map<String, String> getMap(String propertyName) { |
| 123 | + DeclarativeConfigProperties propertyValue = |
| 124 | + getPropertyValue( |
| 125 | + propertyName, |
| 126 | + DeclarativeConfigProperties.class, |
| 127 | + DeclarativeConfigProperties::getStructured); |
| 128 | + if (propertyValue == null) { |
| 129 | + return Collections.emptyMap(); |
| 130 | + } |
| 131 | + Map<String, String> result = new HashMap<>(); |
| 132 | + propertyValue |
| 133 | + .getPropertyKeys() |
| 134 | + .forEach( |
| 135 | + key -> { |
| 136 | + String value = propertyValue.getString(key); |
| 137 | + if (value == null) { |
| 138 | + return; |
| 139 | + } |
| 140 | + result.put(key, value); |
| 141 | + }); |
| 142 | + return Collections.unmodifiableMap(result); |
| 143 | + } |
| 144 | + |
| 145 | + @Nullable |
| 146 | + private <T> T getPropertyValue( |
| 147 | + String property, |
| 148 | + Class<T> clazz, |
| 149 | + BiFunction<DeclarativeConfigProperties, String, T> extractor) { |
| 150 | + T override = clazz.cast(overrideValues.get(property)); |
| 151 | + if (override != null) { |
| 152 | + return override; |
| 153 | + } |
| 154 | + |
| 155 | + String[] segments = getSegments(translateProperty(property)); |
| 156 | + if (segments.length == 0) { |
| 157 | + return null; |
| 158 | + } |
| 159 | + |
| 160 | + // Extract the value by walking to the N-1 entry |
| 161 | + DeclarativeConfigProperties target = baseNode; |
| 162 | + if (segments.length > 1) { |
| 163 | + for (int i = 0; i < segments.length - 1; i++) { |
| 164 | + target = target.getStructured(segments[i], empty()); |
| 165 | + } |
| 166 | + } |
| 167 | + String lastPart = segments[segments.length - 1]; |
| 168 | + |
| 169 | + return extractor.apply(target, lastPart); |
| 170 | + } |
| 171 | + |
| 172 | + static String[] getSegments(String property) { |
| 173 | + if (property.startsWith(OTEL_INSTRUMENTATION_PREFIX)) { |
| 174 | + property = property.substring(OTEL_INSTRUMENTATION_PREFIX.length()); |
| 175 | + } |
| 176 | + // Split the remainder of the property on "." |
| 177 | + return property.replace('-', '_').split("\\."); |
| 178 | + } |
| 179 | + |
| 180 | + private String translateProperty(String property) { |
| 181 | + for (Map.Entry<String, String> entry : mappings.entrySet()) { |
| 182 | + if (property.startsWith(entry.getKey())) { |
| 183 | + return entry.getValue() + property.substring(entry.getKey().length()); |
| 184 | + } |
| 185 | + } |
| 186 | + return property; |
| 187 | + } |
| 188 | +} |
0 commit comments