• 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} using scoped Spans, install it in the current
31  * context, and add annotations.
32  */
33 public final class MultiSpansScopedTracing {
34   // Per class Tracer.
35   private static final Tracer tracer = Tracing.getTracer();
36 
MultiSpansScopedTracing()37   private MultiSpansScopedTracing() {}
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     try (Scope ss = tracer.spanBuilder("MyChildSpan").startScopedSpan()) {
46       doSomeOtherWork();
47     }
48   }
49 
doWork()50   private static void doWork() {
51     tracer.getCurrentSpan().addAnnotation("Annotation to the root Span before child is created.");
52     doSomeMoreWork();
53     tracer.getCurrentSpan().addAnnotation("Annotation to the root Span after child is ended.");
54   }
55 
56   /**
57    * Main method.
58    *
59    * @param args the main arguments.
60    */
main(String[] args)61   public static void main(String[] args) {
62 
63     // WARNING: Be careful before you set sampler value to always sample, especially in
64     // production environment. Trace data is often very large in size and is expensive to
65     // collect. This is why rather than collecting traces for every request(i.e. alwaysSample),
66     // downsampling is prefered.
67     //
68     // By default, OpenCensus provides a probabilistic sampler that will trace once in every
69     // 10,000 requests, that's why if default probabilistic sampler is used
70     // you might not see trace data printed or exported and this is expected behavior.
71 
72     TraceConfig traceConfig = Tracing.getTraceConfig();
73     traceConfig.updateActiveTraceParams(
74         traceConfig.getActiveTraceParams().toBuilder().setSampler(Samplers.alwaysSample()).build());
75 
76     LoggingTraceExporter.register();
77     try (Scope ss = tracer.spanBuilderWithExplicitParent("MyRootSpan", null).startScopedSpan()) {
78       doWork();
79     }
80 
81     // Wait for a duration longer than reporting duration (5s) to ensure spans are exported.
82     // Spans are exported every 5 seconds
83     sleep(5100);
84   }
85 }
86