Thundra APM
Search…
Deployment Integrations
Powered By GitBook
Configuring Integrations for Go SDK

Configuring AWS SDK Trace

Thundra's Go agent provides you with the capability to trace AWS SDK by wrapping the session object that AWS SDK provides. You can easily start using it by simply wrapping your session objects as shown in the following code:
1
package main
2
3
import (
4
"github.com/aws/aws-lambda-go/lambda"
5
"github.com/aws/aws-sdk-go/aws"
6
"github.com/aws/aws-sdk-go/aws/session"
7
"github.com/aws/aws-sdk-go/service/dynamodb"
8
"github.com/thundra-io/thundra-lambda-agent-go/thundra"
9
thundraaws "github.com/thundra-io/thundra-lambda-agent-go/wrappers/aws"
10
)
11
12
// Your lambda handler
13
func handler() (string, error) {
14
// Create a new session object
15
sess, _ := session.NewSession(&aws.Config{
16
Region: aws.String("us-west-2")},
17
)
18
19
// Wrap it using the thundraaws.Wrap method
20
sess = thundraaws.Wrap(sess)
21
22
// Create a new client using the wrapped session
23
svc := dynamodb.New(sess)
24
25
// Use the client as normal, Thundra will automatically
26
// create spans for the AWS SDK calls
27
svc.PutItem(&dynamodb.PutItemInput{
28
Item: map[string]*dynamodb.AttributeValue{
29
"AlbumTitle": {
30
S: aws.String("Somewhat Famous"),
31
},
32
"Artist": {
33
S: aws.String("No One You Know"),
34
},
35
"SongTitle": {
36
S: aws.String("Call Me Today"),
37
},
38
},
39
ReturnConsumedCapacity: aws.String("TOTAL"),
40
TableName: aws.String("Music"),
41
})
42
43
return "Hello, Thundra!", nil
44
}
45
46
func main() {
47
// Wrap your lambda handler with Thundra
48
lambda.Start(thundra.Wrap(handler))
49
}
Copied!

Configuring Mongo SDK Trace

1
package main
2
3
import (
4
"context"
5
6
"github.com/aws/aws-lambda-go/events"
7
"github.com/aws/aws-lambda-go/lambda"
8
"github.com/thundra-io/thundra-lambda-agent-go/thundra"
9
thundramongo "github.com/thundra-io/thundra-lambda-agent-go/wrappers/mongodb"
10
"go.mongodb.org/mongo-driver/mongo"
11
"go.mongodb.org/mongo-driver/mongo/options"
12
)
13
14
type Post struct {
15
Author string
16
Text string
17
}
18
19
func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
20
21
client, _ := mongo.Connect(context.Background(), options.Client().ApplyURI("mongodb://localhost:27017").SetMonitor(thundramongo.NewCommandMonitor()))
22
23
collection := client.Database("test").Collection("posts")
24
post := Post{"Mike", "My first blog!"}
25
26
// Insert post to mongodb
27
collection.InsertOne(context.TODO(), post)
28
29
return events.APIGatewayProxyResponse{
30
Body: "res",
31
StatusCode: 200,
32
}, nil
33
}
34
35
func main() {
36
lambda.Start(thundra.Wrap(handler))
37
}
38
Copied!

Configuring Mysql SDK Trace

1
package main
2
3
import (
4
"context"
5
"database/sql"
6
7
"github.com/aws/aws-lambda-go/lambda"
8
"github.com/go-sql-driver/mysql"
9
"github.com/thundra-io/thundra-lambda-agent-go/thundra"
10
thundrardb "github.com/thundra-io/thundra-lambda-agent-go/wrappers/rdb"
11
)
12
13
func handler(ctx context.Context) {
14
15
// Register wrapped driver for tracing
16
// Note that driver name registered should be different than "mysql" as it is already registered on init method of
17
// this package otherwise it panics.
18
sql.Register("thundra-mysql", thundrardb.Wrap(&mysql.MySQLDriver{}))
19
20
// Get the database handle with registered driver name
21
db, err := sql.Open("thundra-mysql", "user:[email protected](docker.for.mac.localhost:3306)/db")
22
23
if err != nil {
24
// Just for example purpose. You should use proper error handling instead of panic
25
panic(err.Error())
26
}
27
defer db.Close()
28
29
rows, err := db.Query("SELECT * FROM test")
30
if err != nil {
31
thundra.Logger.Error(err)
32
}
33
defer rows.Close()
34
}
35
36
func main() {
37
lambda.Start(thundra.Wrap(handler))
38
}
Copied!

Configuring PostgreSQL SDK Trace

1
package main
2
3
import (
4
"context"
5
"database/sql"
6
7
"github.com/aws/aws-lambda-go/lambda"
8
"github.com/lib/pq"
9
"github.com/thundra-io/thundra-lambda-agent-go/thundra"
10
thundrardb "github.com/thundra-io/thundra-lambda-agent-go/wrappers/rdb"
11
)
12
13
func handler(ctx context.Context) {
14
// Register wrapped driver for tracing
15
// Note that driver name registered should be different than "postgres"
16
// as it is already registered on init method of this package
17
// otherwise it panics.
18
sql.Register("thundra-postgres", thundrardb.Wrap(&pq.Driver{}))
19
20
// Get the database handle with registered driver name
21
db, err := sql.Open("thundra-postgres", "postgres://user:[email protected]:5432/db?sslmode=disable")
22
23
_, err = db.Exec("CREATE table IF NOT EXISTS test(id int, type text)")
24
25
if err != nil {
26
// Just for example purpose. You should use proper error handling instead of panic
27
panic(err.Error())
28
}
29
defer db.Close()
30
31
// Perform a query
32
rows, err := db.Query("SELECT * FROM test")
33
if err != nil {
34
thundra.Logger.Error(err)
35
}
36
defer rows.Close()
37
}
38
39
func main() {
40
lambda.Start(thundra.Wrap(handler))
41
}
42
Copied!

