|
1 | 1 | package datadog.trace.instrumentation.aws.v0;
|
2 | 2 |
|
3 | 3 | import static datadog.trace.bootstrap.instrumentation.api.ResourceNamePriorities.RPC_COMMAND_NAME;
|
| 4 | +import static datadog.trace.core.datastreams.TagsProcessor.DIRECTION_OUT; |
4 | 5 |
|
5 | 6 | import com.amazonaws.AmazonWebServiceRequest;
|
6 | 7 | import com.amazonaws.AmazonWebServiceResponse;
|
7 | 8 | import com.amazonaws.Request;
|
8 | 9 | import com.amazonaws.Response;
|
| 10 | +import com.amazonaws.http.HttpMethodName; |
9 | 11 | import datadog.trace.api.Config;
|
10 | 12 | import datadog.trace.api.DDTags;
|
11 | 13 | import datadog.trace.api.cache.DDCache;
|
|
20 | 22 | import datadog.trace.bootstrap.instrumentation.api.Tags;
|
21 | 23 | import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
|
22 | 24 | import datadog.trace.bootstrap.instrumentation.decorator.HttpClientDecorator;
|
| 25 | +import datadog.trace.core.datastreams.TagsProcessor; |
23 | 26 | import java.net.URI;
|
| 27 | +import java.util.LinkedHashMap; |
24 | 28 | import java.util.List;
|
25 | 29 | import java.util.Locale;
|
26 | 30 | import java.util.regex.Matcher;
|
@@ -75,20 +79,25 @@ public AgentSpan onRequest(final AgentSpan span, final Request request) {
|
75 | 79 | super.onRequest(span, request);
|
76 | 80 |
|
77 | 81 | final String awsServiceName = request.getServiceName();
|
| 82 | + final String awsSimplifiedServiceName = simplifyServiceName(awsServiceName); |
78 | 83 | final AmazonWebServiceRequest originalRequest = request.getOriginalRequest();
|
79 | 84 | final Class<?> awsOperation = originalRequest.getClass();
|
80 | 85 | final GetterAccess access = GetterAccess.of(originalRequest);
|
81 | 86 |
|
82 | 87 | span.setTag(InstrumentationTags.AWS_AGENT, COMPONENT_NAME);
|
83 | 88 | span.setTag(InstrumentationTags.AWS_SERVICE, awsServiceName);
|
84 |
| - span.setTag(InstrumentationTags.TOP_LEVEL_AWS_SERVICE, simplifyServiceName(awsServiceName)); |
| 89 | + span.setTag(InstrumentationTags.TOP_LEVEL_AWS_SERVICE, awsSimplifiedServiceName); |
85 | 90 | span.setTag(InstrumentationTags.AWS_OPERATION, awsOperation.getSimpleName());
|
86 | 91 | span.setTag(InstrumentationTags.AWS_ENDPOINT, request.getEndpoint().toString());
|
87 | 92 |
|
88 | 93 | CharSequence awsRequestName = AwsNameCache.getQualifiedName(request);
|
89 |
| - |
90 | 94 | span.setResourceName(awsRequestName, RPC_COMMAND_NAME);
|
91 | 95 |
|
| 96 | + if ("s3".equalsIgnoreCase(awsSimplifiedServiceName) |
| 97 | + && span.traceConfig().isDataStreamsEnabled()) { |
| 98 | + span.setTag(Tags.HTTP_REQUEST_CONTENT_LENGTH, getRequestContentLength(request)); |
| 99 | + } |
| 100 | + |
92 | 101 | switch (awsRequestName.toString()) {
|
93 | 102 | case "SQS.SendMessage":
|
94 | 103 | case "SQS.SendMessageBatch":
|
@@ -233,12 +242,68 @@ public AgentSpan onRequest(final AgentSpan span, final Request request) {
|
233 | 242 | return span;
|
234 | 243 | }
|
235 | 244 |
|
| 245 | + public AgentSpan onServiceResponse( |
| 246 | + final AgentSpan span, final String awsService, final Response response) { |
| 247 | + if ("s3".equalsIgnoreCase(simplifyServiceName(awsService)) |
| 248 | + && span.traceConfig().isDataStreamsEnabled()) { |
| 249 | + long responseSize = getResponseContentLength(response); |
| 250 | + span.setTag(Tags.HTTP_RESPONSE_CONTENT_LENGTH, responseSize); |
| 251 | + |
| 252 | + String key = getSpanTagAsString(span, InstrumentationTags.AWS_OBJECT_KEY); |
| 253 | + String bucket = getSpanTagAsString(span, InstrumentationTags.AWS_BUCKET_NAME); |
| 254 | + String awsOperation = getSpanTagAsString(span, InstrumentationTags.AWS_OPERATION); |
| 255 | + |
| 256 | + if (key != null && bucket != null && awsOperation != null) { |
| 257 | + // GetObjectMetadataRequest may return the object if it's not "HEAD" |
| 258 | + if (HttpMethodName.GET.name().equals(span.getTag(Tags.HTTP_METHOD)) |
| 259 | + && ("GetObjectMetadataRequest".equalsIgnoreCase(awsOperation) |
| 260 | + || "GetObjectRequest".equalsIgnoreCase(awsOperation))) { |
| 261 | + LinkedHashMap<String, String> sortedTags = new LinkedHashMap<>(); |
| 262 | + |
| 263 | + sortedTags.put(TagsProcessor.DIRECTION_TAG, TagsProcessor.DIRECTION_IN); |
| 264 | + sortedTags.put(TagsProcessor.DATASET_NAME_TAG, key); |
| 265 | + sortedTags.put(TagsProcessor.DATASET_NAMESPACE_TAG, bucket); |
| 266 | + sortedTags.put(TagsProcessor.TOPIC_TAG, bucket); |
| 267 | + sortedTags.put(TagsProcessor.TYPE_TAG, "s3"); |
| 268 | + |
| 269 | + AgentTracer.get() |
| 270 | + .getDataStreamsMonitoring() |
| 271 | + .setCheckpoint(span, sortedTags, 0, responseSize); |
| 272 | + } |
| 273 | + |
| 274 | + if ("PutObjectRequest".equalsIgnoreCase(awsOperation) |
| 275 | + || "UploadPartRequest".equalsIgnoreCase(awsOperation)) { |
| 276 | + Object requestSize = span.getTag(Tags.HTTP_REQUEST_CONTENT_LENGTH); |
| 277 | + long payloadSize = 0; |
| 278 | + if (requestSize != null) { |
| 279 | + payloadSize = (long) requestSize; |
| 280 | + } |
| 281 | + |
| 282 | + LinkedHashMap<String, String> sortedTags = new LinkedHashMap<>(); |
| 283 | + |
| 284 | + sortedTags.put(TagsProcessor.DIRECTION_TAG, DIRECTION_OUT); |
| 285 | + sortedTags.put(TagsProcessor.DATASET_NAME_TAG, key); |
| 286 | + sortedTags.put(TagsProcessor.DATASET_NAMESPACE_TAG, bucket); |
| 287 | + sortedTags.put(TagsProcessor.TOPIC_TAG, bucket); |
| 288 | + sortedTags.put(TagsProcessor.TYPE_TAG, "s3"); |
| 289 | + |
| 290 | + AgentTracer.get() |
| 291 | + .getDataStreamsMonitoring() |
| 292 | + .setCheckpoint(span, sortedTags, 0, payloadSize); |
| 293 | + } |
| 294 | + } |
| 295 | + } |
| 296 | + |
| 297 | + return span; |
| 298 | + } |
| 299 | + |
236 | 300 | @Override
|
237 | 301 | public AgentSpan onResponse(final AgentSpan span, final Response response) {
|
238 | 302 | if (response.getAwsResponse() instanceof AmazonWebServiceResponse) {
|
239 | 303 | final AmazonWebServiceResponse awsResp = (AmazonWebServiceResponse) response.getAwsResponse();
|
240 | 304 | span.setTag(InstrumentationTags.AWS_REQUEST_ID, awsResp.getRequestId());
|
241 | 305 | }
|
| 306 | + |
242 | 307 | return super.onResponse(span, response);
|
243 | 308 | }
|
244 | 309 |
|
|
0 commit comments