-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
aws.go
279 lines (259 loc) · 8.9 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
// Copyright 2019, 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 translator // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsxrayexporter/internal/translator"
import (
"bytes"
"strconv"
"github.com/aws/aws-sdk-go/aws"
"go.opentelemetry.io/collector/pdata/pcommon"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
awsxray "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/xray"
)
func makeAws(attributes map[string]pcommon.Value, resource pcommon.Resource) (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
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.StringVal()
case conventions.AttributeCloudPlatform:
service = value.StringVal()
case conventions.AttributeCloudAccountID:
account = value.StringVal()
case conventions.AttributeCloudAvailabilityZone:
zone = value.StringVal()
case conventions.AttributeHostID:
hostID = value.StringVal()
case conventions.AttributeHostType:
hostType = value.StringVal()
case conventions.AttributeHostImageID:
amiID = value.StringVal()
case conventions.AttributeContainerName:
if container == "" {
container = value.StringVal()
}
case conventions.AttributeK8SPodName:
podUID = value.StringVal()
case conventions.AttributeServiceNamespace:
namespace = value.StringVal()
case conventions.AttributeServiceInstanceID:
deployID = value.StringVal()
case conventions.AttributeServiceVersion:
versionLabel = value.StringVal()
case conventions.AttributeTelemetrySDKName:
sdkName = value.StringVal()
case conventions.AttributeTelemetrySDKLanguage:
sdkLanguage = value.StringVal()
case conventions.AttributeTelemetrySDKVersion:
sdkVersion = value.StringVal()
case conventions.AttributeTelemetryAutoVersion:
autoVersion = value.StringVal()
case conventions.AttributeContainerID:
containerID = value.StringVal()
case conventions.AttributeK8SClusterName:
clusterName = value.StringVal()
case conventions.AttributeAWSECSClusterARN:
clusterArn = value.StringVal()
case conventions.AttributeAWSECSContainerARN:
containerArn = value.StringVal()
case conventions.AttributeAWSECSTaskARN:
taskArn = value.StringVal()
case conventions.AttributeAWSECSTaskFamily:
taskFamily = value.StringVal()
case conventions.AttributeAWSECSLaunchtype:
launchType = value.StringVal()
case conventions.AttributeAWSLogGroupNames:
logGroups = value.SliceVal()
case conventions.AttributeAWSLogGroupARNs:
logGroupArns = value.SliceVal()
}
return true
})
if awsOperation, ok := attributes[awsxray.AWSOperationAttribute]; ok {
operation = awsOperation.StringVal()
} else if rpcMethod, ok := attributes[conventions.AttributeRPCMethod]; ok {
operation = rpcMethod.StringVal()
}
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.StringVal()
}
case awsxray.AWSRegionAttribute:
remoteRegion = value.StringVal()
case awsxray.AWSRequestIDAttribute:
fallthrough
case awsxray.AWSRequestIDAttribute2:
requestID = value.StringVal()
case awsxray.AWSQueueURLAttribute:
fallthrough
case awsxray.AWSQueueURLAttribute2:
queueURL = value.StringVal()
case awsxray.AWSTableNameAttribute:
fallthrough
case awsxray.AWSTableNameAttribute2:
tableName = value.StringVal()
default:
filtered[key] = value
}
}
if cloud != conventions.AttributeCloudProviderAWS && cloud != "" {
return filtered, nil // not AWS so return nil
}
// 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 just recording the names
if logGroupArns != (pcommon.Slice{}) && logGroupArns.Len() > 0 {
cwl = getLogGroupMetadata(logGroupArns, true)
} else if logGroups != (pcommon.Slice{}) && logGroups.Len() > 0 {
cwl = getLogGroupMetadata(logGroups, 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),
}
return filtered, awsData
}
// 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).StringVal()),
LogGroup: awsxray.String(parseLogGroup(logGroups.At(i).StringVal())),
})
} else {
lgm = append(lgm, awsxray.LogGroupMetadata{
LogGroup: awsxray.String(logGroups.At(i).StringVal()),
})
}
}
return lgm
}
func parseLogGroup(arn string) string {
i := bytes.LastIndexByte([]byte(arn), byte(':'))
if i != -1 {
return arn[i+1:]
}
return arn
}