Search in sources :

Example 6 with TraceParams

use of io.opencensus.trace.config.TraceParams in project instrumentation-java by census-instrumentation.

the class TraceConfigImplTest method updateTraceParams.

@Test
public void updateTraceParams() {
    TraceParams traceParams = TraceParams.DEFAULT.toBuilder().setSampler(Samplers.alwaysSample()).setMaxNumberOfAttributes(8).setMaxNumberOfAnnotations(9).setMaxNumberOfNetworkEvents(10).setMaxNumberOfLinks(11).build();
    traceConfig.updateActiveTraceParams(traceParams);
    assertThat(traceConfig.getActiveTraceParams()).isEqualTo(traceParams);
    traceConfig.updateActiveTraceParams(TraceParams.DEFAULT);
    assertThat(traceConfig.getActiveTraceParams()).isEqualTo(TraceParams.DEFAULT);
}
Also used : TraceParams(io.opencensus.trace.config.TraceParams) Test(org.junit.Test)

Example 7 with TraceParams

use of io.opencensus.trace.config.TraceParams in project instrumentation-java by census-instrumentation.

the class OcAgentTraceExporterIntegrationTest method testExportSpans.

@Test
public void testExportSpans() throws InterruptedException, IOException {
    // Mock a real-life scenario in production, where Agent is not enabled at first, then enabled
    // after an outage. Users should be able to see traces shortly after Agent is up.
    // Configure to be always-sampled.
    TraceConfig traceConfig = Tracing.getTraceConfig();
    TraceParams activeTraceParams = traceConfig.getActiveTraceParams();
    traceConfig.updateActiveTraceParams(activeTraceParams.toBuilder().setSampler(Samplers.alwaysSample()).build());
    // Register the OcAgent Exporter first.
    // Agent is not yet up and running so Exporter will just retry connection.
    OcAgentTraceExporter.createAndRegister(OcAgentTraceExporterConfiguration.builder().setServiceName(SERVICE_NAME).setUseInsecure(true).setEnableConfig(false).build());
    // Create one root span and 5 children.
    try (Scope scope = tracer.spanBuilder("root").startScopedSpan()) {
        for (int i = 0; i < 5; i++) {
            // Fake work
            doWork("first-iteration-child-" + i, i);
        }
    }
    // Wait 5s so that SpanExporter exports exports all spans.
    Thread.sleep(5000);
    // No interaction with Agent so far.
    assertThat(fakeOcAgentTraceServiceGrpc.getExportTraceServiceRequests()).isEmpty();
    // Image an outage happened, now start Agent. Exporter should be able to connect to Agent
    // when the next batch of SpanData arrives.
    agent.start();
    // Create one root span and 8 children.
    try (Scope scope = tracer.spanBuilder("root2").startScopedSpan()) {
        for (int i = 0; i < 8; i++) {
            // Fake work
            doWork("second-iteration-child-" + i, i);
        }
    }
    // Wait 5s so that SpanExporter exports exports all spans.
    Thread.sleep(5000);
    List<ExportTraceServiceRequest> exportRequests = fakeOcAgentTraceServiceGrpc.getExportTraceServiceRequests();
    assertThat(exportRequests.size()).isAtLeast(2);
    ExportTraceServiceRequest firstRequest = exportRequests.get(0);
    Node expectedNode = OcAgentNodeUtils.getNodeInfo(SERVICE_NAME);
    Node actualNode = firstRequest.getNode();
    assertThat(actualNode.getIdentifier().getHostName()).isEqualTo(expectedNode.getIdentifier().getHostName());
    assertThat(actualNode.getIdentifier().getPid()).isEqualTo(expectedNode.getIdentifier().getPid());
    assertThat(actualNode.getLibraryInfo()).isEqualTo(expectedNode.getLibraryInfo());
    assertThat(actualNode.getServiceInfo()).isEqualTo(expectedNode.getServiceInfo());
    List<io.opencensus.proto.trace.v1.Span> spanProtos = new ArrayList<>();
    for (int i = 1; i < exportRequests.size(); i++) {
        spanProtos.addAll(exportRequests.get(i).getSpansList());
    }
    // On some platforms (e.g Windows) SpanData will never be dropped, so spans from the first batch
    // may also be exported after Agent is up.
    assertThat(spanProtos.size()).isAtLeast(9);
    Set<String> exportedSpanNames = new HashSet<>();
    for (io.opencensus.proto.trace.v1.Span spanProto : spanProtos) {
        if ("root2".equals(spanProto.getName().getValue())) {
            assertThat(spanProto.getChildSpanCount().getValue()).isEqualTo(8);
            assertThat(spanProto.getParentSpanId()).isEqualTo(ByteString.EMPTY);
        } else if ("root".equals(spanProto.getName().getValue())) {
            // This won't happen on Linux but does happen on Windows.
            assertThat(spanProto.getChildSpanCount().getValue()).isEqualTo(5);
            assertThat(spanProto.getParentSpanId()).isEqualTo(ByteString.EMPTY);
        }
        exportedSpanNames.add(spanProto.getName().getValue());
    }
    // The second batch of spans should be exported no matter what.
    assertThat(exportedSpanNames).contains("root2");
    for (int i = 0; i < 8; i++) {
        assertThat(exportedSpanNames).contains("second-iteration-child-" + i);
    }
}
Also used : ExportTraceServiceRequest(io.opencensus.proto.agent.trace.v1.ExportTraceServiceRequest) Node(io.opencensus.proto.agent.common.v1.Node) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) TraceParams(io.opencensus.trace.config.TraceParams) Span(io.opencensus.trace.Span) Scope(io.opencensus.common.Scope) TraceConfig(io.opencensus.trace.config.TraceConfig) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with TraceParams

