Skip to content

Commit 282deaf

Browse files
srnagartrask
andauthored
More cleanup to exporter code (Azure#19602)
* More cleanup to exporter code * Remove unused imports * Update sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorTraceExporter.java Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com> Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
1 parent f3b65a9 commit 282deaf

File tree

4 files changed

+55
-47
lines changed

4 files changed

+55
-47
lines changed

sdk/monitor/azure-monitor-opentelemetry-exporter/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,6 @@
7777
<version>5.6.3</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-params;external_dependency} -->
7878
<scope>test</scope>
7979
</dependency>
80-
<dependency>
81-
<groupId>com.azure</groupId>
82-
<artifactId>azure-core-test</artifactId>
83-
<version>1.5.3</version> <!-- {x-version-update;com.azure:azure-core-test;dependency} -->
84-
<scope>test</scope>
85-
</dependency>
8680

8781
<dependency>
8882
<groupId>com.azure</groupId>

sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorExporterBuilder.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33

44
package com.azure.monitor.opentelemetry.exporter;
55

6-
import com.azure.monitor.opentelemetry.exporter.implementation.ApplicationInsightsClientImpl;
7-
import com.azure.monitor.opentelemetry.exporter.implementation.ApplicationInsightsClientImplBuilder;
8-
import com.azure.monitor.opentelemetry.exporter.implementation.NdJsonSerializer;
96
import com.azure.core.http.HttpClient;
107
import com.azure.core.http.HttpPipeline;
118
import com.azure.core.http.policy.HttpLogDetailLevel;
129
import com.azure.core.http.policy.HttpLogOptions;
1310
import com.azure.core.http.policy.HttpPipelinePolicy;
1411
import com.azure.core.http.policy.RetryPolicy;
12+
import com.azure.core.util.ClientOptions;
1513
import com.azure.core.util.Configuration;
1614
import com.azure.core.util.logging.ClientLogger;
1715
import com.azure.core.util.serializer.JacksonAdapter;
16+
import com.azure.monitor.opentelemetry.exporter.implementation.ApplicationInsightsClientImpl;
17+
import com.azure.monitor.opentelemetry.exporter.implementation.ApplicationInsightsClientImplBuilder;
18+
import com.azure.monitor.opentelemetry.exporter.implementation.NdJsonSerializer;
1819
import com.fasterxml.jackson.databind.module.SimpleModule;
1920
import io.opentelemetry.sdk.trace.export.SpanExporter;
2021

