-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
aws.go
341 lines (314 loc) · 10.4 KB
/
aws.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package translator // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsxrayexporter/internal/translator"
import (
"strconv"
"strings"
"github.com/aws/aws-sdk-go/aws"
"go.opentelemetry.io/collector/pdata/pcommon"
conventions "go.opentelemetry.io/collector/semconv/v1.12.0"
awsxray "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/xray"
)
func makeAws(attributes map[string]pcommon.Value, resource pcommon.Resource, logGroupNames []string) (map[string]pcommon.Value, *awsxray.AWSData) {
var (
cloud string
service string
account string
zone string
hostID string
hostType string
amiID string
container string
namespace string
deployID string
versionLabel string
operation string
remoteRegion string
requestID string
queueURL string
tableName string
tableNames []string
sdk string
sdkName string
sdkLanguage string
sdkVersion string
autoVersion string
containerID string
clusterName string
podUID string
clusterArn string
containerArn string
taskArn string
taskFamily string
launchType string
logGroups pcommon.Slice
logGroupArns pcommon.Slice
cwl []awsxray.LogGroupMetadata
ec2 *awsxray.EC2Metadata
ecs *awsxray.ECSMetadata
ebs *awsxray.BeanstalkMetadata
eks *awsxray.EKSMetadata
)
filtered := make(map[string]pcommon.Value)
resource.Attributes().Range(func(key string, value pcommon.Value) bool {
switch key {
case conventions.AttributeCloudProvider:
cloud = value.Str()
case conventions.AttributeCloudPlatform:
service = value.Str()
case conventions.AttributeCloudAccountID:
account = value.Str()
case conventions.AttributeCloudAvailabilityZone:
zone = value.Str()
case conventions.AttributeHostID:
hostID = value.Str()
case conventions.AttributeHostType:
hostType = value.Str()
case conventions.AttributeHostImageID:
amiID = value.Str()
case conventions.AttributeContainerName:
if container == "" {
container = value.Str()
}
case conventions.AttributeK8SPodName:
podUID = value.Str()
case conventions.AttributeServiceNamespace:
namespace = value.Str()
case conventions.AttributeServiceInstanceID:
deployID = value.Str()
case conventions.AttributeServiceVersion:
versionLabel = value.Str()
case conventions.AttributeTelemetrySDKName:
sdkName = value.Str()
case conventions.AttributeTelemetrySDKLanguage:
sdkLanguage = value.Str()
case conventions.AttributeTelemetrySDKVersion:
sdkVersion = value.Str()
case conventions.AttributeTelemetryAutoVersion:
autoVersion = value.Str()
case conventions.AttributeContainerID:
containerID = value.Str()
case conventions.AttributeK8SClusterName:
clusterName = value.Str()
case conventions.AttributeAWSECSClusterARN:
clusterArn = value.Str()
case conventions.AttributeAWSECSContainerARN:
containerArn = value.Str()
case conventions.AttributeAWSECSTaskARN:
taskArn = value.Str()
case conventions.AttributeAWSECSTaskFamily:
taskFamily = value.Str()
case conventions.AttributeAWSECSLaunchtype:
launchType = value.Str()
case conventions.AttributeAWSLogGroupNames:
logGroups = normalizeToSlice(value)
case conventions.AttributeAWSLogGroupARNs:
logGroupArns = normalizeToSlice(value)
}
return true
})
if awsOperation, ok := attributes[awsxray.AWSOperationAttribute]; ok {
operation = awsOperation.Str()
} else if rpcMethod, ok := attributes[conventions.AttributeRPCMethod]; ok {
operation = rpcMethod.Str()
}
for key, value := range attributes {
switch key {
case conventions.AttributeRPCMethod:
// Determinstically handled with if else above
case awsxray.AWSOperationAttribute:
// Determinstically handled with if else above
case awsxray.AWSAccountAttribute:
if value.Type() != pcommon.ValueTypeEmpty {
account = value.Str()
}
case awsxray.AWSRegionAttribute:
remoteRegion = value.Str()
case awsxray.AWSRequestIDAttribute:
fallthrough
case awsxray.AWSRequestIDAttribute2:
requestID = value.Str()
case awsxray.AWSQueueURLAttribute:
fallthrough
case awsxray.AWSQueueURLAttribute2:
queueURL = value.Str()
case awsxray.AWSTableNameAttribute:
fallthrough
case awsxray.AWSTableNameAttribute2:
tableName = value.Str()
default:
filtered[key] = value
}
}
if cloud != conventions.AttributeCloudProviderAWS && cloud != "" {
return filtered, nil // not AWS so return nil
}
// Favor Semantic Conventions for specific SQS and DynamoDB attributes.
if value, ok := attributes[conventions.AttributeMessagingURL]; ok {
queueURL = value.Str()
}
if value, ok := attributes[conventions.AttributeAWSDynamoDBTableNames]; ok {
switch value.Type() {
case pcommon.ValueTypeSlice:
if value.Slice().Len() == 1 {
tableName = value.Slice().At(0).Str()
} else if value.Slice().Len() > 1 {
tableName = ""
tableNames = []string{}
for i := 0; i < value.Slice().Len(); i++ {
tableNames = append(tableNames, value.Slice().At(i).Str())
}
}
case pcommon.ValueTypeStr:
tableName = value.Str()
}
}
// EC2 - add ec2 metadata to xray request if
// 1. cloud.platfrom is set to "aws_ec2" or
// 2. there is an non-blank host/instance id found
if service == conventions.AttributeCloudPlatformAWSEC2 || hostID != "" {
ec2 = &awsxray.EC2Metadata{
InstanceID: awsxray.String(hostID),
AvailabilityZone: awsxray.String(zone),
InstanceSize: awsxray.String(hostType),
AmiID: awsxray.String(amiID),
}
}
// ECS
if service == conventions.AttributeCloudPlatformAWSECS {
ecs = &awsxray.ECSMetadata{
ContainerName: awsxray.String(container),
ContainerID: awsxray.String(containerID),
AvailabilityZone: awsxray.String(zone),
ContainerArn: awsxray.String(containerArn),
ClusterArn: awsxray.String(clusterArn),
TaskArn: awsxray.String(taskArn),
TaskFamily: awsxray.String(taskFamily),
LaunchType: awsxray.String(launchType),
}
}
// Beanstalk
if service == conventions.AttributeCloudPlatformAWSElasticBeanstalk && deployID != "" {
deployNum, err := strconv.ParseInt(deployID, 10, 64)
if err != nil {
deployNum = 0
}
ebs = &awsxray.BeanstalkMetadata{
Environment: awsxray.String(namespace),
DeploymentID: aws.Int64(deployNum),
VersionLabel: awsxray.String(versionLabel),
}
}
// EKS or native Kubernetes
if service == conventions.AttributeCloudPlatformAWSEKS || clusterName != "" {
eks = &awsxray.EKSMetadata{
ClusterName: awsxray.String(clusterName),
Pod: awsxray.String(podUID),
ContainerID: awsxray.String(containerID),
}
}
// Since we must couple log group ARNs and Log Group Names in the same CWLogs object, we first try to derive the
// names from the ARN, then fall back to recording the names, if they do not exist in the resource
// then pull from them from config.
switch {
case logGroupArns != (pcommon.Slice{}) && logGroupArns.Len() > 0:
cwl = getLogGroupMetadata(logGroupArns, true)
case logGroups != (pcommon.Slice{}) && logGroups.Len() > 0:
cwl = getLogGroupMetadata(logGroups, false)
case logGroupNames != nil:
configSlice := pcommon.NewSlice()
configSlice.EnsureCapacity(len(logGroupNames))
for _, s := range logGroupNames {
configSlice.AppendEmpty().SetStr(s)
}
cwl = getLogGroupMetadata(configSlice, false)
}
if sdkName != "" && sdkLanguage != "" {
// Convention for SDK name for xray SDK information is e.g., `X-Ray SDK for Java`, `X-Ray for Go`.
// We fill in with e.g, `opentelemetry for java` by using the conventions
sdk = sdkName + " for " + sdkLanguage
} else {
sdk = sdkName
}
xray := &awsxray.XRayMetaData{
SDK: awsxray.String(sdk),
SDKVersion: awsxray.String(sdkVersion),
AutoInstrumentation: aws.Bool(autoVersion != ""),
}
awsData := &awsxray.AWSData{
AccountID: awsxray.String(account),
Beanstalk: ebs,
CWLogs: cwl,
ECS: ecs,
EC2: ec2,
EKS: eks,
XRay: xray,
Operation: awsxray.String(operation),
RemoteRegion: awsxray.String(remoteRegion),
RequestID: awsxray.String(requestID),
QueueURL: awsxray.String(queueURL),
TableName: awsxray.String(tableName),
TableNames: tableNames,
}
return filtered, awsData
}
func getLogGroupNamesOrArns(logGroupNamesOrArns string) []string {
// Split the input string by '&'
items := strings.Split(logGroupNamesOrArns, "&")
// Filter out empty strings
var result []string
for _, item := range items {
if item != "" {
result = append(result, item)
}
}
return result
}
// Normalize value to slice.
// 1. String values are converted to a slice so that we can also handle resource
// attributes that are set using the OTEL_RESOURCE_ATTRIBUTES
// (multiple log group names or arns are separate by & like this "log-group1&log-group2&log-group3")
// 2. Slices are kept as they are
// 3. Other types will result in a empty slice so that we avoid panic.
func normalizeToSlice(v pcommon.Value) pcommon.Slice {
switch v.Type() {
case pcommon.ValueTypeStr:
s := pcommon.NewSlice()
logGroupNamesOrArns := getLogGroupNamesOrArns(v.Str())
for _, logGroupOrArn := range logGroupNamesOrArns {
s.AppendEmpty().SetStr(logGroupOrArn)
}
return s
case pcommon.ValueTypeSlice:
return v.Slice()
default:
return pcommon.NewSlice()
}
}
// Given an array of log group ARNs, create a corresponding amount of LogGroupMetadata objects with log_group and arn
// populated, or given an array of just log group names, create the LogGroupMetadata objects with arn omitted
func getLogGroupMetadata(logGroups pcommon.Slice, isArn bool) []awsxray.LogGroupMetadata {
var lgm []awsxray.LogGroupMetadata
for i := 0; i < logGroups.Len(); i++ {
if isArn {
lgm = append(lgm, awsxray.LogGroupMetadata{
Arn: awsxray.String(logGroups.At(i).Str()),
LogGroup: awsxray.String(parseLogGroup(logGroups.At(i).Str())),
})
} else {
lgm = append(lgm, awsxray.LogGroupMetadata{
LogGroup: awsxray.String(logGroups.At(i).Str()),
})
}
}
return lgm
}
// Log group name will always be in the 7th position of the ARN
// https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/iam-access-control-overview-cwl.html#CWL_ARN_Format
func parseLogGroup(arn string) string {
parts := strings.Split(arn, ":")
if len(parts) >= 7 {
return parts[6]
}
return arn
}