|
| 1 | +# Send traces and query them with AWS X-Ray |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +In this tutorial, you use the AWS CLI to send trace segments to AWS X-Ray, query trace summaries, retrieve full trace details, create a trace group with a filter expression, and view the service graph. You then delete the group during cleanup. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +- AWS CLI installed and configured with appropriate permissions. |
| 10 | +- Python 3 installed (used to generate trace IDs and timestamps). |
| 11 | +- An IAM principal with permissions for `xray:PutTraceSegments`, `xray:GetTraceSummaries`, `xray:BatchGetTraces`, `xray:CreateGroup`, `xray:DeleteGroup`, and `xray:GetServiceGraph`. |
| 12 | + |
| 13 | +## Step 1: Send a trace segment |
| 14 | + |
| 15 | +Generate a trace ID and send a segment representing an HTTP request, followed by a subsegment representing a downstream call. |
| 16 | + |
| 17 | +```bash |
| 18 | +TRACE_ID=$(python3 -c "import time,random;print(f'1-{int(time.time()):08x}-{random.randbytes(12).hex()}')") |
| 19 | +NOW=$(python3 -c "import time;print(time.time())") |
| 20 | +END=$(python3 -c "import time;print(time.time()+0.5)") |
| 21 | + |
| 22 | +SEGMENT="{\"trace_id\":\"$TRACE_ID\",\"id\":\"$(openssl rand -hex 8)\",\"name\":\"tutorial-service\",\"start_time\":$NOW,\"end_time\":$END,\"http\":{\"request\":{\"method\":\"GET\",\"url\":\"https://example.com/api/items\"},\"response\":{\"status\":200}}}" |
| 23 | + |
| 24 | +aws xray put-trace-segments --trace-segment-documents "$SEGMENT" |
| 25 | +``` |
| 26 | + |
| 27 | +X-Ray trace IDs follow the format `1-<unix epoch hex>-<96-bit random hex>`. Each segment needs a unique 64-bit hex ID, a name, and start/end times as Unix epoch floats. |
| 28 | + |
| 29 | +Send a subsegment linked to the parent segment: |
| 30 | + |
| 31 | +```bash |
| 32 | +PARENT_ID=$(echo "$SEGMENT" | python3 -c "import sys,json;print(json.load(sys.stdin)['id'])") |
| 33 | +SUBSEG="{\"trace_id\":\"$TRACE_ID\",\"id\":\"$(openssl rand -hex 8)\",\"name\":\"database-query\",\"start_time\":$NOW,\"end_time\":$END,\"parent_id\":\"$PARENT_ID\"}" |
| 34 | +aws xray put-trace-segments --trace-segment-documents "$SUBSEG" |
| 35 | +``` |
| 36 | + |
| 37 | +Subsegments use `parent_id` to link to their parent segment. This creates the hierarchy visible in the X-Ray trace map. |
| 38 | + |
| 39 | +## Step 2: Get trace summaries |
| 40 | + |
| 41 | +Query recent trace summaries. X-Ray takes a few seconds to index new traces. |
| 42 | + |
| 43 | +```bash |
| 44 | +START_TIME=$(python3 -c "import time;print(int(time.time()-60))") |
| 45 | +END_TIME=$(python3 -c "import time;print(int(time.time()))") |
| 46 | + |
| 47 | +aws xray get-trace-summaries \ |
| 48 | + --start-time "$START_TIME" --end-time "$END_TIME" \ |
| 49 | + --query 'TraceSummaries[:3].{TraceId:Id,Duration:Duration,Status:Http.HttpStatus,Method:Http.HttpMethod}' \ |
| 50 | + --output table |
| 51 | +``` |
| 52 | + |
| 53 | +Trace summaries include the trace ID, duration, HTTP status, and response time. Use `--filter-expression` to narrow results by service name, status code, or annotation. |
| 54 | + |
| 55 | +## Step 3: Get full trace details |
| 56 | + |
| 57 | +Retrieve the complete trace including all segments and subsegments. |
| 58 | + |
| 59 | +```bash |
| 60 | +aws xray batch-get-traces --trace-ids "$TRACE_ID" \ |
| 61 | + --query 'Traces[0].Segments[].{Id:Id}' --output table |
| 62 | +``` |
| 63 | + |
| 64 | +`batch-get-traces` returns the raw segment documents. You can retrieve up to 5 trace IDs per call. |
| 65 | + |
| 66 | +## Step 4: Create a trace group |
| 67 | + |
| 68 | +Create a group that filters traces by service name. |
| 69 | + |
| 70 | +```bash |
| 71 | +GROUP_NAME="tut-group-$(openssl rand -hex 4)" |
| 72 | + |
| 73 | +GROUP_ARN=$(aws xray create-group --group-name "$GROUP_NAME" \ |
| 74 | + --filter-expression 'service("tutorial-service")' \ |
| 75 | + --query 'Group.GroupARN' --output text) |
| 76 | +echo "Group ARN: $GROUP_ARN" |
| 77 | +``` |
| 78 | + |
| 79 | +Groups let you organize traces by filter expression. Each group generates its own service graph and CloudWatch metrics. |
| 80 | + |
| 81 | +## Step 5: Get the service graph |
| 82 | + |
| 83 | +View the service graph for the time window containing your traces. |
| 84 | + |
| 85 | +```bash |
| 86 | +aws xray get-service-graph \ |
| 87 | + --start-time "$START_TIME" --end-time "$END_TIME" \ |
| 88 | + --query 'Services[].{Name:Name,Type:Type,Edges:Edges|length(@)}' \ |
| 89 | + --output table |
| 90 | +``` |
| 91 | + |
| 92 | +The service graph shows services as nodes and their connections as edges. It may take a few seconds after sending traces for the graph to populate. |
| 93 | + |
| 94 | +## Cleanup |
| 95 | + |
| 96 | +Delete the trace group. Trace data itself expires automatically based on your X-Ray retention settings (default 30 days). |
| 97 | + |
| 98 | +```bash |
| 99 | +aws xray delete-group --group-arn "$GROUP_ARN" |
| 100 | +``` |
| 101 | + |
| 102 | +The script automates all steps including cleanup: |
| 103 | + |
| 104 | +```bash |
| 105 | +bash aws-xray-gs.sh |
| 106 | +``` |
| 107 | + |
| 108 | +## Related resources |
| 109 | + |
| 110 | +- [Sending trace data to X-Ray](https://docs.aws.amazon.com/xray/latest/devguide/xray-api-sendingdata.html) |
| 111 | +- [Retrieving trace data](https://docs.aws.amazon.com/xray/latest/devguide/xray-api-gettingdata.html) |
| 112 | +- [Using groups in X-Ray](https://docs.aws.amazon.com/xray/latest/devguide/xray-console-groups.html) |
| 113 | +- [AWS X-Ray pricing](https://aws.amazon.com/xray/pricing/) |
0 commit comments