// 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 SpinSortValidation() { var vector = new CorrelationVectorV2(); var spinParameters = new SpinParameters { Entropy = SpinEntropy.Two, Interval = SpinCounterInterval.Fine, Periodicity = SpinCounterPeriodicity.Short }; uint lastSpinValue = 0; var wrappedCounter = 0; for (int i = 0; i < 100; i++) { // The cV after a Spin will look like <cvBase>.0.<spinValue>.0, so the spinValue is at index = 2. var spinValue = uint.Parse(CorrelationVector.Spin(vector.Value, spinParameters).Value.Split('.')[2]); // Count the number of times the counter wraps. if (spinValue <= lastSpinValue) { wrappedCounter++; } lastSpinValue = spinValue; // Wait for 10ms. Task.Delay(10).Wait(); } // The counter should wrap at most 1 time. Assert.IsTrue(wrappedCounter <= 1); }
private Task HandleExceptionAsync(HttpContext httpContext, Exception exception) { HttpStatusCode code = exception.ToHttpStatusCode(); RequestContext requestContext = httpContext.Items.Get <RequestContext>(); IWorkContext context = requestContext.Context ?? WorkContext.Empty; if (code == HttpStatusCode.InternalServerError) { AspMvcEventSource.Log.Error(context, exception.Message, exception); } CorrelationVector cv = null; Tag tag = null; if (requestContext != null) { cv = requestContext.Context.Cv; tag = requestContext.Context.Tag; } _webEventLog.HttpError( requestContext.Context, httpContext.Request, httpContext.Response, exception.Message); return(WriteExceptionAsync(httpContext, exception, code, cv, tag)); }
public void ConvertFromVectorBaseToGuidBackToVectorBase() { // CV bases which have four zero least significant bits meaning a conversion to a Guid will retain all // information. // CV Base -> Guid -> CV Base conversions result in: // /////////////////////A -> ffffffff-ffff-ffff-ffff-fffffffffffc -> /////////////////////A // /////////////////////Q -> ffffffff-ffff-ffff-ffff-fffffffffffd -> /////////////////////Q // /////////////////////g -> ffffffff-ffff-ffff-ffff-fffffffffffe -> /////////////////////g // /////////////////////w -> ffffffff-ffff-ffff-ffff-ffffffffffff -> /////////////////////w string[] validGuidVectorBases = new string[] { "/////////////////////A", "/////////////////////Q", "/////////////////////g", "/////////////////////w", }; foreach (string vectorBase in validGuidVectorBases) { var correlationVector = CorrelationVector.Parse($"{vectorBase}.0"); Guid baseAsGuid = correlationVector.GetBaseAsGuid(); var correlationVectorFromGuid = new CorrelationVectorV2(baseAsGuid); Assert.AreEqual(correlationVector.Value, correlationVectorFromGuid.Value, $"Correlation vector base -> guid -> correlation vector base should result in the same vector base for {vectorBase}"); } }
public void GetBaseAsGuidV1Test() { var correlationVector = new CorrelationVector(CorrelationVectorVersion.V1); Assert.ThrowsException <InvalidOperationException>(() => correlationVector.GetBaseAsGuid(), "V1 correlation vector base cannot be converted to a guid"); }
// convert log to dictionary private Dictionary <string, object> GetDictionary(string method, string message, LogLevel logLevel, HttpContext context = null) { Dictionary <string, object> data = new Dictionary <string, object> { { "Date", DateTime.UtcNow }, { "LogName", Name }, { "Method", method }, { "Message", message }, { "LogLevel", logLevel.ToString() }, }; if (context != null && context.Items != null) { data.Add("Path", context.Request.Path + (string.IsNullOrWhiteSpace(context.Request.QueryString.Value) ? string.Empty : context.Request.QueryString.Value)); if (context.Items != null) { CorrelationVector cv = CorrelationVectorExtensions.GetCorrelationVectorFromContext(context); if (cv != null) { data.Add("CVector", cv.Value); } } } return(data); }
// log the request private static void LogRequest(HttpContext context, CorrelationVector cv, double ttfb, double duration) { DateTime dt = DateTime.UtcNow; Dictionary <string, object> log = new Dictionary <string, object> { { "Date", dt }, { "StatusCode", context.Response.StatusCode }, { "TTFB", ttfb }, { "Duration", duration }, { "Verb", context.Request.Method }, { "Path", GetPathAndQuerystring(context.Request) }, { "Host", context.Request.Headers["Host"].ToString() }, { "ClientIP", GetClientIp(context) }, { "UserAgent", context.Request.Headers["User-Agent"].ToString() }, { "CVector", cv.Value }, { "CosmosName", App.CosmosName }, { "CosmosQueryId", "todo" }, { "CosmosRUs", 1.23 }, { "Region", App.Region }, { "Zone", App.Zone }, { "PodType", App.PodType }, }; Interlocked.Increment(ref counter); // write the results to the console Console.WriteLine(JsonSerializer.Serialize(log)); }
public void ParseCorrelationVectorV1Test() { var correlationVector = CorrelationVector.Parse("ifCuqpnwiUimg7Pk.1"); var splitVector = correlationVector.Value.Split('.'); Assert.AreEqual("ifCuqpnwiUimg7Pk", splitVector[0], "Correlation Vector base was not parsed properly"); Assert.AreEqual("1", splitVector[1], "Correlation Vector extension was not parsed properly"); }
public void GetBaseAsGuidV2Test() { var guid = System.Guid.NewGuid(); var correlationVector = new CorrelationVector(guid); Guid baseAsGuid = correlationVector.GetBaseAsGuid(); Assert.AreEqual(guid, baseAsGuid, "Correlation Vector base as a guid should be the same as the initial guid"); }
public ILogger CreateLogger(string categoryName) { if (this.correlationVector is not null) { this.correlationVector = CorrelationVector.Extend(this.correlationVector.ToString()); } return(new CVLogger(categoryName, this)); }
public static RestClient SetCv(this RestClient self, CorrelationVector cv = null) { Verify.IsNotNull(nameof(self), self); cv = cv ?? new CorrelationVector(); self.Properties.TryAdd(() => new CvHeader(cv)); return(self); }
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 void SimpleIncrementCorrelationVectorTest() { var correlationVector = new CorrelationVector(); correlationVector.Increment(); var splitVector = correlationVector.Value.Split('.'); Assert.AreEqual("1", splitVector[1], "Correlation Vector extension should have been incremented by one"); }
public void CreateV2CorrelationVectorTest() { var correlationVector = new CorrelationVector(CorrelationVectorVersion.V2); var splitVector = correlationVector.Value.Split('.'); Assert.AreEqual(2, splitVector.Length, "Correlation Vector should be created with two components separated by a '.'"); Assert.AreEqual(22, splitVector[0].Length, "Correlation Vector base should be 22 character long"); Assert.AreEqual("0", splitVector[1], "Correlation Vector extension should start with zero"); }
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 ValidateCreationTest() { CorrelationVector.ValidateCorrelationVectorDuringCreation = true; var correlationVector = new CorrelationVector(); correlationVector.Increment(); var splitVector = correlationVector.Value.Split('.'); Assert.AreEqual("1", splitVector[1], "Correlation Vector extension should have been incremented by one"); }
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 CreateCorrelationVectorFromGuidTest() { var guid = System.Guid.NewGuid(); var correlationVector = new CorrelationVector(guid); var splitVector = correlationVector.Value.Split('.'); Assert.AreEqual(2, splitVector.Length, "Correlation Vector should be created with two components separated by a '.'"); Assert.AreEqual(22, splitVector[0].Length, "Correlation Vector base should be 22 character long"); Assert.AreEqual("0", splitVector[1], "Correlation Vector extension should start with zero"); }
public void ParseCorrelationVectorV2Test() { var correlationVector = CorrelationVector.Parse("Y58xO9ov0kmpPvkiuzMUVA.3.4.5"); var splitVector = correlationVector.Value.Split('.'); Assert.AreEqual(4, splitVector.Length, "Correlation Vector was not parsed properly"); Assert.AreEqual("Y58xO9ov0kmpPvkiuzMUVA", splitVector[0], "Correlation Vector base was not parsed properly"); Assert.AreEqual("3", splitVector[1], "Correlation Vector extension was not parsed properly"); Assert.AreEqual("4", splitVector[2], "Correlation Vector extension was not parsed properly"); Assert.AreEqual("5", splitVector[3], "Correlation Vector extension was not parsed properly"); }
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"); }); }
/// <summary> /// Get the Correlation Vector base /// </summary> /// <param name="correlationVector">Correlation Vector</param> /// <returns>string</returns> public static string GetBase(this CorrelationVector correlationVector) { if (correlationVector == null) { throw new ArgumentNullException(nameof(correlationVector)); } return(correlationVector.Version switch { CorrelationVectorVersion.V1 => correlationVector.Value.Substring(0, 16), _ => correlationVector.Value.Substring(0, 22), });
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 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); }
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); }
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); }