Skip to content

Incorrectly nesting fetch requests #715

Description

@ChuckJonas

I have two output fetch requests that happen one after another.

  1. A call to lamdba layer to get secrets
  2. A call to an external API

For some reason, the external API call is getting nested under the localhost segment

Image

I've made sure the close the secrets segment and event tried flushing it. It seems like it's probably happening outside my custom segments

import * as AWSXRay from "aws-xray-sdk";

type Secret = {
  SecretString: string;
};

export async function getSecrets(secretId: string): Promise<string> {
  const rootSegment = AWSXRay.getSegment();
  const subsegment = rootSegment?.addNewSubsegment("Secrets Manager");

  try {
    subsegment?.addAnnotation("secretId", secretId);


    const uri = `http://localhost:2773/secretsmanager/get?secretId=${secretId}`;
    const res = await fetch(uri, {
      headers: {
        "X-Aws-Parameters-Secrets-Token": process.env.AWS_SESSION_TOKEN!,
      },
    });

    subsegment?.addMetadata("request", {
      url: uri,
      method: "GET",
    });

    const data = (await res.json()) as Secret;
    return data["SecretString"];
  } catch (error) {
    subsegment?.addError(error as Error);
    throw error;
  } finally {
    subsegment?.close();
    subsegment?.flush();
  }
}
// xray init, called at top of lambda
import * as AWSXRay from "aws-xray-sdk";

import { captureFetchGlobal } from "aws-xray-sdk-fetch";

export const initXRay = () => {
  AWSXRay.captureHTTPsGlobal(require("http"));
  AWSXRay.captureHTTPsGlobal(require("https"));
  captureFetchGlobal();
};

Raw Trace

    {
            "Id": "c5a8d4141cdf256a",
            "Document": {
                "id": "c5a8d4141cdf256a",
                "name": "dev-eg-practifi-braze",
                "start_time": 1744472658.073189,
                "trace_id": "1-67fa8a51-6a8f27832663e1347fae739c",
                "end_time": 1744472660.633492,
                "parent_id": "33ca25824e061177",
                "annotations": {
                    "aws:responseLatency": 2540.15,
                    "aws:runtimeOverhead": 19.412,
                    "aws:extensionOverhead": 0.004,
                    "aws:responseDuration": 0.081
                },
                "origin": "AWS::Lambda::Function",
                "subsegments": [
                    {
                        "id": "a102671b59c8c6de",
                        "name": "Overhead",
                        "start_time": 1744472660.612,
                        "end_time": 1744472660.632004
                    },
                    {
                        "id": "143fee48aa516ab3",
                        "name": "localhost",
                        "start_time": 1744472658.151,
                        "end_time": 1744472659.152,
                        "http": {
                            "request": {
                                "url": "http://localhost:2773/secretsmanager/",
                                "method": "GET"
                            },
                            "response": {
                                "status": 200,
                                "content_length": 323
                            }
                        },
                        "namespace": "remote",
                        "subsegments": [
                            {
                                "id": "a55e83fa4b9460fa",
                                "name": "rest.iad-05.braze.com",
                                "start_time": 1744472659.234,
                                "end_time": 1744472660.507,
                                "http": {
                                    "request": {
                                        "url": "https://rest.iad-05.braze.com/users/track/sync",
                                        "method": "POST"
                                    },
                                    "response": {
                                        "status": 201
                                    }
                                },
                                "namespace": "remote"
                            },
                            {
                                "id": "5180154238c3762f",
                                "name": "Braze API",
                                "start_time": 1744472659.233,
                                "end_time": 1744472660.572,
                                "annotations": {
                                    "method": "track user"
                                }
                            }
                        ]
                    },
                    {
                        "id": "507c230d4d8340de",
                        "name": "Init",
                        "start_time": 1744472657.479804,
                        "end_time": 1744472658.068975
                    },
                    {
                        "id": "ae0087abd50d0eae",
                        "name": "Secrets Manager",
                        "start_time": 1744472658.112,
                        "end_time": 1744472659.231,
                        "annotations": {
                            "secretId": "arn:aws:secretsmanager"
                        }
                    }
                ]
            }
        },

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions