Skip to content

Commit 962d36f

Browse files
authored
[Bug] Fixed SigV4 Signer not Automatically Injecting Security Token Header (#186)
1 parent 1f4f454 commit 962d36f

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/AWS.Distro.OpenTelemetry.AutoInstrumentation/OtlpAwsSpanExporter/OtlpAwsSpanExporter.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,9 @@ private async Task<IRequest> GetSignedSigV4Request(byte[] content)
174174
HttpMethod = "POST",
175175
ContentStream = new MemoryStream(content),
176176
Endpoint = this.endpoint,
177+
SignatureVersion = SignatureVersion.SigV4,
177178
};
178179

179-
request.Headers.Add("Host", this.endpoint.Host);
180-
request.Headers.Add("content-type", ContentType);
181-
182-
ImmutableCredentials credentials = await this.authenticator.GetCredentialsAsync();
183-
184180
AmazonXRayConfig config = new AmazonXRayConfig()
185181
{
186182
AuthenticationRegion = this.region,
@@ -189,6 +185,18 @@ private async Task<IRequest> GetSignedSigV4Request(byte[] content)
189185
RegionEndpoint = RegionEndpoint.GetBySystemName(this.region),
190186
};
191187

188+
ImmutableCredentials credentials = await this.authenticator.GetCredentialsAsync();
189+
190+
// Need to explictily add this for using temporary security credentials from AWS STS.
191+
// SigV4 signing library does not automatically add this header.
192+
if (credentials.UseToken && credentials.Token != null)
193+
{
194+
request.Headers.Add("x-amz-security-token", credentials.Token);
195+
}
196+
197+
request.Headers.Add("Host", this.endpoint.Host);
198+
request.Headers.Add("content-type", ContentType);
199+
192200
this.authenticator.Sign(request, config, credentials);
193201

194202
return request;
@@ -210,7 +218,7 @@ internal class RetryHelper
210218
// This is to ensure there is no flakiness with the number of times spans are exported in the retry window. Not part of the upstream's implementation
211219
private const int BufferWindow = 20;
212220
private static readonly ILoggerFactory Factory = LoggerFactory.Create(builder => builder.AddProvider(new ConsoleLoggerProvider()));
213-
private static readonly ILogger Logger = Factory.CreateLogger<RetryHelper>();
221+
private static readonly ILogger Logger = Factory.CreateLogger<OtlpAwsSpanExporter>();
214222

215223
#if !NET6_0_OR_GREATER
216224
private static readonly Random Randomizer = new Random();
@@ -235,7 +243,7 @@ internal class RetryHelper
235243
{
236244
if (HasDeadlinePassed(deadline, 0))
237245
{
238-
Logger.LogInformation("Timeout of {Deadline}ms reached, stopping retries", deadline.Millisecond);
246+
Logger.LogDebug("Timeout of {Deadline}ms reached, stopping retries", deadline.Millisecond);
239247
return response;
240248
}
241249

@@ -275,12 +283,12 @@ internal class RetryHelper
275283
delayDuration = TimeSpan.FromMilliseconds(GetRandomNumber(0, currentDelay));
276284
}
277285

278-
Logger.LogInformation("Spans were not exported with status code: {StatusCode}. Checking to see if retryable again after: {DelayMilliseconds} ms", response.StatusCode, delayDuration.Milliseconds);
286+
Logger.LogDebug("Spans were not exported with status code: {StatusCode}. Checking to see if retryable again after: {DelayMilliseconds} ms", response.StatusCode, delayDuration.Milliseconds);
279287

280288
// If delay exceeds deadline. We drop the http requesst completely.
281289
if (HasDeadlinePassed(deadline, delayDuration.Milliseconds))
282290
{
283-
Logger.LogInformation("Timeout will be reached after {Delay}ms delay. Dropping Spans with status code {StatusCode}.", delayDuration.Milliseconds, response.StatusCode);
291+
Logger.LogDebug("Timeout will be reached after {Delay}ms delay. Dropping Spans with status code {StatusCode}.", delayDuration.Milliseconds, response.StatusCode);
284292
return response;
285293
}
286294

@@ -298,14 +306,14 @@ internal class RetryHelper
298306
currentDelay = CalculateNextRetryDelay(currentDelay);
299307
if (!HasDeadlinePassed(deadline, delayDuration.Milliseconds))
300308
{
301-
Logger.LogInformation("{@ExceptionMessage}. Retrying again after {@Delay}ms", exceptionName, delayDuration.Milliseconds);
309+
Logger.LogDebug("{@ExceptionMessage}. Retrying again after {@Delay}ms", exceptionName, delayDuration.Milliseconds);
302310

303311
await Task.Delay(delayDuration);
304312
continue;
305313
}
306314
}
307315

308-
Logger.LogInformation("Timeout will be reached after {Delay}ms delay. Dropping spans with exception: {@ExceptionMessage}", delayDuration.Milliseconds, e);
316+
Logger.LogDebug("Timeout will be reached after {Delay}ms delay. Dropping spans with exception: {@ExceptionMessage}", delayDuration.Milliseconds, e);
309317
throw;
310318
}
311319
}

0 commit comments

Comments
 (0)