-
Notifications
You must be signed in to change notification settings - Fork 293
/
AWSTracingPipelineHandler.cs
234 lines (197 loc) · 7.67 KB
/
AWSTracingPipelineHandler.cs
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.Runtime.Internal;
using Amazon.Util;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Extensions.AWS.Trace;
using OpenTelemetry.Trace;
namespace OpenTelemetry.Instrumentation.AWS.Implementation;
internal class AWSTracingPipelineHandler : PipelineHandler
{
internal const string ActivitySourceName = "Amazon.AWS.AWSClientInstrumentation";
private static readonly AWSXRayPropagator AwsPropagator = new AWSXRayPropagator();
private static readonly Action<IDictionary<string, string>, string, string> Setter = (carrier, name, value) =>
{
carrier[name] = value;
};
private static readonly ActivitySource AWSSDKActivitySource = new ActivitySource(ActivitySourceName);
private readonly AWSClientInstrumentationOptions options;
public AWSTracingPipelineHandler(AWSClientInstrumentationOptions options)
{
this.options = options;
}
public override void InvokeSync(IExecutionContext executionContext)
{
var activity = this.ProcessBeginRequest(executionContext);
try
{
base.InvokeSync(executionContext);
}
catch (Exception ex)
{
if (activity != null)
{
ProcessException(activity, ex);
}
throw;
}
finally
{
if (activity != null)
{
ProcessEndRequest(executionContext, activity);
}
}
}
public override async Task<T> InvokeAsync<T>(IExecutionContext executionContext)
{
T? ret = null;
var activity = this.ProcessBeginRequest(executionContext);
try
{
ret = await base.InvokeAsync<T>(executionContext).ConfigureAwait(false);
}
catch (Exception ex)
{
if (activity != null)
{
ProcessException(activity, ex);
}
throw;
}
finally
{
if (activity != null)
{
ProcessEndRequest(executionContext, activity);
}
}
return ret;
}
private static void ProcessEndRequest(IExecutionContext executionContext, Activity activity)
{
var responseContext = executionContext.ResponseContext;
var requestContext = executionContext.RequestContext;
if (activity.IsAllDataRequested)
{
if (Utils.GetTagValue(activity, AWSSemanticConventions.AttributeAWSRequestId) == null)
{
activity.SetTag(AWSSemanticConventions.AttributeAWSRequestId, FetchRequestId(requestContext, responseContext));
}
var httpResponse = responseContext.HttpResponse;
if (httpResponse != null)
{
int statusCode = (int)httpResponse.StatusCode;
AddStatusCodeToActivity(activity, statusCode);
activity.SetTag(AWSSemanticConventions.AttributeHttpResponseContentLength, httpResponse.ContentLength);
}
}
activity.Stop();
}
private static void ProcessException(Activity activity, Exception ex)
{
if (activity.IsAllDataRequested)
{
activity.RecordException(ex);
activity.SetStatus(Status.Error.WithDescription(ex.Message));
if (ex is AmazonServiceException amazonServiceException)
{
AddStatusCodeToActivity(activity, (int)amazonServiceException.StatusCode);
activity.SetTag(AWSSemanticConventions.AttributeAWSRequestId, amazonServiceException.RequestId);
}
}
}
private static void AddRequestSpecificInformation(Activity activity, IRequestContext requestContext, string service)
{
if (AWSServiceHelper.ServiceParameterMap.TryGetValue(service, out string parameter))
{
AmazonWebServiceRequest request = requestContext.OriginalRequest;
var property = request.GetType().GetProperty(parameter);
if (property != null)
{
if (AWSServiceHelper.ParameterAttributeMap.TryGetValue(parameter, out string attribute))
{
activity.SetTag(attribute, property.GetValue(request));
}
}
}
if (AWSServiceType.IsDynamoDbService(service))
{
activity.SetTag(SemanticConventions.AttributeDbSystem, AWSSemanticConventions.AttributeValueDynamoDb);
}
else if (AWSServiceType.IsSqsService(service))
{
SqsRequestContextHelper.AddAttributes(
requestContext, AWSMessagingUtils.InjectIntoDictionary(new PropagationContext(activity.Context, Baggage.Current)));
}
else if (AWSServiceType.IsSnsService(service))
{
SnsRequestContextHelper.AddAttributes(
requestContext, AWSMessagingUtils.InjectIntoDictionary(new PropagationContext(activity.Context, Baggage.Current)));
}
}
private static void AddStatusCodeToActivity(Activity activity, int status_code)
{
activity.SetTag(AWSSemanticConventions.AttributeHttpStatusCode, status_code);
}
private static string FetchRequestId(IRequestContext requestContext, IResponseContext responseContext)
{
string request_id = string.Empty;
var response = responseContext.Response;
if (response != null)
{
request_id = response.ResponseMetadata.RequestId;
}
else
{
var request_headers = requestContext.Request.Headers;
if (string.IsNullOrEmpty(request_id) && request_headers.TryGetValue("x-amzn-RequestId", out string req_id))
{
request_id = req_id;
}
if (string.IsNullOrEmpty(request_id) && request_headers.TryGetValue("x-amz-request-id", out req_id))
{
request_id = req_id;
}
if (string.IsNullOrEmpty(request_id) && request_headers.TryGetValue("x-amz-id-2", out req_id))
{
request_id = req_id;
}
}
return request_id;
}
private Activity? ProcessBeginRequest(IExecutionContext executionContext)
{
var requestContext = executionContext.RequestContext;
var service = AWSServiceHelper.GetAWSServiceName(requestContext);
var operation = AWSServiceHelper.GetAWSOperationName(requestContext);
Activity? activity = AWSSDKActivitySource.StartActivity(service + "." + operation, ActivityKind.Client);
if (activity == null)
{
return null;
}
if (this.options.SuppressDownstreamInstrumentation)
{
SuppressInstrumentationScope.Enter();
}
if (activity.IsAllDataRequested)
{
activity.SetTag(AWSSemanticConventions.AttributeAWSServiceName, service);
activity.SetTag(AWSSemanticConventions.AttributeAWSOperationName, operation);
var client = executionContext.RequestContext.ClientConfig;
if (client != null)
{
var region = client.RegionEndpoint?.SystemName;
activity.SetTag(AWSSemanticConventions.AttributeAWSRegion, region ?? AWSSDKUtils.DetermineRegion(client.ServiceURL));
}
AddRequestSpecificInformation(activity, requestContext, service);
}
AwsPropagator.Inject(new PropagationContext(activity.Context, Baggage.Current), requestContext.Request.Headers, Setter);
return activity;
}
}