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.implcore.trace;
18 
19 import io.opencensus.common.Clock;
20 import io.opencensus.implcore.internal.EventQueue;
21 import io.opencensus.implcore.internal.SimpleEventQueue;
22 import io.opencensus.implcore.trace.RecordEventsSpanImpl.StartEndHandler;
23 import io.opencensus.implcore.trace.config.TraceConfigImpl;
24 import io.opencensus.implcore.trace.export.ExportComponentImpl;
25 import io.opencensus.implcore.trace.internal.RandomHandler;
26 import io.opencensus.implcore.trace.propagation.PropagationComponentImpl;
27 import io.opencensus.trace.TraceComponent;
28 import io.opencensus.trace.Tracer;
29 import io.opencensus.trace.config.TraceConfig;
30 import io.opencensus.trace.export.ExportComponent;
31 import io.opencensus.trace.propagation.PropagationComponent;
32 
33 /**
34  * Helper class to allow sharing the code for all the {@link TraceComponent} implementations. This
35  * class cannot use inheritance because in version 0.5.* the constructor of the {@code
36  * TraceComponent} is package protected.
37  *
38  * <p>This can be changed back to inheritance when version 0.5.* is no longer supported.
39  */
40 public final class TraceComponentImplBase {
41   private final ExportComponentImpl exportComponent;
42   private final PropagationComponent propagationComponent = new PropagationComponentImpl();
43   private final Clock clock;
44   private final TraceConfig traceConfig = new TraceConfigImpl();
45   private final Tracer tracer;
46 
47   /**
48    * Creates a new {@code TraceComponentImplBase}.
49    *
50    * @param clock the clock to use throughout tracing.
51    * @param randomHandler the random number generator for generating trace and span IDs.
52    * @param eventQueue the queue implementation.
53    */
TraceComponentImplBase(Clock clock, RandomHandler randomHandler, EventQueue eventQueue)54   public TraceComponentImplBase(Clock clock, RandomHandler randomHandler, EventQueue eventQueue) {
55     this.clock = clock;
56     // TODO(bdrutu): Add a config/argument for supportInProcessStores.
57     if (eventQueue instanceof SimpleEventQueue) {
58       exportComponent = ExportComponentImpl.createWithoutInProcessStores(eventQueue);
59     } else {
60       exportComponent = ExportComponentImpl.createWithInProcessStores(eventQueue);
61     }
62     StartEndHandler startEndHandler =
63         new StartEndHandlerImpl(
64             exportComponent.getSpanExporter(),
65             exportComponent.getRunningSpanStore(),
66             exportComponent.getSampledSpanStore(),
67             eventQueue);
68     tracer = new TracerImpl(randomHandler, startEndHandler, clock, traceConfig);
69   }
70 
getTracer()71   public Tracer getTracer() {
72     return tracer;
73   }
74 
getPropagationComponent()75   public PropagationComponent getPropagationComponent() {
76     return propagationComponent;
77   }
78 
getClock()79   public final Clock getClock() {
80     return clock;
81   }
82 
getExportComponent()83   public ExportComponent getExportComponent() {
84     return exportComponent;
85   }
86 
getTraceConfig()87   public TraceConfig getTraceConfig() {
88     return traceConfig;
89   }
90 }
91