-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathecs.go
179 lines (142 loc) · 6.11 KB
/
ecs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ecs
import (
"bytes"
"context"
"fmt"
"net/http"
"os"
"strings"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/translator/conventions"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal"
)
const (
TypeStr = "ecs"
tmde3EnvVar = "ECS_CONTAINER_METADATA_URI"
tmde4EnvVar = "ECS_CONTAINER_METADATA_URI_V4"
)
var _ internal.Detector = (*Detector)(nil)
type Detector struct {
provider ecsMetadataProvider
}
func NewDetector(params component.ProcessorCreateParams) (internal.Detector, error) {
return &Detector{provider: &ecsMetadataProviderImpl{logger: params.Logger, client: &http.Client{}}}, nil
}
// Records metadata retrieved from the ECS Task Metadata Endpoint (TMDE) as resource attributes
// TODO(willarmiros): Replace all attribute fields and enums with values defined in "conventions" once they exist
func (d *Detector) Detect(context.Context) (pdata.Resource, error) {
res := pdata.NewResource()
tmde := getTmdeFromEnv()
// Fail fast if neither env var is present
if tmde == "" {
// TODO: Log a more specific error with zap
return res, nil
}
tmdeResp, err := d.provider.fetchTaskMetaData(tmde)
if err != nil || tmdeResp == nil {
return res, err
}
attr := res.Attributes()
attr.InsertString(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderAWS)
attr.InsertString("cloud.infrastructure_service", "ECS")
attr.InsertString("aws.ecs.task.arn", tmdeResp.TaskARN)
attr.InsertString("aws.ecs.task.family", tmdeResp.Family)
region, account := parseRegionAndAccount(tmdeResp.TaskARN)
if account != "" {
attr.InsertString(conventions.AttributeCloudAccount, account)
}
if region != "" {
attr.InsertString(conventions.AttributeCloudRegion, region)
}
// TMDE returns the the cluster short name or ARN, so we need to construct the ARN if necessary
attr.InsertString("aws.ecs.cluster.arn", constructClusterArn(tmdeResp.Cluster, region, account))
// The Availability Zone is not available in all Fargate runtimes
if tmdeResp.AvailabilityZone != "" {
attr.InsertString(conventions.AttributeCloudZone, tmdeResp.AvailabilityZone)
}
// The launch type and log data attributes are only available in TMDE v4
switch lt := strings.ToLower(tmdeResp.LaunchType); lt {
case "ec2":
attr.InsertString("aws.ecs.launchtype", "ec2")
case "fargate":
attr.InsertString("aws.ecs.launchtype", "fargate")
}
selfMetaData, err := d.provider.fetchContainerMetaData(tmde)
if err != nil || selfMetaData == nil {
return res, err
}
logAttributes := [4]string{"aws.log.group.names", "aws.log.group.arns", "aws.log.stream.names", "aws.log.stream.arns"}
for i, attribVal := range getValidLogData(tmdeResp.Containers, selfMetaData, account) {
if attribVal.ArrayVal().Len() > 0 {
attr.Insert(logAttributes[i], attribVal)
}
}
return res, nil
}
func getTmdeFromEnv() string {
var tmde string
if tmde = strings.TrimSpace(os.Getenv(tmde4EnvVar)); tmde == "" {
tmde = strings.TrimSpace(os.Getenv(tmde3EnvVar))
}
return tmde
}
func constructClusterArn(cluster, region, account string) string {
// If cluster is already an ARN, return it
if bytes.IndexByte([]byte(cluster), byte(':')) != -1 {
return cluster
}
return fmt.Sprintf("arn:aws:ecs:%s:%s:cluster/%s", region, account, cluster)
}
// Parses the AWS Account ID and AWS Region from a task ARN
// See: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-account-settings.html#ecs-resource-ids
func parseRegionAndAccount(taskARN string) (region string, account string) {
parts := strings.Split(taskARN, ":")
if len(parts) >= 5 {
return parts[3], parts[4]
}
return "", ""
}
// Filter out non-normal containers, our own container since we assume the collector is run as a sidecar,
// "init" containers which only run at startup then shutdown (as indicated by the "KnownStatus" attribute),
// containers not using AWS Logs, and those without log group metadata to get the final lists of valid log data
// See: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v4.html#task-metadata-endpoint-v4-response
func getValidLogData(containers []Container, self *Container, account string) [4]pdata.AttributeValue {
logGroupNames := pdata.NewAttributeValueArray()
logGroupArns := pdata.NewAttributeValueArray()
logStreamNames := pdata.NewAttributeValueArray()
logStreamArns := pdata.NewAttributeValueArray()
for _, container := range containers {
logData := container.LogOptions
if container.Type == "NORMAL" &&
container.KnownStatus == "RUNNING" &&
container.LogDriver == "awslogs" &&
self.DockerID != container.DockerID &&
logData != (LogData{}) {
logGroupNames.ArrayVal().Append(pdata.NewAttributeValueString(logData.LogGroup))
logGroupArns.ArrayVal().Append(pdata.NewAttributeValueString(constructLogGroupArn(logData.Region, account, logData.LogGroup)))
logStreamNames.ArrayVal().Append(pdata.NewAttributeValueString(logData.Stream))
logStreamArns.ArrayVal().Append(pdata.NewAttributeValueString(constructLogStreamArn(logData.Region, account, logData.LogGroup, logData.Stream)))
}
}
return [4]pdata.AttributeValue{logGroupNames, logGroupArns, logStreamNames, logStreamArns}
}
func constructLogGroupArn(region, account, group string) string {
return fmt.Sprintf("arn:aws:logs:%s:%s:log-group:%s", region, account, group)
}
func constructLogStreamArn(region, account, group, stream string) string {
return fmt.Sprintf("%s:log-stream:%s", constructLogGroupArn(region, account, group), stream)
}