|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.extension; |
| 7 | + |
| 8 | +import static io.opentelemetry.sdk.autoconfigure.spi.internal.StructuredConfigProperties.empty; |
| 9 | + |
| 10 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
| 11 | +import io.opentelemetry.sdk.autoconfigure.spi.internal.StructuredConfigProperties; |
| 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.function.BiFunction; |
| 18 | +import javax.annotation.Nullable; |
| 19 | + |
| 20 | +/** |
| 21 | + * A {@link ConfigProperties} which resolves properties based on {@link StructuredConfigProperties}. |
| 22 | + * |
| 23 | + * <p>Only properties starting with "otel.instrumentation." are resolved. Others return null (or |
| 24 | + * default value if provided). |
| 25 | + * |
| 26 | + * <p>To resolve: |
| 27 | + * |
| 28 | + * <ul> |
| 29 | + * <li>"otel.instrumentation" refers to the ".instrumentation.java" node |
| 30 | + * <li>The portion of the property after "otel.instrumentation." is split into segments based on |
| 31 | + * ".". |
| 32 | + * <li>For each N-1 segment, we walk down the tree to find the relevant leaf {@link |
| 33 | + * StructuredConfigProperties}. |
| 34 | + * <li>We extract the property from the resolved {@link StructuredConfigProperties} using the last |
| 35 | + * segment as the property key. |
| 36 | + * </ul> |
| 37 | + * |
| 38 | + * <p>For example, given the following YAML, asking for {@code |
| 39 | + * ConfigProperties#getString("otel.instrumentation.common.string_key")} yields "value": |
| 40 | + * |
| 41 | + * <pre> |
| 42 | + * instrumentation: |
| 43 | + * java: |
| 44 | + * common: |
| 45 | + * string_key: value |
| 46 | + * </pre> |
| 47 | + */ |
| 48 | +final class StructuredConfigPropertiesBridge implements ConfigProperties { |
| 49 | + |
| 50 | + private static final String OTEL_INSTRUMENTATION_PREFIX = "otel.instrumentation."; |
| 51 | + |
| 52 | + // The node at .instrumentation.java |
| 53 | + private final StructuredConfigProperties instrumentationJavaNode; |
| 54 | + |
| 55 | + StructuredConfigPropertiesBridge(StructuredConfigProperties rootStructuredConfigProperties) { |
| 56 | + instrumentationJavaNode = |
| 57 | + rootStructuredConfigProperties |
| 58 | + .getStructured("instrumentation", empty()) |
| 59 | + .getStructured("java", empty()); |
| 60 | + } |
| 61 | + |
| 62 | + @Nullable |
| 63 | + @Override |
| 64 | + public String getString(String propertyName) { |
| 65 | + return getPropertyValue(propertyName, StructuredConfigProperties::getString); |
| 66 | + } |
| 67 | + |
| 68 | + @Nullable |
| 69 | + @Override |
| 70 | + public Boolean getBoolean(String propertyName) { |
| 71 | + return getPropertyValue(propertyName, StructuredConfigProperties::getBoolean); |
| 72 | + } |
| 73 | + |
| 74 | + @Nullable |
| 75 | + @Override |
| 76 | + public Integer getInt(String propertyName) { |
| 77 | + return getPropertyValue(propertyName, StructuredConfigProperties::getInt); |
| 78 | + } |
| 79 | + |
| 80 | + @Nullable |
| 81 | + @Override |
| 82 | + public Long getLong(String propertyName) { |
| 83 | + return getPropertyValue(propertyName, StructuredConfigProperties::getLong); |
| 84 | + } |
| 85 | + |
| 86 | + @Nullable |
| 87 | + @Override |
| 88 | + public Double getDouble(String propertyName) { |
| 89 | + return getPropertyValue(propertyName, StructuredConfigProperties::getDouble); |
| 90 | + } |
| 91 | + |
| 92 | + @Nullable |
| 93 | + @Override |
| 94 | + public Duration getDuration(String propertyName) { |
| 95 | + Long millis = getPropertyValue(propertyName, StructuredConfigProperties::getLong); |
| 96 | + if (millis == null) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + return Duration.ofMillis(millis); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public List<String> getList(String propertyName) { |
| 104 | + List<String> propertyValue = |
| 105 | + getPropertyValue( |
| 106 | + propertyName, |
| 107 | + (properties, lastPart) -> properties.getScalarList(lastPart, String.class)); |
| 108 | + return propertyValue == null ? Collections.emptyList() : propertyValue; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public Map<String, String> getMap(String propertyName) { |
| 113 | + StructuredConfigProperties propertyValue = |
| 114 | + getPropertyValue(propertyName, StructuredConfigProperties::getStructured); |
| 115 | + if (propertyValue == null) { |
| 116 | + return Collections.emptyMap(); |
| 117 | + } |
| 118 | + Map<String, String> result = new HashMap<>(); |
| 119 | + propertyValue |
| 120 | + .getPropertyKeys() |
| 121 | + .forEach( |
| 122 | + key -> { |
| 123 | + String value = propertyValue.getString(key); |
| 124 | + if (value == null) { |
| 125 | + return; |
| 126 | + } |
| 127 | + result.put(key, value); |
| 128 | + }); |
| 129 | + return Collections.unmodifiableMap(result); |
| 130 | + } |
| 131 | + |
| 132 | + @Nullable |
| 133 | + private <T> T getPropertyValue( |
| 134 | + String property, BiFunction<StructuredConfigProperties, String, T> extractor) { |
| 135 | + if (!property.startsWith(OTEL_INSTRUMENTATION_PREFIX)) { |
| 136 | + return null; |
| 137 | + } |
| 138 | + String suffix = property.substring(OTEL_INSTRUMENTATION_PREFIX.length()); |
| 139 | + // Split the remainder of the property on ".", and walk to the N-1 entry |
| 140 | + String[] segments = suffix.split("\\."); |
| 141 | + if (segments.length == 0) { |
| 142 | + return null; |
| 143 | + } |
| 144 | + StructuredConfigProperties target = instrumentationJavaNode; |
| 145 | + if (segments.length > 1) { |
| 146 | + for (int i = 0; i < segments.length - 1; i++) { |
| 147 | + target = target.getStructured(segments[i], empty()); |
| 148 | + } |
| 149 | + } |
| 150 | + String lastPart = segments[segments.length - 1]; |
| 151 | + return extractor.apply(target, lastPart); |
| 152 | + } |
| 153 | +} |
0 commit comments