Search in sources :

Example 11 with TraceParams

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

the class HttpServletFilterIntegrationTests method setup.

@Before
@Override
public void setup() {
    super.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)

Example 12 with TraceParams

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

the class SpanBuilderImpl method startSpanInternal.

private Span startSpanInternal(@Nullable SpanContext parentContext, @Nullable Boolean hasRemoteParent, String name, @Nullable Sampler sampler, List<Span> parentLinks, @Nullable Boolean recordEvents, @Nullable Kind kind, @Nullable Span parentSpan) {
    TraceParams activeTraceParams = options.traceConfig.getActiveTraceParams();
    Random random = options.randomHandler.current();
    TraceId traceId;
    SpanId spanId = SpanId.generateRandomId(random);
    SpanId parentSpanId = null;
    // TODO(bdrutu): Handle tracestate correctly not just propagate.
    Tracestate tracestate = TRACESTATE_DEFAULT;
    if (parentContext == null || !parentContext.isValid()) {
        // New root span.
        traceId = TraceId.generateRandomId(random);
        // This is a root span so no remote or local parent.
        hasRemoteParent = null;
    } else {
        // New child span.
        traceId = parentContext.getTraceId();
        parentSpanId = parentContext.getSpanId();
        tracestate = parentContext.getTracestate();
    }
    TraceOptions traceOptions = makeSamplingDecision(parentContext, hasRemoteParent, name, sampler, parentLinks, traceId, spanId, activeTraceParams) ? SAMPLED_TRACE_OPTIONS : NOT_SAMPLED_TRACE_OPTIONS;
    if (traceOptions.isSampled() || Boolean.TRUE.equals(recordEvents)) {
        // Pass the timestamp converter from the parent to ensure that the recorded events are in
        // the right order. Implementation uses System.nanoTime() which is monotonically increasing.
        TimestampConverter timestampConverter = null;
        if (parentSpan instanceof RecordEventsSpanImpl) {
            RecordEventsSpanImpl parentRecordEventsSpan = (RecordEventsSpanImpl) parentSpan;
            timestampConverter = parentRecordEventsSpan.getTimestampConverter();
            parentRecordEventsSpan.addChild();
        }
        Span span = RecordEventsSpanImpl.startSpan(SpanContext.create(traceId, spanId, traceOptions, tracestate), name, kind, parentSpanId, hasRemoteParent, activeTraceParams, options.startEndHandler, timestampConverter, options.clock);
        linkSpans(span, parentLinks);
        return span;
    } else {
        return NoRecordEventsSpanImpl.create(SpanContext.create(traceId, spanId, traceOptions, tracestate));
    }
}
Also used : Random(java.util.Random) TimestampConverter(io.opencensus.implcore.internal.TimestampConverter) TraceId(io.opencensus.trace.TraceId) TraceOptions(io.opencensus.trace.TraceOptions) TraceParams(io.opencensus.trace.config.TraceParams) Tracestate(io.opencensus.trace.Tracestate) Span(io.opencensus.trace.Span) SpanId(io.opencensus.trace.SpanId)

Example 13 with TraceParams

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

the class TraceProtoUtilsTest method applyUpdatedConfig.

@Test
public void applyUpdatedConfig() {
    TraceConfig configProto = TraceConfig.newBuilder().setProbabilitySampler(ProbabilitySampler.newBuilder().setSamplingProbability(0.01).build()).build();
    UpdatedLibraryConfig updatedLibraryConfig = UpdatedLibraryConfig.newBuilder().setConfig(configProto).build();
    TraceParams traceParams = TraceProtoUtils.getUpdatedTraceParams(updatedLibraryConfig, mockTraceConfig);
    TraceParams expectedParams = DEFAULT_PARAMS.toBuilder().setSampler(Samplers.probabilitySampler(0.01)).build();
    Mockito.verify(mockTraceConfig, Mockito.times(1)).getActiveTraceParams();
    assertThat(traceParams).isEqualTo(expectedParams);
}
Also used : UpdatedLibraryConfig(io.opencensus.proto.agent.trace.v1.UpdatedLibraryConfig) TraceConfig(io.opencensus.proto.trace.v1.TraceConfig) TraceParams(io.opencensus.trace.config.TraceParams) Test(org.junit.Test)

Example 14 with TraceParams

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

the class OcAgentTraceServiceRpcHandlersTest method config_CreateAndSend.

