前提条件
完成vpce接入。
背景信息
Jaeger是Uber推出的一款开源分布式追踪系统,兼容OpenTracing API,已在Uber大规模使用,且已加入CNCF开源组织。其主要功能是聚合来自各个异构系统的实时监控数据。
接入步骤
1、安装依赖包
dotnet add package Jaeger
dotnet add package OpenTracing
dotnet add package OpenTracing.Contrib.NetCore
2、查看接入点信息
应用列表的接入指引会根据您所在资源池提供“通过 HTTP 上报数据”的ENDPOINT(天翼云vpc网络接入点)、鉴权TOKEN信息。
3、初始化SDK并设置exporters
新建文件JaegerServiceCollectionExtensions.cs,代码如下
using System;
using System.Reflection;
using Jaeger;
using Jaeger.Reporters;
using Jaeger.Samplers;
using Jaeger.Senders.Thrift;
using Microsoft.Extensions.Logging;
using OpenTracing;
using OpenTracing.Contrib.NetCore.Configuration;
using OpenTracing.Util;
namespace Microsoft.Extensions.DependencyInjection
{
public static class JaegerServiceCollectionExtensions
{
private static readonly Uri _jaegerUri = new Uri("endpoint"); //替换为上报数据的Endpoint
public static IServiceCollection AddJaeger(this IServiceCollection services)
{
if (services == null)
throw new ArgumentNullException(nameof(services));
services.AddSingleton<ITracer>(serviceProvider =>
{
var serviceName = ""; //修改为自定义的服务名
//ILoggerFactory loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
var loggerFactory = LoggerFactory.Create(builder =>
{
// 添加控制台日志提供程序
builder.AddConsole();
// (可选)其他配置,例如设置日志级别
// builder.SetMinimumLevel(LogLevel.Debug);
});
ISampler sampler = new ConstSampler(sample: true);
//将token替换为自己的鉴权token
IReporter reporter = new RemoteReporter.Builder()
.WithSender(new HttpSender.Builder(_jaegerUri.ToString()).WithAuth("token").Build())
.Build();
//IReporter reporter = new LoggingReporter(loggerFactory);
ITracer tracer = new Tracer.Builder(serviceName)
.WithLoggerFactory(loggerFactory)
.WithSampler(sampler)
.WithReporter(reporter)
.Build();
GlobalTracer.Register(tracer);
return tracer;
});
// Prevent endless loops when OpenTracing is tracking HTTP requests to Jaeger.
services.Configure<HttpHandlerDiagnosticOptions>(options =>
{
options.IgnorePatterns.Add(request => _jaegerUri.IsBaseOf(request.RequestUri));
});
return services;
}
}
}
4、开启链路跟踪
using Jaeger;
using Jaeger.Reporters;
using Jaeger.Samplers;
using Microsoft.Extensions.Logging;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddJaeger(); //ITracer对象的初始化和注册
builder.Services.AddOpenTracing(); //开启Http链路
builder.Services.AddControllers();
var app = builder.Build();
app.MapControllers();
app.Run();