Skip to content

Commit b2cbdab

Browse files
authored
Merge pull request #311 from aws-samples/adot-go-update
Adot GO update
2 parents b78f489 + f6fdbce commit b2cbdab

File tree

28 files changed

+1097
-1524
lines changed

28 files changed

+1097
-1524
lines changed

PetAdoptions/cdk/pet_stack/lib/services.ts

Lines changed: 71 additions & 79 deletions
Large diffs are not rendered by default.

PetAdoptions/cdk/pet_stack/lib/services/ecs-service.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export abstract class EcsService extends Construct {
9595

9696
// Build locally the image only if the repository URI is not specified
9797
// Can help speed up builds if we are not rebuilding anything
98-
const image = props.repositoryURI? this.containerImageFromRepository(props.repositoryURI) : this.createContainerImage()
98+
const image = props.repositoryURI ? this.containerImageFromRepository(props.repositoryURI) : this.createContainerImage()
9999

100100
this.container = this.taskDefinition.addContainer('container', {
101101
image: image,
@@ -122,7 +122,7 @@ export abstract class EcsService extends Construct {
122122
//*/
123123

124124
// sidecar for instrumentation collecting
125-
switch(props.instrumentation) {
125+
switch (props.instrumentation) {
126126

127127
// we don't add any sidecar if instrumentation is none
128128
case "none": {
@@ -169,7 +169,7 @@ export abstract class EcsService extends Construct {
169169
}
170170
}
171171

172-
abstract containerImageFromRepository(repositoryURI: string) : ecs.ContainerImage;
172+
abstract containerImageFromRepository(repositoryURI: string): ecs.ContainerImage;
173173

174174
abstract createContainerImage(): ecs.ContainerImage;
175175

@@ -187,11 +187,11 @@ export abstract class EcsService extends Construct {
187187

188188
private addOtelCollectorContainer(taskDefinition: ecs.FargateTaskDefinition, logging: ecs.AwsLogDriver) {
189189
taskDefinition.addContainer('aws-otel-collector', {
190-
image: ecs.ContainerImage.fromRegistry('public.ecr.aws/aws-observability/aws-otel-collector:v0.32.0'),
191-
memoryLimitMiB: 256,
192-
cpu: 256,
193-
command: ["--config", "/etc/ecs/ecs-xray.yaml"],
194-
logging
190+
image: ecs.ContainerImage.fromRegistry('public.ecr.aws/aws-observability/aws-otel-collector:v0.41.1'),
191+
memoryLimitMiB: 256,
192+
cpu: 256,
193+
command: ["--config", "/etc/ecs/ecs-xray.yaml"],
194+
logging
195195
});
196196
}
197197
}

PetAdoptions/payforadoption-go/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.20 as builder
1+
FROM golang:1.23 as builder
22
WORKDIR /go/src/app
33
COPY . .
44
ENV GOPROXY=https://goproxy.io,direct
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM rust:latest as builder
2+
WORKDIR /app
3+
RUN
4+
COPY . .
5+
RUN cargo install drill
6+
CMD ["./benchmark.sh"]
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
## Drill for PayForAdoption
2+
3+
Using [drill](https://github.com/fcsonline/drill), this allows to generate
4+
traffic in the PetSite EKS cluster or locally in dev, without spinning ECS
5+
traffic generator.
6+
7+
This can be useful to test all APIs or boost traffic for Application Signals.
8+
9+
### (Optional) Pick your benchmark environment
10+
11+
In the `benchmark.yaml` file, change the base URL to your PayForAdoption ALB
12+
endpoint if you are not testing it locally as the default is local.
13+
14+
```yaml
15+
concurrency: 4
16+
base: "http://Servic-payfo-[.....].eu-central-1.elb.amazonaws.com"
17+
```
18+
19+
### Running locally
20+
21+
[Install drill](https://github.com/fcsonline/drill?tab=readme-ov-file#install)
22+
and run
23+
24+
```bash
25+
drill --benchmark benchmark.yaml
26+
```
27+
28+
### Running in EKS
29+
30+
1. Authenticate against your EKS cluster
31+
32+
```bash
33+
aws eks update-kubeconfig --name PetSite --region <AWS_REGION>
34+
```
35+
36+
2. Create an ECR image `drill-payforadoption`
37+
38+
3. Build and push
39+
40+
```bash
41+
aws ecr get-login-password --region <YOUR REGION> | docker login --username AWS --password-stdin <ACCOUNT_ID>.dkr.ecr.<AWS_REGION>.amazonaws.com
42+
docker buildx build -t drill-payforadoption . --platform=linux/amd64
43+
docker tag drill-payforadoption:latest <ACCOUNT_ID>.dkr.ecr.<AWS_REGION>.amazonaws.com/drill-payforadoption:latest
44+
docker push <ACCOUNT_ID>.dkr.ecr.<AWS_REGION>.amazonaws.com/drill-payforadoption:latest
45+
```
46+
47+
4. Run in EKS
48+
49+
```bash
50+
kubectl run -it drill-payforadoption --image <ACCOUNT_ID>.dkr.ecr.<AWS_REGION>.amazonaws.com/drill-payforadoption:latest
51+
```
52+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
while true
4+
do
5+
drill -s --benchmark benchmark.yaml
6+
sleep 1
7+
done
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
concurrency: 4
3+
base: "http://localhost:80"
4+
iterations: 4
5+
rampup: 2
6+
7+
plan:
8+
- name: Health check
9+
request:
10+
url: /health/status
11+
method: GET
12+
13+
- name: Adopt Bunnies
14+
request:
15+
url: /api/home/completeadoption?petId={{ item }}&petType=bunny
16+
method: POST
17+
body: ""
18+
with_items:
19+
- "023"
20+
- "024"
21+
- "025"
22+
- "026"
23+
- "invalid_bunny_id"
24+
25+
- name: Adopt Kittens
26+
request:
27+
url: /api/home/completeadoption?petId={{ item }}&petType=kitten
28+
method: POST
29+
body: ""
30+
with_items:
31+
- "016"
32+
- "017"
33+
- "018"
34+
- "019"
35+
- "020"
36+
- "021"
37+
- "022"
38+
- "invalid_kitten_id"
39+
40+
- name: Adopt Puppies
41+
request:
42+
url: /api/home/completeadoption?petId={{ item }}&petType=puppy
43+
method: POST
44+
body: ""
45+
with_items:
46+
- "001"
47+
- "002"
48+
- "003"
49+
- "004"
50+
- "005"
51+
- "006"
52+
- "007"
53+
- "008"
54+
- "009"
55+
- "010"
56+
- "011"
57+
- "012"
58+
- "013"
59+
- "014"
60+
- "015"
61+
- "invalid_puppy_id"
62+
63+
- name: Cleanup Adoptions
64+
request:
65+
url: /api/home/cleanupadoptions
66+
method: POST
67+
body: ""

PetAdoptions/payforadoption-go/config.go

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import (
55
"encoding/json"
66
"fmt"
77
"net/url"
8-
"os"
98
"petadoptions/payforadoption"
109

11-
"github.com/aws/aws-sdk-go/aws"
12-
"github.com/aws/aws-sdk-go/aws/session"
13-
"github.com/aws/aws-sdk-go/service/secretsmanager"
14-
"github.com/aws/aws-sdk-go/service/ssm"
15-
"github.com/aws/aws-xray-sdk-go/xray"
10+
"github.com/aws/aws-sdk-go-v2/aws"
11+
"github.com/aws/aws-sdk-go-v2/config"
12+
"github.com/aws/aws-sdk-go-v2/service/secretsmanager"
13+
"github.com/aws/aws-sdk-go-v2/service/ssm"
14+
"github.com/go-kit/log"
15+
"github.com/go-kit/log/level"
1616
"github.com/spf13/viper"
1717
)
1818

@@ -21,86 +21,84 @@ type dbConfig struct {
2121
Port int
2222
}
2323

24-
// config is injected as environment variable
25-
26-
func fetchConfig() (payforadoption.Config, error) {
24+
func fetchConfig(ctx context.Context, logger log.Logger) (payforadoption.Config, error) {
2725

2826
// fetch from env
2927
viper.AutomaticEnv() // Bind automatically all env vars that have the same prefix
3028

29+
awsCfg, err := config.LoadDefaultConfig(ctx)
30+
if err != nil {
31+
level.Error(logger).Log("aws", err)
32+
}
33+
3134
cfg := payforadoption.Config{
3235
UpdateAdoptionURL: viper.GetString("UPDATE_ADOPTION_URL"),
3336
RDSSecretArn: viper.GetString("RDS_SECRET_ARN"),
3437
AWSRegion: viper.GetString("AWS_REGION"),
38+
AWSCfg: awsCfg,
3539
}
3640

3741
if cfg.UpdateAdoptionURL == "" || cfg.RDSSecretArn == "" {
38-
return fetchConfigFromParameterStore(cfg.AWSRegion)
42+
return fetchConfigFromParameterStore(ctx, cfg)
3943
}
4044

4145
return cfg, nil
4246
}
4347

44-
func fetchConfigFromParameterStore(region string) (payforadoption.Config, error) {
45-
svc := ssm.New(session.New(&aws.Config{Region: aws.String(region)}))
46-
xray.AWS(svc.Client)
47-
ctx, seg := xray.BeginSegment(context.Background(), "payforadoption")
48-
defer seg.Close(nil)
49-
50-
res, err := svc.GetParametersWithContext(ctx, &ssm.GetParametersInput{
51-
Names: []*string{
52-
aws.String("/petstore/updateadoptionstatusurl"),
53-
aws.String("/petstore/rdssecretarn"),
54-
aws.String("/petstore/s3bucketname"),
55-
aws.String("/petstore/dynamodbtablename"),
48+
func fetchConfigFromParameterStore(ctx context.Context, cfg payforadoption.Config) (payforadoption.Config, error) {
49+
svc := ssm.NewFromConfig(cfg.AWSCfg)
50+
51+
res, err := svc.GetParameters(ctx, &ssm.GetParametersInput{
52+
Names: []string{
53+
"/petstore/updateadoptionstatusurl",
54+
"/petstore/rdssecretarn",
55+
"/petstore/s3bucketname",
56+
"/petstore/dynamodbtablename",
5657
},
5758
})
5859

59-
cfg := payforadoption.Config{}
60-
cfg.AWSRegion = region
60+
newCfg := payforadoption.Config{}
61+
newCfg.AWSCfg = cfg.AWSCfg
62+
newCfg.AWSRegion = cfg.AWSCfg.Region
6163

6264
if err != nil {
63-
return cfg, err
65+
return newCfg, err
6466
}
6567

6668
for _, p := range res.Parameters {
69+
pValue := aws.ToString(p.Value)
6770

68-
switch aws.StringValue(p.Name) {
71+
switch aws.ToString(p.Name) {
6972
case "/petstore/rdssecretarn":
70-
cfg.RDSSecretArn = aws.StringValue(p.Value)
73+
newCfg.RDSSecretArn = pValue
7174
case "/petstore/updateadoptionstatusurl":
72-
cfg.UpdateAdoptionURL = aws.StringValue(p.Value)
75+
newCfg.UpdateAdoptionURL = pValue
7376
case "/petstore/s3bucketname":
74-
cfg.S3BucketName = aws.StringValue(p.Value)
77+
newCfg.S3BucketName = pValue
7578
case "/petstore/dynamodbtablename":
76-
cfg.DynamoDBTable = aws.StringValue(p.Value)
79+
newCfg.DynamoDBTable = pValue
7780
}
7881
}
7982

80-
return cfg, err
83+
return newCfg, err
8184
}
8285

83-
func getSecretValue(secretID, region string) (string, error) {
84-
85-
svc := secretsmanager.New(session.New(&aws.Config{Region: aws.String(region)}))
86-
xray.AWS(svc.Client)
87-
ctx, seg := xray.BeginSegment(context.Background(), "payforadoption")
88-
89-
res, err := svc.GetSecretValueWithContext(ctx, &secretsmanager.GetSecretValueInput{
90-
SecretId: aws.String(secretID),
86+
func getSecretValue(ctx context.Context, cfg payforadoption.Config) (string, error) {
87+
svc := secretsmanager.NewFromConfig(cfg.AWSCfg)
88+
res, err := svc.GetSecretValue(ctx, &secretsmanager.GetSecretValueInput{
89+
SecretId: aws.String(cfg.RDSSecretArn),
9190
})
92-
seg.Close(nil)
9391

9492
if err != nil {
9593
return "", err
9694
}
9795

98-
return aws.StringValue(res.SecretString), nil
96+
return aws.ToString(res.SecretString), nil
9997
}
10098

10199
// Call aws secrets manager and return parsed sql server query str
102-
func getRDSConnectionString(secretid string) (string, error) {
103-
jsonstr, err := getSecretValue(secretid, os.Getenv("AWS_REGION"))
100+
func getRDSConnectionString(ctx context.Context, cfg payforadoption.Config) (string, error) {
101+
jsonstr, err := getSecretValue(ctx, cfg)
104102
if err != nil {
105103
return "", err
106104
}
@@ -118,5 +116,11 @@ func getRDSConnectionString(secretid string) (string, error) {
118116
Path: c.Dbname,
119117
}
120118

121-
return u.String(), nil
119+
fmt.Println(u.String())
120+
121+
connStr := u.String()
122+
connStr += "?sslmode=disable"
123+
124+
// return u.String(), nil
125+
return connStr, nil
122126
}

0 commit comments

Comments
 (0)