@@ -29,11 +30,10 @@
2930
* {@link SpanExporter} interface defined by OpenTelemetry API specification.
3031
*/
3132
public final class AzureMonitorExporterBuilder {
32-
public static final String APPLICATIONINSIGHTS_CONNECTION_STRING = "APPLICATIONINSIGHTS_CONNECTION_STRING";
33+
private static final String APPLICATIONINSIGHTS_CONNECTION_STRING = "APPLICATIONINSIGHTS_CONNECTION_STRING";
3334
private final ClientLogger logger = new ClientLogger(AzureMonitorExporterBuilder.class);
3435
private final ApplicationInsightsClientImplBuilder restServiceClientBuilder;
3536
private String instrumentationKey;
36-
private String endpoint;
3737
private String connectionString;
3838
private AzureMonitorExporterServiceVersion serviceVersion;
3939

@@ -53,10 +53,8 @@ public AzureMonitorExporterBuilder() {
5353
*/
5454
AzureMonitorExporterBuilder endpoint(String endpoint) {
5555
Objects.requireNonNull(endpoint, "'endpoint' cannot be null.");
56-
5756
try {
5857
URL url = new URL(endpoint);
59-
this.endpoint = endpoint;
6058
restServiceClientBuilder.host(url.getProtocol() + "://" + url.getHost());
6159
} catch (MalformedURLException ex) {
6260
throw logger.logExceptionAsWarning(
@@ -140,6 +138,18 @@ public AzureMonitorExporterBuilder configuration(Configuration configuration) {
140138
return this;
141139
}
142140

141+
142+
/**
143+
* Sets the client options such as application ID and custom headers to set on a request.
144+
*
145+
* @param clientOptions The client options.
146+
* @return The updated {@link AzureMonitorExporterBuilder} object.
147+
*/
148+
public AzureMonitorExporterBuilder clientOptions(ClientOptions clientOptions) {
149+
restServiceClientBuilder.clientOptions(clientOptions);
150+
return this;
151+
}
152+
143153
/**
144154
* Sets the connection string to use for exporting telemetry events to Azure Monitor.
145155
* @param connectionString The connection string for the Azure Monitor resource.

sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/AzureMonitorTraceExporter.java

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public final class AzureMonitorTraceExporter implements SpanExporter {
122122
*/
123123
@Override
124124
public CompletableResultCode export(Collection<SpanData> spans) {
125-
125+
CompletableResultCode completableResultCode = new CompletableResultCode();
126126
try {
127127
List<TelemetryItem> telemetryItems = new ArrayList<>();
128128
for (SpanData span : spans) {
@@ -131,11 +131,11 @@ public CompletableResultCode export(Collection<SpanData> spans) {
131131
}
132132
client.export(telemetryItems)
133133
.subscriberContext(Context.of(Tracer.DISABLE_TRACING_KEY, true))
134-
.subscribe();
135-
return CompletableResultCode.ofSuccess();
134+
.subscribe(ignored -> { }, error -> completableResultCode.fail(), completableResultCode::succeed);
135+
return completableResultCode;
136136
} catch (Throwable t) {
137137
logger.error(t.getMessage(), t);
138-
return CompletableResultCode.ofFailure();
138+
return completableResultCode.fail();
139139
}
140140
}
141141

@@ -160,25 +160,18 @@ private void export(SpanData span, List<TelemetryItem> telemetryItems) {
160160
String instrumentationName = span.getInstrumentationLibraryInfo().getName();
161161
Matcher matcher = COMPONENT_PATTERN.matcher(instrumentationName);
162162
String stdComponent = matcher.matches() ? matcher.group(1) : null;
163-
if ("jms".equals(stdComponent) && !span.getParentSpanContext().isValid() && kind == SpanKind.CONSUMER) {
164-
// no need to capture these, at least is consistent with prior behavior
165-
// these tend to be frameworks pulling messages which are then pushed to consumers
166-
// where we capture them
167-
return;
168-
}
169163
if (kind == SpanKind.INTERNAL) {
170164
if ("spring-scheduling".equals(stdComponent) && !span.getParentSpanContext().isValid()) {
171-
// TODO (srnagar) need semantic convention for determining whether to map INTERNAL to request or dependency
172-
// (or need clarification to use SERVER for this)
165+
// if (!span.getParentSpanContext().isValid()) {
166+
// TODO (trask) need semantic convention for determining whether to map INTERNAL to request or
167+
// dependency (or need clarification to use SERVER for this)
173168
exportRequest(span, telemetryItems);
174169
} else {
175170
exportRemoteDependency(span, true, telemetryItems);
176171
}
177172
} else if (kind == SpanKind.CLIENT || kind == SpanKind.PRODUCER) {
178173
exportRemoteDependency(span, false, telemetryItems);
179174
} else if (kind == SpanKind.CONSUMER && !span.getParentSpanContext().isRemote()) {
180-
// TODO (srnagar) need spec clarification, but it seems polling for messages can be CONSUMER also
181-
// in which case the span will not have a remote parent and should be treated as a dependency instead of a request
182175
exportRemoteDependency(span, false, telemetryItems);
183176
} else if (kind == SpanKind.SERVER || kind == SpanKind.CONSUMER) {
184177
exportRequest(span, telemetryItems);
@@ -199,6 +192,7 @@ private static List<TelemetryExceptionDetails> minimalParse(String errorStack) {
199192
} else {
200193
details.setTypeName(line);
201194
}
195+
// TODO (trask): map OpenTelemetry exception to Application Insights exception better
202196
details.setStack(errorStack);
203197
return Collections.singletonList(details);
204198
}
@@ -305,7 +299,7 @@ private void applyHttpClientSpan(Attributes attributes, RemoteDependencyData tel
305299
target += ":" + uri.getPort();
306300
}
307301
} catch (URISyntaxException e) {
308-
// TODO (srnagar) "log once"
302+
// TODO (trask) "log once"
309303
logger.error(e.getMessage());
310304
logger.verbose(e.getMessage(), e);
311305
}
@@ -323,7 +317,7 @@ private void applyHttpClientSpan(Attributes attributes, RemoteDependencyData tel
323317
} else {
324318
// using "Http (tracked component)" is important for dependencies that go cross-component (have an appId in their target field)
325319
// if you use just HTTP, Breeze will remove appid from the target
326-
// TODO (srnagar) remove this once confirmed by zakima that it is no longer needed
320+
// TODO (trask) remove this once confirmed by zakima that it is no longer needed
327321
telemetry.setType("Http (tracked component)");
328322
telemetry.setTarget(target + " | " + targetAppId);
329323
}
@@ -413,9 +407,8 @@ private void applyMessagingClientSpan(Attributes attributes, RemoteDependencyDat
413407

414408
private static int getDefaultPortForDbSystem(String dbSystem) {
415409
switch (dbSystem) {
416-
// TODO (srnagar) replace these with constants from OpenTelemetry API after upgrading to 0.10.0
417-
// TODO (srnagar) add these default ports to the OpenTelemetry database semantic conventions spec
418-
// TODO (srnagar) need to add more default ports once jdbc instrumentation reports net.peer.*
410+
// TODO (trask) add these default ports to the OpenTelemetry database semantic conventions spec
411+
// TODO (trask) need to add more default ports once jdbc instrumentation reports net.peer.*
419412
case "mongodb":
420413
return 27017;
421414
case "cassandra":
@@ -454,7 +447,7 @@ private void exportRequest(SpanData span, List<TelemetryItem> telemetryItems) {
454447
if (source == null) {
455448
String messagingSystem = attributes.get(SemanticAttributes.MESSAGING_SYSTEM);
456449
if (messagingSystem != null) {
457-
// TODO (srnagar) should this pass default port for messaging.system?
450+
// TODO (trask) should this pass default port for messaging.system?
458451
source = nullAwareConcat(getTargetFromPeerAttributes(attributes, 0),
459452
attributes.get(SemanticAttributes.MESSAGING_DESTINATION), "/");
460453
if (source == null) {
@@ -565,16 +558,10 @@ private void exportEvents(SpanData span, Double samplingPercentage, List<Telemet
565558

566559
if (event.getAttributes().get(SemanticAttributes.EXCEPTION_TYPE) != null
567560
|| event.getAttributes().get(SemanticAttributes.EXCEPTION_MESSAGE) != null) {
568-
// TODO (srnagar): Remove this boolean after we can confirm that the exception duplicate
569-
// is a bug from the opentelmetry-java-instrumentation
570-
if (!foundException) {
571-
// TODO (srnagar): map OpenTelemetry exception to Application Insights exception better
572-
String stacktrace = event.getAttributes().get(SemanticAttributes.EXCEPTION_STACKTRACE);
573-
if (stacktrace != null) {
574-
trackException(stacktrace, span, operationId, span.getSpanId(), samplingPercentage, telemetryItems);
575-
}
561+
String stacktrace = event.getAttributes().get(SemanticAttributes.EXCEPTION_STACKTRACE);
562+
if (stacktrace != null) {
563+
trackException(stacktrace, span, operationId, span.getSpanId(), samplingPercentage, telemetryItems);
576564
}
577-
foundException = true;
578565
} else {
579566
telemetryItem.setSampleRate(samplingPercentage.floatValue());
580567
telemetryItems.add(telemetryItem);

sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/ApplicationInsightsClientImplBuilder.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
import com.azure.core.http.policy.HttpLogOptions;
1212
import com.azure.core.http.policy.HttpLoggingPolicy;
1313
import com.azure.core.http.policy.HttpPipelinePolicy;
14-
import com.azure.core.http.policy.HttpPolicyProviders;
1514
import com.azure.core.http.policy.RetryPolicy;
1615
import com.azure.core.http.policy.UserAgentPolicy;
16+
import com.azure.core.util.ClientOptions;
1717
import com.azure.core.util.Configuration;
1818
import com.azure.core.util.CoreUtils;
1919
import com.azure.core.util.serializer.JacksonAdapter;
2020
import com.azure.core.util.serializer.SerializerAdapter;
21+
2122
import java.util.ArrayList;
2223
import java.util.List;
2324
import java.util.Map;
@@ -31,6 +32,7 @@ public final class ApplicationInsightsClientImplBuilder {
3132

3233
private final Map<String, String> properties =
3334
CoreUtils.getProperties("azure-monitor-opentelemetry-exporter.properties");
35+
private ClientOptions clientOptions;
3436

3537
public ApplicationInsightsClientImplBuilder() {
3638
this.pipelinePolicies = new ArrayList<>();
@@ -166,6 +168,17 @@ public ApplicationInsightsClientImplBuilder addPolicy(HttpPipelinePolicy customP
166168
return this;
167169
}
168170

171+
/**
172+
* Sets the client options such as application ID and custom headers to set on a request.
173+
*
174+
* @param clientOptions The client options.
175+
* @return The updated {@link ApplicationInsightsClientImplBuilder} object.
176+
*/
177+
public ApplicationInsightsClientImplBuilder clientOptions(ClientOptions clientOptions) {
178+
this.clientOptions = clientOptions;
179+
return this;
180+
}
181+
169182
/**
170183
* Builds an instance of ApplicationInsightsClientImpl with the provided parameters.
171184
*
@@ -191,16 +204,20 @@ private HttpPipeline createHttpPipeline() {
191204
if (httpLogOptions == null) {
192205
httpLogOptions = new HttpLogOptions();
193206
}
207+
208+
if (clientOptions == null) {
209+
clientOptions = new ClientOptions();
210+
}
194211
List<HttpPipelinePolicy> policies = new ArrayList<>();
195212
String clientName = properties.getOrDefault(SDK_NAME, "UnknownName");
196213
String clientVersion = properties.getOrDefault(SDK_VERSION, "UnknownVersion");
197-
policies.add(
198-
new UserAgentPolicy(httpLogOptions.getApplicationId(), clientName, clientVersion, buildConfiguration));
199-
HttpPolicyProviders.addBeforeRetryPolicies(policies);
214+
215+
String applicationId = CoreUtils.getApplicationId(clientOptions, httpLogOptions);
216+
217+
policies.add(new UserAgentPolicy(applicationId, clientName, clientVersion, buildConfiguration));
200218
policies.add(retryPolicy == null ? new RetryPolicy() : retryPolicy);
201219
policies.add(new CookiePolicy());
202220
policies.addAll(this.pipelinePolicies);
203-
HttpPolicyProviders.addAfterRetryPolicies(policies);
204221
policies.add(new HttpLoggingPolicy(httpLogOptions));
205222
HttpPipeline httpPipeline =
206223
new HttpPipelineBuilder()

0 commit comments

Comments
 (0)