• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017, OpenCensus Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.opencensus.examples.trace;
18 
19 import static io.opencensus.examples.trace.Utils.sleep;
20 
21 import io.opencensus.common.Scope;
22 import io.opencensus.exporter.trace.logging.LoggingTraceExporter;
23 import io.opencensus.trace.Span;
24 import io.opencensus.trace.Tracer;
25 import io.opencensus.trace.Tracing;
26 import io.opencensus.trace.config.TraceConfig;
27 import io.opencensus.trace.samplers.Samplers;
28 
29 /**
30  * Example showing how to create a child {@link Span}, install it to the current context and add
31  * annotations.
32  */
33 public final class MultiSpansContextTracing {
34   // Per class Tracer.
35   private static final Tracer tracer = Tracing.getTracer();
36 
MultiSpansContextTracing()37   private MultiSpansContextTracing() {}
38 
doSomeOtherWork()39   private static void doSomeOtherWork() {
40     tracer.getCurrentSpan().addAnnotation("Annotation to the child Span");
41   }
42 
doSomeMoreWork()43   private static void doSomeMoreWork() {
44     // Create a child Span of the current Span.
45     Span span = tracer.spanBuilder("MyChildSpan").startSpan();
46     try (Scope ws = tracer.withSpan(span)) {
47       doSomeOtherWork();
48     }
49     span.end();
50   }
51 
doWork()52   private static void doWork() {
53     tracer.getCurrentSpan().addAnnotation("Annotation to the root Span before child is created.");
54     doSomeMoreWork();
55     tracer.getCurrentSpan().addAnnotation("Annotation to the root Span after child is ended.");
56   }
57 
58   /**
59    * Main method.
60    *
61    * @param args the main arguments.
62    */
main(String[] args)63   public static void main(String[] args) {
64 
65     // WARNING: Be careful before you set sampler value to always sample, especially in
66     // production environment. Trace data is often very large in size and is expensive to
67     // collect. This is why rather than collecting traces for every request(i.e. alwaysSample),
68     // downsampling is prefered.
69     //
70     // By default, OpenCensus provides a probabilistic sampler that will trace once in every
71     // 10,000 requests, that's why if default probabilistic sampler is used
72     // you might not see trace data printed or exported and this is expected behavior.
73 
74     TraceConfig traceConfig = Tracing.getTraceConfig();
75     traceConfig.updateActiveTraceParams(
76         traceConfig.getActiveTraceParams().toBuilder().setSampler(Samplers.alwaysSample()).build());
77 
78     LoggingTraceExporter.register();
79     Span span = tracer.spanBuilderWithExplicitParent("MyRootSpan", null).startSpan();
80     try (Scope ws = tracer.withSpan(span)) {
81       doWork();
82     }
83     span.end();
84 
85     // Wait for a duration longer than reporting duration (5s) to ensure spans are exported.
86     // Spans are exported every 5 seconds
87     sleep(5100);
88   }
89 }
90