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.export;
18 
19 import io.opencensus.common.Duration;
20 import io.opencensus.implcore.internal.EventQueue;
21 import io.opencensus.trace.export.ExportComponent;
22 import io.opencensus.trace.export.RunningSpanStore;
23 import io.opencensus.trace.export.SampledSpanStore;
24 
25 /** Implementation of the {@link ExportComponent}. */
26 public final class ExportComponentImpl extends ExportComponent {
27   private static final int EXPORTER_BUFFER_SIZE = 32;
28   // Enforces that trace export exports data at least once every 5 seconds.
29   private static final Duration EXPORTER_SCHEDULE_DELAY = Duration.create(5, 0);
30 
31   private final SpanExporterImpl spanExporter;
32   private final RunningSpanStoreImpl runningSpanStore;
33   private final SampledSpanStoreImpl sampledSpanStore;
34 
35   @Override
getSpanExporter()36   public SpanExporterImpl getSpanExporter() {
37     return spanExporter;
38   }
39 
40   @Override
getRunningSpanStore()41   public RunningSpanStoreImpl getRunningSpanStore() {
42     return runningSpanStore;
43   }
44 
45   @Override
getSampledSpanStore()46   public SampledSpanStoreImpl getSampledSpanStore() {
47     return sampledSpanStore;
48   }
49 
50   @Override
shutdown()51   public void shutdown() {
52     sampledSpanStore.shutdown();
53     spanExporter.shutdown();
54   }
55 
56   /**
57    * Returns a new {@code ExportComponentImpl} that has valid instances for {@link RunningSpanStore}
58    * and {@link SampledSpanStore}.
59    *
60    * @return a new {@code ExportComponentImpl}.
61    */
createWithInProcessStores(EventQueue eventQueue)62   public static ExportComponentImpl createWithInProcessStores(EventQueue eventQueue) {
63     return new ExportComponentImpl(true, eventQueue);
64   }
65 
66   /**
67    * Returns a new {@code ExportComponentImpl} that has {@code null} instances for {@link
68    * RunningSpanStore} and {@link SampledSpanStore}.
69    *
70    * @return a new {@code ExportComponentImpl}.
71    */
createWithoutInProcessStores(EventQueue eventQueue)72   public static ExportComponentImpl createWithoutInProcessStores(EventQueue eventQueue) {
73     return new ExportComponentImpl(false, eventQueue);
74   }
75 
76   /**
77    * Constructs a new {@code ExportComponentImpl}.
78    *
79    * @param supportInProcessStores {@code true} to instantiate {@link RunningSpanStore} and {@link
80    *     SampledSpanStore}.
81    */
ExportComponentImpl(boolean supportInProcessStores, EventQueue eventQueue)82   private ExportComponentImpl(boolean supportInProcessStores, EventQueue eventQueue) {
83     this.spanExporter = SpanExporterImpl.create(EXPORTER_BUFFER_SIZE, EXPORTER_SCHEDULE_DELAY);
84     this.runningSpanStore =
85         supportInProcessStores
86             ? new InProcessRunningSpanStoreImpl()
87             : RunningSpanStoreImpl.getNoopRunningSpanStoreImpl();
88     this.sampledSpanStore =
89         supportInProcessStores
90             ? new InProcessSampledSpanStoreImpl(eventQueue)
91             : SampledSpanStoreImpl.getNoopSampledSpanStoreImpl();
92   }
93 }
94