use of io.opencensus.common.Scope in project instrumentation-java by census-instrumentation.
the class OcAgentExportersQuickStart method main.
/**
* Main launcher of the example.
*/
public static void main(String[] args) throws InterruptedException {
// Always sample for demo purpose. DO NOT use in production.
configureAlwaysSample();
registerAllViews();
LongGauge gauge = registerGauge();
String endPoint = getStringOrDefaultFromArgs(args, 0, DEFAULT_ENDPOINT);
registerAgentExporters(endPoint);
try (Scope scope = tracer.spanBuilder("root").startScopedSpan()) {
int iteration = 1;
while (true) {
doWork(iteration, random.nextInt(10), gauge);
iteration++;
Thread.sleep(5000);
}
} catch (InterruptedException e) {
logger.info("Thread interrupted, exiting in 5 seconds.");
// Wait 5s so that last batch will be exported.
Thread.sleep(5000);
}
}
use of io.opencensus.common.Scope in project instrumentation-java by census-instrumentation.
the class OcAgentExportersQuickStart method doWork.
private static void doWork(int iteration, int jobs, LongGauge gauge) {
String childSpanName = "iteration-" + iteration;
LabelValue value = LabelValue.create(childSpanName);
LongPoint point = gauge.getOrCreateTimeSeries(Collections.singletonList(value));
try (Scope scope = tracer.spanBuilder(childSpanName).startScopedSpan()) {
for (int i = 0; i < jobs; i++) {
String grandChildSpanName = childSpanName + "-job-" + i;
try (Scope childScope = tracer.spanBuilder(grandChildSpanName).startScopedSpan()) {
point.set(jobs - i);
String line = generateRandom(random.nextInt(128));
processLine(line);
recordStat(M_LINES_IN, 1L);
recordStat(M_LINE_LENGTHS, (long) line.length());
} catch (Exception e) {
tracer.getCurrentSpan().setStatus(Status.INTERNAL.withDescription(e.toString()));
}
}
}
}
use of io.opencensus.common.Scope in project instrumentation-java by census-instrumentation.
the class MultiSpansContextTracing method doSomeMoreWork.
private static void doSomeMoreWork() {
// Create a child Span of the current Span.
Span span = tracer.spanBuilder("MyChildSpan").startSpan();
try (Scope ws = tracer.withSpan(span)) {
doSomeOtherWork();
}
span.end();
}
use of io.opencensus.common.Scope 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);
}
use of io.opencensus.common.Scope in project instrumentation-java by census-instrumentation.
the class ZPagesTester method recordExampleData.
private static void recordExampleData() throws InterruptedException {
Tracing.getExportComponent().getSampledSpanStore().registerSpanNamesForCollection(Collections.singletonList(SPAN_NAME));
// Use old RPC constants to get interval stats.
RpcViews.registerAllViews();
SpanBuilder spanBuilder = tracer.spanBuilder(SPAN_NAME).setRecordEvents(true).setSampler(Samplers.alwaysSample());
try (Scope scope = spanBuilder.startScopedSpan()) {
tracer.getCurrentSpan().addAnnotation("Starts recording.");
MeasureMap measureMap = statsRecorder.newMeasureMap().put(RpcMeasureConstants.RPC_CLIENT_STARTED_COUNT, 1).put(RpcMeasureConstants.RPC_CLIENT_FINISHED_COUNT, 1).put(RpcMeasureConstants.RPC_CLIENT_ROUNDTRIP_LATENCY, 1.0).put(RpcMeasureConstants.RPC_CLIENT_REQUEST_COUNT, 1).put(RpcMeasureConstants.RPC_CLIENT_RESPONSE_COUNT, 1).put(RpcMeasureConstants.RPC_CLIENT_REQUEST_BYTES, 1e5).put(RpcMeasureConstants.RPC_CLIENT_RESPONSE_BYTES, 1e5).put(RpcMeasureConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES, 1e5).put(RpcMeasureConstants.RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES, 1e5).put(RpcMeasureConstants.RPC_SERVER_STARTED_COUNT, 1).put(RpcMeasureConstants.RPC_SERVER_FINISHED_COUNT, 1).put(RpcMeasureConstants.RPC_SERVER_SERVER_LATENCY, 1.0).put(RpcMeasureConstants.RPC_SERVER_REQUEST_COUNT, 1).put(RpcMeasureConstants.RPC_SERVER_RESPONSE_COUNT, 1).put(RpcMeasureConstants.RPC_SERVER_REQUEST_BYTES, 1e5).put(RpcMeasureConstants.RPC_SERVER_RESPONSE_BYTES, 1e5).put(RpcMeasureConstants.RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES, 1e5).put(RpcMeasureConstants.RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES, 1e5);
measureMap.record(tagger.currentBuilder().put(RpcMeasureConstants.RPC_STATUS, TagValue.create("OK")).put(RpcMeasureConstants.RPC_METHOD, METHOD).build());
MeasureMap measureMapErrors = statsRecorder.newMeasureMap().put(RpcMeasureConstants.RPC_CLIENT_ERROR_COUNT, 1).put(RpcMeasureConstants.RPC_SERVER_ERROR_COUNT, 1);
measureMapErrors.record(tagger.currentBuilder().put(RpcMeasureConstants.RPC_STATUS, TagValue.create("UNKNOWN")).put(RpcMeasureConstants.RPC_METHOD, METHOD).build());
// sleep for fake work.
Thread.sleep(200);
tracer.getCurrentSpan().addAnnotation("Finish recording.");
}
}
Aggregations