use of io.opencensus.common.Scope in project instrumentation-java by census-instrumentation.
the class TracerTest method startSpanWithInvalidParentFromContext.
@Test
public void startSpanWithInvalidParentFromContext() {
Scope ws = tracer.withSpan(BlankSpan.INSTANCE);
try {
assertThat(tracer.getCurrentSpan()).isSameInstanceAs(BlankSpan.INSTANCE);
when(tracer.spanBuilderWithExplicitParent(same(SPAN_NAME), same(BlankSpan.INSTANCE))).thenReturn(spanBuilder);
assertThat(tracer.spanBuilder(SPAN_NAME)).isSameInstanceAs(spanBuilder);
} finally {
ws.close();
}
}
use of io.opencensus.common.Scope in project instrumentation-java by census-instrumentation.
the class TracerTest method startSpanWithParentFromContext.
@Test
public void startSpanWithParentFromContext() {
Scope ws = tracer.withSpan(span);
try {
assertThat(tracer.getCurrentSpan()).isSameInstanceAs(span);
when(tracer.spanBuilderWithExplicitParent(same(SPAN_NAME), same(span))).thenReturn(spanBuilder);
assertThat(tracer.spanBuilder(SPAN_NAME)).isSameInstanceAs(spanBuilder);
} finally {
ws.close();
}
}
use of io.opencensus.common.Scope in project instrumentation-java by census-instrumentation.
the class MetricReader method readAndExport.
/**
* Reads the metrics from the {@link MetricProducerManager} and exports them to the {@code
* metricExporter}.
*
* @param metricExporter the exporter called to export the metrics read.
* @since 0.19
*/
public void readAndExport(MetricExporter metricExporter) {
Span span = tracer.spanBuilder(spanName).setRecordEvents(true).setSampler(probabilitySampler).startSpan();
Scope scope = tracer.withSpan(span);
try {
ArrayList<Metric> metricsList = new ArrayList<>();
for (MetricProducer metricProducer : metricProducerManager.getAllMetricProducer()) {
metricsList.addAll(metricProducer.getMetrics());
}
metricExporter.export(metricsList);
} catch (Throwable e) {
logger.log(Level.WARNING, "Exception thrown by the metrics exporter.", e);
span.setStatus(Status.UNKNOWN.withDescription("Exception when export metrics: " + exceptionMessage(e)));
} finally {
scope.close();
span.end();
}
}
use of io.opencensus.common.Scope in project instrumentation-java by census-instrumentation.
the class HelloWorldServer method performWork.
// A helper function that performs some work in its own Span.
private static void performWork(Span parent) {
SpanBuilder spanBuilder = tracer.spanBuilderWithExplicitParent("internal_work", parent).setRecordEvents(true);
try (Scope scope = spanBuilder.startScopedSpan()) {
Span span = tracer.getCurrentSpan();
span.putAttribute("my_attribute", AttributeValue.stringAttributeValue("blue"));
span.addAnnotation("Performing work.");
// Working hard here.
sleepFor(20);
span.addAnnotation("Done work.");
}
}
use of io.opencensus.common.Scope in project instrumentation-java by census-instrumentation.
the class TagContextExample method main.
/**
* Main method.
*
* @param args the main arguments.
*/
public static void main(String[] args) {
System.out.println("Hello Stats World");
System.out.println("Default Tags: " + tagger.empty());
System.out.println("Current Tags: " + tagger.getCurrentTagContext());
TagContext tags1 = tagger.emptyBuilder().put(K1, V1).put(K2, V2).build();
try (Scope scopedTagCtx1 = tagger.withTagContext(tags1)) {
System.out.println(" Current Tags: " + tagger.getCurrentTagContext());
System.out.println(" Current == Default + tags1: " + tagger.getCurrentTagContext().equals(tags1));
TagContext tags2 = tagger.toBuilder(tags1).put(K3, V3).put(K4, V4).build();
try (Scope scopedTagCtx2 = tagger.withTagContext(tags2)) {
System.out.println(" Current Tags: " + tagger.getCurrentTagContext());
System.out.println(" Current == Default + tags1 + tags2: " + tagger.getCurrentTagContext().equals(tags2));
statsRecorder.newMeasureMap().put(M1, 0.2).put(M2, 0.4).record();
}
}
System.out.println("Current == Default: " + tagger.getCurrentTagContext().equals(tagger.empty()));
}
Aggregations