| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Threading; |
| | | 6 | | |
| | | 7 | | namespace Azure.Core.Pipeline |
| | | 8 | | { |
| | | 9 | | internal class ReadClientRequestIdPolicy : HttpPipelineSynchronousPolicy |
| | | 10 | | { |
| | 2 | 11 | | private static readonly AsyncLocal<ClientRequestIdScope?> CurrentRequestIdScope = new AsyncLocal<ClientRequestId |
| | | 12 | | |
| | 2 | 13 | | protected ReadClientRequestIdPolicy() |
| | | 14 | | { |
| | 2 | 15 | | } |
| | | 16 | | |
| | 66 | 17 | | public static ReadClientRequestIdPolicy Shared { get; } = new ReadClientRequestIdPolicy(); |
| | | 18 | | |
| | | 19 | | public override void OnSendingRequest(HttpMessage message) |
| | | 20 | | { |
| | 1644 | 21 | | if (message.Request.Headers.TryGetValue(ClientRequestIdPolicy.ClientRequestIdHeader, out string? value)) |
| | | 22 | | { |
| | 6 | 23 | | message.Request.ClientRequestId = value; |
| | | 24 | | } |
| | 1638 | 25 | | else if (CurrentRequestIdScope.Value?.ClientRequestId != null) |
| | | 26 | | { |
| | 4 | 27 | | message.Request.ClientRequestId = CurrentRequestIdScope.Value.ClientRequestId; |
| | | 28 | | } |
| | 1638 | 29 | | } |
| | | 30 | | |
| | | 31 | | internal static IDisposable StartScope(string? clientRequestId) |
| | | 32 | | { |
| | 10 | 33 | | CurrentRequestIdScope.Value = new ClientRequestIdScope(clientRequestId, CurrentRequestIdScope.Value); |
| | | 34 | | |
| | 10 | 35 | | return CurrentRequestIdScope.Value; |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | private class ClientRequestIdScope: IDisposable |
| | | 39 | | { |
| | | 40 | | private readonly ClientRequestIdScope? _parent; |
| | | 41 | | |
| | 10 | 42 | | internal ClientRequestIdScope(string? clientRequestId, ClientRequestIdScope? parent) |
| | | 43 | | { |
| | 10 | 44 | | ClientRequestId = clientRequestId; |
| | 10 | 45 | | _parent = parent; |
| | 10 | 46 | | } |
| | | 47 | | |
| | 10 | 48 | | public string? ClientRequestId { get; } |
| | | 49 | | |
| | | 50 | | public void Dispose() |
| | | 51 | | { |
| | 10 | 52 | | CurrentRequestIdScope.Value = _parent; |
| | 10 | 53 | | } |
| | | 54 | | } |
| | | 55 | | } |
| | | 56 | | } |