Search in sources :

Example 1 with TraceConfig

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

the class OcAgentExportersQuickStart method configureAlwaysSample.

private static void configureAlwaysSample() {
    TraceConfig traceConfig = Tracing.getTraceConfig();
    TraceParams activeTraceParams = traceConfig.getActiveTraceParams();
    traceConfig.updateActiveTraceParams(activeTraceParams.toBuilder().setSampler(Samplers.alwaysSample()).build());
}
Also used : TraceConfig(io.opencensus.trace.config.TraceConfig) TraceParams(io.opencensus.trace.config.TraceParams)

Example 2 with TraceConfig

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

the class HelloWorldClient method initTracing.

private static void initTracing() {
    TraceConfig traceConfig = Tracing.getTraceConfig();
    Logger.getRootLogger().setLevel(Level.INFO);
    traceConfig.updateActiveTraceParams(traceConfig.getActiveTraceParams().toBuilder().setSampler(Samplers.alwaysSample()).build());
    LoggingTraceExporter.register();
    // Register Jaeger Tracing. Refer to https://www.jaegertracing.io/docs/1.8/getting-started/ to
    // run Jaeger
    JaegerTraceExporter.createAndRegister("http://localhost:14268/api/traces", "helloworldclient");
}
Also used : TraceConfig(io.opencensus.trace.config.TraceConfig)

Example 3 with TraceConfig

use of io.opencensus.trace.config.TraceConfig 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 4 with TraceConfig

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

the class MultiSpansContextTracing method main.

/**
 * Main method.
 *
 * @param args the main arguments.
 */
public static void main(String[] args) {
    // WARNING: Be careful before you set sampler value to always sample, especially in
    // production environment. Trace data is often very large in size and is expensive to
    // collect. This is why rather than collecting traces for every request(i.e. alwaysSample),
    // downsampling is prefered.
    // 
    // By default, OpenCensus provides a probabilistic sampler that will trace once in every
    // 10,000 requests, that's why if default probabilistic sampler is used
    // you might not see trace data printed or exported and this is expected behavior.
    TraceConfig traceConfig = Tracing.getTraceConfig();
    traceConfig.updateActiveTraceParams(traceConfig.getActiveTraceParams().toBuilder().setSampler(Samplers.alwaysSample()).build());
    LoggingTraceExporter.register();
    Span span = tracer.spanBuilderWithExplicitParent("MyRootSpan", null).startSpan();
    try (Scope ws = tracer.withSpan(span)) {
        doWork();
    }
    span.end();
    // Wait for a duration longer than reporting duration (5s) to ensure spans are exported.
    // Spans are exported every 5 seconds
    sleep(5100);
}
Also used : Scope(io.opencensus.common.Scope) TraceConfig(io.opencensus.trace.config.TraceConfig) Span(io.opencensus.trace.Span)

Example 5 with TraceConfig

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

the class MultiSpansTracing method main.

/**
 * Main method.
 *
 * @param args the main arguments.
 */
public static void main(String[] args) {
    // WARNING: Be careful before you set sampler value to always sample, especially in
    // production environment. Trace data is often very large in size and is expensive to
    // collect. This is why rather than collecting traces for every request(i.e. alwaysSample),
    // downsampling is prefered.
    // 
    // By default, OpenCensus provides a probabilistic sampler that will trace once in every
    // 10,000 requests, that's why if default probabilistic sampler is used
    // you might not see trace data printed or exported and this is expected behavior.
    TraceConfig traceConfig = Tracing.getTraceConfig();
    traceConfig.updateActiveTraceParams(traceConfig.getActiveTraceParams().toBuilder().setSampler(Samplers.alwaysSample()).build());
    LoggingTraceExporter.register();
    doWork();
    // Wait for a duration longer than reporting duration (5s) to ensure spans are exported.
    // Spans are exported every 5 seconds
    sleep(5100);
}
Also used : TraceConfig(io.opencensus.trace.config.TraceConfig)

Aggregations

TraceConfig (io.opencensus.trace.config.TraceConfig)10 Scope (io.opencensus.common.Scope)3 TraceParams (io.opencensus.trace.config.TraceParams)3 Span (io.opencensus.trace.Span)2 ByteString (com.google.protobuf.ByteString)1 Node (io.opencensus.proto.agent.common.v1.Node)1 ExportTraceServiceRequest (io.opencensus.proto.agent.trace.v1.ExportTraceServiceRequest)1 HTTPServer (io.prometheus.client.exporter.HTTPServer)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1