• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018, 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.exporter.trace.ocagent;
18 
19 import com.google.auto.value.AutoValue;
20 import com.google.common.annotations.VisibleForTesting;
21 import com.google.common.base.Preconditions;
22 import io.netty.handler.ssl.SslContext;
23 import io.opencensus.common.Duration;
24 import javax.annotation.Nullable;
25 import javax.annotation.concurrent.Immutable;
26 
27 /**
28  * Configurations for {@link OcAgentTraceExporter}.
29  *
30  * @since 0.20
31  */
32 @AutoValue
33 @Immutable
34 public abstract class OcAgentTraceExporterConfiguration {
35 
36   @VisibleForTesting static final String DEFAULT_END_POINT = "localhost:55678";
37   @VisibleForTesting static final String DEFAULT_SERVICE_NAME = "OpenCensus";
38   @VisibleForTesting static final Duration DEFAULT_RETRY_INTERVAL = Duration.create(300, 0);
39   @VisibleForTesting static final Duration DEFAULT_DEADLINE = Duration.create(10, 0);
40   @VisibleForTesting static final Duration ZERO = Duration.create(0, 0);
41 
OcAgentTraceExporterConfiguration()42   OcAgentTraceExporterConfiguration() {}
43 
44   /**
45    * Returns the end point of OC-Agent. The end point can be dns, ip:port, etc.
46    *
47    * <p>Default value is "localhost:55678" if not set.
48    *
49    * @return the end point of OC-Agent.
50    * @since 0.20
51    */
getEndPoint()52   public abstract String getEndPoint();
53 
54   /**
55    * Returns whether to disable client transport security for the exporter's gRPC connection or not.
56    *
57    * <p>Default value is true if not set.
58    *
59    * @return whether to disable client transport security for the exporter's gRPC connection or not.
60    * @since 0.20
61    */
getUseInsecure()62   public abstract Boolean getUseInsecure();
63 
64   /**
65    * Returns the {@link SslContext} for secure TLS gRPC connection.
66    *
67    * <p>If not set OcAgent exporter will use insecure connection by default.
68    *
69    * @return the {@code SslContext}.
70    * @since 0.20
71    */
72   @Nullable
getSslContext()73   public abstract SslContext getSslContext();
74 
75   /**
76    * Returns the service name to be used for this {@link OcAgentTraceExporter}.
77    *
78    * <p>Default value is "OpenCensus" if not set.
79    *
80    * @return the service name.
81    * @since 0.20
82    */
getServiceName()83   public abstract String getServiceName();
84 
85   /**
86    * Returns the retry time interval when trying to connect to Agent.
87    *
88    * <p>Default value is 5 minutes.
89    *
90    * @return the retry time interval.
91    * @since 0.20
92    */
getRetryInterval()93   public abstract Duration getRetryInterval();
94 
95   /**
96    * Returns whether the {@link OcAgentTraceExporter} should handle the config streams.
97    *
98    * <p>Config service is enabled by default.
99    *
100    * @return whether the {@code OcAgentTraceExporter} should handle the config streams.
101    * @since 0.20
102    */
getEnableConfig()103   public abstract boolean getEnableConfig();
104 
105   /**
106    * Returns the deadline for exporting to Agent/Collector.
107    *
108    * <p>Default value is 10 seconds.
109    *
110    * @return the export deadline.
111    * @since 0.22
112    */
getDeadline()113   public abstract Duration getDeadline();
114 
115   /**
116    * Returns a new {@link Builder}.
117    *
118    * @return a {@code Builder}.
119    * @since 0.20
120    */
builder()121   public static Builder builder() {
122     return new AutoValue_OcAgentTraceExporterConfiguration.Builder()
123         .setEndPoint(DEFAULT_END_POINT)
124         .setServiceName(DEFAULT_SERVICE_NAME)
125         .setEnableConfig(true)
126         .setUseInsecure(true)
127         .setRetryInterval(DEFAULT_RETRY_INTERVAL)
128         .setDeadline(DEFAULT_DEADLINE);
129   }
130 
131   /**
132    * Builder for {@link OcAgentTraceExporterConfiguration}.
133    *
134    * @since 0.20
135    */
136   @AutoValue.Builder
137   public abstract static class Builder {
138 
Builder()139     Builder() {}
140 
141     /**
142      * Sets the end point of OC-Agent server.
143      *
144      * @param endPoint the end point of OC-Agent.
145      * @return this.
146      * @since 0.20
147      */
setEndPoint(String endPoint)148     public abstract Builder setEndPoint(String endPoint);
149 
150     /**
151      * Sets whether to disable client transport security for the exporter's gRPC connection or not.
152      *
153      * @param useInsecure whether disable client transport security for the exporter's gRPC
154      *     connection.
155      * @return this.
156      * @since 0.20
157      */
setUseInsecure(Boolean useInsecure)158     public abstract Builder setUseInsecure(Boolean useInsecure);
159 
160     /**
161      * Sets the {@link SslContext} for secure TLS gRPC connection.
162      *
163      * @param sslContext the {@code SslContext}.
164      * @return this.
165      * @since 0.20
166      */
setSslContext(SslContext sslContext)167     public abstract Builder setSslContext(SslContext sslContext);
168 
169     /**
170      * Sets the service name to be used for this {@link OcAgentTraceExporter}.
171      *
172      * @param serviceName the service name.
173      * @return this.
174      * @since 0.20
175      */
setServiceName(String serviceName)176     public abstract Builder setServiceName(String serviceName);
177 
178     /**
179      * Sets the retry time interval when trying to connect to Agent.
180      *
181      * @param retryInterval the retry time interval.
182      * @return this.
183      * @since 0.20
184      */
setRetryInterval(Duration retryInterval)185     public abstract Builder setRetryInterval(Duration retryInterval);
186 
187     /**
188      * Sets whether {@link OcAgentTraceExporter} should handle the config streams.
189      *
190      * @param enableConfig whether {@code OcAgentTraceExporter} should handle the config streams.
191      * @return this.
192      * @since 0.20
193      */
setEnableConfig(boolean enableConfig)194     public abstract Builder setEnableConfig(boolean enableConfig);
195 
196     /**
197      * Sets the deadline for exporting to Agent/Collector.
198      *
199      * @param deadline the export deadline.
200      * @return this
201      * @since 0.22
202      */
setDeadline(Duration deadline)203     public abstract Builder setDeadline(Duration deadline);
204 
205     // TODO(songya): add an option that controls whether to always keep the RPC connection alive.
206 
getRetryInterval()207     abstract Duration getRetryInterval();
208 
autoBuild()209     abstract OcAgentTraceExporterConfiguration autoBuild();
210 
getDeadline()211     abstract Duration getDeadline();
212 
213     /**
214      * Builds a {@link OcAgentTraceExporterConfiguration}.
215      *
216      * @return a {@code OcAgentTraceExporterConfiguration}.
217      * @since 0.20
218      */
build()219     public OcAgentTraceExporterConfiguration build() {
220       Preconditions.checkArgument(getDeadline().compareTo(ZERO) > 0, "Deadline must be positive.");
221       Preconditions.checkArgument(
222           getRetryInterval().compareTo(ZERO) > 0, "Retry interval must be positive.");
223       return autoBuild();
224     }
225   }
226 }
227