Skip to content

Commit

Permalink
Adding the unit test for Lambda environment variables and fixing the …
Browse files Browse the repository at this point in the history
…version in the build file
  • Loading branch information
Jeel Mehta committed Mar 6, 2025
1 parent 57cf7b3 commit c8580e2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 32 deletions.
8 changes: 1 addition & 7 deletions exporters/aws-otel-otlp-udp-exporter/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ plugins {
}

group = "software.opentelemetry.exporters.otlp.udp"
version = "1.0-SNAPSHOT"
version = "0.0.1"

repositories {
mavenLocal()
Expand Down Expand Up @@ -63,17 +63,11 @@ sourceSets {
java {
srcDirs("src/main/java")
}
resources {
srcDirs("src/main/resources")
}
}
test {
java {
srcDirs("src/test/java")
}
resources {
srcDirs("src/test/resources")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package software.opentelemetry.exporters.otlp.udp;

import static java.util.Objects.requireNonNull;
//import static software.amazon.opentelemetry.javaagent.providers.AwsApplicationSignalsCustomizerProvider.AWS_LAMBDA_FUNCTION_NAME_CONFIG;
//import static software.amazon.opentelemetry.javaagent.providers.AwsApplicationSignalsCustomizerProvider.AWS_XRAY_DAEMON_ADDRESS_CONFIG;

import java.util.Map;

public final class OtlpUdpSpanExporterBuilder {

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

private UdpSender sender;
private String tracePayloadPrefix = FORMAT_OTEL_SAMPLED_TRACES_BINARY_PREFIX;
private Map<String, String> environmentVariables = System.getenv();

private static final String AWS_LAMBDA_FUNCTION_NAME_CONFIG = "AWS_LAMBDA_FUNCTION_NAME";
private static final String AWS_XRAY_DAEMON_ADDRESS_CONFIG = "AWS_XRAY_DAEMON_ADDRESS";
Expand All @@ -61,36 +62,49 @@ public OtlpUdpSpanExporterBuilder setPayloadSampleDecision(TracePayloadSampleDec
return this;
}

// For testing purposes
public OtlpUdpSpanExporterBuilder withEnvironmentVariables(Map<String, String> env) {
this.environmentVariables = env;
return this;
}

// NEW: Added getter for testing
Map<String, String> getEnvironmentVariables() {
return environmentVariables;
}

public OtlpUdpSpanExporter build() {
if (sender == null) {
String endpoint = null;

// If in Lambda environment, try to get X-Ray daemon address
if (isLambdaEnvironment()) {
endpoint = System.getenv(AWS_XRAY_DAEMON_ADDRESS_CONFIG);
}

if (endpoint != null) {
// Use the endpoint from Lambda environment
try {
String[] parts = endpoint.split(":");
String host = parts[0];
int port = Integer.parseInt(parts[1]);
this.sender = new UdpSender(host, port);
} catch (Exception e) {
// Fallback to defaults if parsing fails
this.sender = new UdpSender(DEFAULT_HOST, DEFAULT_PORT);
}
} else {
// Use defaults if not in Lambda or if daemon address is not available
String endpoint = null;

// If in Lambda environment, try to get X-Ray daemon address
if (isLambdaEnvironment()) {
endpoint = environmentVariables.get(AWS_XRAY_DAEMON_ADDRESS_CONFIG);
if (endpoint != null && !endpoint.isEmpty()) {
try {
String[] parts = endpoint.split(":");
String host = parts[0];
int port = Integer.parseInt(parts[1]);
this.sender = new UdpSender(host, port);
return new OtlpUdpSpanExporter(
this.sender, PROTOCOL_HEADER + PROTOCOL_DELIMITER + tracePayloadPrefix);
} catch (Exception e) {
// Fallback to defaults if parsing fails
this.sender = new UdpSender(DEFAULT_HOST, DEFAULT_PORT);
}
}
}

// Use defaults if not in Lambda or if daemon address is invalid/unavailable
this.sender = new UdpSender(DEFAULT_HOST, DEFAULT_PORT);
}
return new OtlpUdpSpanExporter(
this.sender, PROTOCOL_HEADER + PROTOCOL_DELIMITER + tracePayloadPrefix);
}
private static boolean isLambdaEnvironment() {
return System.getenv(AWS_LAMBDA_FUNCTION_NAME_CONFIG) != null;
}

private boolean isLambdaEnvironment() {
String functionName = environmentVariables.get(AWS_LAMBDA_FUNCTION_NAME_CONFIG);
return functionName != null && !functionName.isEmpty();
}

// Only for testing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import io.opentelemetry.sdk.trace.data.StatusData;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;

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

@Test
public void shouldUseExpectedEnvironmentVariablesToConfigureEndpoint() {
// Create a test environment map
Map<String, String> testEnv = new HashMap<>();
testEnv.put("AWS_LAMBDA_FUNCTION_NAME", "testFunctionName");
testEnv.put("AWS_XRAY_DAEMON_ADDRESS", "someaddress:1234");

// Create builder with test environment
OtlpUdpSpanExporterBuilder builder =
new OtlpUdpSpanExporterBuilder().withEnvironmentVariables(testEnv);

// Verify that environment variables are set correctly
assertThat(builder.getEnvironmentVariables())
.containsEntry("AWS_LAMBDA_FUNCTION_NAME", "testFunctionName")
.containsEntry("AWS_XRAY_DAEMON_ADDRESS", "someaddress:1234");

// Build the exporter and verify the configuration
OtlpUdpSpanExporter exporter = builder.build();
UdpSender sender = exporter.getSender();

assertThat(sender.getEndpoint().getHostName()).isEqualTo("someaddress");
assertThat(sender.getEndpoint().getPort()).isEqualTo(1234);
}

@Test
public void testExportDefaultBehavior() {
UdpSender senderMock = mock(UdpSender.class);
Expand Down

0 comments on commit c8580e2

Please sign in to comment.