Skip to content

Commit 2ee6e42

Browse files
kryalamatrask
andauthored
Bump otel version and add sdkversionstring to telemetry (Azure#25162)
* initial commit * Convert asserts to assertj * Some updates * Read otel version * Oops * More oops * More oops * address comments * fix indentation * make tests accessible Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
1 parent 0471720 commit 2ee6e42

File tree

7 files changed

+114
-18
lines changed

7 files changed

+114
-18
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
import com.azure.monitor.opentelemetry.exporter.implementation.models.TelemetryExceptionData;
1515
import com.azure.monitor.opentelemetry.exporter.implementation.models.TelemetryExceptionDetails;
1616
import com.azure.monitor.opentelemetry.exporter.implementation.models.TelemetryItem;
17-
import com.azure.monitor.opentelemetry.exporter.utils.FormattedDuration;
17+
import com.azure.monitor.opentelemetry.exporter.implementation.FormattedDuration;
18+
import com.azure.monitor.opentelemetry.exporter.implementation.VersionGenerator;
1819
import io.opentelemetry.api.common.AttributeKey;
1920
import io.opentelemetry.api.common.Attributes;
2021
import io.opentelemetry.api.trace.SpanId;
@@ -691,7 +692,8 @@ private void initTelemetry(TelemetryItem telemetry, MonitorDomain data, String t
691692
telemetry.setName(telemetryName);
692693
telemetry.setInstrumentationKey(instrumentationKey);
693694
telemetry.setTags(new HashMap<>());
694-
695+
// Set AI Internal SDK Version
696+
telemetry.getTags().put(ContextTagKeys.AI_INTERNAL_SDK_VERSION.toString(), VersionGenerator.getSdkVersion());
695697
data.setVersion(2);
696698

697699
MonitorBase monitorBase = new MonitorBase();

sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/utils/FormattedDuration.java renamed to sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/implementation/FormattedDuration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
package com.azure.monitor.opentelemetry.exporter.utils;
4+
package com.azure.monitor.opentelemetry.exporter.implementation;
55

66
import static java.util.concurrent.TimeUnit.DAYS;
77
import static java.util.concurrent.TimeUnit.HOURS;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.monitor.opentelemetry.exporter.implementation;
5+
6+
import com.azure.core.util.CoreUtils;
7+
import java.util.Map;
8+
9+
/**
10+
* This class contains several utility functions to populate sdk version string
11+
*/
12+
public final class VersionGenerator {
13+
private static final String UNKNOWN_VERSION_VALUE = "unknown";
14+
15+
private static final String artifactName;
16+
private static final String artifactVersion;
17+
18+
private static final String sdkVersionString;
19+
20+
static {
21+
Map<String, String> properties =
22+
CoreUtils.getProperties("azure-monitor-opentelemetry-exporter.properties");
23+
24+
artifactName = properties.get("name");
25+
artifactVersion = properties.get("version");
26+
27+
sdkVersionString = "java" +
28+
getJavaVersion() +
29+
":" +
30+
"ot" + getOpenTelemetryApiVersion() +
31+
":" +
32+
"ext" + artifactVersion;
33+
}
34+
35+
/**
36+
* This method returns artifact name.
37+
* @return artifactName.
38+
*/
39+
public static String getArtifactName() {
40+
return artifactName;
41+
}
42+
43+
/**
44+
* This method returns artifact version.
45+
* @return artifactVersion.
46+
*/
47+
public static String getArtifactVersion() {
48+
return artifactVersion;
49+
}
50+
51+
/**
52+
* This method returns sdk version string as per the below format
53+
* javaX:otelY:extZ
54+
* X = Java version, Y = opentelemetry version, Z = exporter version
55+
* @return sdkVersionString.
56+
*/
57+
public static String getSdkVersion() {
58+
return sdkVersionString;
59+
}
60+
61+
private static String getJavaVersion() {
62+
return System.getProperty("java.version");
63+
}
64+
65+
private static String getOpenTelemetryApiVersion() {
66+
Map<String, String> properties =
67+
CoreUtils.getProperties("io/opentelemetry/api/version.properties");
68+
if (properties == null) {
69+
return UNKNOWN_VERSION_VALUE;
70+
}
71+
String version = properties.get("sdk.version");
72+
return version != null ? version : UNKNOWN_VERSION_VALUE;
73+
}
74+
75+
private VersionGenerator() {
76+
}
77+
}

sdk/monitor/azure-monitor-opentelemetry-exporter/src/main/java/com/azure/monitor/opentelemetry/exporter/utils/package-info.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/MonitorExporterClientTestBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import com.azure.monitor.opentelemetry.exporter.implementation.models.MonitorDomain;
1616
import com.azure.monitor.opentelemetry.exporter.implementation.models.RequestData;
1717
import com.azure.monitor.opentelemetry.exporter.implementation.models.TelemetryItem;
18-
import com.azure.monitor.opentelemetry.exporter.utils.FormattedDuration;
18+
import com.azure.monitor.opentelemetry.exporter.implementation.FormattedDuration;
1919

2020
import java.time.Duration;
2121
import java.time.OffsetDateTime;

sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/FormattedDurationTest.java renamed to sdk/monitor/azure-monitor-opentelemetry-exporter/src/test/java/com/azure/monitor/opentelemetry/exporter/implementation/FormattedDurationTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
package com.azure.monitor.opentelemetry.exporter;
4+
package com.azure.monitor.opentelemetry.exporter.implementation;
55

6-
import com.azure.monitor.opentelemetry.exporter.utils.FormattedDuration;
76
import org.junit.jupiter.api.Test;
87

98
import static org.junit.jupiter.api.Assertions.assertEquals;
109

1110
public class FormattedDurationTest {
1211
@Test
13-
void testGetFormattedDurationMilliSeconds() {
12+
public void testGetFormattedDurationMilliSeconds() {
1413
String formattedDuration = FormattedDuration.getFormattedDuration(42657024);
1514
assertEquals("00:00:00.042657", formattedDuration);
1615
}
1716

1817
@Test
19-
void testGetFormattedDurationSeconds() {
18+
public void testGetFormattedDurationSeconds() {
2019
String formattedDuration = FormattedDuration.getFormattedDuration(42657024000L);
2120
assertEquals("00:00:42.657024", formattedDuration);
2221
}
2322

2423
@Test
25-
void testGetFormattedDurationMinutes() {
24+
public void testGetFormattedDurationMinutes() {
2625
String formattedDuration = FormattedDuration.getFormattedDuration(426570240000L);
2726
assertEquals("00:07:06.570240", formattedDuration);
2827
}
2928

3029
@Test
31-
void testGetFormattedDurationHours() {
30+
public void testGetFormattedDurationHours() {
3231
String formattedDuration = FormattedDuration.getFormattedDuration(4265702400000L);
3332
assertEquals("01:11:05.702400", formattedDuration);
3433
}
3534

3635
@Test
37-
void testGetFormattedDurationDays() {
36+
public void testGetFormattedDurationDays() {
3837
String formattedDuration = FormattedDuration.getFormattedDuration(426570240000000L);
3938
assertEquals("4.22:29:30.240000", formattedDuration);
4039
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.azure.monitor.opentelemetry.exporter.implementation;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
public class VersionTest {
9+
@Test
10+
public void testArtifactName() {
11+
assertEquals("azure-monitor-opentelemetry-exporter", VersionGenerator.getArtifactName());
12+
}
13+
14+
@Test
15+
public void testArtifactVersion() {
16+
assertTrue(VersionGenerator.getArtifactVersion().matches("[0-9].[0-9].[0-9].*"));
17+
}
18+
19+
@Test
20+
public void testSdkVersion() {
21+
// OpenTelemetry added version.properties files in 1.3.0
22+
// but testing against OpenTelemetry 1.0.0, so it's unknown in this test
23+
assertTrue(VersionGenerator.getSdkVersion().matches("java[0-9._]+:ot([0-9.]+|unknown):ext[0-9.]+.*"));
24+
}
25+
}

0 commit comments

Comments
 (0)