Skip to content
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

Change OpenCensus shim default sampling to defer to OpenTelemetry #5604

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import io.opencensus.trace.TraceComponent;
import io.opencensus.trace.Tracer;
import io.opencensus.trace.config.TraceConfig;
import io.opencensus.trace.config.TraceParams;
import io.opencensus.trace.export.ExportComponent;
import io.opencensus.trace.propagation.PropagationComponent;
import io.opencensus.trace.samplers.Samplers;

/**
* Implementation of the {@link TraceComponent} for OpenTelemetry migration, which uses the
Expand All @@ -25,7 +27,7 @@ public final class OpenTelemetryTraceComponentImpl extends TraceComponent {
new OpenTelemetryPropagationComponentImpl();
private final ExportComponent noopExportComponent = ExportComponent.newNoopExportComponent();
private final Clock clock;
private final TraceConfig traceConfig = new TraceConfigImpl();
private final TraceConfig traceConfig = makeTraceConfig();
private final Tracer tracer;

/** Public constructor to be used with reflection loading. */
Expand Down Expand Up @@ -60,4 +62,13 @@ public ExportComponent getExportComponent() {
public TraceConfig getTraceConfig() {
return traceConfig;
}

private static TraceConfig makeTraceConfig() {
// Use the default TraceParams except use an always-on sampler. This defers the
// sampling decision to OpenTelemetry
TraceConfig traceConfig = new TraceConfigImpl();
traceConfig.updateActiveTraceParams(
TraceParams.DEFAULT.toBuilder().setSampler(Samplers.alwaysSample()).build());
return traceConfig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,14 @@ public void testNoSampleDoesNotExport() {
}

@Test
public void testNoRecordDoesNotExport() {
public void testOpenCensusSamplerIsAlwaysOn() {
// OpenTelemetryTraceComponentImpl provides this behavior
assertThat(Tracing.getTraceConfig().getActiveTraceParams().getSampler())
.isEqualTo(Samplers.alwaysSample());
}

@Test
public void testByDefaultDoesExport() {
io.opencensus.trace.Tracer tracer = Tracing.getTracer();
try (io.opencensus.common.Scope scope =
tracer.spanBuilder("OpenCensusSpan").setRecordEvents(false).startScopedSpan()) {
Expand All @@ -333,7 +340,7 @@ public void testNoRecordDoesNotExport() {
span.putAttribute("testKey", AttributeValue.stringAttributeValue("testValue"));
}
Tracing.getExportComponent().shutdown();
verify(spanExporter, never()).export(anyCollection());
verify(spanExporter, times(1)).export(anyCollection());
}

private static void createOpenCensusScopedSpanWithChildSpan(
Expand Down