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

Commit b9d4304

Browse files
authored
Exporter/Instana: Use configurations and add deadline option. (#1891)
* Exporter/Instana: Use configurations and add deadline option. * Add missing file (git add -A)
1 parent cae2744 commit b9d4304

5 files changed

Lines changed: 172 additions & 64 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ break behaviors for applications that rely on this to be always enabled.
55
- Provide a `Deadline` option to Stackdriver Stats exporter. Default value is 10 seconds.
66
Also provide a `MetricServiceStub` option so that advanced users can use a custom Stackdriver
77
Monitoring client to make RPCs.
8-
- Use `JaegerExporterConfiguration` for creating `JaegerTraceExporter`. Provide a `Deadline` option
9-
with default value 10 seconds.
10-
- Use `ZipkinExporterConfiguration` for creating `ZipkinTraceExporter`. Provide a `Deadline` option
11-
with default value 10 seconds.
8+
- Use `Configuration` builder pattern for creating `JaegerTraceExporter`, `ZipkinTraceExporter` and
9+
`InstanaTraceExporter`. Provide a `Deadline` option with default value 10 seconds.
1210
- Provide a `Deadline` option to Datadog and Elasticsearch exporter. Default value is 10 seconds.
1311
- Extract the common timeout logic of Trace exporters to `opencensus-exporter-trace-util`.
1412

exporters/trace/instana/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ description = 'OpenCensus Trace Instana Exporter'
66
}
77

