-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First try rate limited sampler for initialSampler of XRay remote samp…
…ler. (#68)
- Loading branch information
Anuraag Agrawal
authored
Aug 24, 2021
1 parent
9b9514e
commit 8d5794a
Showing
4 changed files
with
114 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
aws-xray/src/main/java/io/opentelemetry/contrib/awsxray/OrElseSampler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package io.opentelemetry.contrib.awsxray; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.trace.SpanKind; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.sdk.trace.data.LinkData; | ||
import io.opentelemetry.sdk.trace.samplers.Sampler; | ||
import io.opentelemetry.sdk.trace.samplers.SamplingDecision; | ||
import io.opentelemetry.sdk.trace.samplers.SamplingResult; | ||
import java.util.List; | ||
|
||
class OrElseSampler implements Sampler { | ||
|
||
private final Sampler first; | ||
private final Sampler second; | ||
|
||
OrElseSampler(Sampler first, Sampler second) { | ||
this.first = first; | ||
this.second = second; | ||
} | ||
|
||
@Override | ||
public SamplingResult shouldSample( | ||
Context parentContext, | ||
String traceId, | ||
String name, | ||
SpanKind spanKind, | ||
Attributes attributes, | ||
List<LinkData> parentLinks) { | ||
SamplingResult result = | ||
first.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks); | ||
if (result.getDecision() != SamplingDecision.DROP) { | ||
return result; | ||
} | ||
return second.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks); | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "OrElse{" | ||
+ "first:" | ||
+ first.getDescription() | ||
+ ", second:" | ||
+ second.getDescription() | ||
+ "}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
aws-xray/src/test/java/io/opentelemetry/contrib/awsxray/OrElseSamplerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package io.opentelemetry.contrib.awsxray; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.trace.SpanKind; | ||
import io.opentelemetry.api.trace.TraceId; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.sdk.trace.samplers.Sampler; | ||
import io.opentelemetry.sdk.trace.samplers.SamplingDecision; | ||
import io.opentelemetry.sdk.trace.samplers.SamplingResult; | ||
import java.util.Collections; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class OrElseSamplerTest { | ||
|
||
@Test | ||
void firstWins() { | ||
Sampler sampler = new OrElseSampler(Sampler.alwaysOn(), Sampler.alwaysOff()); | ||
assertThat(doSample(sampler).getDecision()).isEqualTo(SamplingDecision.RECORD_AND_SAMPLE); | ||
} | ||
|
||
@Test | ||
void fallsBackToSecond() { | ||
Sampler sampler = new OrElseSampler(Sampler.alwaysOff(), Sampler.alwaysOn()); | ||
assertThat(doSample(sampler).getDecision()).isEqualTo(SamplingDecision.RECORD_AND_SAMPLE); | ||
sampler = new OrElseSampler(Sampler.alwaysOff(), Sampler.alwaysOff()); | ||
assertThat(doSample(sampler).getDecision()).isEqualTo(SamplingDecision.DROP); | ||
} | ||
|
||
private SamplingResult doSample(Sampler sampler) { | ||
return sampler.shouldSample( | ||
Context.current(), | ||
TraceId.fromLongs(1, 2), | ||
"span", | ||
SpanKind.CLIENT, | ||
Attributes.empty(), | ||
Collections.emptyList()); | ||
} | ||
} |