// extend correlation vector private static CorrelationVector ExtendCVector(HttpContext context) { CorrelationVector cv; if (context.Request.Headers.ContainsKey(CVHeader)) { try { // extend the correlation vector cv = CorrelationVector.Extend(context.Request.Headers[CVHeader].ToString()); } catch { // create a new correlation vector cv = new CorrelationVector(); } } else { // create a new correlation vector cv = new CorrelationVector(); } return(cv); }
public void ThrowWithTooBigExtensionCorrelationVectorValue() { Assert.ThrowsException <ArgumentException>(() => { CorrelationVector.ValidateCorrelationVectorDuringCreation = true; /* Bigger than INT32 */ var vector = CorrelationVector.Extend("tul4NUsfs9Cl7mOf.11111111111111111111111111111"); }); }
public void ThrowWithTooBigCorrelationVectorValueV2() { Assert.ThrowsException <ArgumentException>(() => { CorrelationVector.ValidateCorrelationVectorDuringCreation = true; /* Bigger than 127 chars */ var vector = CorrelationVector.Extend("KZY+dsX2jEaZesgCPjJ2Ng.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647"); }); }
public void ThrowWithTooBigCorrelationVectorValue() { Assert.ThrowsException <ArgumentException>(() => { CorrelationVector.ValidateCorrelationVectorDuringCreation = true; /* Bigger than 63 chars */ var vector = CorrelationVector.Extend("tul4NUsfs9Cl7mOf.2147483647.2147483647.2147483647.2147483647.2147483647"); }); }
public ILogger CreateLogger(string categoryName) { if (this.correlationVector is not null) { this.correlationVector = CorrelationVector.Extend(this.correlationVector.ToString()); } return(new CVLogger(categoryName, this)); }
public void ExtendPastMaxWithTerminationSignV2() { CorrelationVector.ValidateCorrelationVectorDuringCreation = false; const string baseVector = "KZY+dsX2jEaZesgCPjJ2Ng.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.2141"; // we hit 127 chars limit, will append "!" to vector var vector = CorrelationVector.Extend(baseVector); Assert.AreEqual(string.Concat(baseVector, CorrelationVectorV2.TerminationSign), vector.Value); }
public void ExtendPastMaxWithTerminationSign() { CorrelationVector.ValidateCorrelationVectorDuringCreation = false; const string baseVector = "tul4NUsfs9Cl7mOf.2147483647.2147483647.2147483647.214748364.23"; // we hit 63 chars limit, will append "!" to vector var vector = CorrelationVector.Extend(baseVector); Assert.AreEqual(string.Concat(baseVector, CorrelationVectorV1.TerminationSign), vector.Value); }
public void ThrowWithTooManyCharsCorrelationVectorValue() { CorrelationVector.ValidateCorrelationVectorDuringCreation = false; // This shouldn't throw since we skip validation var vector = CorrelationVector.Extend("tul4NUsfs9Cl7mOfN/dupsl.1"); Assert.ThrowsException <ArgumentException>(() => { CorrelationVector.ValidateCorrelationVectorDuringCreation = true; vector = CorrelationVector.Extend("tul4NUsfs9Cl7mOfN/dupsl.1"); }); }
public void ExtendNullCorrelationVector() { CorrelationVector.ValidateCorrelationVectorDuringCreation = false; // This shouldn't throw since we skip validation var vector = CorrelationVector.Extend(null); Assert.IsTrue(vector.ToString() == ".0"); Assert.ThrowsException <ArgumentException>(() => { CorrelationVector.ValidateCorrelationVectorDuringCreation = true; vector = CorrelationVector.Extend(null); Assert.IsTrue(vector.ToString() == ".0"); } ); }
public void SimpleExtendCorrelationVectorTest() { CorrelationVector correlationVector = new CorrelationVectorV1(); var splitVector = correlationVector.Value.Split('.'); var vectorBase = splitVector[0]; var extension = splitVector[1]; correlationVector = CorrelationVector.Extend(correlationVector.Value); splitVector = correlationVector.Value.Split('.'); Assert.AreEqual(3, splitVector.Length, "Correlation Vector should contain 3 components separated by a '.' after extension"); Assert.AreEqual(vectorBase, splitVector[0], "Correlation Vector base should contain the same base after extension"); Assert.AreEqual(extension, splitVector[1], "Correlation Vector should preserve original "); Assert.AreEqual("0", splitVector[2], "Correlation Vector new extension should start with zero"); }
public void ImmutableWithTerminationSign() { CorrelationVector.ValidateCorrelationVectorDuringCreation = false; const string cv = "tul4NUsfs9Cl7mOf.2147483647.2147483647.2147483647.21474836479.0!"; var vector = CorrelationVector.Extend(cv); //extend do nothing Assert.AreEqual(cv, vector.Value); Assert.ThrowsException <InvalidOperationException>(() => CorrelationVector.Spin(cv)); vector.Increment(); // Increment does nothing since it has termination sign Assert.AreEqual(cv, vector.Value); }
public void IncrementPastMaxWithNoErrorsV2() { CorrelationVector.ValidateCorrelationVectorDuringCreation = false; var vector = CorrelationVector.Extend("KZY+dsX2jEaZesgCPjJ2Ng.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.214"); vector.Increment(); Assert.AreEqual("KZY+dsX2jEaZesgCPjJ2Ng.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.214.1", vector.Value); for (int i = 0; i < 20; i++) { vector.Increment(); } // We hit 127 chars so we silently stopped counting Assert.AreEqual("KZY+dsX2jEaZesgCPjJ2Ng.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.214.9!", vector.Value); }
public void IncrementPastMaxWithNoErrors() { CorrelationVector.ValidateCorrelationVectorDuringCreation = false; var vector = CorrelationVector.Extend("tul4NUsfs9Cl7mOf.2147483647.2147483647.2147483647.21474836479"); vector.Increment(); Assert.AreEqual("tul4NUsfs9Cl7mOf.2147483647.2147483647.2147483647.21474836479.1", vector.Value); for (int i = 0; i < 20; i++) { vector.Increment(); } // We hit 63 chars so we silently stopped counting Assert.AreEqual("tul4NUsfs9Cl7mOf.2147483647.2147483647.2147483647.21474836479.9!", vector.Value); }
private void IssueRequest(string correlationVector) { // Override the AsyncLocal Instance that flowed from the parent thread // CorrelationVector.Current = CorrelationVector.Extend(correlationVector); var request = new HttpRequestMessage( HttpMethod.Get, "https://consumerstorefd.corp.microsoft.com/health/keepalive"); using (HttpClient client = new HttpClient(new DependencyClientHandler("ConsumerStore", "WebService"))) { var response = client.SendAsync(request, "Async_Issue_Request", "KeepAlive", "v1.0").Result; s_outgoingCorrelationVectors.Add(TryGetHeaderValue(response.RequestMessage.Headers, "MS-CV")); } }
public void ImmutableWithTerminationSignV2() { CorrelationVector.ValidateCorrelationVectorDuringCreation = false; const string cv = "KZY+dsX2jEaZesgCPjJ2Ng.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.2147483647.214.0!"; var vector = CorrelationVector.Extend(cv); //extend do nothing Assert.AreEqual(cv, vector.Value); vector = CorrelationVector.Spin(cv); //Spin do nothing Assert.AreEqual(cv, vector.Value); vector.Increment(); // Increment do nothing since it has termination sign Assert.AreEqual(cv, vector.Value); }
public void OnNext(KeyValuePair <string, object> value) { if (value.Key == "Microsoft.AspNet.HttpReqIn.Start") { // This happens on incoming requests to ASP.NET. Grab the MS-CV header and store // it on CorrelationVector.Current and the HttpContext.Current. // string msCvHeader = HttpContext.Current?.Request?.Headers?["MS-CV"]?.ToString(); CorrelationVector correlationVector; if (!string.IsNullOrWhiteSpace(msCvHeader)) { correlationVector = CorrelationVector.Extend(msCvHeader); } else { correlationVector = new CorrelationVector(); } CorrelationVector.Current = correlationVector; HttpContext.Current.Items.Add("CorrelationVector.Current", correlationVector); } else if (value.Key == "Microsoft.AspNet.HttpReqIn.ActivityLost.Stop") { // This is a work-around for a known issue due to hop between managed and // unmanaged code. Per Liudmila Molkova who pointed me to the code that emits // the event... // https://github.com/aspnet/Microsoft.AspNet.TelemetryCorrelation/blob/master/src/Microsoft.AspNet.TelemetryCorrelation/TelemetryCorrelationHttpModule.cs // CorrelationVector.Current = HttpContext.Current.Items["CorrelationVector.Current"] as CorrelationVector; } else if (value.Key == "System.Net.Http.Desktop.HttpRequestOut.Start") { // This happens on outgoing Http requests via Http Client. // if (!(value.Value.GetType().GetProperty("Request")?.GetValue(value.Value, null) is HttpWebRequest requestMessage)) { return; } // Record the current date so latency can be calculated // requestMessage.Date = DateTime.Now; if (!string.IsNullOrWhiteSpace(requestMessage.Headers["MS-CV"])) { // Request already has a CV. This is expected if an HttpClient SendAsync extension method // is used to pass along request dependency info that should be used for logging. // return; } if (CorrelationVector.Current == null) { // Is this the right thing if there is no CV? Just create one? // CorrelationVector.Current = new CorrelationVector(); } requestMessage.Headers.Add("MS-CV", CorrelationVector.Current.Increment()); } else if (value.Key == "System.Net.Http.Desktop.HttpRequestOut.Ex.Stop") { // This happens at the end of some outgoing Http requests, like when the // response is await'ed and the response has no body. // if (!(value.Value.GetType().GetProperty("Request")?.GetValue(value.Value, null) is HttpWebRequest requestMessage) || !(value.Value.GetType().GetProperty("StatusCode")?.GetValue(value.Value, null) is HttpStatusCode responseStatusCode) || !(value.Value.GetType().GetProperty("Headers")?.GetValue(value.Value, null) is WebHeaderCollection responseHeaders)) { return; } LogHttpOutComplete(requestMessage, responseStatusCode, responseHeaders["Content-Length"]); } else if (value.Key == "System.Net.Http.Desktop.HttpRequestOut.Stop") { // This happens for the remainder of outgoing Http requests. // if (!(value.Value.GetType().GetProperty("Request")?.GetValue(value.Value, null) is HttpWebRequest requestMessage) || !(value.Value.GetType().GetProperty("Response")?.GetValue(value.Value, null) is HttpWebResponse responseMessage)) { return; } LogHttpOutComplete(requestMessage, responseMessage.StatusCode, responseMessage.ContentLength.ToString()); } }