Skip to content

Commit c8580e2

Browse files
author
Jeel Mehta
committed
Adding the unit test for Lambda environment variables and fixing the version in the build file
1 parent 57cf7b3 commit c8580e2

File tree

3 files changed

+66
-32
lines changed

3 files changed

+66
-32
lines changed

exporters/aws-otel-otlp-udp-exporter/build.gradle.kts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ plugins {
2020
}
2121

2222
group = "software.opentelemetry.exporters.otlp.udp"
23-
version = "1.0-SNAPSHOT"
23+
version = "0.0.1"
2424

2525
repositories {
2626
mavenLocal()
@@ -63,17 +63,11 @@ sourceSets {
6363
java {
6464
srcDirs("src/main/java")
6565
}
66-
resources {
67-
srcDirs("src/main/resources")
68-
}
6966
}
7067
test {
7168
java {
7269
srcDirs("src/test/java")
7370
}
74-
resources {
75-
srcDirs("src/test/resources")
76-
}
7771
}
7872
}
7973

exporters/aws-otel-otlp-udp-exporter/src/main/java/software/opentelemetry/exporters/otlp/udp/OtlpUdpSpanExporterBuilder.java

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
package software.opentelemetry.exporters.otlp.udp;
1717

1818
import static java.util.Objects.requireNonNull;
19-
//import static software.amazon.opentelemetry.javaagent.providers.AwsApplicationSignalsCustomizerProvider.AWS_LAMBDA_FUNCTION_NAME_CONFIG;
20-
//import static software.amazon.opentelemetry.javaagent.providers.AwsApplicationSignalsCustomizerProvider.AWS_XRAY_DAEMON_ADDRESS_CONFIG;
19+
20+
import java.util.Map;
2121