@Test
public void config_CreateAndSend() throws InterruptedException {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    traceServiceGrpc.setCountDownLatch(countDownLatch);
    // Config RPC handler needs to be running in another thread.
    Runnable configRunnable = new Runnable() {

        @Override
        public void run() {
            TraceServiceStub stub = getStub(serverName);
            OcAgentTraceServiceConfigRpcHandler configRpcHandler = OcAgentTraceServiceConfigRpcHandler.create(stub, mockTraceConfig);
            // connection should succeed
            assertThat(configRpcHandler.isCompleted()).isFalse();
            // this will block this thread
            configRpcHandler.sendInitialMessage(NODE);
        }
    };
    Thread configThread = new Thread(configRunnable);
    configThread.setDaemon(true);
    configThread.setName("TestConfigRpcHandlerThread");
    configThread.start();
    // Wait until fake agent received the first message.
    countDownLatch.await();
    traceServiceGrpc.closeConfigStream();
    // Verify fake Agent (server) received the expected CurrentLibraryConfig.
    CurrentLibraryConfig expectedCurrentConfig = CurrentLibraryConfig.newBuilder().setNode(NODE).setConfig(TRACE_CONFIG_DEFAULT_PROTO).build();
    assertThat(traceServiceGrpc.getCurrentLibraryConfigs()).containsExactly(expectedCurrentConfig);
    // Verify ConfigRpcHandler (client) received the expected UpdatedLibraryConfig.
    TraceParams expectedParams = TraceProtoUtils.getUpdatedTraceParams(traceServiceGrpc.getUpdatedLibraryConfig(), mockTraceConfig);
    verify(mockTraceConfig, times(1)).updateActiveTraceParams(expectedParams);
}
Also used : CurrentLibraryConfig(io.opencensus.proto.agent.trace.v1.CurrentLibraryConfig) TraceServiceStub(io.opencensus.proto.agent.trace.v1.TraceServiceGrpc.TraceServiceStub) CountDownLatch(java.util.concurrent.CountDownLatch) TraceParams(io.opencensus.trace.config.TraceParams) Test(org.junit.Test)

Example 15 with TraceParams

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

the class BasicSetup method enableOpenCensus.

/**
 * Enables OpenCensus metric and traces.
 *
 * <p>This will register all basic {@link io.opencensus.stats.View}s. When coupled with an agent,
 * it allows users to monitor application behavior.
 *
 * <p>Example usage for maven:
 *
 * <pre>{@code
 * <dependency>
 *   <groupId>io.opencensus</groupId>
 *   <artifactId>opencensus-contrib-observability-ready-util</artifactId>
 *   <version>${opencensus.version}</version>
 * </dependency>
 * }</pre>
 *
 * <p>It is recommended to call this method before doing any RPC call to avoid missing stats.
 *
 * <pre>{@code
 * BasicSetup.enableOpenCensus();
 * }</pre>
 *
 * @param endPoint the end point of OC-Agent.
 * @param probability the desired probability of sampling. Must be within [0.0, 1.0].
 * @since 0.25
 */
public static void enableOpenCensus(String endPoint, double probability) {
    // register basic rpc views
    RpcViews.registerAllGrpcBasicViews();
    // set sampling rate
    TraceConfig traceConfig = Tracing.getTraceConfig();
    TraceParams activeTraceParams = traceConfig.getActiveTraceParams();
    traceConfig.updateActiveTraceParams(activeTraceParams.toBuilder().setSampler(Samplers.probabilitySampler(probability)).build());
    String serviceName = firstNonNull(System.getenv("SERVICE_NAME"), DEAFULT_SERVICE_NAME);
    // create and register Trace Agent Exporter
    OcAgentTraceExporter.createAndRegister(OcAgentTraceExporterConfiguration.builder().setEndPoint(endPoint).setServiceName(serviceName).setUseInsecure(true).setEnableConfig(false).build());
    // create and register Metrics Agent Exporter
    OcAgentMetricsExporter.createAndRegister(OcAgentMetricsExporterConfiguration.builder().setEndPoint(endPoint).setServiceName(serviceName).setUseInsecure(true).build());
}
Also used : TraceConfig(io.opencensus.trace.config.TraceConfig) TraceParams(io.opencensus.trace.config.TraceParams)

Aggregations

TraceParams (io.opencensus.trace.config.TraceParams)18 Test (org.junit.Test)9 SpanData (io.opencensus.trace.export.SpanData)5 TraceConfig (io.opencensus.trace.config.TraceConfig)4 TestHandler (io.opencensus.testing.export.TestHandler)3 SpanExporter (io.opencensus.trace.export.SpanExporter)3 Before (org.junit.Before)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 HashMap (java.util.HashMap)2 ByteString (com.google.protobuf.ByteString)1 TimestampConverter (io.opencensus.implcore.internal.TimestampConverter)1 Node (io.opencensus.proto.agent.common.v1.Node)1 CurrentLibraryConfig (io.opencensus.proto.agent.trace.v1.CurrentLibraryConfig)1 ExportTraceServiceRequest (io.opencensus.proto.agent.trace.v1.ExportTraceServiceRequest)1 TraceServiceStub (io.opencensus.proto.agent.trace.v1.TraceServiceGrpc.TraceServiceStub)1 UpdatedLibraryConfig (io.opencensus.proto.agent.trace.v1.UpdatedLibraryConfig)1 ConstantSampler (io.opencensus.proto.trace.v1.ConstantSampler)1