I am trying to create subsegments in golang and lambda but it not works for me:
This is my code:
package main
import (
"context"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-xray-sdk-go/xray"
)
func handler(ctx context.Context, event int) (string, error) {
awsContext, segment := xray.BeginSegment(ctx, "service-A")
segment.Close(nil)
sumResult, err := performSum(awsContext, event, event)
if err != nil {
return "", err
}
return fmt.Sprintf("Resultado: %d", sumResult), nil
}
func performSum(ctx context.Context, num1 int, num2 int) (int, error) {
_, subSeg := xray.BeginSubsegment(ctx, "service-B")
myMap := make(map[string]interface{})
myMap["test"] = "test value"
subSeg.Annotations = myMap
defer subSeg.Close(nil)
return num1 + num2, nil
}
func main() {
lambda.Start(handler)
}
the segment is created but subsegment not, y try wit xRay.Capture and I have the same result.
I have in my Xray console:
(Client) -> (My lambda Lambda Context) -> (My lambda Lambda Function)
(Client) -> (service-A)
I don't know why my parent Segment not is my lambda, is other client And I don't know why my subsegment not appear.
And I have readed all AWS post and all people do the same, but I don't understand what happen with my basic example.
this is the code using Capture, but only in the console appear the Segment:
(Client) -> (My lambda Lambda Context) -> (My lambda Lambda Function)
(Client) -> (Test)
And I take the example of the SDK test:
https://github.com/aws/aws-xray-sdk-go/blob/master/xray/capture_test.go
But it not works!
func handler(ctx context.Context, event int) (string, error) {
logger.InitAppContext(&ctx)
ctx, root := xray.BeginSegment(ctx, "Test")
err := xray.Capture(ctx, "TestService", func(context.Context) error {
logger.Info(ctx, "Inside Capture")
time.Sleep(2 * time.Second)
root.Close(nil)
return nil
})
return fmt.Sprintf("Resultado: %d", 0), err
}
func main() {
lambda.Start(handler)
}
Other thing that i did was create an HTTP request with xray, because with HTTP request xray create automatically the subsegment and it works, Then in the SDK library I include some logs
This is the segment Json with the Http request (it works, the subsegment appear in the console):
{
"trace_id":"1-64dcfa36-3b2d172543534b4d0c07827f",
"id":"c150f7dc4062e071",
"name":"www.google.com",
"start_time":1692203575.1287372,
"end_time":1692203575.8716385,
"parent_id":"37a80e6fee94619c",
"type":"subsegment",
"namespace":"remote",
"http":{
"request":{
"method":"GET",
"url":"https://www.google.com"
},
"response":{
"status":200
}
},
"subsegments":[
{
"trace_id":"1-64dcfa36-3b2d172543534b4d0c07827f",
"id":"a5fb68c55cd525a5",
"name":"connect",
"start_time":1692203575.128795,
"end_time":1692203575.3913538,
"parent_id":"37a80e6fee94619c",
"metadata":{
"http":{
"connection":{
"reused":false,
"was_idle":false
}
}
},
"subsegments":[
{
"trace_id":"1-64dcfa36-3b2d172543534b4d0c07827f",
"id":"57008dcea715c1f2",
"name":"dns",
"start_time":1692203575.1288614,
"end_time":1692203575.133045,
"parent_id":"37a80e6fee94619c",
"metadata":{
"http":{
"dns":{
"addresses":[
{
"IP":"172.253.63.147",
"Zone":""
},
{
"IP":"172.253.63.99",
"Zone":""
},
{
"IP":"172.253.63.103",
"Zone":""
},
{
"IP":"172.253.63.105",
"Zone":""
},
{
"IP":"172.253.63.104",
"Zone":""
},
{
"IP":"172.253.63.106",
"Zone":""
},
{
"IP":"2607:f8b0:4004:c09::63",
"Zone":""
},
{
"IP":"2607:f8b0:4004:c09::6a",
"Zone":""
},
{
"IP":"2607:f8b0:4004:c09::68",
"Zone":""
},
{
"IP":"2607:f8b0:4004:c09::67",
"Zone":""
}
],
"coalesced":false
}
}
},
"Dummy":false
},
{
"trace_id":"1-64dcfa36-3b2d172543534b4d0c07827f",
"id":"c39998ed3d0d880d",
"name":"dial",
"start_time":1692203575.133106,
"end_time":1692203575.1430182,
"parent_id":"37a80e6fee94619c",
"metadata":{
"http":{
"connect":{
"network":"tcp"
}
}
},
"Dummy":false
},
{
"trace_id":"1-64dcfa36-3b2d172543534b4d0c07827f",
"id":"54572f92d93d9010",
"name":"tls",
"start_time":1692203575.1441321,
"end_time":1692203575.3912807,
"parent_id":"37a80e6fee94619c",
"metadata":{
"http":{
"tls":{
"cipher_suite":4865,
"did_resume":false,
"negotiated_protocol":"h2",
"negotiated_protocol_is_mutual":true
}
}
},
"Dummy":false
}
],
"Dummy":false
},
{
"trace_id":"1-64dcfa36-3b2d172543534b4d0c07827f",
"id":"0451ff84d78b5450",
"name":"request",
"start_time":1692203575.3913682,
"end_time":1692203575.3914084,
"parent_id":"37a80e6fee94619c",
"Dummy":false
},
{
"trace_id":"1-64dcfa36-3b2d172543534b4d0c07827f",
"id":"99487356836991ff",
"name":"response",
"start_time":1692203575.3914256,
"end_time":1692203575.8714898,
"parent_id":"37a80e6fee94619c",
"Dummy":false
}
],
"Dummy":false
}
But this is the Json of the request that was built with the code of BeginSegment that i share:
{
"trace_id": "1-64de1eea-85ad2d01b432e20eb6440b91",
"id": "6ded59db0c459e1a",
"name": "service-A",
"start_time": 1692278506.7173197,
"end_time": 1692278506.7180274,
"aws": {
"xray": {
"sdk_version": "1.8.1",
"sdk": "X-Ray for Go"
}
},
"service": {
"version": "1.2.3",
"runtime_version": "go1.19.2",
"runtime": "gc"
},
"annotations": {
"parTest": "test parent"
},
"subsegments": [
{
"trace_id": "1-64de1eea-85ad2d01b432e20eb6440b91",
"id": "aa38e4906cab13b9",
"name": "service-B",
"start_time": 1692278506.7177432,
"end_time": 1692278506.7180173,
"parent_id": "6ded59db0c459e1a",
"annotations": {
"test": "test value"
},
"Dummy": false
}
],
"Dummy": false
}
veo que no hay un parent_id en el segundo, no se si eso puede influir