Skip to content

Commit fb10f70

Browse files
committed
more
1 parent 8bb4b5b commit fb10f70

File tree

5 files changed

+36
-25
lines changed

5 files changed

+36
-25
lines changed

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/configuration/Configuration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ public static class Sampling {
165165
// fixed percentage of requests
166166
@Nullable public Double percentage;
167167

168-
// default is 5 requests per second
168+
// default is 5 requests per second (set in ConfigurationBuilder if neither percentage nor
169+
// limitPerSecond was configured)
169170
@Nullable public Double limitPerSecond;
170171
}
171172

@@ -602,7 +603,8 @@ public static class SamplingOverride {
602603
@Nullable public TelemetryKind telemetryKind;
603604

604605
// TODO (trask) should this be named "standalone" (and meaning flipped)
605-
// TODO (trask) need to add test for this
606+
// especially if we aren't going to change meaning of "request" to include unparented INTERNAL
607+
// spans
606608
public boolean inRequest = true;
607609

608610
// not using include/exclude, because you can still get exclude with this by adding a second

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/exporter/AgentLogExporter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,12 @@ public CompletableResultCode export(Collection<LogData> logs) {
112112
SamplingOverrides samplingOverrides =
113113
stack != null ? exceptionSamplingOverrides : logSamplingOverrides;
114114

115-
Double samplingPercentage = samplingOverrides.getOverridePercentage(log.getAttributes());
116-
117115
SpanContext spanContext = log.getSpanContext();
118116

117+
boolean inRequest = spanContext.isValid();
118+
Double samplingPercentage =
119+
samplingOverrides.getOverridePercentage(inRequest, log.getAttributes());
120+
119121
if (samplingPercentage != null && !shouldSample(spanContext, samplingPercentage)) {
120122
continue;
121123
}

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/AiOverrideSampler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public SamplingResult shouldSample(
6666
? requestSamplingOverrides
6767
: dependencySamplingOverrides;
6868

69-
Sampler override = samplingOverrides.getOverride(attributes);
69+
boolean inRequest = parentSpanContext.isValid();
70+
Sampler override = samplingOverrides.getOverride(inRequest, attributes);
7071
if (override != null) {
7172
return override.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks);
7273
}

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/SamplingOverrides.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ public SamplingOverrides(List<SamplingOverride> overrides) {
4747
}
4848

4949
@Nullable
50-
public Sampler getOverride(Attributes attributes) {
50+
public Sampler getOverride(boolean inRequest, Attributes attributes) {
5151
LazyHttpUrl lazyHttpUrl = new LazyHttpUrl(attributes);
5252
for (MatcherGroup matcherGroups : matcherGroups) {
53-
if (matcherGroups.matches(attributes, lazyHttpUrl)) {
53+
if (matcherGroups.matches(inRequest, attributes, lazyHttpUrl)) {
5454
return matcherGroups.getSampler();
5555
}
5656
}
@@ -59,21 +59,23 @@ public Sampler getOverride(Attributes attributes) {
5959

6060
// used to do sampling inside the log exporter
6161
@Nullable
62-
public Double getOverridePercentage(Attributes attributes) {
62+
public Double getOverridePercentage(boolean inRequest, Attributes attributes) {
6363
for (MatcherGroup matcherGroups : matcherGroups) {
64-
if (matcherGroups.matches(attributes, null)) {
64+
if (matcherGroups.matches(inRequest, attributes, null)) {
6565
return matcherGroups.getPercentage();
6666
}
6767
}
6868
return null;
6969
}
7070

7171
private static class MatcherGroup {
72+
private final boolean inRequest;
7273
private final List<TempPredicate> predicates;
7374
private final Sampler sampler;
7475
private final SamplingPercentage samplingPercentage;
7576

7677
private MatcherGroup(SamplingOverride override) {
78+
inRequest = override.inRequest;
7779
predicates = new ArrayList<>();
7880
for (SamplingOverrideAttribute attribute : override.attributes) {
7981
predicates.add(toPredicate(attribute));
@@ -90,7 +92,11 @@ Sampler getSampler() {
9092
return samplingPercentage.get();
9193
}
9294

93-
private boolean matches(Attributes attributes, @Nullable LazyHttpUrl lazyHttpUrl) {
95+
private boolean matches(
96+
boolean inRequest, Attributes attributes, @Nullable LazyHttpUrl lazyHttpUrl) {
97+
if (this.inRequest != inRequest) {
98+
return false;
99+
}
94100
for (TempPredicate predicate : predicates) {
95101
if (!predicate.test(attributes, lazyHttpUrl)) {
96102
return false;

agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/sampling/SamplingOverridesTest.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import java.util.List;
3737
import org.junit.jupiter.api.Test;
3838

39-
// TODO (trask) add coverage for TelemetryKind
39+
// TODO (trask) add coverage for TelemetryKind and inRequest
4040
class SamplingOverridesTest {
4141

4242
@Test
@@ -47,7 +47,7 @@ void shouldSampleByDefault() {
4747
Attributes attributes = Attributes.empty();
4848

4949
// when
50-
Sampler sampler = samplingOverrides.getOverride(attributes);
50+
Sampler sampler = samplingOverrides.getOverride(true, attributes);
5151

5252
// expect
5353
assertThat(sampler).isNull();
@@ -62,7 +62,7 @@ void shouldFilterStrictMatch() {
6262
Attributes attributes = Attributes.of(AttributeKey.stringKey("one"), "1");
6363

6464
// when
65-
Sampler sampler = samplingOverrides.getOverride(attributes);
65+
Sampler sampler = samplingOverrides.getOverride(true, attributes);
6666

6767
// expect
6868
assertThat(sampler).isNotNull();
@@ -78,7 +78,7 @@ void shouldNotFilterStrictMatch() {
7878
Attributes attributes = Attributes.of(AttributeKey.stringKey("one"), "2");
7979

8080
// when
81-
Sampler sampler = samplingOverrides.getOverride(attributes);
81+
Sampler sampler = samplingOverrides.getOverride(true, attributes);
8282

8383
// expect
8484
assertThat(sampler).isNull();
@@ -93,7 +93,7 @@ void shouldNotFilterMissingStrictMatch() {
9393
Attributes attributes = Attributes.of(AttributeKey.stringKey("two"), "1");
9494

9595
// when
96-
Sampler sampler = samplingOverrides.getOverride(attributes);
96+
Sampler sampler = samplingOverrides.getOverride(true, attributes);
9797

9898
// expect
9999
assertThat(sampler).isNull();
@@ -108,7 +108,7 @@ void shouldFilterRegexpMatch() {
108108
Attributes attributes = Attributes.of(AttributeKey.stringKey("one"), "11");
109109

110110
// when
111-
Sampler sampler = samplingOverrides.getOverride(attributes);
111+
Sampler sampler = samplingOverrides.getOverride(true, attributes);
112112

113113
// expect
114114
assertThat(sampler).isNotNull();
@@ -124,7 +124,7 @@ void shouldNotFilterRegexpMatch() {
124124
Attributes attributes = Attributes.of(AttributeKey.stringKey("one"), "22");
125125

126126
// when
127-
Sampler sampler = samplingOverrides.getOverride(attributes);
127+
Sampler sampler = samplingOverrides.getOverride(true, attributes);
128128

129129
// expect
130130
assertThat(sampler).isNull();
@@ -139,7 +139,7 @@ void shouldNotFilterMissingRegexpMatch() {
139139
Attributes attributes = Attributes.of(AttributeKey.stringKey("two"), "11");
140140

141141
// when
142-
Sampler sampler = samplingOverrides.getOverride(attributes);
142+
Sampler sampler = samplingOverrides.getOverride(true, attributes);
143143

144144
// expect
145145
assertThat(sampler).isNull();
@@ -153,7 +153,7 @@ void shouldFilterKeyOnlyMatch() {
153153
Attributes attributes = Attributes.of(AttributeKey.stringKey("one"), "11");
154154

155155
// when
156-
Sampler sampler = samplingOverrides.getOverride(attributes);
156+
Sampler sampler = samplingOverrides.getOverride(true, attributes);
157157

158158
// expect
159159
assertThat(sampler).isNotNull();
@@ -168,7 +168,7 @@ void shouldNotFilterKeyOnlyMatch() {
168168
Attributes attributes = Attributes.of(AttributeKey.stringKey("two"), "22");
169169

170170
// when
171-
Sampler sampler = samplingOverrides.getOverride(attributes);
171+
Sampler sampler = samplingOverrides.getOverride(true, attributes);
172172

173173
// expect
174174
assertThat(sampler).isNull();
@@ -185,7 +185,7 @@ void shouldFilterMultiAttributes() {
185185
Attributes.of(AttributeKey.stringKey("one"), "1", AttributeKey.stringKey("two"), "22");
186186

187187
// when
188-
Sampler sampler = samplerOverride.getOverride(attributes);
188+
Sampler sampler = samplerOverride.getOverride(true, attributes);
189189

190190
// expect
191191
assertThat(sampler).isNotNull();
@@ -203,7 +203,7 @@ void shouldNotFilterMultiAttributes() {
203203
Attributes.of(AttributeKey.stringKey("one"), "2", AttributeKey.stringKey("two"), "22");
204204

205205
// when
206-
Sampler sampler = samplingOverrides.getOverride(attributes);
206+
Sampler sampler = samplingOverrides.getOverride(true, attributes);
207207

208208
// expect
209209
assertThat(sampler).isNull();
@@ -221,7 +221,7 @@ void shouldFilterMultiConfigsBothMatch() {
221221
Attributes.of(AttributeKey.stringKey("one"), "1", AttributeKey.stringKey("two"), "22");
222222

223223
// when
224-
Sampler sampler = samplingOverrides.getOverride(attributes);
224+
Sampler sampler = samplingOverrides.getOverride(true, attributes);
225225

226226
// expect
227227
assertThat(sampler).isNotNull();
@@ -240,7 +240,7 @@ void shouldFilterMultiConfigsOneMatch() {
240240
Attributes.of(AttributeKey.stringKey("one"), "2", AttributeKey.stringKey("two"), "22");
241241

242242
// when
243-
Sampler sampler = samplingOverrides.getOverride(attributes);
243+
Sampler sampler = samplingOverrides.getOverride(true, attributes);
244244

245245
// expect
246246
assertThat(sampler).isNotNull();
@@ -259,7 +259,7 @@ void shouldNotFilterMultiConfigsNoMatch() {
259259
Attributes.of(AttributeKey.stringKey("one"), "2", AttributeKey.stringKey("two"), "33");
260260

261261
// when
262-
Sampler sampler = samplingOverrides.getOverride(attributes);
262+
Sampler sampler = samplingOverrides.getOverride(true, attributes);
263263

264264
// expect
265265
assertThat(sampler).isNull();

0 commit comments

Comments
 (0)