-
Notifications
You must be signed in to change notification settings - Fork 958
Enforce IncludedExcludeModel .included and .excluded are not empty #8266
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
base: main
Are you sure you want to change the base?
Changes from all commits
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,37 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.sdk.extension.incubator.fileconfig; | ||
|
|
||
| import io.opentelemetry.api.incubator.config.DeclarativeConfigException; | ||
| import io.opentelemetry.sdk.common.internal.IncludeExcludePredicate; | ||
| import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.IncludeExcludeModel; | ||
| import java.util.List; | ||
| import java.util.function.Predicate; | ||
|
|
||
| final class IncludeExcludeFactory implements Factory<IncludeExcludeModel, Predicate<String>> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (minor/style) instead of having a dedicated factory class maybe we could have used a static factory method on
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This factory pattern is employed widely in declarative config with the general pattern being one factory per model, with each factory responsible for translating an instance of the model to an instance of a corresponding SDK component. They're all stateless, but having singleton instances is still useful because it forces consistency in the pattern. Can't move to IncludeExcludePredicate because the model class isn't there. Could have a static method in IncludeExcludePredicate which accepts |
||
|
|
||
| private static final IncludeExcludeFactory INSTANCE = new IncludeExcludeFactory(); | ||
|
|
||
| private IncludeExcludeFactory() {} | ||
|
|
||
| static IncludeExcludeFactory getInstance() { | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| @Override | ||
| public Predicate<String> create(IncludeExcludeModel model, DeclarativeConfigContext context) { | ||
| List<String> included = model.getIncluded(); | ||
| if (included != null && included.isEmpty()) { | ||
| throw new DeclarativeConfigException("included must not be empty"); | ||
| } | ||
| List<String> excluded = model.getExcluded(); | ||
| if (excluded != null && excluded.isEmpty()) { | ||
| throw new DeclarativeConfigException("excluded must not be empty"); | ||
| } | ||
|
|
||
| return IncludeExcludePredicate.createPatternMatching(included, excluded); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.sdk.extension.incubator.fileconfig; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import io.opentelemetry.api.incubator.config.DeclarativeConfigException; | ||
| import io.opentelemetry.common.ComponentLoader; | ||
| import io.opentelemetry.sdk.common.internal.IncludeExcludePredicate; | ||
| import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.IncludeExcludeModel; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.function.Predicate; | ||
| import java.util.stream.Stream; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
|
|
||
| class IncludeExcludeFactoryTest { | ||
|
|
||
| private final DeclarativeConfigContext context = | ||
| new DeclarativeConfigContext(ComponentLoader.forClassLoader(getClass().getClassLoader())); | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("createArguments") | ||
| void create(IncludeExcludeModel model, Predicate<String> expectedPredicate) { | ||
| Predicate<String> predicate = IncludeExcludeFactory.getInstance().create(model, context); | ||
| assertThat(predicate.toString()).isEqualTo(expectedPredicate.toString()); | ||
| } | ||
|
|
||
| private static Stream<Arguments> createArguments() { | ||
| return Stream.of( | ||
| // included null, excluded null | ||
| Arguments.of( | ||
| new IncludeExcludeModel().withIncluded(null).withExcluded(null), | ||
| IncludeExcludePredicate.createPatternMatching(null, null)), | ||
| // included present, excluded null | ||
| Arguments.of( | ||
| new IncludeExcludeModel().withIncluded(Arrays.asList("foo", "bar")).withExcluded(null), | ||
| IncludeExcludePredicate.createPatternMatching(Arrays.asList("foo", "bar"), null)), | ||
| // included null, excluded present | ||
| Arguments.of( | ||
| new IncludeExcludeModel() | ||
| .withIncluded(null) | ||
| .withExcluded(Collections.singletonList("baz")), | ||
| IncludeExcludePredicate.createPatternMatching(null, Collections.singletonList("baz"))), | ||
| // both included and excluded present | ||
| Arguments.of( | ||
| new IncludeExcludeModel() | ||
| .withIncluded(Arrays.asList("foo", "bar")) | ||
| .withExcluded(Collections.singletonList("baz")), | ||
| IncludeExcludePredicate.createPatternMatching( | ||
| Arrays.asList("foo", "bar"), Collections.singletonList("baz")))); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("createInvalidArguments") | ||
| void createInvalid(IncludeExcludeModel model, String expectedMessage) { | ||
| assertThatThrownBy(() -> IncludeExcludeFactory.getInstance().create(model, context)) | ||
| .isInstanceOf(DeclarativeConfigException.class) | ||
| .hasMessage(expectedMessage); | ||
| } | ||
|
|
||
| private static Stream<Arguments> createInvalidArguments() { | ||
| return Stream.of( | ||
| Arguments.of( | ||
| new IncludeExcludeModel().withIncluded(Collections.emptyList()).withExcluded(null), | ||
| "included must not be empty"), | ||
| Arguments.of( | ||
| new IncludeExcludeModel().withIncluded(null).withExcluded(Collections.emptyList()), | ||
| "excluded must not be empty")); | ||
| } | ||
| } |
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.
Since IncludeExcludeFactory is a recurring concept in the config schema, create a dedicated factory which consistently enforces the constraints