Configuring Redis SDK Trace

1
package main
2
3
import (
4
"fmt"
5
6
"github.com/aws/aws-lambda-go/lambda"
7
"github.com/thundra-io/thundra-lambda-agent-go/thundra"
8
thundraredigo "github.com/thundra-io/thundra-lambda-agent-go/wrappers/redis/redigo"
9
)
10
11
func test() {
12
conn, err := thundraredigo.Dial("tcp", "docker.for.mac.localhost:6379")
13
if err != nil {
14
panic(err.Error())
15
}
16
defer conn.Close()
17
18
ret, _ := conn.Do("SET", "mykey", "hello")
19
fmt.Printf("%s\n", ret)
20
21
ret, _ = conn.Do("GET", "mykey")
22
fmt.Printf("%s\n", ret)
23
24
}
25
26
func main() {
27
lambda.Start(thundra.Wrap(test))
28
}
29
Copied!

Configuring HTTP SDK Trace

1
package main
2
3
import (
4
"fmt"
5
"net/http"
6
7
"github.com/aws/aws-lambda-go/events"
8
"github.com/aws/aws-lambda-go/lambda"
9
"github.com/thundra-io/thundra-lambda-agent-go/thundra"
10
thundrahttp "github.com/thundra-io/thundra-lambda-agent-go/wrappers/http"
11
)
12
13
func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
14
15
thundraHttpClient := thundrahttp.Wrap(http.Client{})
16
17
resp, err := thundraHttpClient.Get("URL")
18
if err == nil {
19
fmt.Println(resp)
20
}
21
fmt.Println(err)
22
23
return events.APIGatewayProxyResponse{
24
Body: "res",
25
StatusCode: 200,
26
}, nil
27
}
28
29
func main() {
30
lambda.Start(thundra.Wrap(handler))
31
}
32
Copied!

Configuring Elasticsearch SDK Trace

1
package main
2
3
import (
4
"context"
5
"encoding/json"
6
"fmt"
7
"net/http"
8
"reflect"
9
10
"github.com/aws/aws-lambda-go/lambda"
11
"github.com/thundra-io/thundra-lambda-agent-go/thundra"
12
thundraelastic "github.com/thundra-io/thundra-lambda-agent-go/wrappers/elastic/olivere"
13
"gopkg.in/olivere/elastic.v6"
14
)
15
16
func getClient() *elastic.Client {
17
18
var httpClient = thundraelastic.Wrap(&http.Client{})
19
var client, _ = elastic.NewClient(
20
elastic.SetURL("http://localhost:9200"),
21
elastic.SetHttpClient(httpClient),
22
elastic.SetSniff(false),
23
elastic.SetHealthcheck(false),
24
)
25
return client
26
}
27
28
// Tweet is a structure used for serializing/deserializing data in Elasticsearch.
29
type Tweet struct {
30
User string `json:"user"`
31
Message string `json:"message"`
32
Retweets int `json:"retweets"`
33
Image string `json:"image,omitempty"`
34
Tags []string `json:"tags,omitempty"`
35
Location string `json:"location,omitempty"`
36
}
37
38
func handler() {
39
40
// Starting with elastic.v5, you must pass a context to execute each service
41
ctx := context.Background()
42
43
// Obtain a client and connect to the default Elasticsearch installation
44
// on 127.0.0.1:9200. Of course you can configure your client to connect
45
// to other hosts and configure it in various other ways.
46
client := getClient()
47
48
// Index a tweet (using JSON serialization)
49
tweet1 := Tweet{User: "olivere", Message: "Take Five", Retweets: 0}
50
put1, err := client.Index().
51
Index("thundra").
52
Type("users").
53
Id("id").
54
BodyJson(tweet1).
55
Do(ctx)
56
if err != nil {
57
// Handle error
58
panic(err)
59
}
60
fmt.Printf("Indexed tweet %s to index %s, type %s\n", put1.Id, put1.Index, put1.Type)
61
62
// Get tweet with specified ID
63
get1, err := client.Get().
64
Index("twitter").
65
Type("tweet").
66
Id("1").
67
Do(ctx)
68
if err != nil {
69
// Handle error
70
panic(err)
71
}
72
if get1.Found {
73
fmt.Printf("Got document %s in version %d from index %s, type %s\n", get1.Id, get1.Version, get1.Index, get1.Type)
74
}
75
76
// Flush to make sure the documents got written.
77
_, err = client.Flush().Index("twitter").Do(ctx)
78
if err != nil {
79
panic(err)
80
}
81
82
// Search with a term query
83
termQuery := elastic.NewTermQuery("user", "olivere")
84
searchResult, err := client.Search().
85
Index("twitter"). // search in index "twitter"
86
Query(termQuery). // specify the query
87
Sort("user", true). // sort by "user" field, ascending
88
From(0).Size(10). // take documents 0-9
89
Pretty(true). // pretty print request and response JSON
90
Do(ctx) // execute
91
if err != nil {
92
// Handle error
93
panic(err)
94
}
95
96
}
97
98
func main() {
99
lambda.Start(thundra.Wrap(handler))
100
}
101
Copied!
Last modified 1mo ago