Skip to content

Commit 3e46fe5

Browse files
committed
revert + alternative approach
1 parent fe6a321 commit 3e46fe5

5 files changed

Lines changed: 41 additions & 20 deletions

File tree

sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/fileconfig/ComposableRuleBasedSamplerFactory.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,15 @@ private static AttributeMatcher attributePatternsMatcher(
9898
if (attributePatternsModel == null) {
9999
return null;
100100
}
101+
List<String> included = attributePatternsModel.getIncluded();
102+
if (included != null && included.isEmpty()) {
103+
// empty included should be treated as include everything by default.
104+
included = null;
105+
}
101106
return new AttributeMatcher(
102107
requireNonNull(attributePatternsModel.getKey(), "attribute_patterns key"),
103108
IncludeExcludePredicate.createPatternMatching(
104-
attributePatternsModel.getIncluded(), attributePatternsModel.getExcluded()));
109+
included, attributePatternsModel.getExcluded()));
105110
}
106111

107112
// Visible for testing

sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/fileconfig/ResourceFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ private static Predicate<String> detectorAttributeFilter(
9393
if (included == null && excluded == null) {
9494
return ResourceFactory::matchAll;
9595
}
96+
// when "included" is omitted in configuration, we get an empty list
97+
// in this context it should be interpreted as "include everything"
98+
if (included != null && included.isEmpty()) {
99+
included = null;
100+
}
96101
return IncludeExcludePredicate.createPatternMatching(included, excluded);
97102
}
98103
}

sdk-extensions/incubator/src/test/java/io/opentelemetry/sdk/extension/incubator/fileconfig/ResourceFactoryTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,12 @@ private static Stream<Arguments> createWithDetectorsArgs() {
141141
Arguments.of(
142142
Collections.singletonList("*o*"),
143143
Collections.singletonList("order"),
144-
Resource.getDefault().toBuilder().put("color", "red").build()));
144+
Resource.getDefault().toBuilder().put("color", "red").build()),
145+
// empty include should be treated as include all
146+
Arguments.of(
147+
Collections.emptyList(),
148+
Collections.singletonList("order"),
149+
Resource.getDefault().toBuilder().put("color", "red").put("shape", "square").build()));
145150
}
146151

147152
@ParameterizedTest

sdk/common/src/main/java/io/opentelemetry/sdk/common/internal/IncludeExcludePredicate.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import static java.util.stream.Collectors.joining;
1010