use of io.opencensus.trace.config.TraceParams in project instrumentation-java by census-instrumentation.

the class TraceProtoUtils method fromTraceConfigProto.

/**
 * Converts {@link TraceConfig} to {@link TraceParams}.
 *
 * @param traceConfigProto {@code TraceConfig}.
 * @param currentTraceParams current {@code TraceParams}.
 * @return updated {@code TraceParams}.
 * @since 0.17
 */
static TraceParams fromTraceConfigProto(TraceConfig traceConfigProto, TraceParams currentTraceParams) {
    TraceParams.Builder builder = currentTraceParams.toBuilder();
    if (traceConfigProto.hasConstantSampler()) {
        ConstantSampler constantSampler = traceConfigProto.getConstantSampler();
        ConstantDecision decision = constantSampler.getDecision();
        if (ConstantDecision.ALWAYS_ON.equals(decision)) {
            builder.setSampler(Samplers.alwaysSample());
        } else if (ConstantDecision.ALWAYS_OFF.equals(decision)) {
            builder.setSampler(Samplers.neverSample());
        }
    // else if (ConstantDecision.ALWAYS_PARENT.equals(decision)) {
    // For ALWAYS_PARENT, don't need to update configs since in Java by default parent sampling
    // decision always takes precedence.
    // }
    } else if (traceConfigProto.hasProbabilitySampler()) {
        builder.setSampler(Samplers.probabilitySampler(traceConfigProto.getProbabilitySampler().getSamplingProbability()));
    }
    // TODO: add support for RateLimitingSampler.
    return builder.build();
}
Also used : ConstantSampler(io.opencensus.proto.trace.v1.ConstantSampler) ConstantDecision(io.opencensus.proto.trace.v1.ConstantSampler.ConstantDecision) TraceParams(io.opencensus.trace.config.TraceParams)

Example 9 with TraceParams

use of io.opencensus.trace.config.TraceParams in project instrumentation-java by census-instrumentation.

the class TraceProtoUtils method getUpdatedTraceParams.

// Creates an updated TraceParams with the given UpdatedLibraryConfig message and current
// TraceParams, then applies the updated TraceParams.
static TraceParams getUpdatedTraceParams(UpdatedLibraryConfig config, io.opencensus.trace.config.TraceConfig traceConfig) {
    TraceParams currentParams = traceConfig.getActiveTraceParams();
    TraceConfig traceConfigProto = config.getConfig();
    return fromTraceConfigProto(traceConfigProto, currentParams);
}
Also used : TraceConfig(io.opencensus.proto.trace.v1.TraceConfig) TraceParams(io.opencensus.trace.config.TraceParams)

Example 10 with TraceParams

use of io.opencensus.trace.config.TraceParams in project instrumentation-java by census-instrumentation.

the class TraceWebAsyncClientAutoConfigurationTest method setup.

@Before
public void setup() {
    handler = new TestHandler();
    SpanExporter exporter = Tracing.getExportComponent().getSpanExporter();
    exporter.registerHandler("testing", handler);
    TraceParams params = Tracing.getTraceConfig().getActiveTraceParams().toBuilder().setSampler(Samplers.alwaysSample()).build();
    Tracing.getTraceConfig().updateActiveTraceParams(params);
}
Also used : SpanExporter(io.opencensus.trace.export.SpanExporter) TestHandler(io.opencensus.testing.export.TestHandler) TraceParams(io.opencensus.trace.config.TraceParams) Before(org.junit.Before)

Aggregations

TraceParams (io.opencensus.trace.config.TraceParams)22 Test (org.junit.Test)9 SpanData (io.opencensus.trace.export.SpanData)5 TraceConfig (io.opencensus.trace.config.TraceConfig)4 Before (org.junit.Before)4 TestHandler (io.opencensus.testing.export.TestHandler)3 SpanId (io.opencensus.trace.SpanId)3 SpanExporter (io.opencensus.trace.export.SpanExporter)3 Scope (io.opencensus.common.Scope)2 TraceConfig (io.opencensus.proto.trace.v1.TraceConfig)2 AttributeValue (io.opencensus.trace.AttributeValue)2 Span (io.opencensus.trace.Span)2 SpanContext (io.opencensus.trace.SpanContext)2 TraceId (io.opencensus.trace.TraceId)2 TraceOptions (io.opencensus.trace.TraceOptions)2 Tracestate (io.opencensus.trace.Tracestate)2 HashMap (java.util.HashMap)2 Random (java.util.Random)2 ByteString (com.google.protobuf.ByteString)1 Clock (io.opencensus.common.Clock)1