@@ -99,6 +99,39 @@ static void fetchMetadata(
99
99
}
100
100
}
101
101
102
+ private static Optional <String > getAccountId (@ Nullable String arn ) {
103
+ return getArnPart (arn , ArnPart .ACCOUNT );
104
+ }
105
+
106
+ private static Optional <String > getRegion (@ Nullable String arn ) {
107
+ return getArnPart (arn , ArnPart .REGION );
108
+ }
109
+
110
+ private static enum ArnPart {
111
+ REGION (3 ),
112
+ ACCOUNT (4 );
113
+
114
+ final int partIndex ;
115
+
116
+ private ArnPart (int partIndex ) {
117
+ this .partIndex = partIndex ;
118
+ }
119
+ }
120
+
121
+ private static Optional <String > getArnPart (@ Nullable String arn , ArnPart arnPart ) {
122
+ if (arn == null ) {
123
+ return Optional .empty ();
124
+ }
125
+
126
+ String [] arnParts = arn .split (":" );
127
+
128
+ if (arnPart .partIndex >= arnParts .length ) {
129
+ return Optional .empty ();
130
+ }
131
+
132
+ return Optional .of (arnParts [arnPart .partIndex ]);
133
+ }
134
+
102
135
// Suppression is required for CONTAINER_IMAGE_TAG until we are ready to upgrade.
103
136
@ SuppressWarnings ("deprecation" )
104
137
static void parseResponse (
@@ -109,17 +142,27 @@ static void parseResponse(
109
142
return ;
110
143
}
111
144
145
+ // Either the container ARN or the task ARN, they both contain the
146
+ // account id and region tokens we need later for the cloud.account.id
147
+ // and cloud.region attributes.
148
+ String arn = null ;
149
+
112
150
while (parser .nextToken () != JsonToken .END_OBJECT ) {
113
151
String value = parser .nextTextValue ();
114
152
switch (parser .currentName ()) {
153
+ case "AvailabilityZone" :
154
+ attrBuilders .put (ResourceAttributes .CLOUD_AVAILABILITY_ZONE , value );
155
+ break ;
115
156
case "DockerId" :
116
157
attrBuilders .put (ResourceAttributes .CONTAINER_ID , value );
117
158
break ;
118
159
case "DockerName" :
119
160
attrBuilders .put (ResourceAttributes .CONTAINER_NAME , value );
120
161
break ;
121
162
case "ContainerARN" :
163
+ arn = value ;
122
164
attrBuilders .put (ResourceAttributes .AWS_ECS_CONTAINER_ARN , value );
165
+ attrBuilders .put (ResourceAttributes .CLOUD_RESOURCE_ID , value );
123
166
logArnBuilder .setContainerArn (value );
124
167
break ;
125
168
case "Image" :
@@ -149,6 +192,7 @@ static void parseResponse(
149
192
logArnBuilder .setRegion (value );
150
193
break ;
151
194
case "TaskARN" :
195
+ arn = value ;
152
196
attrBuilders .put (ResourceAttributes .AWS_ECS_TASK_ARN , value );
153
197
break ;
154
198
case "LaunchType" :
@@ -165,6 +209,10 @@ static void parseResponse(
165
209
break ;
166
210
}
167
211
}
212
+
213
+ getRegion (arn ).ifPresent (region -> attrBuilders .put (ResourceAttributes .CLOUD_REGION , region ));
214
+ getAccountId (arn )
215
+ .ifPresent (accountId -> attrBuilders .put (ResourceAttributes .CLOUD_ACCOUNT_ID , accountId ));
168
216
}
169
217
170
218
private EcsResource () {}
@@ -196,9 +244,7 @@ void setLogStreamName(@Nullable String logStreamName) {
196
244
}
197
245
198
246
void setContainerArn (@ Nullable String containerArn ) {
199
- if (containerArn != null ) {
200
- account = containerArn .split (":" )[4 ];
201
- }
247
+ account = getAccountId (containerArn ).orElse (null );
202
248
}
203
249
204
250
Optional <String > getLogGroupArn () {
0 commit comments