88
dependencies {
9+
compileOnly libraries.auto_value
10+
911
compile project(':opencensus-api'),
1012
project(':opencensus-exporter-trace-util'),
1113
libraries.guava
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright 2019, 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.instana;
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.opencensus.common.Duration;
23+
import javax.annotation.concurrent.Immutable;
24+
25+
/**
26+
* Configuration for {@link InstanaTraceExporter}.
27+
*
28+
* @since 0.22
29+
*/
30+
@AutoValue
31+
@Immutable
32+
public abstract class InstanaExporterConfiguration {
33+
34+
@VisibleForTesting static final Duration DEFAULT_DEADLINE = Duration.create(10, 0);
35+
@VisibleForTesting static final Duration ZERO = Duration.fromMillis(0);
36+
37+
InstanaExporterConfiguration() {}
38+
39+
/**
40+
* Returns the endpoint of the Instana agent.
41+
*
42+
* @return the endpoint of the Instana agent.
43+
* @since 0.22
44+
*/
45+
public abstract String getAgentEndpoint();
46+
47+
/**
48+
* Returns the deadline for exporting to Instana.
49+
*
50+
* <p>Default value is 10 seconds.
51+
*
52+
* @return the export deadline.
53+
* @since 0.22
54+
*/
55+
public abstract Duration getDeadline();
56+
57+
/**
58+
* Return a new {@link Builder}.
59+
*
60+
* @return a {@code Builder}
61+
* @since 0.22
62+
*/
63+
public static Builder builder() {
64+
return new AutoValue_InstanaExporterConfiguration.Builder().setDeadline(DEFAULT_DEADLINE);
65+
}
66+
67+
/**
68+
* Builder for {@link InstanaExporterConfiguration}.
69+
*
70+
* @since 0.22
71+
*/
72+
@AutoValue.Builder
73+
public abstract static class Builder {
74+
75+
Builder() {}
76+
77+
/**
78+
* Sets the endpoint of Instana agent to send traces to. E.g
79+
* http://localhost:42699/com.instana.plugin.generic.trace
80+
*
81+
* @param agentEndpoint the endpoint of the agent.
82+
* @return this.
83+
* @since 0.22
84+
*/
85+
public abstract Builder setAgentEndpoint(String agentEndpoint);
86+
87+
/**
88+
* Sets the deadline for exporting to Instana.
89+
*
90+
* @param deadline the export deadline.
91+
* @return this
92+
* @since 0.22
93+
*/
94+
public abstract Builder setDeadline(Duration deadline);
95+
96+
abstract Duration getDeadline();
97+
98+
abstract InstanaExporterConfiguration autoBuild();
99+
100+
/**
101+
* Builds a {@link InstanaExporterConfiguration}.
102+
*
103+
* @return a {@code InstanaExporterConfiguration}.
104+
* @since 0.22
105+
*/
106+
public InstanaExporterConfiguration build() {
107+
Preconditions.checkArgument(getDeadline().compareTo(ZERO) > 0, "Deadline must be positive.");
108+
return autoBuild();
109+
}
110+
}
111+
}

exporters/trace/instana/src/main/java/io/opencensus/exporter/trace/instana/InstanaExporterHandler.java

Lines changed: 31 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,16 @@
2323
import io.opencensus.common.Duration;
2424
import io.opencensus.common.Function;
2525
import io.opencensus.common.Functions;
26-
import io.opencensus.common.Scope;
2726
import io.opencensus.common.Timestamp;
27+
import io.opencensus.exporter.trace.util.TimeLimitedHandler;
2828
import io.opencensus.trace.AttributeValue;
29-
import io.opencensus.trace.Sampler;
3029
import io.opencensus.trace.Span.Kind;
3130
import io.opencensus.trace.SpanContext;
3231
import io.opencensus.trace.SpanId;
3332
import io.opencensus.trace.Status;
3433
import io.opencensus.trace.TraceId;
35-
import io.opencensus.trace.Tracer;
36-
import io.opencensus.trace.Tracing;
3734
import io.opencensus.trace.export.SpanData;
38-
import io.opencensus.trace.export.SpanExporter;
39-
import io.opencensus.trace.samplers.Samplers;
35+
import java.io.Closeable;
4036
import java.io.IOException;
4137
import java.io.InputStream;
4238
import java.io.OutputStream;
@@ -63,13 +59,13 @@
6359
* Major TODO is the limitation of Instana to only suport 64bit trace ids, which will be resolved.
6460
* Until then it is crossing fingers and treating it as 50% sampler :).
6561
*/
66-
final class InstanaExporterHandler extends SpanExporter.Handler {
62+
final class InstanaExporterHandler extends TimeLimitedHandler {
6763

68-
private static final Tracer tracer = Tracing.getTracer();
69-
private static final Sampler probabilitySpampler = Samplers.probabilitySampler(0.0001);
64+
private static final String EXPORT_SPAN_NAME = "ExportInstanaTraces";
7065
private final URL agentEndpoint;
7166

72-
InstanaExporterHandler(URL agentEndpoint) {
67+
InstanaExporterHandler(URL agentEndpoint, Duration deadline) {
68+
super(deadline, EXPORT_SPAN_NAME);
7369
this.agentEndpoint = agentEndpoint;
7470
}
7571

@@ -180,56 +176,36 @@ static String convertToJson(Collection<SpanData> spanDataList) {
180176
}
181177

182178
@Override
183-
public void export(Collection<SpanData> spanDataList) {
184-
// Start a new span with explicit 1/10000 sampling probability to avoid the case when user
185-
// sets the default sampler to always sample and we get the gRPC span of the instana
186-
// export call always sampled and go to an infinite loop.
187-
Scope scope =
188-
tracer.spanBuilder("ExportInstanaTraces").setSampler(probabilitySpampler).startScopedSpan();
179+
public void timeLimitedExport(Collection<SpanData> spanDataList) throws Exception {
180+
String json = convertToJson(spanDataList);
181+
182+
OutputStream outputStream = null;
183+
InputStream inputStream = null;
189184
try {
190-
String json = convertToJson(spanDataList);
185+
HttpURLConnection connection = (HttpURLConnection) agentEndpoint.openConnection();
186+
connection.setRequestMethod("POST");
187+
connection.setDoOutput(true);
188+
outputStream = connection.getOutputStream();
189+
outputStream.write(json.getBytes(Charset.defaultCharset()));
190+
outputStream.flush();
191+
inputStream = connection.getInputStream();
192+
if (connection.getResponseCode() != 200) {
193+
throw new Exception("Response " + connection.getResponseCode());
194+
}
195+
} finally {
196+
closeStream(inputStream);
197+
closeStream(outputStream);
198+
}
199+
}
191200

192-
OutputStream outputStream = null;
193-
InputStream inputStream = null;
201+
// Closes an input or output stream and ignores potential IOException.
202+
private static void closeStream(@javax.annotation.Nullable Closeable stream) {
203+
if (stream != null) {
194204
try {
195-
HttpURLConnection connection = (HttpURLConnection) agentEndpoint.openConnection();
196-
connection.setRequestMethod("POST");
197-
connection.setDoOutput(true);
198-
outputStream = connection.getOutputStream();
199-
outputStream.write(json.getBytes(Charset.defaultCharset()));
200-
outputStream.flush();
201-
inputStream = connection.getInputStream();
202-
if (connection.getResponseCode() != 200) {
203-
tracer
204-
.getCurrentSpan()
205-
.setStatus(
206-
Status.UNKNOWN.withDescription("Response " + connection.getResponseCode()));
207-
}
205+
stream.close();
208206
} catch (IOException e) {
209-
tracer
210-
.getCurrentSpan()
211-
.setStatus(
212-
Status.UNKNOWN.withDescription(
213-
e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage()));
214-
// dropping span batch
215-
} finally {
216-
if (inputStream != null) {
217-
try {
218-
inputStream.close();
219-
} catch (IOException e) {
220-
// ignore
221-
}
222-
}
223-
if (outputStream != null) {
224-
try {
225-
outputStream.close();
226-
} catch (IOException e) {
227-
// ignore
228-
}
229-
}
207+
// ignore
230208
}
231-
} finally {
232-
scope.close();
233209
}
234210
}
235211
}

exporters/trace/instana/src/main/java/io/opencensus/exporter/trace/instana/InstanaTraceExporter.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
*
3535
* <pre>{@code
3636
* public static void main(String[] args) {
37-
* InstanaTraceExporter.createAndRegister("http://localhost:42699/com.instana.plugin.generic.trace");
37+
* String agentEndpoint = "http://localhost:42699/com.instana.plugin.generic.trace";
38+
* InstanaTraceExporter.createAndRegister(
39+
* InstanaExporterConfiguration.builder().setAgentEndpoint(agentEndpoint).build());
3840
* ... // Do work.
3941
* }
4042
* }</pre>
@@ -56,20 +58,39 @@ private InstanaTraceExporter() {}
5658
* Creates and registers the Instana Trace exporter to the OpenCensus library. Only one Instana
5759
* exporter can be registered at any point.
5860
*
59-
* @param agentEndpoint Ex http://localhost:42699/com.instana.plugin.generic.trace
61+
* @param configuration Configuration for InstanaTraceExporter.
6062
* @throws MalformedURLException if the agentEndpoint is not a valid http url.
6163
* @throws IllegalStateException if a Instana exporter is already registered.
62-
* @since 0.12
64+
* @since 0.22
6365
*/
64-
public static void createAndRegister(String agentEndpoint) throws MalformedURLException {
66+
public static void createAndRegister(InstanaExporterConfiguration configuration)
67+
throws MalformedURLException {
6568
synchronized (monitor) {
6669
checkState(handler == null, "Instana exporter is already registered.");
67-
Handler newHandler = new InstanaExporterHandler(new URL(agentEndpoint));
70+
Handler newHandler =
71+
new InstanaExporterHandler(
72+
new URL(configuration.getAgentEndpoint()), configuration.getDeadline());
6873
handler = newHandler;
6974
register(Tracing.getExportComponent().getSpanExporter(), newHandler);
7075
}
7176
}
7277

78+
/**
79+
* Creates and registers the Instana Trace exporter to the OpenCensus library. Only one Instana
80+
* exporter can be registered at any point.
81+
*
82+
* @param agentEndpoint Ex http://localhost:42699/com.instana.plugin.generic.trace
83+
* @throws MalformedURLException if the agentEndpoint is not a valid http url.
84+
* @throws IllegalStateException if a Instana exporter is already registered.
85+
* @since 0.12
86+
* @deprecated in favor of {@link #createAndRegister(InstanaExporterConfiguration)}.
87+
*/
88+
@Deprecated
89+
public static void createAndRegister(String agentEndpoint) throws MalformedURLException {
90+
createAndRegister(
91+
InstanaExporterConfiguration.builder().setAgentEndpoint(agentEndpoint).build());
92+
}
93+
7394
/**
7495
* Registers the {@code InstanaTraceExporter}.
7596
*

0 commit comments

Comments
 (0)