Skip to content

Export traces from containerized app run by AppRunner to X-Ray

0

Hi, I'm banging my head against my current setup and can't tell if I'm making progress or regress.

I have a dockerised rust application which is instrumented by some opentelemetry-* crates. When I start a local jaeger instance, the traces from my app are exported to http://127.0.0.1:4317. Everything looks fine.

Now I'd like to see replicate this setup in aws and see the traces in aws x-ray. So I followed the instructions on https://docs.aws.amazon.com/apprunner/latest/dg/monitor-xray.html.

The relevant cdk part looks like this:

    const appRunnerInstanceRole = new iam.Role(
      this,
      `${this.stackName}-apprunner-instance-role`,
      {
        assumedBy: new iam.ServicePrincipal("tasks.apprunner.amazonaws.com"),
        description: `${this.stackName}-apprunner-instance-role`,
        inlinePolicies: {
          "update-service-apprunner-instance-policy": new iam.PolicyDocument({
            statements: [
              new iam.PolicyStatement({
                effect: iam.Effect.ALLOW,
                // definition https://docs.aws.amazon.com/aws-managed-policy/latest/reference/AWSXRayDaemonWriteAccess.html
                actions: [
                  "xray:PutTraceSegments",
                  "xray:PutTelemetryRecords",
                  "xray:GetSamplingRules",
                  "xray:GetSamplingTargets",
                  "xray:GetSamplingStatisticSummaries",
                ],
                resources: ["*"],
              }),
            ],
          }),
        },
      },
    );

    const observabilityConfig = new apprunner.CfnObservabilityConfiguration(
      this,
      "ObservabilityConfig",
      {
        observabilityConfigurationName: "apprunner-observability-config",
        traceConfiguration: {
          vendor: "AWSXRAY",
        },
      },
    );

    const appRunnerService = new apprunner.CfnService(
      this,
      `${this.stackName}-apprunner-service`,
      {
        serviceName: props.appName,
        observabilityConfiguration: {
          observabilityEnabled: true,
          observabilityConfigurationArn:
            observabilityConfig.attrObservabilityConfigurationArn,
        },
        sourceConfiguration: {
          authenticationConfiguration: {
            accessRoleArn: appRunnerAccessRole.roleArn,
          },
          autoDeploymentsEnabled: true,
          imageRepository: {
            imageIdentifier: `${this.account}.dkr.ecr.${this.region}.amazonaws.com/${props.appName}:latest`,
            imageRepositoryType: "ECR",
            imageConfiguration: {
              port: "8000",
              runtimeEnvironmentVariables: [
                ...
              ],
            },
          },
        },
        instanceConfiguration: {
          instanceRoleArn: appRunnerInstanceRole.roleArn,
          cpu: "1024",
          memory: "2048",
        },
        healthCheckConfiguration: {
          protocol: "HTTP",
          path: "/health",
        },
        networkConfiguration: {
          egressConfiguration: {
            egressType: "VPC",
            vpcConnectorArn: vpcConnector.attrVpcConnectorArn,
          },
        },
      },
    );

The Dockerfile looks like the following:

...

FROM debian:bookworm-slim as runtime

WORKDIR /app

RUN apt-get update -y \
  && apt-get install -y --no-install-recommends openssl ca-certificates \
  # Clean up
  && apt-get autoremove -y \
  && apt-get clean -y \
  && rm -rf /var/lib/apt/lists/*

COPY --from=builder /app/target/release/api api
ENV APP_ENVIRONMENT production
EXPOSE 8000

ENTRYPOINT ["./api"]

My application is trying to send logs to http://localhost:4317 because I've seen it in the aws doc I mentioned earlier. But all I can see in the logs is

{
   ...
    "msg": "Failed during the export process",
    "hostname": ...,
    "file": ...
    "reason": "ExportFailed(Status { code: Cancelled, message: \", detailed error message: Timeout expired\" })"
}

What exactly happens under the hood when observabilityEnabled is enabled? What steps are needed to receive the traces in X-Ray? Maybe there is a relevant repo that could point me in the right direction?

I'd appreciate any help I can get.

1 Answer
0

Thank you for providing such detailed information about your setup. Let me help you understand what's happening and provide some guidance on how to proceed with exporting traces from your containerized app run by AppRunner to X-Ray.

When you enable observability with X-Ray in App Runner, several things happen under the hood:

  1. App Runner sets up the necessary infrastructure to collect and send traces to X-Ray.
  2. It uses the AWS Distro for OpenTelemetry (ADOT) to integrate with X-Ray.
  3. The X-Ray daemon is automatically deployed as part of your App Runner service.

The error you're seeing suggests that your application is trying to send traces to http://localhost:4317, but it's not able to connect. This is likely because the X-Ray daemon is not listening on that specific port in the App Runner environment.

To resolve this issue and properly export traces to X-Ray, you should make the following adjustments:

  1. Instead of sending traces directly to http://localhost:4317, you should configure your application to use the AWS X-Ray SDK or the ADOT SDK for trace collection.

  2. Update your Dockerfile to include the necessary OTEL dependencies. You can do this by adding the following to your Dockerfile:

    RUN apt-get update && apt-get install -y curl
    RUN curl -L https://github.com/aws-observability/aws-otel-collector/releases/latest/download/aws-otel-collector-linux-amd64.tar.gz | tar -xz
    
  3. Set up the required environment variables in your App Runner service configuration. Add these to your runtimeEnvironmentVariables in the CDK code:

    runtimeEnvironmentVariables: [
      { name: "OTEL_EXPORTER_OTLP_ENDPOINT", value: "http://localhost:4317" },
      { name: "OTEL_RESOURCE_ATTRIBUTES", value: "service.name=YOUR_SERVICE_NAME" },
      { name: "AWS_REGION", value: this.region },
    ],
  4. In your Rust application, make sure you're using the OpenTelemetry SDK with the OTLP exporter configured to send traces to the X-Ray daemon. You might need to update your dependencies and code to ensure compatibility with the ADOT setup.

  5. Ensure that your App Runner instance role (appRunnerInstanceRole in your CDK code) has the necessary permissions to write to X-Ray. You've already included the correct permissions in your code, which is good.

By making these changes, your application should be able to send traces to the X-Ray daemon, which will then forward them to the X-Ray service. You can then view your traces in the AWS X-Ray console or through the App Runner console's Observability tab.

Remember to rebuild and redeploy your application after making these changes. If you continue to experience issues, you may want to check the App Runner logs for any additional error messages or consult the AWS documentation for troubleshooting steps specific to App Runner and X-Ray integration.
Sources
Tracing for your App Runner application with X-Ray - AWS App Runner
AWS App Runner and X-Ray - AWS X-Ray
Release: App Runner launches built-in X-Ray support on April 12, 2022 - AWS App Runner
enum TraceConfigurationVendor · AWS CDK

answered 2 years ago

  • Instead of sending traces directly to http://localhost:4317, you should configure your application to use the AWS X-Ray SDK or the ADOT SDK for trace collection.

    Both are not available for rust lang.

    Would it make more sense to send traces via http to the https://xray.AWS Region.amazonaws.com/v1/traces endpoint?

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.