2222
public final class OtlpUdpSpanExporterBuilder {
2323

@@ -36,6 +36,7 @@ public final class OtlpUdpSpanExporterBuilder {
3636

3737
private UdpSender sender;
3838
private String tracePayloadPrefix = FORMAT_OTEL_SAMPLED_TRACES_BINARY_PREFIX;
39+
private Map<String, String> environmentVariables = System.getenv();
3940

4041
private static final String AWS_LAMBDA_FUNCTION_NAME_CONFIG = "AWS_LAMBDA_FUNCTION_NAME";
4142
private static final String AWS_XRAY_DAEMON_ADDRESS_CONFIG = "AWS_XRAY_DAEMON_ADDRESS";
@@ -61,36 +62,49 @@ public OtlpUdpSpanExporterBuilder setPayloadSampleDecision(TracePayloadSampleDec
6162
return this;
6263
}
6364

65+
// For testing purposes
66+
public OtlpUdpSpanExporterBuilder withEnvironmentVariables(Map<String, String> env) {
67+
this.environmentVariables = env;
68+
return this;
69+
}
70+
71+
// NEW: Added getter for testing
72+
Map<String, String> getEnvironmentVariables() {
73+
return environmentVariables;
74+
}
75+
6476
public OtlpUdpSpanExporter build() {
6577
if (sender == null) {
66-
String endpoint = null;
67-
68-
// If in Lambda environment, try to get X-Ray daemon address
69-
if (isLambdaEnvironment()) {
70-
endpoint = System.getenv(AWS_XRAY_DAEMON_ADDRESS_CONFIG);
71-
}
72-
73-
if (endpoint != null) {
74-
// Use the endpoint from Lambda environment
75-
try {
76-
String[] parts = endpoint.split(":");
77-
String host = parts[0];
78-
int port = Integer.parseInt(parts[1]);
79-
this.sender = new UdpSender(host, port);
80-
} catch (Exception e) {
81-
// Fallback to defaults if parsing fails
82-
this.sender = new UdpSender(DEFAULT_HOST, DEFAULT_PORT);
83-
}
84-
} else {
85-
// Use defaults if not in Lambda or if daemon address is not available
78+
String endpoint = null;
79+
80+
// If in Lambda environment, try to get X-Ray daemon address
81+
if (isLambdaEnvironment()) {
82+
endpoint = environmentVariables.get(AWS_XRAY_DAEMON_ADDRESS_CONFIG);
83+
if (endpoint != null && !endpoint.isEmpty()) {
84+
try {
85+
String[] parts = endpoint.split(":");
86+
String host = parts[0];
87+
int port = Integer.parseInt(parts[1]);
88+
this.sender = new UdpSender(host, port);
89+
return new OtlpUdpSpanExporter(
90+
this.sender, PROTOCOL_HEADER + PROTOCOL_DELIMITER + tracePayloadPrefix);
91+
} catch (Exception e) {
92+
// Fallback to defaults if parsing fails
8693
this.sender = new UdpSender(DEFAULT_HOST, DEFAULT_PORT);
94+
}
8795
}
96+
}
97+
98+
// Use defaults if not in Lambda or if daemon address is invalid/unavailable
99+
this.sender = new UdpSender(DEFAULT_HOST, DEFAULT_PORT);
88100
}
89101
return new OtlpUdpSpanExporter(
90102
this.sender, PROTOCOL_HEADER + PROTOCOL_DELIMITER + tracePayloadPrefix);
91-
}
92-
private static boolean isLambdaEnvironment() {
93-
return System.getenv(AWS_LAMBDA_FUNCTION_NAME_CONFIG) != null;
103+
}
104+
105+
private boolean isLambdaEnvironment() {
106+
String functionName = environmentVariables.get(AWS_LAMBDA_FUNCTION_NAME_CONFIG);
107+
return functionName != null && !functionName.isEmpty();
94108
}
95109

96110
// Only for testing

exporters/aws-otel-otlp-udp-exporter/src/test/java/software/opentelemetry/exporters/otlp/udp/UdpExporterTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import io.opentelemetry.sdk.trace.data.StatusData;
2929
import java.nio.charset.StandardCharsets;
3030
import java.util.Collections;
31+
import java.util.HashMap;
32+
import java.util.Map;
3133
import org.junit.jupiter.api.Test;
3234

3335
public class UdpExporterTest {
@@ -65,6 +67,30 @@ public void testUdpExporterWithInvalidEndpoint() {
6567
.hasMessage("Invalid endpoint, must be a valid URL: invalidhost");
6668
}
6769

70+
@Test
71+
public void shouldUseExpectedEnvironmentVariablesToConfigureEndpoint() {
72+
// Create a test environment map
73+
Map<String, String> testEnv = new HashMap<>();
74+
testEnv.put("AWS_LAMBDA_FUNCTION_NAME", "testFunctionName");
75+
testEnv.put("AWS_XRAY_DAEMON_ADDRESS", "someaddress:1234");
76+
77+
// Create builder with test environment
78+
OtlpUdpSpanExporterBuilder builder =
79+
new OtlpUdpSpanExporterBuilder().withEnvironmentVariables(testEnv);
80+
81+
// Verify that environment variables are set correctly
82+
assertThat(builder.getEnvironmentVariables())
83+
.containsEntry("AWS_LAMBDA_FUNCTION_NAME", "testFunctionName")
84+
.containsEntry("AWS_XRAY_DAEMON_ADDRESS", "someaddress:1234");
85+
86+
// Build the exporter and verify the configuration
87+
OtlpUdpSpanExporter exporter = builder.build();
88+
UdpSender sender = exporter.getSender();
89+
90+
assertThat(sender.getEndpoint().getHostName()).isEqualTo("someaddress");
91+
assertThat(sender.getEndpoint().getPort()).isEqualTo(1234);
92+
}
93+
6894
@Test
6995
public void testExportDefaultBehavior() {
7096
UdpSender senderMock = mock(UdpSender.class);

0 commit comments

Comments
 (0)