Skip to content

Commit 40eb061

Browse files
authored
Merge pull request #3 from jippi/ensure-consistent-request-uuid
Refactor internal state to ensure consistent request id in logging
2 parents 89cfe9f + 152c7d2 commit 40eb061

File tree

9 files changed

+285
-180
lines changed

9 files changed

+285
-180
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ sudo: required
33
language: go
44

55
go:
6-
- 1.9
6+
- "1.11"
77

88
services:
99
- docker

Gopkg.lock

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,7 @@ required = ["github.com/davecgh/go-spew/spew"]
6565
[[override]]
6666
name = "github.com/docker/libnetwork"
6767
revision = "1f28166bb386cf9223d2d00a28382b0e474be314"
68+
69+
[[constraint]]
70+
name = "github.com/cenkalti/backoff"
71+
version = "2.1.1"

internal/aws.go

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"strings"
66
"time"
77

8-
"github.com/armon/go-metrics"
98
"github.com/aws/aws-sdk-go-v2/aws"
109
"github.com/aws/aws-sdk-go-v2/aws/external"
1110
"github.com/aws/aws-sdk-go-v2/service/iam"
@@ -37,22 +36,20 @@ func ConfigureAWS() {
3736
stsService = sts.New(cfg)
3837
}
3938

40-
func readRoleFromAWS(role string, labels []metrics.Label) (*iam.Role, []metrics.Label, error) {
41-
logWithLabels(labels).Infof("Looking for IAM role for %s", role)
39+
func readRoleFromAWS(role string, request *Request) (*iam.Role, error) {
40+
request.log.Infof("Looking for IAM role for %s", role)
4241

4342
roleObject := &iam.Role{}
44-
4543
if roleObject, ok := roleCache.Get(role); ok {
46-
labels = append(labels, metrics.Label{Name: "read_role_from_aws_cache", Value: "hit"})
47-
48-
logWithLabels(labels).Infof("Found IAM role %s in cache", role)
49-
return roleObject.(*iam.Role), labels, nil
44+
request.setLabel("read_role_from_aws_cache", "hit")
45+
request.log.Infof("Found IAM role %s in cache", role)
46+
return roleObject.(*iam.Role), nil
5047
}
5148

52-
labels = append(labels, metrics.Label{Name: "read_role_from_aws_cache", Value: "miss"})
49+
request.setLabel("read_role_from_aws_cache", "miss")
5350

5451
if strings.Contains(role, "@") { // IAM_ROLE=my-role@012345678910
55-
logWithLabels(labels).Infof("Constructing IAM role info for %s manually", role)
52+
request.log.Infof("Constructing IAM role info for %s manually", role)
5653
chunks := strings.SplitN(role, "@", 2)
5754
nameChunks := strings.Split(chunks[0], "/")
5855

@@ -61,7 +58,7 @@ func readRoleFromAWS(role string, labels []metrics.Label) (*iam.Role, []metrics.
6158
RoleName: aws.String(nameChunks[len(nameChunks)-1]),
6259
}
6360
} else if strings.HasPrefix(role, "arn:aws:iam") { // IAM_ROLE=arn:aws:iam::012345678910:role/my-role
64-
logWithLabels(labels).Infof("Using IAM role ARN as is for %s", role)
61+
request.log.Infof("Using IAM role ARN as is for %s", role)
6562

6663
chunks := strings.SplitN(role, ":role/", 2)
6764
nameChunks := strings.Split(chunks[1], "/")
@@ -71,50 +68,46 @@ func readRoleFromAWS(role string, labels []metrics.Label) (*iam.Role, []metrics.
7168
RoleName: aws.String(nameChunks[len(nameChunks)-1]),
7269
}
7370
} else { // IAM_ROLE=my-role
74-
logWithLabels(labels).Infof("Requesting IAM role info for %s from AWS", role)
71+
request.log.Infof("Requesting IAM role info for %s from AWS", role)
7572
req := iamService.GetRoleRequest(&iam.GetRoleInput{
7673
RoleName: aws.String(role),
7774
})
7875

7976
resp, err := req.Send()
8077
if err != nil {
81-
return nil, labels, err
78+
return nil, err
8279
}
8380

8481
roleObject = resp.Role
8582
}
8683

8784
roleCache.Set(role, roleObject, cache.DefaultExpiration)
88-
return roleObject, labels, nil
85+
return roleObject, nil
8986
}
9087

91-
func assumeRoleFromAWS(arn string, labels []metrics.Label) (*sts.AssumeRoleOutput, []metrics.Label, error) {
92-
logWithLabels(labels).Infof("Looking for STS Assume Role for %s", arn)
88+
func assumeRoleFromAWS(arn string, request *Request) (*sts.AssumeRoleOutput, error) {
89+
request.log.Infof("Looking for STS Assume Role for %s", arn)
9390

9491
if assumedRole, ok := permissionCache.Get(arn); ok {
95-
labels = append(labels, metrics.Label{Name: "assume_role_from_aws_cache", Value: "hit"})
96-
97-
logWithLabels(labels).Infof("Found STS Assume Role %s in cache", arn)
98-
return assumedRole.(*sts.AssumeRoleOutput), labels, nil
92+
request.setLabel("assume_role_from_aws_cache", "hit")
93+
request.log.Infof("Found STS Assume Role %s in cache", arn)
94+
return assumedRole.(*sts.AssumeRoleOutput), nil
9995
}
100-
labels = append(labels, metrics.Label{Name: "assume_role_from_aws_cache", Value: "miss"})
10196

102-
logWithLabels(labels).Infof("Requesting STS Assume Role info for %s from AWS", arn)
97+
request.setLabel("assume_role_from_aws_cache", "miss")
98+
request.log.Infof("Requesting STS Assume Role info for %s from AWS", arn)
10399
req := stsService.AssumeRoleRequest(&sts.AssumeRoleInput{
104100
RoleArn: aws.String(arn),
105101
RoleSessionName: aws.String("go-metadataproxy"),
106102
})
107103

108104
assumedRole, err := req.Send()
109105
if err != nil {
110-
return nil, labels, err
106+
return nil, err
111107
}
112108

113109
ttl := assumedRole.Credentials.Expiration.Sub(time.Now()) - 1*time.Minute
114-
115-
logWithLabels(labels).Infof("Will cache STS Assumed Role info for %s in %s", arn, ttl.String())
116-
110+
request.log.Infof("Will cache STS Assumed Role info for %s in %s", arn, ttl.String())
117111
permissionCache.Set(arn, assumedRole, ttl)
118-
119-
return assumedRole, labels, nil
112+
return assumedRole, nil
120113
}

internal/docker.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import (
55
"os"
66
"strings"
77

8-
metrics "github.com/armon/go-metrics"
98
"github.com/fsouza/go-dockerclient"
109
log "github.com/sirupsen/logrus"
1110
)
1211

1312
var (
14-
dockerClient *docker.Client
15-
defaultRole = os.Getenv("DEFAULT_ROLE")
16-
copyDockerLabels = strings.Split(os.Getenv("COPY_DOCKER_LABELS"), ",")
17-
copyDockerEnvs = strings.Split(os.Getenv("COPY_DOCKER_ENV"), ",")
13+
dockerClient *docker.Client
14+
defaultRole = os.Getenv("DEFAULT_ROLE")
15+
copyDockerLabels = strings.Split(os.Getenv("COPY_DOCKER_LABELS"), ",")
16+
copyDockerEnvs = strings.Split(os.Getenv("COPY_DOCKER_ENV"), ",")
17+
copyRequestHeaders = strings.Split(os.Getenv("COPY_REQUEST_HEADERS"), ",")
1818
)
1919

2020
// ConfigureDocker will setup a docker client used during normal operations
@@ -35,44 +35,49 @@ func ConfigureDocker() {
3535
dockerClient = client
3636
}
3737

38-
func findDockerContainer(ip string, labels []metrics.Label) (*docker.Container, []metrics.Label, error) {
38+
func findDockerContainer(ip string, request *Request) (*docker.Container, error) {
3939
var container *docker.Container
4040

41-
logWithLabels(labels).Infof("Looking up container info for %s in docker", ip)
41+
request.log.Infof("Looking up container info for %s in docker", ip)
4242
containers, err := dockerClient.ListContainers(docker.ListContainersOptions{All: true})
4343
if err != nil {
44-
return nil, labels, err
44+
return nil, err
4545
}
4646

47-
container, err = findContainerByIP(ip, labels, containers)
47+
container, err = findContainerByIP(ip, request, containers)
4848
if err != nil {
49-
return nil, labels, err
49+
return nil, err
5050
}
5151

52+
additionalLabels := make(map[string]string)
5253
if len(copyDockerLabels) > 0 {
5354
for _, label := range copyDockerLabels {
5455
if v, ok := container.Config.Labels[label]; ok {
55-
labels = append(labels, metrics.Label{Name: labelName("container", label), Value: v})
56+
additionalLabels[labelName("container", label)] = v
5657
}
5758
}
5859
}
5960

6061
if len(copyDockerEnvs) > 0 {
6162
for _, label := range copyDockerEnvs {
6263
if v, ok := findDockerContainerEnvValue(container, label); ok {
63-
labels = append(labels, metrics.Label{Name: labelName("container", label), Value: v})
64+
additionalLabels[labelName("container", label)] = v
6465
}
6566
}
6667
}
6768

68-
return container, labels, nil
69+
if len(additionalLabels) > 0 {
70+
request.setLabels(additionalLabels)
71+
}
72+
73+
return container, nil
6974
}
7075

71-
func findContainerByIP(ip string, labels []metrics.Label, containers []docker.APIContainers) (*docker.Container, error) {
76+
func findContainerByIP(ip string, request *Request, containers []docker.APIContainers) (*docker.Container, error) {
7277
for _, container := range containers {
7378
for name, network := range container.Networks.Networks {
7479
if network.IPAddress == ip {
75-
logWithLabels(labels).Infof("Found container IP '%s' in %+v within network '%s'", ip, container.Names, name)
80+
request.log.Infof("Found container IP '%s' in %+v within network '%s'", ip, container.Names, name)
7681

7782
inspectedContainer, err := dockerClient.InspectContainer(container.ID)
7883
if err != nil {
@@ -87,13 +92,13 @@ func findContainerByIP(ip string, labels []metrics.Label, containers []docker.AP
8792
return nil, fmt.Errorf("Could not find any container with IP %s", ip)
8893
}
8994

90-
func findDockerContainerIAMRole(container *docker.Container) (string, error) {
95+
func findDockerContainerIAMRole(container *docker.Container, request *Request) (string, error) {
9196
if v, ok := findDockerContainerEnvValue(container, "IAM_ROLE"); ok {
9297
return v, nil
9398
}
9499

95100
if defaultRole != "" {
96-
log.Infof("Could not find IAM_ROLE in the container, returning DEFAULT_ROLE %s", defaultRole)
101+
request.log.Infof("Could not find IAM_ROLE in the container, returning DEFAULT_ROLE %s", defaultRole)
97102
return defaultRole, nil
98103
}
99104

0 commit comments

Comments
 (0)