public void TestJson() { var tracer = new DefaultTracer(); { using var span = tracer.NewSpan("test"); Thread.Sleep(100); { using var span2 = tracer.NewSpan("test2"); Thread.Sleep(200); span2.SetError(new Exception("My Error"), null); } } var m = new MyModel { AppId = "Test", Builders = tracer.TakeAll() }; var json = m.ToJson(); var model = json.ToJsonEntity <MyModel>(); Assert.NotNull(model); Assert.NotEmpty(model.Builders); Assert.Equal(2, model.Builders.Length); //Assert.Equal("test", model.Builders[0].Name); //Assert.Equal("test2", model.Builders[1].Name); var test2 = model.Builders.First(e => e.Name == "test2"); Assert.Equal(1, test2.ErrorSamples.Count); }
public async void TestHttpClient() { var tracer = new DefaultTracer(); var http = tracer.CreateHttpClient(); http.DefaultRequestHeaders.UserAgent.ParseAdd("tracer_test v1.3"); await http.GetStringAsync("https://newlifex.com?id=1234"); await Assert.ThrowsAnyAsync <Exception>(async() => { // 故意写错地址,让它抛出异常 await http.GetStringAsync("https://www.newlifexxx.com/notfound?name=stone"); }); // 取出全部跟踪数据 var bs = tracer.TakeAll(); var keys = bs.Select(e => e.Name).ToArray(); Assert.Equal(2, bs.Length); Assert.Contains("https://newlifex.com/", keys); Assert.Contains("https://www.newlifexxx.com/notfound", keys); // 其中一项 var builder = bs.FirstOrDefault(e => e.Name == "https://www.newlifexxx.com/notfound"); Assert.Equal(1, builder.Total); Assert.Equal(1, builder.Errors); var span = builder.ErrorSamples[0]; //Assert.Equal("tracer_test v1.3", span.Tag); Assert.Equal("/notfound?name=stone", span.Tag); }
public void Test1() { var tracer = new DefaultTracer { Log = XTrace.Log }; DAL.GlobalTracer = tracer; // 先删掉原来可能有的 var r0 = Role.FindByName("Stone"); r0?.Delete(); var r = new Role(); r.Name = "Stone"; r.Insert(); var r2 = Role.FindByName("Stone"); XTrace.WriteLine("FindByName: {0}", r2.ToJson()); r.Enable = true; r.Update(); var r3 = Role.Find(Role._.Name == "STONE"); XTrace.WriteLine("Find: {0}", r3.ToJson()); r.Delete(); var n = Role.FindCount(); XTrace.WriteLine("count={0}", n); var bs = tracer.TakeAll(); Assert.NotNull(bs); Assert.True(bs.Length >= 3); var keys = bs.Select(e => e.Name).ToArray(); Assert.Contains("db:Membership:Query:Role", keys); Assert.Contains("db:Membership:SelectCount:Role", keys); Assert.Contains("db:Membership:Insert:Role", keys); Assert.Contains("db:Membership:Update:Role", keys); Assert.Contains("db:Membership:Delete:Role", keys); }
public async void TestHttp3() { //var tracer = NewLife.Log.DefaultTracer.Instance; var tracer = new DefaultTracer(); var observer = new DiagnosticListenerObserver { Tracer = tracer }; observer.Subscribe("HttpHandlerDiagnosticListener", null, null, null); var http = new HttpClient(); await http.GetStringAsync("http://www.newlifex.com?id=1234"); var builders = tracer.TakeAll(); Assert.Single(builders); Assert.Single(builders[0].Samples); Assert.Null(builders[0].ErrorSamples); }
public void TestSamples() { var tracer = new DefaultTracer { MaxSamples = 2, MaxErrors = 11 }; // 正常采样 for (var i = 0; i < 10; i++) { using var span = tracer.NewSpan("test"); } var builder = tracer.BuildSpan("test"); var samples = builder.Samples; Assert.NotNull(samples); Assert.Equal(10, builder.Total); Assert.Equal(tracer.MaxSamples, samples.Count); Assert.NotEqual(samples[0].TraceId, samples[1].TraceId); Assert.NotEqual(samples[0].Id, samples[1].Id); // 异常采样 for (var i = 0; i < 20; i++) { using var span = tracer.NewSpan("test"); span.SetError(new Exception("My Error"), null); } var errors = builder.ErrorSamples; Assert.NotNull(errors); Assert.Equal(10 + 20, builder.Total); Assert.Equal(tracer.MaxErrors, errors.Count); Assert.NotEqual(errors[0].TraceId, errors[1].TraceId); Assert.NotEqual(errors[0].Id, errors[1].Id); var js = tracer.TakeAll().ToJson(); }
public void Test1() { var tracer = new DefaultTracer { MaxSamples = 2, MaxErrors = 11 }; //Assert.Throws<ArgumentNullException>(() => tracer.BuildSpan(null)); // 空名称 { var builder = tracer.BuildSpan(null); Assert.NotNull(builder); Assert.NotNull(builder.Name); Assert.Empty(builder.Name); } // 标准用法 { var builder = tracer.BuildSpan("test"); Assert.NotNull(builder); Assert.Equal(tracer, builder.Tracer); Assert.Equal("test", builder.Name); Assert.True(builder.StartTime > 0); Assert.Equal(0, builder.EndTime); using var span = builder.Start(); span.Tag = "任意业务数据"; Assert.NotEmpty(span.TraceId); Assert.NotEmpty(span.Id); Assert.Null(span.ParentId); Assert.Equal(DateTime.Today, span.StartTime.ToDateTime().ToLocalTime().Date); Assert.Equal(span, DefaultSpan.Current); Thread.Sleep(100); span.Dispose(); Assert.Null(DefaultSpan.Current); var cost = span.EndTime - span.StartTime; Assert.True(cost >= 100); Assert.Null(span.Error); Assert.Equal(1, builder.Total); Assert.Equal(0, builder.Errors); Assert.Equal(cost, builder.Cost); Assert.Equal(cost, builder.MaxCost); Assert.Equal(cost, builder.MinCost); } // 快速用法 { using var span2 = tracer.NewSpan("test2"); Thread.Sleep(200); span2.Dispose(); var cost = span2.EndTime - span2.StartTime; Assert.True(cost >= 200); Assert.Null(span2.Error); var builder2 = tracer.BuildSpan("test2"); Assert.Equal(1, builder2.Total); Assert.Equal(0, builder2.Errors); Assert.Equal(cost, builder2.Cost); Assert.Equal(cost, builder2.MaxCost); Assert.Equal(cost, builder2.MinCost); } var js = tracer.TakeAll().ToJson(); Assert.Contains("\"Tag\":\"任意业务数据\"", js); }