Skip to content

Commit 4c12c82

Browse files
Adding IKey and EndpointAddress in the ApplicationInsights Transmission Status log. (#9705)
Adding IKey and EndpointAddress in the log.
1 parent 6ae1059 commit 4c12c82

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/WebJobs.Script/Config/TransmissionStatusHandler.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ internal static string FormattedLog(object sender, TransmissionStatusEventArgs a
4444
Count = n.Count()
4545
});
4646

47+
// Log all the unique ikeys in the transmission, avoid storing the complete key.
48+
IEnumerable<string> iKeys = transmission?.TelemetryItems.GroupBy(n => n.Context.InstrumentationKey)
49+
.Select(n => n.Key.Length > 24 ? $"{n.Key[..24]}************" : n.Key);
50+
4751
IngestionServiceResponse response = null;
4852
if (!string.IsNullOrWhiteSpace(args?.Response?.Content))
4953
{
@@ -70,7 +74,9 @@ internal static string FormattedLog(object sender, TransmissionStatusEventArgs a
7074
ErrorMessage = topErrorMessage,
7175
ErrorCode = topStatusCode,
7276
ResponseTimeInMs = args?.ResponseDurationInMs,
73-
RetryAfterHeader = args?.Response?.RetryAfterHeader
77+
RetryAfterHeader = args?.Response?.RetryAfterHeader,
78+
EndpointAddress = transmission?.EndpointAddress.OriginalString,
79+
IKeys = iKeys
7480
};
7581
return JsonSerializer.Serialize(log, LogMessageContext.Default.LogMessage);
7682
}
@@ -136,7 +142,11 @@ internal class LogMessage
136142

137143
public string RetryAfterHeader { get; set; }
138144

145+
public string EndpointAddress { get; set; }
146+
139147
public IEnumerable<TelemetryItem> Items { get; set; }
148+
149+
public IEnumerable<string> IKeys { get; set; }
140150
}
141151

142152
internal class TelemetryItem

test/WebJobs.Script.Tests/Configuration/TransmissionStatusHandlerTest.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,49 @@ public void FormattedLog_Success()
100100
Assert.Equal(3, log["items"].Count());
101101
Assert.Null(log.Value<string>("errorMessage"));
102102
}
103+
104+
[Fact]
105+
public void FormattedLog_InstrumentationKeys()
106+
{
107+
TransmissionStatusHandler handler = new();
108+
var request1 = new RequestTelemetry();
109+
request1.Context.InstrumentationKey = "AAAAA-AAAAAAAAAA-AAAAAAAA-AAAAAAAA";
110+
111+
var request2 = new RequestTelemetry();
112+
request2.Context.InstrumentationKey = "CCCCC-CCCCCCCCCCCCCCCCC-";
113+
114+
var trace1 = new TraceTelemetry();
115+
trace1.Context.InstrumentationKey = "BBBBB-BBBBBBBBB-BBBBBBBBBBB-BBBBBBB";
116+
117+
var trace2 = new TraceTelemetry();
118+
trace2.Context.InstrumentationKey = "BBBBB-BBBBBBBBB-BBBBBBBBBBB-BBBBBBB";
119+
120+
Transmission transmission = new(new("https://test"), new List<ITelemetry>() { request1, request2, trace1, trace2 }, new TimeSpan(500));
121+
IngestionServiceResponse backendResponse = new()
122+
{
123+
ItemsAccepted = 100,
124+
ItemsReceived = 100
125+
};
126+
HttpWebResponseWrapper response = new()
127+
{
128+
StatusCode = 200,
129+
StatusDescription = "ok",
130+
Content = JsonConvert.SerializeObject(backendResponse, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() })
131+
};
132+
133+
TransmissionStatusEventArgs args = new(response, 100);
134+
JObject log = JsonConvert.DeserializeObject<JObject>(TransmissionStatusHandler.FormattedLog(transmission, args));
135+
136+
Assert.Equal(200, log.Value<int>("statusCode"));
137+
Assert.Equal("https://test", log.Value<string>("endpointAddress"));
138+
Assert.Equal("ok", log.Value<string>("statusDescription"));
139+
Assert.Equal(100, log.Value<int>("responseTimeInMs"));
140+
Assert.True(log.ContainsKey("id"));
141+
Assert.Equal(2, log["items"].Count());
142+
Assert.Equal(3, log["iKeys"].Count());
143+
Assert.Null(log.Value<string>("errorMessage"));
144+
string keys = log["iKeys"].ToString();
145+
Assert.True(keys.Contains("AAAAA-AAAAAAAAAA-AAAAAAA************") && keys.Contains("BBBBB-BBBBBBBBB-BBBBBBBB************") && keys.Contains("CCCCC-CCCCCCCCCCCCCCCCC-"));
146+
}
103147
}
104148
}

0 commit comments

Comments
 (0)