• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016-17, 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.trace;
18 
19 import io.opencensus.common.Clock;
20 import io.opencensus.internal.ZeroTimeClock;
21 import io.opencensus.trace.config.TraceConfig;
22 import io.opencensus.trace.export.ExportComponent;
23 import io.opencensus.trace.propagation.PropagationComponent;
24 
25 /**
26  * Class that holds the implementation instances for {@link Tracer}, {@link PropagationComponent},
27  * {@link Clock}, {@link ExportComponent} and {@link TraceConfig}.
28  *
29  * <p>Unless otherwise noted all methods (on component) results are cacheable.
30  *
31  * @since 0.5
32  */
33 public abstract class TraceComponent {
34 
35   /**
36    * Returns the {@link Tracer} with the provided implementations. If no implementation is provided
37    * then no-op implementations will be used.
38    *
39    * @return the {@code Tracer} implementation.
40    * @since 0.5
41    */
getTracer()42   public abstract Tracer getTracer();
43 
44   /**
45    * Returns the {@link PropagationComponent} with the provided implementation. If no implementation
46    * is provided then no-op implementation will be used.
47    *
48    * @return the {@code PropagationComponent} implementation.
49    * @since 0.5
50    */
getPropagationComponent()51   public abstract PropagationComponent getPropagationComponent();
52 
53   /**
54    * Returns the {@link Clock} with the provided implementation.
55    *
56    * @return the {@code Clock} implementation.
57    * @since 0.5
58    */
getClock()59   public abstract Clock getClock();
60 
61   /**
62    * Returns the {@link ExportComponent} with the provided implementation. If no implementation is
63    * provided then no-op implementations will be used.
64    *
65    * @return the {@link ExportComponent} implementation.
66    * @since 0.5
67    */
getExportComponent()68   public abstract ExportComponent getExportComponent();
69 
70   /**
71    * Returns the {@link TraceConfig} with the provided implementation. If no implementation is
72    * provided then no-op implementations will be used.
73    *
74    * @return the {@link TraceConfig} implementation.
75    * @since 0.5
76    */
getTraceConfig()77   public abstract TraceConfig getTraceConfig();
78 
79   /**
80    * Returns an instance that contains no-op implementations for all the instances.
81    *
82    * @return an instance that contains no-op implementations for all the instances.
83    */
newNoopTraceComponent()84   static TraceComponent newNoopTraceComponent() {
85     return new NoopTraceComponent();
86   }
87 
88   private static final class NoopTraceComponent extends TraceComponent {
89     private final ExportComponent noopExportComponent = ExportComponent.newNoopExportComponent();
90 
91     @Override
getTracer()92     public Tracer getTracer() {
93       return Tracer.getNoopTracer();
94     }
95 
96     @Override
getPropagationComponent()97     public PropagationComponent getPropagationComponent() {
98       return PropagationComponent.getNoopPropagationComponent();
99     }
100 
101     @Override
getClock()102     public Clock getClock() {
103       return ZeroTimeClock.getInstance();
104     }
105 
106     @Override
getExportComponent()107     public ExportComponent getExportComponent() {
108       return noopExportComponent;
109     }
110 
111     @Override
getTraceConfig()112     public TraceConfig getTraceConfig() {
113       return TraceConfig.getNoopTraceConfig();
114     }
115 
NoopTraceComponent()116     private NoopTraceComponent() {}
117   }
118 }
119