Skip to content

Commit

Permalink
Adding additional check to SpanExporterBuilder to see if we are alrea…
Browse files Browse the repository at this point in the history
…dy working in Lambda environment or not
  • Loading branch information
Jeel Mehta committed Mar 5, 2025
1 parent 3dbbae0 commit 57cf7b3
Showing 1 changed file with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +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;

public final class OtlpUdpSpanExporterBuilder {

Expand All @@ -35,6 +37,9 @@ public final class OtlpUdpSpanExporterBuilder {
private UdpSender sender;
private String tracePayloadPrefix = FORMAT_OTEL_SAMPLED_TRACES_BINARY_PREFIX;

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";

public OtlpUdpSpanExporterBuilder setEndpoint(String endpoint) {
requireNonNull(endpoint, "endpoint must not be null");
try {
Expand All @@ -58,10 +63,34 @@ public OtlpUdpSpanExporterBuilder setPayloadSampleDecision(TracePayloadSampleDec

public OtlpUdpSpanExporter build() {
if (sender == null) {
this.sender = new UdpSender(DEFAULT_HOST, DEFAULT_PORT);
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
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;
}

// Only for testing
Expand Down

0 comments on commit 57cf7b3

Please sign in to comment.