Skip to content

Commit 1555fc6

Browse files
committed
javadocs, deps and class restrictions, unit tests
1 parent 68fc80c commit 1555fc6

File tree

5 files changed

+62
-10
lines changed

5 files changed

+62
-10
lines changed

awsagentprovider/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ dependencies {
4141
// Export configuration
4242
compileOnly("io.opentelemetry:opentelemetry-exporter-otlp")
4343
// For Udp emitter
44-
implementation("io.opentelemetry:opentelemetry-exporter-otlp-common")
45-
implementation("io.opentelemetry:opentelemetry-exporter-common")
44+
compileOnly("io.opentelemetry:opentelemetry-exporter-otlp-common")
4645

4746
testImplementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure")
4847
testImplementation("io.opentelemetry:opentelemetry-sdk-testing")
4948
testImplementation("io.opentelemetry:opentelemetry-extension-aws")
5049
testImplementation("io.opentelemetry:opentelemetry-extension-trace-propagators")
5150
testImplementation("com.google.guava:guava")
51+
testRuntimeOnly("io.opentelemetry:opentelemetry-exporter-otlp-common")
5252

5353
compileOnly("com.google.code.findbugs:jsr305:3.0.2")
5454
testImplementation("org.mockito:mockito-core:5.3.1")

awsagentprovider/src/main/java/software/amazon/opentelemetry/javaagent/providers/OtlpUdpSpanExporter.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,26 @@
2828
import java.util.logging.Logger;
2929
import javax.annotation.concurrent.Immutable;
3030

31+
/**
32+
* Exports spans via UDP, using OpenTelemetry's protobuf model. The protobuf modelled spans are
33+
* Base64 encoded and prefixed with AWS X-Ray specific information before being sent over to {@link
34+
* UdpSender}.
35+
*
36+
* <p>This exporter is NOT meant for generic use since the payload is prefixed with AWS X-Ray
37+
* specific information.
38+
*/
3139
@Immutable
32-
public class OtlpUdpSpanExporter implements SpanExporter {
40+
class OtlpUdpSpanExporter implements SpanExporter {
3341

3442
private static final Logger logger = Logger.getLogger(OtlpUdpSpanExporter.class.getName());
43+
44+
// The protocol header and delimiter is required for sending data to X-Ray Daemon or when running
45+
// in Lambda.
46+
// https://docs.aws.amazon.com/xray/latest/devguide/xray-api-sendingdata.html#xray-api-daemon
3547
private static final String PROTOCOL_HEADER = "{\"format\": \"json\", \"version\": 1}";
3648
private static final char PROTOCOL_DELIMITER = '\n';
49+
50+
// These prefixes help the backend identify if the spans payload is sampled or not.
3751
private static final String FORMAT_OTEL_SAMPLED_TRACES_BINARY_PREFIX = "T1S";
3852
private static final String FORMAT_OTEL_UNSAMPLED_TRACES_BINARY_PREFIX = "T1U";
3953

awsagentprovider/src/main/java/software/amazon/opentelemetry/javaagent/providers/OtlpUdpSpanExporterBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import java.net.SocketException;
2121

22-
public final class OtlpUdpSpanExporterBuilder {
22+
final class OtlpUdpSpanExporterBuilder {
2323

2424
private static final String DEFAULT_HOST = "127.0.0.1";
2525
private static final int DEFAULT_PORT = 2000;

awsagentprovider/src/main/java/software/amazon/opentelemetry/javaagent/providers/UdpSender.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,22 @@
2424
import java.util.logging.Level;
2525
import java.util.logging.Logger;
2626

27-
public class UdpSender {
27+
/**
28+
* This class represents a UDP sender that sends data to a specified endpoint. It is used to send
29+
* data to a remote host and port using UDP protocol.
30+
*/
31+
class UdpSender {
2832
private static final Logger logger = Logger.getLogger(UdpSender.class.getName());
2933

30-
private final DatagramSocket socket;
34+
private DatagramSocket socket;
3135
private final InetSocketAddress endpoint;
3236

3337
public UdpSender(String host, int port) throws SocketException {
3438
this.endpoint = new InetSocketAddress(host, port);
3539
try {
36-
socket = new DatagramSocket();
40+
this.socket = new DatagramSocket();
3741
} catch (SocketException e) {
3842
logger.log(Level.SEVERE, "Exception while instantiating UdpSender socket.", e);
39-
throw e;
4043
}
4144
}
4245

awsagentprovider/src/test/java/software/amazon/opentelemetry/javaagent/providers/UdpExporterTest.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.opentelemetry.sdk.resources.Resource;
2727
import io.opentelemetry.sdk.trace.data.SpanData;
2828
import io.opentelemetry.sdk.trace.data.StatusData;
29+
import java.nio.charset.StandardCharsets;
2930
import java.util.Collections;
3031
import org.junit.jupiter.api.Test;
3132

@@ -62,7 +63,7 @@ public void testUdpExporterWithInvalidEndpoint() {
6263
}
6364

6465
@Test
65-
public void testExport() {
66+
public void testExportDefaultBehavior() {
6667
UdpSender senderMock = mock(UdpSender.class);
6768

6869
// mock SpanData
@@ -73,9 +74,43 @@ public void testExport() {
7374

7475
// assert that the senderMock.send is called once
7576
verify(senderMock, times(1)).send(any(byte[].class));
77+
verify(senderMock)
78+
.send(
79+
argThat(
80+
(byte[] bytes) -> {
81+
assertThat(bytes.length).isGreaterThan(0);
82+
String payload = new String(bytes, StandardCharsets.UTF_8);
83+
assertThat(payload)
84+
.startsWith("{\"format\": \"json\", \"version\": 1}" + "\n" + "T1S");
85+
return true;
86+
}));
7687
}
7788

78-
private static SpanData buildSpanDataMock() {
89+
@Test
90+
public void testExportWithSampledFalse() {
91+
UdpSender senderMock = mock(UdpSender.class);
92+
93+
// mock SpanData
94+
SpanData spanData = buildSpanDataMock();
95+
96+
OtlpUdpSpanExporter exporter =
97+
new OtlpUdpSpanExporterBuilder().setSender(senderMock).setSampled(false).build();
98+
exporter.export(Collections.singletonList(spanData));
99+
100+
verify(senderMock, times(1)).send(any(byte[].class));
101+
verify(senderMock)
102+
.send(
103+
argThat(
104+
(byte[] bytes) -> {
105+
assertThat(bytes.length).isGreaterThan(0);
106+
String payload = new String(bytes, StandardCharsets.UTF_8);
107+
assertThat(payload)
108+
.startsWith("{\"format\": \"json\", \"version\": 1}" + "\n" + "T1U");
109+
return true;
110+
}));
111+
}
112+
113+
private SpanData buildSpanDataMock() {
79114
SpanData mockSpanData = mock(SpanData.class);
80115

81116
Attributes spanAttributes =

0 commit comments

Comments
 (0)