Skip to content

aws lambda CustomRuntimeEventLoop should propagate the tracing header #987

@amondnet

Description

@amondnet

Is your feature request related to a problem? Please describe.
We cannot use x-ray when using a custom runtime event loop.

Describe the solution you'd like

https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html

Get the X-Ray tracing header from the Lambda-Runtime-Trace-Id header in the API response. Set the _X_AMZN_TRACE_ID environment variable locally or com.amazonaws.xray.traceHeader system property with the same value. The X-Ray SDK uses this value to connect trace data between services.

String traceId = response.getHeaders().getFirst("Lambda-Runtime-Trace-Id");
if (traceId != null) {
	if (logger.isDebugEnabled()) {
		logger.debug("Lambda-Runtime-Trace-Id: " + traceId);
	}
	try {
		// The X-Ray SDK uses this value to connect trace data between services.
		System.setProperty("com.amazonaws.xray.traceHeader", traceId);
	} catch (Exception e) {
		if (logger.isDebugEnabled()) {
			logger.debug("Failed to set amazon x-ray trace id", e);
		}
	}
}

Describe alternatives you've considered
We can also set an environment variable, but in that case we must use reflection. By default, environment variables are immutable.

  private static Map<String, String> getModifiableEnvironment() throws Exception {
    Class pe = Class.forName("java.lang.ProcessEnvironment");
    Method getenv = pe.getDeclaredMethod("getenv");
    getenv.setAccessible(true);
    Object unmodifiableEnvironment = getenv.invoke(null);
    Class map = Class.forName("java.util.Collections$UnmodifiableMap");
    Field m = map.getDeclaredField("m");
    m.setAccessible(true);
    return (Map) m.get(unmodifiableEnvironment);
  }

Additional context

https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html#runtimes-api-next
aws/aws-sdk-java#2298 (comment)
aws/aws-xray-sdk-java#252
aws/aws-xray-sdk-java#251

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions