• 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.exporter.trace.logging.LoggingTraceExporter;
22 import io.opencensus.trace.Span;
23 import io.opencensus.trace.Tracer;
24 import io.opencensus.trace.Tracing;
25 import io.opencensus.trace.config.TraceConfig;
26 import io.opencensus.trace.samplers.Samplers;
27 
28 /** Example showing how to directly create a child {@link Span} and add annotations. */
29 public final class MultiSpansTracing {
30   // Per class Tracer.
31   private static final Tracer tracer = Tracing.getTracer();
32 
MultiSpansTracing()33   private MultiSpansTracing() {}
34 
doWork()35   private static void doWork() {
36     Span rootSpan = tracer.spanBuilderWithExplicitParent("MyRootSpan", null).startSpan();
37     rootSpan.addAnnotation("Annotation to the root Span before child is created.");
38     Span childSpan = tracer.spanBuilderWithExplicitParent("MyChildSpan", rootSpan).startSpan();
39     childSpan.addAnnotation("Annotation to the child Span");
40     childSpan.end();
41     rootSpan.addAnnotation("Annotation to the root Span after child is ended.");
42     rootSpan.end();
43   }
44 
45   /**
46    * Main method.
47    *
48    * @param args the main arguments.
49    */
main(String[] args)50   public static void main(String[] args) {
51 
52     // WARNING: Be careful before you set sampler value to always sample, especially in
53     // production environment. Trace data is often very large in size and is expensive to
54     // collect. This is why rather than collecting traces for every request(i.e. alwaysSample),
55     // downsampling is prefered.
56     //
57     // By default, OpenCensus provides a probabilistic sampler that will trace once in every
58     // 10,000 requests, that's why if default probabilistic sampler is used
59     // you might not see trace data printed or exported and this is expected behavior.
60 
61     TraceConfig traceConfig = Tracing.getTraceConfig();
62     traceConfig.updateActiveTraceParams(
63         traceConfig.getActiveTraceParams().toBuilder().setSampler(Samplers.alwaysSample()).build());
64 
65     LoggingTraceExporter.register();
66     doWork();
67 
68     // Wait for a duration longer than reporting duration (5s) to ensure spans are exported.
69     // Spans are exported every 5 seconds
70     sleep(5100);
71   }
72 }
73