Skip to content

Commit

Permalink
minor: add http/aws requests tracer support
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosoik authored Apr 2, 2022
1 parent 1328b71 commit e44d80e
Show file tree
Hide file tree
Showing 14 changed files with 586 additions and 208 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,41 @@ func main() {
}
```

For tracing AWS SDK v2.0 calls check the following example:

```go
client := &http.Client{
Transport: lumigotracer.NewTransport(http.DefaultTransport),
}

// for AWS SDK v1.x
sess := session.Must(session.NewSession(&aws.Config{
HTTPClient: client,
}))

svc := s3.New(sess)

// for AWS SDK v2.x
cfg, _ := config.LoadDefaultConfig(context.Background(), config.WithHTTPClient(client))
svc := s3.NewFromConfig(cfg)

```

For tracing HTTP calls check the following example:

```go
client := &http.Client{
Transport: lumigotracer.NewTransport(http.DefaultTransport),
}
req, _ := http.NewRequest("GET", "https://<your-url>", nil)

// for net/http
res, err := client.Do(req)

// for golang.org/x/net/context/ctxhttp
res, err := ctxhttp.Do(context.Background(), client, req)
```

In your lambda environment variables you need to set `LUMIGO_USE_TRACER_EXTENSION: true` and use the following layer for `us-east-1`: `arn:aws:lambda:us-east-1:114300393969:layer:lumigo-tracer-extension:36`. The layer will be available in more regions soon.

## Contributing
Expand Down
33 changes: 33 additions & 0 deletions _example/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import (
"context"
"errors"
"fmt"
"net/http"
"os"
"strconv"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/ssm"
"github.com/aws/aws-sdk-go/aws"
lumigotracer "github.com/lumigo-io/go-tracer-beta"
)

Expand All @@ -17,7 +22,33 @@ type MyEvent struct {
}

func HandleRequest(ctx context.Context, name MyEvent) (events.APIGatewayProxyResponse, error) {
client := &http.Client{
Transport: lumigotracer.NewTransport(http.DefaultTransport),
}
cfg, _ := config.LoadDefaultConfig(context.Background(), config.WithHTTPClient(client))
cfg.Region = "us-east-1"
// testing S3
svc := s3.NewFromConfig(cfg)
_, err := svc.ListBuckets(context.Background(), &s3.ListBucketsInput{})
if err != nil {
return events.APIGatewayProxyResponse{Body: "", StatusCode: 500}, err
}

svc.CreateBucket(context.Background(), &s3.CreateBucketInput{
Bucket: aws.String("test-bucket-go-tracer-2"),
})

// testing SSM
ssmClient := ssm.NewFromConfig(cfg)
input := &ssm.GetParameterInput{
Name: aws.String("parameter-name"),
}
_, err = ssmClient.GetParameter(context.Background(), input)
if err != nil {
return events.APIGatewayProxyResponse{Body: "ssm error", StatusCode: 500}, err
}
response := fmt.Sprintf("Hello %s!", name.Name)

returnErr, ok := os.LookupEnv("RETURN_ERROR")
if !ok {
return events.APIGatewayProxyResponse{Body: response, StatusCode: 200}, nil
Expand All @@ -26,9 +57,11 @@ func HandleRequest(ctx context.Context, name MyEvent) (events.APIGatewayProxyRes
if err != nil {
return events.APIGatewayProxyResponse{Body: response, StatusCode: 500}, err
}
// testing return error
if isReturnErr {
return events.APIGatewayProxyResponse{Body: response, StatusCode: 500}, errors.New("failed error")
}
// testing return
return events.APIGatewayProxyResponse{Body: response, StatusCode: 200}, nil
}

Expand Down
11 changes: 9 additions & 2 deletions _example/terraform/lambda.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ resource "aws_lambda_function" "otel" {

role = aws_iam_role.lambda_exec.arn

layers = ["arn:aws:lambda:us-east-1:114300393969:layer:lumigo-tracer-extension:33"]
layers = ["arn:aws:lambda:us-east-1:114300393969:layer:lumigo-tracer-extension:37"]

environment {
variables = {
Expand Down Expand Up @@ -59,7 +59,14 @@ resource "aws_iam_role_policy" "lambda_s3_read" {
"Statement": [
{
"Action": [
"s3:ListAllMyBuckets"
"s3:*"
],
"Effect": "Allow",
"Resource": "*"
},
{
"Action": [
"ssm:*"
],
"Effect": "Allow",
"Resource": "*"
Expand Down
37 changes: 0 additions & 37 deletions extensions.go

This file was deleted.

24 changes: 1 addition & 23 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ go 1.17

require (
github.com/aws/aws-lambda-go v1.27.0
github.com/aws/aws-sdk-go v1.42.23
github.com/aws/aws-sdk-go-v2 v1.16.1
github.com/aws/aws-sdk-go-v2/config v1.10.2
github.com/google/uuid v1.3.0
github.com/pkg/errors v0.9.1
github.com/segmentio/ksuid v1.0.4
Expand All @@ -16,37 +14,20 @@ require (
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816
go.opentelemetry.io/contrib/detectors/aws/lambda v0.27.0
go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda v0.27.0
go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws v0.27.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.27.0
go.opentelemetry.io/otel v1.3.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.2.0
go.opentelemetry.io/otel/sdk v1.3.0
go.opentelemetry.io/otel/trace v1.3.0
golang.org/x/net v0.0.0-20220325170049-de3da57026de
)

require (
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.1 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.6.2 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.8.1 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.8 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.2 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.1 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.1 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.2 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.2 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.13.2 // indirect
github.com/aws/aws-sdk-go-v2/service/s3 v1.26.2 // indirect
github.com/aws/aws-sdk-go-v2/service/ssm v1.24.0 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.6.1 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.10.1 // indirect
github.com/aws/smithy-go v1.11.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.2 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/go-logr/logr v1.2.1 // indirect
github.com/go-logr/stdr v1.2.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mitchellh/mapstructure v1.4.2 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
Expand All @@ -56,9 +37,6 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
go.opentelemetry.io/otel/internal/metric v0.25.0 // indirect
go.opentelemetry.io/otel/metric v0.25.0 // indirect
golang.org/x/net v0.0.0-20220325170049-de3da57026de // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
Expand Down
Loading

0 comments on commit e44d80e

Please sign in to comment.