Skip to content

Commit 42056f5

Browse files
authored
Introduce ConfigProvider API (#6549)
1 parent b3e3fff commit 42056f5

File tree

95 files changed

+1542
-746
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+1542
-746
lines changed

api/incubator/build.gradle.kts

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ dependencies {
1414

1515
annotationProcessor("com.google.auto.value:auto-value")
1616

17+
// To use parsed config file as input for InstrumentationConfigUtilTest
18+
testImplementation(project(":sdk-extensions:incubator"))
19+
1720
testImplementation(project(":sdk:testing"))
1821
testImplementation(project(":api:testing-internal"))
1922

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.api.incubator.config;
7+
8+
import javax.annotation.Nullable;
9+
import javax.annotation.concurrent.ThreadSafe;
10+
11+
/**
12+
* A registry for accessing declarative configuration.
13+
*
14+
* <p>The name <i>Provider</i> is for consistency with other languages and it is <b>NOT</b> loaded
15+
* using reflection.
16+
*
17+
* <p>See {@link InstrumentationConfigUtil} for convenience methods for extracting config from
18+
* {@link ConfigProvider}.
19+
*/
20+
@ThreadSafe
21+
public interface ConfigProvider {
22+
23+
/**
24+
* Returns the {@link DeclarativeConfigProperties} corresponding to <a
25+
* href="https://github.com/open-telemetry/opentelemetry-configuration/blob/main/schema/instrumentation.json">instrumentation
26+
* config</a>, or {@code null} if unavailable.
27+
*
28+
* @return the instrumentation {@link DeclarativeConfigProperties}
29+
*/
30+
@Nullable
31+
DeclarativeConfigProperties getInstrumentationConfig();
32+
33+
/** Returns a no-op {@link ConfigProvider}. */
34+
static ConfigProvider noop() {
35+
return () -> null;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.api.incubator.config;
7+
8+
/** An exception that is thrown when errors occur with declarative configuration. */
9+
public final class DeclarativeConfigException extends RuntimeException {
10+
11+
private static final long serialVersionUID = 3036584181551130522L;
12+
13+
/** Create a new configuration exception with specified {@code message} and without a cause. */
14+
public DeclarativeConfigException(String message) {
15+
super(message);
16+
}
17+
18+
/** Create a new configuration exception with specified {@code message} and {@code cause}. */
19+
public DeclarativeConfigException(String message, Throwable cause) {
20+
super(message, cause);
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.opentelemetry.sdk.autoconfigure.spi.internal;
6+
package io.opentelemetry.api.incubator.config;
77

88
import static io.opentelemetry.api.internal.ConfigUtil.defaultIfNull;
99

10-
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
11-
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
1210
import java.util.List;
1311
import java.util.Set;
1412
import javax.annotation.Nullable;
1513

1614
/**
17-
* An interface for accessing structured configuration data.
15+
* An interface for accessing declarative configuration data.
1816
*
19-
* <p>An instance of {@link StructuredConfigProperties} is equivalent to a <a
17+
* <p>An instance of {@link DeclarativeConfigProperties} is equivalent to a <a
2018
* href="https://yaml.org/spec/1.2.2/#3211-nodes">YAML mapping node</a>. It has accessors for
2119
* reading scalar properties, {@link #getStructured(String)} for reading children which are
2220
* themselves mappings, and {@link #getStructuredList(String)} for reading children which are
@@ -25,24 +23,24 @@
2523
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
2624
* at any time.
2725
*/
28-
public interface StructuredConfigProperties {
26+
public interface DeclarativeConfigProperties {
2927

3028
/**
31-
* Return an empty {@link StructuredConfigProperties} instance.
29+
* Return an empty {@link DeclarativeConfigProperties} instance.
3230
*
3331
* <p>Useful for walking the tree without checking for null. For example, to access a string key
3432
* nested at .foo.bar.baz, call: {@code config.getStructured("foo", empty()).getStructured("bar",
3533
* empty()).getString("baz")}.
3634
*/
37-
static StructuredConfigProperties empty() {
38-
return EmptyStructuredConfigProperties.getInstance();
35+
static DeclarativeConfigProperties empty() {
36+
return EmptyDeclarativeConfigProperties.getInstance();
3937
}
4038

4139
/**
4240
* Returns a {@link String} configuration property.
4341
*
4442
* @return null if the property has not been configured
45-
* @throws ConfigurationException if the property is not a valid scalar string
43+
* @throws DeclarativeConfigException if the property is not a valid scalar string
4644
*/
4745
@Nullable
4846
String getString(String name);
@@ -52,7 +50,7 @@ static StructuredConfigProperties empty() {
5250
*
5351
* @return a {@link String} configuration property or {@code defaultValue} if a property with
5452
* {@code name} has not been configured
55-
* @throws ConfigurationException if the property is not a valid scalar string
53+
* @throws DeclarativeConfigException if the property is not a valid scalar string
5654
*/
5755
default String getString(String name, String defaultValue) {
5856
return defaultIfNull(getString(name), defaultValue);
@@ -63,7 +61,7 @@ default String getString(String name, String defaultValue) {
6361
* {@link Boolean#parseBoolean(String)} for handling the values.
6462
*
6563
* @return null if the property has not been configured
66-
* @throws ConfigurationException if the property is not a valid scalar boolean
64+
* @throws DeclarativeConfigException if the property is not a valid scalar boolean
6765
*/
6866
@Nullable
6967
Boolean getBoolean(String name);
@@ -73,7 +71,7 @@ default String getString(String name, String defaultValue) {
7371
*
7472
* @return a {@link Boolean} configuration property or {@code defaultValue} if a property with
7573
* {@code name} has not been configured
76-
* @throws ConfigurationException if the property is not a valid scalar boolean
74+
* @throws DeclarativeConfigException if the property is not a valid scalar boolean
7775
*/
7876
default boolean getBoolean(String name, boolean defaultValue) {
7977
return defaultIfNull(getBoolean(name), defaultValue);
@@ -86,7 +84,7 @@ default boolean getBoolean(String name, boolean defaultValue) {
8684
* {@link Long#intValue()} which may result in loss of precision.
8785
*
8886
* @return null if the property has not been configured
89-
* @throws ConfigurationException if the property is not a valid scalar integer
87+
* @throws DeclarativeConfigException if the property is not a valid scalar integer
9088
*/
9189
@Nullable
9290
Integer getInt(String name);
@@ -99,7 +97,7 @@ default boolean getBoolean(String name, boolean defaultValue) {
9997
*
10098
* @return a {@link Integer} configuration property or {@code defaultValue} if a property with
10199
* {@code name} has not been configured
102-
* @throws ConfigurationException if the property is not a valid scalar integer
100+
* @throws DeclarativeConfigException if the property is not a valid scalar integer
103101
*/
104102
default int getInt(String name, int defaultValue) {
105103
return defaultIfNull(getInt(name), defaultValue);
@@ -109,7 +107,7 @@ default int getInt(String name, int defaultValue) {
109107
* Returns a {@link Long} configuration property.
110108
*
111109
* @return null if the property has not been configured
112-
* @throws ConfigurationException if the property is not a valid scalar long
110+
* @throws DeclarativeConfigException if the property is not a valid scalar long
113111
*/
114112
@Nullable
115113
Long getLong(String name);
@@ -119,7 +117,7 @@ default int getInt(String name, int defaultValue) {
119117
*
120118
* @return a {@link Long} configuration property or {@code defaultValue} if a property with {@code
121119
* name} has not been configured
122-
* @throws ConfigurationException if the property is not a valid scalar long
120+
* @throws DeclarativeConfigException if the property is not a valid scalar long
123121
*/
124122
default long getLong(String name, long defaultValue) {
125123
return defaultIfNull(getLong(name), defaultValue);
@@ -129,7 +127,7 @@ default long getLong(String name, long defaultValue) {
129127
* Returns a {@link Double} configuration property.
130128
*
131129
* @return null if the property has not been configured
132-
* @throws ConfigurationException if the property is not a valid scalar double
130+
* @throws DeclarativeConfigException if the property is not a valid scalar double
133131
*/
134132
@Nullable
135133
Double getDouble(String name);
@@ -139,7 +137,7 @@ default long getLong(String name, long defaultValue) {
139137
*
140138
* @return a {@link Double} configuration property or {@code defaultValue} if a property with
141139
* {@code name} has not been configured
142-
* @throws ConfigurationException if the property is not a valid scalar double
140+
* @throws DeclarativeConfigException if the property is not a valid scalar double
143141
*/
144142
default double getDouble(String name, double defaultValue) {
145143
return defaultIfNull(getDouble(name), defaultValue);
@@ -153,8 +151,8 @@ default double getDouble(String name, double defaultValue) {
153151
* @param scalarType the scalar type, one of {@link String}, {@link Boolean}, {@link Long} or
154152
* {@link Double}
155153
* @return a {@link List} configuration property, or null if the property has not been configured
156-
* @throws ConfigurationException if the property is not a valid sequence of scalars, or if {@code
157-
* scalarType} is not supported
154+
* @throws DeclarativeConfigException if the property is not a valid sequence of scalars, or if
155+
* {@code scalarType} is not supported
158156
*/
159157
@Nullable
160158
<T> List<T> getScalarList(String name, Class<T> scalarType);
@@ -163,56 +161,58 @@ default double getDouble(String name, double defaultValue) {
163161
* Returns a {@link List} configuration property. Entries which are not strings are converted to
164162
* their string representation.
165163
*
166-
* @see ConfigProperties#getList(String name)
164+
* @param name the property name
165+
* @param scalarType the scalar type, one of {@link String}, {@link Boolean}, {@link Long} or
166+
* {@link Double}
167167
* @return a {@link List} configuration property or {@code defaultValue} if a property with {@code
168168
* name} has not been configured
169-
* @throws ConfigurationException if the property is not a valid sequence of scalars
169+
* @throws DeclarativeConfigException if the property is not a valid sequence of scalars
170170
*/
171171
default <T> List<T> getScalarList(String name, Class<T> scalarType, List<T> defaultValue) {
172172
return defaultIfNull(getScalarList(name, scalarType), defaultValue);
173173
}
174174

175175
/**
176-
* Returns a {@link StructuredConfigProperties} configuration property.
176+
* Returns a {@link DeclarativeConfigProperties} configuration property.
177177
*
178178
* @return a map-valued configuration property, or {@code null} if {@code name} has not been
179179
* configured
180-
* @throws ConfigurationException if the property is not a mapping
180+
* @throws DeclarativeConfigException if the property is not a mapping
181181
*/
182182
@Nullable
183-
StructuredConfigProperties getStructured(String name);
183+
DeclarativeConfigProperties getStructured(String name);
184184

185185
/**
186-
* Returns a {@link StructuredConfigProperties} configuration property.
186+
* Returns a list of {@link DeclarativeConfigProperties} configuration property.
187187
*
188188
* @return a map-valued configuration property, or {@code defaultValue} if {@code name} has not
189189
* been configured
190-
* @throws ConfigurationException if the property is not a mapping
190+
* @throws DeclarativeConfigException if the property is not a mapping
191191
*/
192-
default StructuredConfigProperties getStructured(
193-
String name, StructuredConfigProperties defaultValue) {
192+
default DeclarativeConfigProperties getStructured(
193+
String name, DeclarativeConfigProperties defaultValue) {
194194
return defaultIfNull(getStructured(name), defaultValue);
195195
}
196196

197197
/**
198-
* Returns a list of {@link StructuredConfigProperties} configuration property.
198+
* Returns a list of {@link DeclarativeConfigProperties} configuration property.
199199
*
200200
* @return a list of map-valued configuration property, or {@code null} if {@code name} has not
201201
* been configured
202-
* @throws ConfigurationException if the property is not a sequence of mappings
202+
* @throws DeclarativeConfigException if the property is not a sequence of mappings
203203
*/
204204
@Nullable
205-
List<StructuredConfigProperties> getStructuredList(String name);
205+
List<DeclarativeConfigProperties> getStructuredList(String name);
206206

207207
/**
208-
* Returns a list of {@link StructuredConfigProperties} configuration property.
208+
* Returns a list of {@link DeclarativeConfigProperties} configuration property.
209209
*
210210
* @return a list of map-valued configuration property, or {@code defaultValue} if {@code name}
211211
* has not been configured
212-
* @throws ConfigurationException if the property is not a sequence of mappings
212+
* @throws DeclarativeConfigException if the property is not a sequence of mappings
213213
*/
214-
default List<StructuredConfigProperties> getStructuredList(
215-
String name, List<StructuredConfigProperties> defaultValue) {
214+
default List<DeclarativeConfigProperties> getStructuredList(
215+
String name, List<DeclarativeConfigProperties> defaultValue) {
216216
return defaultIfNull(getStructuredList(name), defaultValue);
217217
}
218218

Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.opentelemetry.sdk.autoconfigure.spi.internal;
6+
package io.opentelemetry.api.incubator.config;
77

88
import java.util.Collections;
99
import java.util.List;
1010
import java.util.Set;
1111
import javax.annotation.Nullable;
1212

13-
/** Empty instance of {@link StructuredConfigProperties}. */
14-
final class EmptyStructuredConfigProperties implements StructuredConfigProperties {
13+
/** Empty instance of {@link DeclarativeConfigProperties}. */
14+
final class EmptyDeclarativeConfigProperties implements DeclarativeConfigProperties {
1515

16-
private static final EmptyStructuredConfigProperties INSTANCE =
17-
new EmptyStructuredConfigProperties();
16+
private static final EmptyDeclarativeConfigProperties INSTANCE =
17+
new EmptyDeclarativeConfigProperties();
1818

19-
private EmptyStructuredConfigProperties() {}
19+
private EmptyDeclarativeConfigProperties() {}
2020

21-
static EmptyStructuredConfigProperties getInstance() {
21+
static EmptyDeclarativeConfigProperties getInstance() {
2222
return INSTANCE;
2323
}
2424

@@ -60,13 +60,13 @@ public <T> List<T> getScalarList(String name, Class<T> scalarType) {
6060

6161
@Nullable
6262
@Override
63-
public StructuredConfigProperties getStructured(String name) {
63+
public DeclarativeConfigProperties getStructured(String name) {
6464
return null;
6565
}
6666

6767
@Nullable
6868
@Override
69-
public List<StructuredConfigProperties> getStructuredList(String name) {
69+
public List<DeclarativeConfigProperties> getStructuredList(String name) {
7070
return null;
7171
}
7272

0 commit comments

Comments
 (0)