Skip to content

Commit d0fbe8d

Browse files
authored
[Monitor Exporter] Replace instrumentationKey with connectionString in builder (Azure#17179)
* Replace instrumentationKey with connectionString in builder
1 parent effdb53 commit d0fbe8d

File tree

8 files changed

+109
-16
lines changed

8 files changed

+109
-16
lines changed

sdk/monitor/microsoft-opentelemetry-exporter-azuremonitor/CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Release History
22

3-
## 1.0.0-beta.2 (Unreleased)
4-
3+
## 1.0.0-beta.2 (2020-11-10)
4+
### Breaking changes
5+
- Moved this package to `com.microsoft` Maven group.
6+
- Renamed artifact to `microsoft-opentelemetry-exporter-azuremonitor`.
7+
- Replaced `instrumentationKey()` with `connectionString()` in the `AzureMonitorExporterBuilder`.
58

69
## 1.0.0-beta.1 (2020-10-06)
710

sdk/monitor/microsoft-opentelemetry-exporter-azuremonitor/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ For more information, please read [introduction to Application Insights][applica
2323
<dependency>
2424
<groupId>com.microsoft</groupId>
2525
<artifactId>microsoft-opentelemetry-exporter-azuremonitor</artifactId>
26-
<version>1.0.0-beta.1</version>
26+
<version>1.0.0-beta.2</version>
2727
</dependency>
2828
```
2929
[//]: # ({x-version-update-end})
@@ -41,7 +41,7 @@ right corner.
4141
<!-- embedme ./src/samples/java/com/microsoft/opentelemetry/exporter/azuremonitor/ReadmeSamples.java#L26-L28 -->
4242
```java
4343
AzureMonitorExporter azureMonitorExporter = new AzureMonitorExporterBuilder()
44-
.instrumentationKey("{instrumentation-key}")
44+
.connectionString("{connection-string}")
4545
.buildExporter();
4646
```
4747

@@ -53,7 +53,7 @@ The following example shows how to export a collection of available [Spans][span
5353
<!-- embedme ./src/samples/java/com/microsoft/opentelemetry/exporter/azuremonitor/ReadmeSamples.java#L35-L40 -->
5454
```java
5555
AzureMonitorExporter azureMonitorExporter = new AzureMonitorExporterBuilder()
56-
.instrumentationKey("{instrumentation-key}")
56+
.connectionString("{connection-string}")
5757
.buildExporter();
5858

5959
CompletableResultCode resultCode = azureMonitorExporter.export(getSpanDataCollection());

sdk/monitor/microsoft-opentelemetry-exporter-azuremonitor/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,27 @@
112112
</plugin>
113113
</plugins>
114114
</build>
115+
116+
<profiles>
117+
<profile>
118+
<id>java-lts</id>
119+
<activation>
120+
<jdk>[11,)</jdk>
121+
</activation>
122+
<build>
123+
<plugins>
124+
<plugin>
125+
<groupId>org.apache.maven.plugins</groupId>
126+
<artifactId>maven-surefire-plugin</artifactId>
127+
<version>3.0.0-M3</version> <!-- {x-version-update;org.apache.maven.plugins:maven-surefire-plugin;external_dependency} -->
128+
<configuration>
129+
<argLine>
130+
--add-opens com.microsoft.opentelemetry.exporter.azuremonitor/com.microsoft.opentelemetry.exporter.azuremonitor=ALL-UNNAMED
131+
</argLine>
132+
</configuration>
133+
</plugin>
134+
</plugins>
135+
</build>
136+
</profile>
137+
</profiles>
115138
</project>

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

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import java.net.MalformedURLException;
2222
import java.net.URL;
23+
import java.util.HashMap;
24+
import java.util.Map;
2325
import java.util.Objects;
2426

2527
/**
@@ -45,12 +47,12 @@ public AzureMonitorExporterBuilder() {
4547
* @throws NullPointerException if {@code endpoint} is null.
4648
* @throws IllegalArgumentException if {@code endpoint} cannot be parsed into a valid URL.
4749
*/
48-
public AzureMonitorExporterBuilder endpoint(String endpoint) {
50+
AzureMonitorExporterBuilder endpoint(String endpoint) {
4951
Objects.requireNonNull(endpoint, "'endpoint' cannot be null.");
5052

5153
try {
5254
URL url = new URL(endpoint);
53-
restServiceClientBuilder.host(url.getHost());
55+
restServiceClientBuilder.host(url.getProtocol() + "://" + url.getHost());
5456
} catch (MalformedURLException ex) {
5557
throw logger.logExceptionAsWarning(
5658
new IllegalArgumentException("'endpoint' must be a valid URL.", ex));
@@ -134,15 +136,39 @@ public AzureMonitorExporterBuilder configuration(Configuration configuration) {
134136
}
135137

136138
/**
137-
* Sets the instrumentation key to use for exporting telemetry events to Azure Monitor.
138-
* @param instrumentationKey The instrumentation key of the Azure Monitor resource.
139+
* The connection string to use for exporting telemetry events to Azure Monitor.
140+
* @param connectionString The connection string for the Azure Monitor resource.
139141
* @return The updated {@link AzureMonitorExporterBuilder} object.
142+
* @throws NullPointerException If the connection string is {@code null}.
143+
* @throws IllegalArgumentException If the connection string is invalid.
140144
*/
141-
public AzureMonitorExporterBuilder instrumentationKey(String instrumentationKey) {
142-
this.instrumentationKey = instrumentationKey;
145+
public AzureMonitorExporterBuilder connectionString(String connectionString) {
146+
Map<String, String> keyValues = extractKeyValuesFromConnectionString(connectionString);
147+
if (!keyValues.containsKey("InstrumentationKey")) {
148+
throw logger.logExceptionAsError(
149+
new IllegalArgumentException("InstrumentationKey not found in connectionString"));
150+
}
151+
this.instrumentationKey = keyValues.get("InstrumentationKey");
152+
String endpoint = keyValues.get("IngestionEndpoint");
153+
if (endpoint != null) {
154+
this.endpoint(endpoint);
155+
}
143156
return this;
144157
}
145158

159+
private Map<String, String> extractKeyValuesFromConnectionString(String connectionString) {
160+
Objects.requireNonNull(connectionString);
161+
Map<String, String> keyValues = new HashMap<>();
162+
String[] splits = connectionString.split(";");
163+
for (String split : splits) {
164+
String[] keyValPair = split.split("=");
165+
if (keyValPair.length == 2) {
166+
keyValues.put(keyValPair[0], keyValPair[1]);
167+
}
168+
}
169+
return keyValues;
170+
}
171+
146172
/**
147173
* Creates a {@link MonitorExporterClient} based on options set in the builder. Every time {@code
148174
* buildAsyncClient()} is called a new instance of {@link MonitorExporterClient} is created.
@@ -190,7 +216,9 @@ MonitorExporterAsyncClient buildAsyncClient() {
190216
* @throws NullPointerException if the instrumentation key is not set.
191217
*/
192218
public AzureMonitorExporter buildExporter() {
193-
Objects.requireNonNull(instrumentationKey, "'instrumentationKey' cannot be null");
219+
// instrumentationKey is extracted from connectionString, so, if instrumentationKey is null
220+
// then the error message should read "connectionString cannot be null".
221+
Objects.requireNonNull(instrumentationKey, "'connectionString' cannot be null");
194222
return new AzureMonitorExporter(buildClient(), instrumentationKey);
195223
}
196224

sdk/monitor/microsoft-opentelemetry-exporter-azuremonitor/src/samples/java/com/microsoft/opentelemetry/exporter/azuremonitor/AzureMonitorExporterSample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class AzureMonitorExporterSample {
1919
*/
2020
public static void main(String[] args) {
2121
AzureMonitorExporter azureMonitorExporter = new AzureMonitorExporterBuilder()
22-
.instrumentationKey("{instrumentation-key}")
22+
.connectionString("{connection-string}")
2323
.buildExporter();
2424
CompletableResultCode resultCode =
2525
azureMonitorExporter.export(Collections.singleton(new AzureMonitorExporterTest.RequestSpanData()));

sdk/monitor/microsoft-opentelemetry-exporter-azuremonitor/src/samples/java/com/microsoft/opentelemetry/exporter/azuremonitor/ReadmeSamples.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class ReadmeSamples {
2424
*/
2525
public void createExporter() {
2626
AzureMonitorExporter azureMonitorExporter = new AzureMonitorExporterBuilder()
27-
.instrumentationKey("{instrumentation-key}")
27+
.connectionString("{connection-string}")
2828
.buildExporter();
2929
}
3030

@@ -33,7 +33,7 @@ public void createExporter() {
3333
*/
3434
public void exportSpanData() {
3535
AzureMonitorExporter azureMonitorExporter = new AzureMonitorExporterBuilder()
36-
.instrumentationKey("{instrumentation-key}")
36+
.connectionString("{connection-string}")
3737
.buildExporter();
3838

3939
CompletableResultCode resultCode = azureMonitorExporter.export(getSpanDataCollection());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.opentelemetry.exporter.azuremonitor;
5+
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
import java.util.stream.Stream;
12+
13+
/**
14+
* Unit tests for {@link AzureMonitorExporterBuilder}.
15+
*/
16+
public class AzureMonitorExporterBuilderTest {
17+
18+
@ParameterizedTest
19+
@MethodSource("getInvalidConnectionStrings")
20+
public <T extends RuntimeException> void testInvalidConnectionStrings(String connectionString,
21+
Class<T> exceptionExpected) {
22+
Assertions.assertThrows(exceptionExpected, () -> new AzureMonitorExporterBuilder()
23+
.connectionString(connectionString)
24+
.buildExporter());
25+
26+
}
27+
28+
private static Stream<Arguments> getInvalidConnectionStrings() {
29+
return Stream.of(
30+
Arguments.of(null, NullPointerException.class),
31+
Arguments.of("", IllegalArgumentException.class),
32+
Arguments.of("InstrumentationKey=;IngestionEndpoint=url", IllegalArgumentException.class),
33+
Arguments.of("Instrumentation=iKey;IngestionEndpoint=url", IllegalArgumentException.class),
34+
Arguments.of("InstrumentationKey;IngestionEndpoint=url", IllegalArgumentException.class),
35+
Arguments.of("InstrumentationKey;IngestionEndpoint=url", IllegalArgumentException.class),
36+
Arguments.of("IngestionEndpoint=url", IllegalArgumentException.class)
37+
);
38+
}
39+
}

sdk/monitor/microsoft-opentelemetry-exporter-azuremonitor/src/test/java/com/microsoft/opentelemetry/exporter/azuremonitor/AzureMonitorExporterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class AzureMonitorExporterTest extends MonitorExporterClientTestBase {
3434
@Test
3535
public void testExportRequestData() {
3636
AzureMonitorExporter azureMonitorExporter = getClientBuilder()
37-
.instrumentationKey("{instrumentation-key}")
37+
.connectionString("InstrumentationKey=ikey;IngestionEndpoint=https://testendpoint.com")
3838
.buildExporter();
3939
CompletableResultCode export = azureMonitorExporter.export(Collections.singleton(new RequestSpanData()));
4040
Assertions.assertTrue(export.isDone());

0 commit comments

Comments
 (0)