Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit c93f59c

Browse files
authored
Exporter/OcAgent: Make required fields non-null. (#1895)
* Exporter/OcAgent: Make required fields non-null. * Add Javadoc for default values.
1 parent b1558ec commit c93f59c

File tree

10 files changed

+102
-82
lines changed

10 files changed

+102
-82
lines changed

exporters/metrics/ocagent/src/main/java/io/opencensus/exporter/metrics/ocagent/OcAgentMetricsExporter.java

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121
import static com.google.common.base.Preconditions.checkState;
2222

23-
import com.google.common.annotations.VisibleForTesting;
2423
import io.netty.handler.ssl.SslContext;
2524
import io.opencensus.common.Duration;
2625
import io.opencensus.metrics.Metrics;
@@ -47,12 +46,6 @@
4746
@ThreadSafe
4847
public final class OcAgentMetricsExporter {
4948

50-
@VisibleForTesting static final String DEFAULT_END_POINT = "localhost:55678";
51-
private static final String DEFAULT_SERVICE_NAME = "OpenCensus";
52-
private static final Duration DEFAULT_RETRY_INTERVAL = Duration.create(300, 0); // 5 minutes
53-
private static final Duration DEFAULT_EXPORT_INTERVAL = Duration.create(60, 0); // 1 minute
54-
private static final Duration ZERO = Duration.create(0, 0);
55-
5649
private static final Object monitor = new Object();
5750

5851
@GuardedBy("monitor")
@@ -80,29 +73,14 @@ public static void createAndRegister(OcAgentMetricsExporterConfiguration configu
8073
}
8174

8275
private static void createInternal(
83-
@Nullable String endPoint,
84-
@Nullable Boolean useInsecure,
76+
String endPoint,
77+
boolean useInsecure,
8578
@Nullable SslContext sslContext,
86-
@Nullable String serviceName,
87-
@Nullable Duration exportInterval,
88-
@Nullable Duration retryInterval) {
89-
if (endPoint == null || endPoint.isEmpty()) {
90-
endPoint = DEFAULT_END_POINT;
91-
}
92-
if (useInsecure == null) {
93-
useInsecure = false;
94-
}
79+
String serviceName,
80+
Duration exportInterval,
81+
Duration retryInterval) {
9582
checkArgument(
9683
useInsecure == (sslContext == null), "Either use insecure or provide a valid SslContext.");
97-
if (serviceName == null || serviceName.isEmpty()) {
98-
serviceName = DEFAULT_SERVICE_NAME;
99-
}
100-
if (exportInterval == null) {
101-
exportInterval = DEFAULT_EXPORT_INTERVAL;
102-
}
103-
if (retryInterval == null) {
104-
retryInterval = DEFAULT_RETRY_INTERVAL;
105-
}
10684
synchronized (monitor) {
10785
checkState(exporter == null, "OcAgent Metrics exporter is already created.");
10886
exporter =
@@ -126,8 +104,6 @@ private OcAgentMetricsExporter(
126104
Duration exportInterval,
127105
Duration retryInterval,
128106
MetricProducerManager metricProducerManager) {
129-
checkArgument(exportInterval.compareTo(ZERO) > 0, "Duration must be positive");
130-
checkArgument(retryInterval.compareTo(ZERO) > 0, "Duration must be positive");
131107
OcAgentMetricsExporterWorker worker =
132108
new OcAgentMetricsExporterWorker(
133109
endPoint,

exporters/metrics/ocagent/src/main/java/io/opencensus/exporter/metrics/ocagent/OcAgentMetricsExporterConfiguration.java

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package io.opencensus.exporter.metrics.ocagent;
1818

1919
import com.google.auto.value.AutoValue;
20+
import com.google.common.annotations.VisibleForTesting;
21+
import com.google.common.base.Preconditions;
2022
import io.netty.handler.ssl.SslContext;
2123
import io.opencensus.common.Duration;
2224
import javax.annotation.Nullable;
@@ -31,29 +33,39 @@
3133
@Immutable
3234
public abstract class OcAgentMetricsExporterConfiguration {
3335

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_EXPORT_INTERVAL = Duration.create(60, 0);
40+
@VisibleForTesting static final Duration ZERO = Duration.create(0, 0);
41+
3442
OcAgentMetricsExporterConfiguration() {}
3543

3644
/**
3745
* Returns the end point of OC-Agent. The end point can be dns, ip:port, etc.
3846
*
47+
* <p>Default value is "localhost:55678" if not set.
48+
*
3949
* @return the end point of OC-Agent.
4050
* @since 0.20
4151
*/
42-
@Nullable
4352
public abstract String getEndPoint();
4453

4554
/**
4655
* Returns whether to disable client transport security for the exporter's gRPC connection or not.
4756
*
57+
* <p>Default value is true if not set.
58+
*
4859
* @return whether to disable client transport security for the exporter's gRPC connection or not.
4960
* @since 0.20
5061
*/
51-
@Nullable
5262
public abstract Boolean getUseInsecure();
5363

5464
/**
5565
* Returns the {@link SslContext} for secure TLS gRPC connection.
5666
*
67+
* <p>If not set OcAgent exporter will use insecure connection by default.
68+
*
5769
* @return the {@code SslContext}.
5870
* @since 0.20
5971
*/
@@ -63,28 +75,31 @@ public abstract class OcAgentMetricsExporterConfiguration {
6375
/**
6476
* Returns the service name to be used for the {@code OcAgentMetricsExporter}.
6577
*
78+
* <p>Default value is "OpenCensus" if not set.
79+
*
6680
* @return the service name.
6781
* @since 0.20
6882
*/
69-
@Nullable
7083
public abstract String getServiceName();
7184

7285
/**
7386
* Returns the retry time interval when trying to connect to Agent.
7487
*
88+
* <p>Default value is 5 minutes.
89+
*
7590
* @return the retry time interval.
7691
* @since 0.20
7792
*/
78-
@Nullable
7993
public abstract Duration getRetryInterval();
8094

8195
/**
8296
* Returns the export interval between pushes to Agent.
8397
*
98+
* <p>Default value is 1 minute.
99+
*
84100
* @return the export interval.
85101
* @since 0.20
86102
*/
87-
@Nullable
88103
public abstract Duration getExportInterval();
89104

90105
/**
@@ -94,7 +109,12 @@ public abstract class OcAgentMetricsExporterConfiguration {
94109
* @since 0.20
95110
*/
96111
public static Builder builder() {
97-
return new AutoValue_OcAgentMetricsExporterConfiguration.Builder().setUseInsecure(true);
112+
return new AutoValue_OcAgentMetricsExporterConfiguration.Builder()
113+
.setEndPoint(DEFAULT_END_POINT)
114+
.setServiceName(DEFAULT_SERVICE_NAME)
115+
.setRetryInterval(DEFAULT_RETRY_INTERVAL)
116+
.setExportInterval(DEFAULT_EXPORT_INTERVAL)
117+
.setUseInsecure(true);
98118
}
99119

100120
/**
@@ -164,12 +184,24 @@ public abstract static class Builder {
164184

165185
// TODO(songya): add an option that controls whether to always keep the RPC connection alive.
166186

187+
abstract Duration getRetryInterval();
188+
189+
abstract Duration getExportInterval();
190+
191+
abstract OcAgentMetricsExporterConfiguration autoBuild();
192+
167193
/**
168194
* Builds a {@link OcAgentMetricsExporterConfiguration}.
169195
*
170196
* @return a {@code OcAgentMetricsExporterConfiguration}.
171197
* @since 0.20
172198
*/
173-
public abstract OcAgentMetricsExporterConfiguration build();
199+
public OcAgentMetricsExporterConfiguration build() {
200+
Preconditions.checkArgument(
201+
getRetryInterval().compareTo(ZERO) > 0, "Retry interval must be positive.");
202+
Preconditions.checkArgument(
203+
getExportInterval().compareTo(ZERO) > 0, "Export interval must be positive.");
204+
return autoBuild();
205+
}
174206
}
175207
}

exporters/metrics/ocagent/src/test/java/io/opencensus/exporter/metrics/ocagent/OcAgentMetricsExporterConfigurationTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ public class OcAgentMetricsExporterConfigurationTest {
3434
public void defaultConfiguration() {
3535
OcAgentMetricsExporterConfiguration configuration =
3636
OcAgentMetricsExporterConfiguration.builder().build();
37-
assertThat(configuration.getEndPoint()).isNull();
38-
assertThat(configuration.getServiceName()).isNull();
37+
assertThat(configuration.getEndPoint())
38+
.isEqualTo(OcAgentMetricsExporterConfiguration.DEFAULT_END_POINT);
39+
assertThat(configuration.getServiceName())
40+
.isEqualTo(OcAgentMetricsExporterConfiguration.DEFAULT_SERVICE_NAME);
3941
assertThat(configuration.getUseInsecure()).isTrue();
4042
assertThat(configuration.getSslContext()).isNull();
41-
assertThat(configuration.getRetryInterval()).isNull();
42-
assertThat(configuration.getExportInterval()).isNull();
43+
assertThat(configuration.getRetryInterval())
44+
.isEqualTo(OcAgentMetricsExporterConfiguration.DEFAULT_RETRY_INTERVAL);
45+
assertThat(configuration.getExportInterval())
46+
.isEqualTo(OcAgentMetricsExporterConfiguration.DEFAULT_EXPORT_INTERVAL);
4347
}
4448

4549
@Test

exporters/metrics/ocagent/src/test/java/io/opencensus/exporter/metrics/ocagent/OcAgentMetricsExporterIntegrationTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ public class OcAgentMetricsExporterIntegrationTest {
181181
@Before
182182
public void setUp() {
183183
fakeOcAgentMetricsServiceGrpc = new FakeOcAgentMetricsServiceGrpcImpl();
184-
agent = getServer(OcAgentMetricsExporter.DEFAULT_END_POINT, fakeOcAgentMetricsServiceGrpc);
184+
agent =
185+
getServer(
186+
OcAgentMetricsExporterConfiguration.DEFAULT_END_POINT, fakeOcAgentMetricsServiceGrpc);
185187
}
186188

187189
@After

exporters/trace/ocagent/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ dependencies {
99
compileOnly libraries.auto_value
1010

1111
compile project(':opencensus-api'),
12-
project(':opencensus-contrib-resource-util')
12+
project(':opencensus-contrib-resource-util'),
13+
project(':opencensus-exporter-trace-util')
1314

1415
compile (libraries.grpc_core) {
1516
// We will always be more up to date.

exporters/trace/ocagent/src/main/java/io/opencensus/exporter/trace/ocagent/OcAgentTraceExporter.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,7 @@ private OcAgentTraceExporter() {}
5959
* @since 0.20
6060
*/
6161
public static void createAndRegister() {
62-
synchronized (monitor) {
63-
checkState(handler == null, "OC-Agent exporter is already registered.");
64-
OcAgentTraceExporterHandler newHandler = new OcAgentTraceExporterHandler();
65-
registerInternal(newHandler);
66-
}
62+
createAndRegister(OcAgentTraceExporterConfiguration.builder().build());
6763
}
6864

6965
/**

exporters/trace/ocagent/src/main/java/io/opencensus/exporter/trace/ocagent/OcAgentTraceExporterConfiguration.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package io.opencensus.exporter.trace.ocagent;
1818

1919
import com.google.auto.value.AutoValue;
20+
import com.google.common.annotations.VisibleForTesting;
21+
import com.google.common.base.Preconditions;
2022
import io.netty.handler.ssl.SslContext;
2123
import io.opencensus.common.Duration;
2224
import javax.annotation.Nullable;
@@ -31,29 +33,38 @@
3133
@Immutable
3234
public abstract class OcAgentTraceExporterConfiguration {
3335

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 ZERO = Duration.create(0, 0);
40+
3441
OcAgentTraceExporterConfiguration() {}
3542

3643
/**
3744
* Returns the end point of OC-Agent. The end point can be dns, ip:port, etc.
3845
*
46+
* <p>Default value is "localhost:55678" if not set.
47+
*
3948
* @return the end point of OC-Agent.
4049
* @since 0.20
4150
*/
42-
@Nullable
4351
public abstract String getEndPoint();
4452

4553
/**
4654
* Returns whether to disable client transport security for the exporter's gRPC connection or not.
4755
*
56+
* <p>Default value is true if not set.
57+
*
4858
* @return whether to disable client transport security for the exporter's gRPC connection or not.
4959
* @since 0.20
5060
*/
51-
@Nullable
5261
public abstract Boolean getUseInsecure();
5362

5463
/**
5564
* Returns the {@link SslContext} for secure TLS gRPC connection.
5665
*
66+
* <p>If not set OcAgent exporter will use insecure connection by default.
67+
*
5768
* @return the {@code SslContext}.
5869
* @since 0.20
5970
*/
@@ -63,24 +74,28 @@ public abstract class OcAgentTraceExporterConfiguration {
6374
/**
6475
* Returns the service name to be used for this {@link OcAgentTraceExporter}.
6576
*
77+
* <p>Default value is "OpenCensus" if not set.
78+
*
6679
* @return the service name.
6780
* @since 0.20
6881
*/
69-
@Nullable
7082
public abstract String getServiceName();
7183

7284
/**
7385
* Returns the retry time interval when trying to connect to Agent.
7486
*
87+
* <p>Default value is 5 minutes.
88+
*
7589
* @return the retry time interval.
7690
* @since 0.20
7791
*/
78-
@Nullable
7992
public abstract Duration getRetryInterval();
8093

8194
/**
8295
* Returns whether the {@link OcAgentTraceExporter} should handle the config streams.
8396
*
97+
* <p>Config service is enabled by default.
98+
*
8499
* @return whether the {@code OcAgentTraceExporter} should handle the config streams.
85100
* @since 0.20
86101
*/
@@ -94,8 +109,11 @@ public abstract class OcAgentTraceExporterConfiguration {
94109
*/
95110
public static Builder builder() {
96111
return new AutoValue_OcAgentTraceExporterConfiguration.Builder()
112+
.setEndPoint(DEFAULT_END_POINT)
113+
.setServiceName(DEFAULT_SERVICE_NAME)
97114
.setEnableConfig(true)
98-
.setUseInsecure(true);
115+
.setUseInsecure(true)
116+
.setRetryInterval(DEFAULT_RETRY_INTERVAL);
99117
}
100118

101119
/**
@@ -165,12 +183,20 @@ public abstract static class Builder {
165183

166184
// TODO(songya): add an option that controls whether to always keep the RPC connection alive.
167185

186+
abstract Duration getRetryInterval();
187+
188+
abstract OcAgentTraceExporterConfiguration autoBuild();
189+
168190
/**
169191
* Builds a {@link OcAgentTraceExporterConfiguration}.
170192
*
171193
* @return a {@code OcAgentTraceExporterConfiguration}.
172194
* @since 0.20
173195
*/
174-
public abstract OcAgentTraceExporterConfiguration build();
196+
public OcAgentTraceExporterConfiguration build() {
197+
Preconditions.checkArgument(
198+
getRetryInterval().compareTo(ZERO) > 0, "Retry interval must be positive.");
199+
return autoBuild();
200+
}
175201
}
176202
}

0 commit comments

Comments
 (0)