-
Notifications
You must be signed in to change notification settings - Fork 848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update to opentelemetry-configuration v0.3.0 #6733
Changes from all commits
984201f
3b14f2c
9392c98
e74f3cb
20690ef
9e765dc
ff8d60d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.extension.incubator.fileconfig; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.AttributesBuilder; | ||
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; | ||
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.AttributeNameValueModel; | ||
import java.io.Closeable; | ||
import java.util.List; | ||
import javax.annotation.Nullable; | ||
|
||
final class AttributeListFactory | ||
implements Factory<List<AttributeNameValueModel>, io.opentelemetry.api.common.Attributes> { | ||
|
||
private static final AttributeListFactory INSTANCE = new AttributeListFactory(); | ||
|
||
private AttributeListFactory() {} | ||
|
||
static AttributeListFactory getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public io.opentelemetry.api.common.Attributes create( | ||
List<AttributeNameValueModel> model, SpiHelper spiHelper, List<Closeable> closeables) { | ||
AttributesBuilder builder = io.opentelemetry.api.common.Attributes.builder(); | ||
|
||
for (AttributeNameValueModel nameValueModel : model) { | ||
addToBuilder(nameValueModel, builder); | ||
} | ||
|
||
return builder.build(); | ||
} | ||
|
||
private static void addToBuilder( | ||
AttributeNameValueModel nameValueModel, AttributesBuilder builder) { | ||
String name = FileConfigUtil.requireNonNull(nameValueModel.getName(), "attribute name"); | ||
Object value = FileConfigUtil.requireNonNull(nameValueModel.getValue(), "attribute value"); | ||
AttributeNameValueModel.Type type = nameValueModel.getType(); | ||
if (type == null) { | ||
type = AttributeNameValueModel.Type.STRING; | ||
} | ||
switch (type) { | ||
case STRING: | ||
if (value instanceof String) { | ||
builder.put(name, (String) value); | ||
return; | ||
} | ||
break; | ||
case BOOL: | ||
if (value instanceof Boolean) { | ||
builder.put(name, (boolean) value); | ||
return; | ||
} | ||
break; | ||
case INT: | ||
if ((value instanceof Integer) || (value instanceof Long)) { | ||
builder.put(name, ((Number) value).longValue()); | ||
return; | ||
} | ||
break; | ||
case DOUBLE: | ||
if (value instanceof Number) { | ||
builder.put(name, ((Number) value).doubleValue()); | ||
return; | ||
} | ||
break; | ||
case STRING_ARRAY: | ||
List<String> stringList = checkListOfType(value, String.class); | ||
if (stringList != null) { | ||
builder.put(AttributeKey.stringArrayKey(name), stringList); | ||
return; | ||
} | ||
break; | ||
case BOOL_ARRAY: | ||
List<Boolean> boolList = checkListOfType(value, Boolean.class); | ||
if (boolList != null) { | ||
builder.put(AttributeKey.booleanArrayKey(name), boolList); | ||
return; | ||
} | ||
break; | ||
case INT_ARRAY: | ||
List<Long> longList = checkListOfType(value, Long.class); | ||
if (longList != null) { | ||
builder.put(AttributeKey.longArrayKey(name), longList); | ||
return; | ||
} | ||
List<Integer> intList = checkListOfType(value, Integer.class); | ||
if (intList != null) { | ||
builder.put( | ||
AttributeKey.longArrayKey(name), | ||
intList.stream().map(i -> (long) i).collect(toList())); | ||
return; | ||
} | ||
break; | ||
case DOUBLE_ARRAY: | ||
List<Double> doubleList = checkListOfType(value, Double.class); | ||
if (doubleList != null) { | ||
builder.put(AttributeKey.doubleArrayKey(name), doubleList); | ||
return; | ||
} | ||
List<Float> floatList = checkListOfType(value, Float.class); | ||
if (floatList != null) { | ||
builder.put( | ||
AttributeKey.doubleArrayKey(name), | ||
floatList.stream().map(i -> (double) i).collect(toList())); | ||
return; | ||
} | ||
break; | ||
} | ||
throw new ConfigurationException( | ||
"Error processing attribute with name \"" | ||
+ name | ||
+ "\": value did not match type " | ||
+ type.name()); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Nullable | ||
private static <T> List<T> checkListOfType(Object value, Class<T> expectedType) { | ||
if (!(value instanceof List)) { | ||
return null; | ||
Check warning on line 129 in sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/fileconfig/AttributeListFactory.java Codecov / codecov/patchsdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/fileconfig/AttributeListFactory.java#L129
|
||
} | ||
List<?> list = (List<?>) value; | ||
if (list.isEmpty()) { | ||
return null; | ||
Check warning on line 133 in sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/fileconfig/AttributeListFactory.java Codecov / codecov/patchsdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/fileconfig/AttributeListFactory.java#L133
|
||
} | ||
if (!list.stream().allMatch(entry -> expectedType.isAssignableFrom(entry.getClass()))) { | ||
return null; | ||
} | ||
return (List<T>) value; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very strict indeed.