当前位置 : 主页 > 网络编程 > net编程 >

.NET6之MiniAPI(二十八):Tracing

来源:互联网 收集:自由互联 发布时间:2023-09-03
Log,Metrics,Tracing是为服务正常运行保驾护航的三件套,前面有介绍过Log和Metrics,本篇说明说一下Tracing。Tracing的思路和Metrics一样,通过三方服务,来达成效果,这里选用的是Jaeger,通

  Log,Metrics,Tracing是为服务正常运行保驾护航的三件套,前面有介绍过Log和Metrics,本篇说明说一下Tracing。Tracing的思路和Metrics一样,通过三方服务,来达成效果,这里选用的是Jaeger,通过在MiniAPI项目中引入Jaeger和OpenTracing的库,来向Jaeger推送跟踪数据,通过访问http://localhost:16686/search,来查看跟踪结果。

   Jaeger下载地址:

​​  https://www.jaegertracing.io/download/​​

  接来了定议两个项目,Order项目和Stock项目,Order通过httpclient调用Stock来查看商品库存,这样,两个项目就开成了一个调用链路。

Order项目

using Jaeger;
using OpenTracing.Propagation;
using OpenTracing;
using OpenTracing.Util;
using Jaeger.Samplers;
using Jaeger.Senders.Thrift;
using Jaeger.Senders;
using Jaeger.Reporters;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpClient();
//注入OpenTracing服务
builder.Services.AddOpenTracing();
builder.Services.AddSingleton<ITracer>(serviceProvider =>
{
var serviceName = serviceProvider.GetRequiredService<IWebHostEnvironment>().ApplicationName;
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
//配置跟踪
Jaeger.Configuration.SenderConfiguration.DefaultSenderResolver = new SenderResolver(loggerFactory)
.RegisterSenderFactory<ThriftSenderFactory>();
var tracer = new Tracer.Builder(serviceName)
//添加日志框架
.WithLoggerFactory(loggerFactory)
//添加数据采集器
.WithSampler(new ConstSampler(true))
.Build();
//注册全局跟踪器
GlobalTracer.Register(tracer);
return tracer;
});

var app = builder.Build();

app.MapGet("/order", async (ILogger<Program> logger, IHttpClientFactory clientFactory, ITracer tracer) =>
{
using (var scope = tracer.BuildSpan("订单系统").StartActive(true))
{
logger.LogInformation("订单查询库存");
var client = clientFactory.CreateClient();
var content = await client.GetStringAsync("http://localhost:5160/stock/D0001");
return $"stock调用结果:{content}";
}
});

app.Run();

Stock项目

using Jaeger;
using Jaeger.Reporters;
using Jaeger.Samplers;
using Jaeger.Senders;
using Jaeger.Senders.Thrift;
using OpenTracing;
using OpenTracing.Util;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenTracing();
builder.Services.AddSingleton<ITracer>(serviceProvider =>
{
var serviceName = serviceProvider.GetRequiredService<IWebHostEnvironment>().ApplicationName;
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
Jaeger.Configuration.SenderConfiguration.DefaultSenderResolver = new SenderResolver(loggerFactory)
.RegisterSenderFactory<ThriftSenderFactory>();
var tracer = new Tracer.Builder(serviceName)
.WithLoggerFactory(loggerFactory)
.WithSampler(new ConstSampler(true))
.Build();
GlobalTracer.Register(tracer);
return tracer;
});
var app = builder.Build();

app.MapGet("/stock/{no}", (string no, ILogger<Program> logger, ITracer tracer) =>
{
using (var scope = tracer.BuildSpan("库存系统").StartActive(true))
{
logger.LogInformation("按{0}查询库存", no);
return new Product { No = no, Quantity = 1324, Name = "Surface Go 3" };
}
});
app.Run();

public class Product
{
public string? No { get; set; }
public string? Name { get; set; }
public int Quantity { get; set; }
}

  用命令行运行下载的jaeger-all-in-one.exe,在浏览器或postman访问Order项目的/order地址,再进入Jaeger,查看结果:http://localhost:16686/search

.NET6之MiniAPI(二十八):Tracing_Tracing

 

.NET6之MiniAPI(二十八):Tracing_Tracing_02

.NET6之MiniAPI(二十八):Tracing_Tracing_03

.NET6之MiniAPI(二十八):Tracing_Tracing_04

 通过上面的UI,可以很方便地查看请求链条上的调用,当调用关系很复杂时,特别有用。

  想要更快更方便的了解相关知识,可以关注微信公众号

.NET6之MiniAPI(二十八):Tracing_Tracing_05

 

上一篇:.NET6之MiniAPI(二十九):UnitTest
下一篇:没有了
网友评论