From dab6d6f61d4246610a3b10c9c37dbf48bbb96139 Mon Sep 17 00:00:00 2001 From: Yang Song Date: Tue, 14 May 2019 09:50:11 -0700 Subject: [PATCH] Exporter/OcAgent: Add deadline option. (#1899) --- CHANGELOG.md | 2 +- .../trace/ocagent/OcAgentTraceExporter.java | 3 ++- .../OcAgentTraceExporterConfiguration.java | 26 ++++++++++++++++++- .../ocagent/OcAgentTraceExporterHandler.java | 11 +++++--- ...OcAgentTraceExporterConfigurationTest.java | 4 +++ 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30a96eae0c..7c93fa09fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ Also provide a `MetricServiceStub` option so that advanced users can use a custo Monitoring client to make RPCs. - Use `Configuration` builder pattern for creating `JaegerTraceExporter`, `ZipkinTraceExporter` and `InstanaTraceExporter`. Provide a `Deadline` option with default value 10 seconds. -- Provide a `Deadline` option to Datadog and Elasticsearch exporter. Default value is 10 seconds. +- Provide a `Deadline` option to Datadog, Elasticsearch and OcAgent exporter. Default value is 10 seconds. - Extract the common timeout logic of Trace exporters to `opencensus-exporter-trace-util`. ## 0.21.0 - 2019-04-30 diff --git a/exporters/trace/ocagent/src/main/java/io/opencensus/exporter/trace/ocagent/OcAgentTraceExporter.java b/exporters/trace/ocagent/src/main/java/io/opencensus/exporter/trace/ocagent/OcAgentTraceExporter.java index 27c8da0ddb..d921ee0152 100644 --- a/exporters/trace/ocagent/src/main/java/io/opencensus/exporter/trace/ocagent/OcAgentTraceExporter.java +++ b/exporters/trace/ocagent/src/main/java/io/opencensus/exporter/trace/ocagent/OcAgentTraceExporter.java @@ -79,7 +79,8 @@ public static void createAndRegister(OcAgentTraceExporterConfiguration configura configuration.getUseInsecure(), configuration.getSslContext(), configuration.getRetryInterval(), - configuration.getEnableConfig()); + configuration.getEnableConfig(), + configuration.getDeadline()); registerInternal(newHandler); } } diff --git a/exporters/trace/ocagent/src/main/java/io/opencensus/exporter/trace/ocagent/OcAgentTraceExporterConfiguration.java b/exporters/trace/ocagent/src/main/java/io/opencensus/exporter/trace/ocagent/OcAgentTraceExporterConfiguration.java index 59cb2deec3..3e2ed86379 100644 --- a/exporters/trace/ocagent/src/main/java/io/opencensus/exporter/trace/ocagent/OcAgentTraceExporterConfiguration.java +++ b/exporters/trace/ocagent/src/main/java/io/opencensus/exporter/trace/ocagent/OcAgentTraceExporterConfiguration.java @@ -36,6 +36,7 @@ public abstract class OcAgentTraceExporterConfiguration { @VisibleForTesting static final String DEFAULT_END_POINT = "localhost:55678"; @VisibleForTesting static final String DEFAULT_SERVICE_NAME = "OpenCensus"; @VisibleForTesting static final Duration DEFAULT_RETRY_INTERVAL = Duration.create(300, 0); + @VisibleForTesting static final Duration DEFAULT_DEADLINE = Duration.create(10, 0); @VisibleForTesting static final Duration ZERO = Duration.create(0, 0); OcAgentTraceExporterConfiguration() {} @@ -101,6 +102,16 @@ public abstract class OcAgentTraceExporterConfiguration { */ public abstract boolean getEnableConfig(); + /** + * Returns the deadline for exporting to Agent/Collector. + * + *

Default value is 10 seconds. + * + * @return the export deadline. + * @since 0.22 + */ + public abstract Duration getDeadline(); + /** * Returns a new {@link Builder}. * @@ -113,7 +124,8 @@ public static Builder builder() { .setServiceName(DEFAULT_SERVICE_NAME) .setEnableConfig(true) .setUseInsecure(true) - .setRetryInterval(DEFAULT_RETRY_INTERVAL); + .setRetryInterval(DEFAULT_RETRY_INTERVAL) + .setDeadline(DEFAULT_DEADLINE); } /** @@ -181,12 +193,23 @@ public abstract static class Builder { */ public abstract Builder setEnableConfig(boolean enableConfig); + /** + * Sets the deadline for exporting to Agent/Collector. + * + * @param deadline the export deadline. + * @return this + * @since 0.22 + */ + public abstract Builder setDeadline(Duration deadline); + // TODO(songya): add an option that controls whether to always keep the RPC connection alive. abstract Duration getRetryInterval(); abstract OcAgentTraceExporterConfiguration autoBuild(); + abstract Duration getDeadline(); + /** * Builds a {@link OcAgentTraceExporterConfiguration}. * @@ -194,6 +217,7 @@ public abstract static class Builder { * @since 0.20 */ public OcAgentTraceExporterConfiguration build() { + Preconditions.checkArgument(getDeadline().compareTo(ZERO) > 0, "Deadline must be positive."); Preconditions.checkArgument( getRetryInterval().compareTo(ZERO) > 0, "Retry interval must be positive."); return autoBuild(); diff --git a/exporters/trace/ocagent/src/main/java/io/opencensus/exporter/trace/ocagent/OcAgentTraceExporterHandler.java b/exporters/trace/ocagent/src/main/java/io/opencensus/exporter/trace/ocagent/OcAgentTraceExporterHandler.java index e39539e9ca..edc5e47fe6 100644 --- a/exporters/trace/ocagent/src/main/java/io/opencensus/exporter/trace/ocagent/OcAgentTraceExporterHandler.java +++ b/exporters/trace/ocagent/src/main/java/io/opencensus/exporter/trace/ocagent/OcAgentTraceExporterHandler.java @@ -22,20 +22,21 @@ import io.grpc.netty.NettyChannelBuilder; import io.netty.handler.ssl.SslContext; import io.opencensus.common.Duration; +import io.opencensus.exporter.trace.util.TimeLimitedHandler; import io.opencensus.proto.agent.common.v1.Node; import io.opencensus.proto.agent.trace.v1.ExportTraceServiceRequest; import io.opencensus.proto.agent.trace.v1.TraceServiceGrpc; import io.opencensus.trace.export.SpanData; -import io.opencensus.trace.export.SpanExporter.Handler; import java.util.Collection; import java.util.logging.Logger; import javax.annotation.Nullable; /** Exporting handler for OC-Agent Tracing. */ -final class OcAgentTraceExporterHandler extends Handler { +final class OcAgentTraceExporterHandler extends TimeLimitedHandler { private static final Logger logger = Logger.getLogger(OcAgentTraceExporterHandler.class.getName()); + private static final String EXPORT_SPAN_NAME = "ExportOpenCensusProtoSpans"; private final String endPoint; private final Node node; @@ -51,7 +52,9 @@ final class OcAgentTraceExporterHandler extends Handler { boolean useInsecure, @Nullable SslContext sslContext, Duration retryInterval, - boolean enableConfig) { + boolean enableConfig, + Duration deadline) { + super(deadline, EXPORT_SPAN_NAME); this.endPoint = endPoint; this.node = OcAgentNodeUtils.getNodeInfo(serviceName); this.useInsecure = useInsecure; @@ -59,7 +62,7 @@ final class OcAgentTraceExporterHandler extends Handler { } @Override - public void export(Collection spanDataList) { + public void timeLimitedExport(Collection spanDataList) { if (exportRpcHandler == null || exportRpcHandler.isCompleted()) { // If not connected, try to initiate a new connection when a new batch of spans arrive. // Export RPC doesn't respect the retry interval. diff --git a/exporters/trace/ocagent/src/test/java/io/opencensus/exporter/trace/ocagent/OcAgentTraceExporterConfigurationTest.java b/exporters/trace/ocagent/src/test/java/io/opencensus/exporter/trace/ocagent/OcAgentTraceExporterConfigurationTest.java index 7b2f9f4548..daca516d82 100644 --- a/exporters/trace/ocagent/src/test/java/io/opencensus/exporter/trace/ocagent/OcAgentTraceExporterConfigurationTest.java +++ b/exporters/trace/ocagent/src/test/java/io/opencensus/exporter/trace/ocagent/OcAgentTraceExporterConfigurationTest.java @@ -43,6 +43,8 @@ public void defaultConfiguration() { assertThat(configuration.getRetryInterval()) .isEqualTo(OcAgentTraceExporterConfiguration.DEFAULT_RETRY_INTERVAL); assertThat(configuration.getEnableConfig()).isTrue(); + assertThat(configuration.getDeadline()) + .isEqualTo(OcAgentTraceExporterConfiguration.DEFAULT_DEADLINE); } @Test @@ -57,6 +59,7 @@ public void setAndGet() throws SSLException { .setSslContext(sslContext) .setRetryInterval(oneMinute) .setEnableConfig(false) + .setDeadline(oneMinute) .build(); assertThat(configuration.getEndPoint()).isEqualTo("192.168.0.1:50051"); assertThat(configuration.getServiceName()).isEqualTo("service"); @@ -64,5 +67,6 @@ public void setAndGet() throws SSLException { assertThat(configuration.getSslContext()).isEqualTo(sslContext); assertThat(configuration.getRetryInterval()).isEqualTo(oneMinute); assertThat(configuration.getEnableConfig()).isFalse(); + assertThat(configuration.getDeadline()).isEqualTo(oneMinute); } }