|
49 | 49 | */ |
50 | 50 | final class DeclarativeConfigPropertiesBridge implements ConfigProperties { |
51 | 51 |
|
52 | | - private static final String OTEL_INSTRUMENTATION_PREFIX = "otel.instrumentation."; |
53 | | - |
54 | | - // The node at .instrumentation.java |
55 | | - private final DeclarativeConfigProperties instrumentationJavaNode; |
56 | | - |
57 | | - DeclarativeConfigPropertiesBridge(DeclarativeConfigProperties instrumentationNode) { |
58 | | - instrumentationJavaNode = instrumentationNode.getStructured("java", empty()); |
59 | | - } |
60 | | - |
61 | | - @Nullable |
62 | | - @Override |
63 | | - public String getString(String propertyName) { |
64 | | - return getPropertyValue(propertyName, DeclarativeConfigProperties::getString); |
65 | | - } |
66 | | - |
67 | | - @Nullable |
68 | | - @Override |
69 | | - public Boolean getBoolean(String propertyName) { |
70 | | - return getPropertyValue(propertyName, DeclarativeConfigProperties::getBoolean); |
71 | | - } |
72 | | - |
73 | | - @Nullable |
74 | | - @Override |
75 | | - public Integer getInt(String propertyName) { |
76 | | - return getPropertyValue(propertyName, DeclarativeConfigProperties::getInt); |
77 | | - } |
78 | | - |
79 | | - @Nullable |
80 | | - @Override |
81 | | - public Long getLong(String propertyName) { |
82 | | - return getPropertyValue(propertyName, DeclarativeConfigProperties::getLong); |
83 | | - } |
84 | | - |
85 | | - @Nullable |
86 | | - @Override |
87 | | - public Double getDouble(String propertyName) { |
88 | | - return getPropertyValue(propertyName, DeclarativeConfigProperties::getDouble); |
89 | | - } |
90 | | - |
91 | | - @Nullable |
92 | | - @Override |
93 | | - public Duration getDuration(String propertyName) { |
94 | | - Long millis = getPropertyValue(propertyName, DeclarativeConfigProperties::getLong); |
95 | | - if (millis == null) { |
96 | | - return null; |
| 52 | + private static final String OTEL_INSTRUMENTATION_PREFIX = "otel.instrumentation."; |
| 53 | + |
| 54 | + // The node at .instrumentation.java |
| 55 | + private final DeclarativeConfigProperties instrumentationJavaNode; |
| 56 | + |
| 57 | + DeclarativeConfigPropertiesBridge(DeclarativeConfigProperties instrumentationNode) { |
| 58 | + instrumentationJavaNode = instrumentationNode.getStructured("java", empty()); |
97 | 59 | } |
98 | | - return Duration.ofMillis(millis); |
99 | | - } |
100 | | - |
101 | | - @Override |
102 | | - public List<String> getList(String propertyName) { |
103 | | - List<String> propertyValue = |
104 | | - getPropertyValue( |
105 | | - propertyName, |
106 | | - (properties, lastPart) -> properties.getScalarList(lastPart, String.class)); |
107 | | - return propertyValue == null ? Collections.emptyList() : propertyValue; |
108 | | - } |
109 | | - |
110 | | - @Override |
111 | | - public Map<String, String> getMap(String propertyName) { |
112 | | - DeclarativeConfigProperties propertyValue = |
113 | | - getPropertyValue(propertyName, DeclarativeConfigProperties::getStructured); |
114 | | - if (propertyValue == null) { |
115 | | - return Collections.emptyMap(); |
| 60 | + |
| 61 | + @Nullable |
| 62 | + @Override |
| 63 | + public String getString(String propertyName) { |
| 64 | + return getPropertyValue(propertyName, DeclarativeConfigProperties::getString); |
116 | 65 | } |
117 | | - Map<String, String> result = new HashMap<>(); |
118 | | - propertyValue |
119 | | - .getPropertyKeys() |
120 | | - .forEach( |
121 | | - key -> { |
122 | | - String value = propertyValue.getString(key); |
123 | | - if (value == null) { |
124 | | - return; |
125 | | - } |
126 | | - result.put(key, value); |
127 | | - }); |
128 | | - return Collections.unmodifiableMap(result); |
129 | | - } |
130 | | - |
131 | | - @Nullable |
132 | | - private <T> T getPropertyValue( |
133 | | - String property, BiFunction<DeclarativeConfigProperties, String, T> extractor) { |
134 | | - if (instrumentationJavaNode == null) { |
135 | | - return null; |
| 66 | + |
| 67 | + @Nullable |
| 68 | + @Override |
| 69 | + public Boolean getBoolean(String propertyName) { |
| 70 | + return getPropertyValue(propertyName, DeclarativeConfigProperties::getBoolean); |
136 | 71 | } |
137 | 72 |
|
138 | | - if (property.startsWith(OTEL_INSTRUMENTATION_PREFIX)) { |
139 | | - property = property.substring(OTEL_INSTRUMENTATION_PREFIX.length()); |
| 73 | + @Nullable |
| 74 | + @Override |
| 75 | + public Integer getInt(String propertyName) { |
| 76 | + return getPropertyValue(propertyName, DeclarativeConfigProperties::getInt); |
140 | 77 | } |
141 | | - // Split the remainder of the property on "." |
142 | | - String[] segments = property.split("\\."); |
143 | | - if (segments.length == 0) { |
144 | | - return null; |
| 78 | + |
| 79 | + @Nullable |
| 80 | + @Override |
| 81 | + public Long getLong(String propertyName) { |
| 82 | + return getPropertyValue(propertyName, DeclarativeConfigProperties::getLong); |
| 83 | + } |
| 84 | + |
| 85 | + @Nullable |
| 86 | + @Override |
| 87 | + public Double getDouble(String propertyName) { |
| 88 | + return getPropertyValue(propertyName, DeclarativeConfigProperties::getDouble); |
| 89 | + } |
| 90 | + |
| 91 | + @Nullable |
| 92 | + @Override |
| 93 | + public Duration getDuration(String propertyName) { |
| 94 | + Long millis = getPropertyValue(propertyName, DeclarativeConfigProperties::getLong); |
| 95 | + if (millis == null) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + return Duration.ofMillis(millis); |
145 | 99 | } |
146 | 100 |
|
147 | | - // Extract the value by walking to the N-1 entry |
148 | | - DeclarativeConfigProperties target = instrumentationJavaNode; |
149 | | - if (segments.length > 1) { |
150 | | - for (int i = 0; i < segments.length - 1; i++) { |
151 | | - target = target.getStructured(segments[i], empty()); |
152 | | - } |
| 101 | + @Override |
| 102 | + public List<String> getList(String propertyName) { |
| 103 | + List<String> propertyValue = |
| 104 | + getPropertyValue( |
| 105 | + propertyName, |
| 106 | + (properties, lastPart) -> properties.getScalarList(lastPart, String.class)); |
| 107 | + return propertyValue == null ? Collections.emptyList() : propertyValue; |
153 | 108 | } |
154 | | - String lastPart = segments[segments.length - 1]; |
155 | 109 |
|
156 | | - return extractor.apply(target, lastPart); |
157 | | - } |
| 110 | + @Override |
| 111 | + public Map<String, String> getMap(String propertyName) { |
| 112 | + DeclarativeConfigProperties propertyValue = |
| 113 | + getPropertyValue(propertyName, DeclarativeConfigProperties::getStructured); |
| 114 | + if (propertyValue == null) { |
| 115 | + return Collections.emptyMap(); |
| 116 | + } |
| 117 | + Map<String, String> result = new HashMap<>(); |
| 118 | + propertyValue |
| 119 | + .getPropertyKeys() |
| 120 | + .forEach( |
| 121 | + key -> { |
| 122 | + String value = propertyValue.getString(key); |
| 123 | + if (value == null) { |
| 124 | + return; |
| 125 | + } |
| 126 | + result.put(key, value); |
| 127 | + }); |
| 128 | + return Collections.unmodifiableMap(result); |
| 129 | + } |
| 130 | + |
| 131 | + @Nullable |
| 132 | + private <T> T getPropertyValue( |
| 133 | + String property, BiFunction<DeclarativeConfigProperties, String, T> extractor) { |
| 134 | + if (instrumentationJavaNode == null) { |
| 135 | + return null; |
| 136 | + } |
| 137 | + |
| 138 | + String[] segments = getSegments(property); |
| 139 | + if (segments.length == 0) { |
| 140 | + return null; |
| 141 | + } |
| 142 | + |
| 143 | + // Extract the value by walking to the N-1 entry |
| 144 | + DeclarativeConfigProperties 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 | + |
| 152 | + return extractor.apply(target, lastPart); |
| 153 | + } |
| 154 | + |
| 155 | + private static String[] getSegments(String property) { |
| 156 | + if (property.startsWith(OTEL_INSTRUMENTATION_PREFIX)) { |
| 157 | + property = property.substring(OTEL_INSTRUMENTATION_PREFIX.length()); |
| 158 | + } |
| 159 | + // Split the remainder of the property on "." |
| 160 | + return property.split("\\."); |
| 161 | + } |
| 162 | + |
| 163 | + static String yamlPath(String property) { |
| 164 | + String[] segments = getSegments(property); |
| 165 | + if (segments.length == 0) { |
| 166 | + throw new IllegalArgumentException("Invalid property: " + property); |
| 167 | + } |
| 168 | + |
| 169 | + return "'instrumentation/development' / 'java' / '" + String.join("' / '", segments) + "'"; |
| 170 | + } |
158 | 171 | } |
0 commit comments