1111
import java.util.Collection;
12-
import java.util.Collections;
1312
import java.util.LinkedHashSet;
1413
import java.util.Set;
1514
import java.util.StringJoiner;
@@ -30,29 +29,28 @@
3029
public final class IncludeExcludePredicate implements Predicate<String> {
3130

3231
private final boolean globMatchingEnabled;
33-
private final Set<String> included;
34-
private final Set<String> excluded;
32+
@Nullable private final Set<String> included;
33+
@Nullable private final Set<String> excluded;
3534
private final Predicate<String> predicate;
3635

3736
private IncludeExcludePredicate(
3837
@Nullable Collection<String> included,
3938
@Nullable Collection<String> excluded,
4039
boolean globMatchingEnabled) {
4140
this.globMatchingEnabled = globMatchingEnabled;
42-
this.included = included == null ? Collections.emptySet() : new LinkedHashSet<>(included);
43-
this.excluded = excluded == null ? Collections.emptySet() : new LinkedHashSet<>(excluded);
44-
if (this.included.isEmpty() && this.excluded.isEmpty()) {
45-
throw new IllegalArgumentException(
46-
"At least one of include or exclude patterns must not be null or empty");
47-
}
48-
if (this.included.isEmpty()) {
49-
this.predicate = excludedPredicate(this.excluded, globMatchingEnabled);
50-
} else if (this.excluded.isEmpty()) {
51-
this.predicate = includedPredicate(this.included, globMatchingEnabled);
52-
} else {
41+
this.included = included == null ? null : new LinkedHashSet<>(included);
42+
this.excluded = excluded == null ? null : new LinkedHashSet<>(excluded);
43+
if (this.included != null && this.excluded != null) {
5344
this.predicate =
5445
includedPredicate(this.included, globMatchingEnabled)
5546
.and(excludedPredicate(this.excluded, globMatchingEnabled));
47+
} else if (this.included == null && this.excluded != null) {
48+
this.predicate = excludedPredicate(this.excluded, globMatchingEnabled);
49+
} else if (this.excluded == null && this.included != null) {
50+
this.predicate = includedPredicate(this.included, globMatchingEnabled);
51+
} else {
52+
throw new IllegalArgumentException(
53+
"At least one of includedPatterns or excludedPatterns must not be null");
5654
}
5755
}
5856

@@ -87,10 +85,10 @@ public boolean test(String s) {
8785
public String toString() {
8886
StringJoiner joiner = new StringJoiner(", ", "IncludeExcludePredicate{", "}");
8987
joiner.add("globMatchingEnabled=" + globMatchingEnabled);
90-
if (!included.isEmpty()) {
88+
if (included != null) {
9189
joiner.add("included=" + included.stream().collect(joining(", ", "[", "]")));
9290
}
93-
if (!excluded.isEmpty()) {
91+
if (excluded != null) {
9492
joiner.add("excluded=" + excluded.stream().collect(joining(", ", "[", "]")));
9593
}
9694
return joiner.toString();

sdk/common/src/test/java/io/opentelemetry/sdk/common/internal/IncludeExcludePredicateTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ class IncludeExcludePredicateTest {
2121
private static final Predicate<String> EXACT_INCLUDE =
2222
IncludeExcludePredicate.createExactMatching(singletonList("foo"), null);
2323
private static final Predicate<String> EXACT_EXCLUDE =
24-
IncludeExcludePredicate.createExactMatching(Collections.emptyList(), singletonList("bar"));
24+
IncludeExcludePredicate.createExactMatching(null, singletonList("bar"));
2525
private static final Predicate<String> EXACT_INCLUDE_AND_EXCLUDE =
2626
IncludeExcludePredicate.createExactMatching(singletonList("foo"), singletonList("bar"));
2727
private static final Predicate<String> EXACT_MULTI =
2828
IncludeExcludePredicate.createExactMatching(asList("foo", "fooo"), asList("bar", "barr"));
29+
public static final Predicate<String> EXACT_INCLUDE_NONE =
30+
IncludeExcludePredicate.createExactMatching(Collections.emptyList(), null);
2931

3032
private static final Predicate<String> PATTERN_INCLUDE =
3133
IncludeExcludePredicate.createPatternMatching(singletonList("f?o"), null);
@@ -35,6 +37,8 @@ class IncludeExcludePredicateTest {
3537
IncludeExcludePredicate.createPatternMatching(singletonList("f?o"), singletonList("b?r"));
3638
private static final Predicate<String> PATTERN_MULTI =
3739
IncludeExcludePredicate.createPatternMatching(asList("f?o", "f?oo"), asList("b?r", "b?rr"));
40+
public static final Predicate<String> PATTERN_INCLUDE_NONE =
41+
IncludeExcludePredicate.createPatternMatching(Collections.emptyList(), null);
3842

3943
@ParameterizedTest
4044
@MethodSource("testArgs")
@@ -71,6 +75,8 @@ private static Stream<Arguments> testArgs() {
7175
Arguments.of(EXACT_MULTI, "bar", false),
7276
Arguments.of(EXACT_MULTI, "barr", false),
7377
Arguments.of(EXACT_MULTI, "baz", false),
78+
// include none
79+
Arguments.of(EXACT_INCLUDE_NONE, "foo", false),
7480
// pattern matching
7581
// include only
7682
Arguments.of(PATTERN_INCLUDE, "foo", true),
@@ -100,7 +106,9 @@ private static Stream<Arguments> testArgs() {
100106
Arguments.of(PATTERN_MULTI, "bAr", false),
101107
Arguments.of(PATTERN_MULTI, "barr", false),
102108
Arguments.of(PATTERN_MULTI, "bArr", false),
103-
Arguments.of(PATTERN_MULTI, "baz", false));
109+
Arguments.of(PATTERN_MULTI, "baz", false),
110+
// include none
111+
Arguments.of(PATTERN_INCLUDE_NONE, "foo", false));
104112
}
105113

106114
@ParameterizedTest

0 commit comments

Comments
 (0)