-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
ec2.go
165 lines (144 loc) · 4.69 KB
/
ec2.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package ec2 // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/aws/ec2"
import (
"context"
"fmt"
"net/http"
"regexp"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/processor"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
"go.uber.org/zap"
ec2provider "github.com/open-telemetry/opentelemetry-collector-contrib/internal/metadataproviders/aws/ec2"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/aws/ec2/internal/metadata"
)
const (
// TypeStr is type of detector.
TypeStr = "ec2"
tagPrefix = "ec2.tag."
)
var _ internal.Detector = (*Detector)(nil)
type Detector struct {
metadataProvider ec2provider.Provider
tagKeyRegexes []*regexp.Regexp
logger *zap.Logger
rb *metadata.ResourceBuilder
}
func NewDetector(set processor.CreateSettings, dcfg internal.DetectorConfig) (internal.Detector, error) {
cfg := dcfg.(Config)
sess, err := session.NewSession()
if err != nil {
return nil, err
}
tagKeyRegexes, err := compileRegexes(cfg)
if err != nil {
return nil, err
}
return &Detector{
metadataProvider: ec2provider.NewProvider(sess),
tagKeyRegexes: tagKeyRegexes,
logger: set.Logger,
rb: metadata.NewResourceBuilder(cfg.ResourceAttributes),
}, nil
}
func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schemaURL string, err error) {
if _, err = d.metadataProvider.InstanceID(ctx); err != nil {
d.logger.Debug("EC2 metadata unavailable", zap.Error(err))
return pcommon.NewResource(), "", nil
}
meta, err := d.metadataProvider.Get(ctx)
if err != nil {
return pcommon.NewResource(), "", fmt.Errorf("failed getting identity document: %w", err)
}
hostname, err := d.metadataProvider.Hostname(ctx)
if err != nil {
return pcommon.NewResource(), "", fmt.Errorf("failed getting hostname: %w", err)
}
d.rb.SetCloudProvider(conventions.AttributeCloudProviderAWS)
d.rb.SetCloudPlatform(conventions.AttributeCloudPlatformAWSEC2)
d.rb.SetCloudRegion(meta.Region)
d.rb.SetCloudAccountID(meta.AccountID)
d.rb.SetCloudAvailabilityZone(meta.AvailabilityZone)
d.rb.SetHostID(meta.InstanceID)
d.rb.SetHostImageID(meta.ImageID)
d.rb.SetHostType(meta.InstanceType)
d.rb.SetHostName(hostname)
res := d.rb.Emit()
if len(d.tagKeyRegexes) != 0 {
client := getHTTPClientSettings(ctx, d.logger)
tags, err := connectAndFetchEc2Tags(meta.Region, meta.InstanceID, d.tagKeyRegexes, client)
if err != nil {
return res, "", fmt.Errorf("failed fetching ec2 instance tags: %w", err)
}
for key, val := range tags {
res.Attributes().PutStr(tagPrefix+key, val)
}
}
return res, conventions.SchemaURL, nil
}
func getHTTPClientSettings(ctx context.Context, logger *zap.Logger) *http.Client {
client, err := internal.ClientFromContext(ctx)
if err != nil {
client = http.DefaultClient
logger.Debug("Error retrieving client from context thus creating default", zap.Error(err))
}
return client
}
func connectAndFetchEc2Tags(region string, instanceID string, tagKeyRegexes []*regexp.Regexp, client *http.Client) (map[string]string, error) {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(region),
HTTPClient: client},
)
if err != nil {
return nil, err
}
e := ec2.New(sess)
return fetchEC2Tags(e, instanceID, tagKeyRegexes)
}
func fetchEC2Tags(svc ec2iface.EC2API, instanceID string, tagKeyRegexes []*regexp.Regexp) (map[string]string, error) {
ec2Tags, err := svc.DescribeTags(&ec2.DescribeTagsInput{
Filters: []*ec2.Filter{{
Name: aws.String("resource-id"),
Values: []*string{
aws.String(instanceID),
},
}},
})
if err != nil {
return nil, err
}
tags := make(map[string]string)
for _, tag := range ec2Tags.Tags {
matched := regexArrayMatch(tagKeyRegexes, *tag.Key)
if matched {
tags[*tag.Key] = *tag.Value
}
}
return tags, nil
}
func compileRegexes(cfg Config) ([]*regexp.Regexp, error) {
tagRegexes := make([]*regexp.Regexp, len(cfg.Tags))
for i, elem := range cfg.Tags {
regex, err := regexp.Compile(elem)
if err != nil {
return nil, err
}
tagRegexes[i] = regex
}
return tagRegexes, nil
}
func regexArrayMatch(arr []*regexp.Regexp, val string) bool {
for _, elem := range arr {
matched := elem.MatchString(val)
if matched {
return true
}
}
return false
}