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);
}
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));
}
}
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);
}
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);
}
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());
}
Aggregations