Fork me on GitHub

.NET6之MiniAPI(二十八):Tracing

  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

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

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

 

 

posted @ 2022-11-30 20:27  桂素伟  阅读(91)  评论(0编辑  收藏  举报