Skip to content

Commit a66fd14

Browse files
authored
Show correct client request id on debug message (#235)
* Show correct client request id on debug message * Support generated cmdlet * Update code * update code * update code * Update code after review
1 parent 23500e4 commit a66fd14

File tree

6 files changed

+74
-10
lines changed

6 files changed

+74
-10
lines changed

src/Common/CmdletInfoHandler.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Microsoft.WindowsAzure.Commands.Common.Extensions;
1516
using System;
1617
using System.Net.Http;
1718
using System.Threading;
@@ -64,11 +65,7 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
6465
}
6566
if (ClientRequestId != null)
6667
{
67-
if (request.Headers.Contains("x-ms-client-request-id"))
68-
{
69-
request.Headers.Remove("x-ms-client-request-id");
70-
}
71-
request.Headers.TryAddWithoutValidation("x-ms-client-request-id", ClientRequestId);
68+
request.AddClientRequestId(ClientRequestId);
7269
}
7370
return base.SendAsync(request, cancellationToken);
7471
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System.Net.Http;
16+
17+
namespace Microsoft.WindowsAzure.Commands.Common.Extensions
18+
{
19+
public static class HttpRequestMessageExtensions
20+
{
21+
private const string HeaderNameClientRequestId = "x-ms-client-request-id";
22+
23+
/// <summary>
24+
/// Add x-ms-client-request-id to headers of Http Request
25+
/// </summary>
26+
/// <param name="request">Current Http Request</param>
27+
/// <param name="clientRequestId">value of client request id</param>
28+
public static void AddClientRequestId(this HttpRequestMessage request, string clientRequestId)
29+
{
30+
if (request.Headers.Contains(HeaderNameClientRequestId))
31+
{
32+
request.Headers.Remove(HeaderNameClientRequestId);
33+
}
34+
request.Headers.TryAddWithoutValidation(HeaderNameClientRequestId, clientRequestId);
35+
}
36+
37+
}
38+
}

src/Common/RecordingTracingInterceptor.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using Hyak.Common;
1616
using Microsoft.WindowsAzure.Commands.Common;
17+
using Microsoft.WindowsAzure.Commands.Common.Extensions;
1718
using Microsoft.WindowsAzure.Commands.Utilities.Common;
1819
using System;
1920
using System.Collections.Concurrent;
@@ -26,16 +27,19 @@ namespace Microsoft.Azure.ServiceManagement.Common.Models
2627
public class RecordingTracingInterceptor : Hyak.Common.ICloudTracingInterceptor
2728
{
2829

29-
public RecordingTracingInterceptor(ConcurrentQueue<string> queue, IList<Regex> matchers = null)
30+
public RecordingTracingInterceptor(ConcurrentQueue<string> queue, IList<Regex> matchers = null, string clientRequestId = null)
3031
{
3132
MessageQueue = queue;
3233
Matchers = matchers;
34+
this.clientRequestId = clientRequestId;
3335
}
3436

3537
public ConcurrentQueue<string> MessageQueue { get; private set; }
3638

3739
private IList<Regex> Matchers { get; set; }
3840

41+
private string clientRequestId;
42+
3943
private void Write(string message, params object[] arguments)
4044
{
4145
if (arguments == null || arguments.Length == 0)
@@ -65,6 +69,12 @@ public void Enter(string invocationId, object instance, string method, IDictiona
6569

6670
public void SendRequest(string invocationId, HttpRequestMessage request)
6771
{
72+
// CmdletInfoHandler sets/updates x-ms-client-request-id during SendAsync() no matter if SDK sets x-ms-client-request-id.
73+
// Update request here to ensure its value consistent with real result.
74+
if (clientRequestId != null)
75+
{
76+
request.AddClientRequestId(clientRequestId);
77+
}
6878
Write(GeneralUtilities.GetLog(request, Matchers));
6979
}
7080

src/Common/Utilities/GeneralUtilities.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,11 @@ public static string GetHttpRequestLog(string method, string requestUri, HttpHea
311311

312312
public static string GetLog(HttpResponseMessage response, IList<Regex> matchers = null)
313313
{
314+
if (response == null)
315+
{
316+
return string.Empty;
317+
}
318+
314319
string body = response.Content == null ? string.Empty
315320
: FormatString(response.Content.ReadAsStringAsync().Result);
316321

@@ -323,6 +328,11 @@ public static string GetLog(HttpResponseMessage response, IList<Regex> matchers
323328

324329
public static string GetLog(HttpRequestMessage request, IList<Regex> matchers = null)
325330
{
331+
if (request == null)
332+
{
333+
return string.Empty;
334+
}
335+
326336
string body = request.Content == null ? string.Empty
327337
: FormatString(request.Content.ReadAsStringAsync().Result);
328338

src/ResourceManager/Version2016_09_01/AzureRMCmdlet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ protected override void SetupDebuggingTraces()
324324
ServiceClientTracing.IsEnabled = true;
325325
base.SetupDebuggingTraces();
326326
_serviceClientTracingInterceptor = _serviceClientTracingInterceptor
327-
?? new ServiceClientTracingInterceptor(DebugMessages, _matchers);
327+
?? new ServiceClientTracingInterceptor(DebugMessages, _matchers, _clientRequestId);
328328
ServiceClientTracing.AddTracingInterceptor(_serviceClientTracingInterceptor);
329329
}
330330

src/ResourceManager/Version2016_09_01/ServiceClientTracingInterceptor.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using Microsoft.Rest;
1616
using Microsoft.WindowsAzure.Commands.Common;
17+
using Microsoft.WindowsAzure.Commands.Common.Extensions;
1718
using Microsoft.WindowsAzure.Commands.Utilities.Common;
1819
using System;
1920
using System.Collections.Concurrent;
@@ -24,16 +25,19 @@ namespace Microsoft.Azure.Commands.ResourceManager.Common
2425
{
2526
public class ServiceClientTracingInterceptor : IServiceClientTracingInterceptor
2627
{
27-
public ServiceClientTracingInterceptor(ConcurrentQueue<string> queue, IList<Regex> matchers = null)
28+
public ServiceClientTracingInterceptor(ConcurrentQueue<string> queue, IList<Regex> matchers = null, string clientRequestId = null)
2829
{
2930
MessageQueue = queue;
3031
Matchers = matchers;
32+
this.clientRequestId = clientRequestId;
3133
}
3234

3335
public ConcurrentQueue<string> MessageQueue { get; private set; }
3436

3537
private IList<Regex> Matchers { get; set; }
3638

39+
private string clientRequestId;
40+
3741
public void Configuration(string source, string name, string value)
3842
{
3943
// Ignore
@@ -62,8 +66,13 @@ public void ReceiveResponse(string invocationId, System.Net.Http.HttpResponseMes
6266

6367
public void SendRequest(string invocationId, System.Net.Http.HttpRequestMessage request)
6468
{
65-
string requestAsString = request == null ? string.Empty : GeneralUtilities.GetLog(request, Matchers);
66-
MessageQueue.CheckAndEnqueue(requestAsString);
69+
// CmdletInfoHandler sets/updates x-ms-client-request-id during SendAsync() no matter if SDK sets x-ms-client-request-id.
70+
// Update request here to ensure its value consistent with real result.
71+
if (request != null && clientRequestId != null)
72+
{
73+
request.AddClientRequestId(clientRequestId);
74+
}
75+
MessageQueue.CheckAndEnqueue(GeneralUtilities.GetLog(request, Matchers));
6776
}
6877

6978
public void TraceError(string invocationId, Exception exception)

0 commit comments

Comments
 (0)