OpenTelemetry介绍
OpenTelemetry是一组标准和工具的集合,旨在管理观测类数据,如 trace、metrics、logs 等。
通过标准化不同的应用程序和框架如何收集和发出可观察性遥测数据,OpenTelemetry旨在解决这些环境带来的一些挑战。
OpenTelemetry包括:
可观察性遥测(分布式跟踪、指标等)的供应商中立规范
实现用于检测的公共接口的API
应用程序使用SDK为插件作者配置工具和接口以编写导出器
使你能够将数据发送到你选择的遥测后端的导出器
随着OpenTelemetry对可观察性遥测集合的标准化,你可以选择遥测后端而无需更改检测代码。你可以自由选择可为你提供最有效数据分析的平台。
在本文中,我们选择的是Zipkin。
Zipkin介绍
Zipkin是一个分布式追踪系统。它有助于收集对服务架构中的延迟问题进行故障排除所需的计时数据。功能包括收集和查找这些数据。
下面,我们就来演示,如何使用OpenTelemetry搭配Zipkin来追踪ASP.NET Core服务之间的WebAPI和gRPC请求。
Demo
创建3个应用程序,WebApplication1使用Web API调用WebApplication2,WebApplication2使用gRPC调用GrpcService1。
WebApplication1
引用Nuget包,包括预发行版:
OpenTelemetry.Extensions.Hosting
OpenTelemetry.Instrumentation.AspNetCore
OpenTelemetry.Exporter.Zipkin
新增配置,Endpoint对应Zipkin部署地址:
"Zipkin": {"ServiceName": "WebApplication1","Endpoint": "http://192.168.1.11:9411/api/v2/spans"
}
修改Startup.cs:
public void ConfigureServices(IServiceCollection services)
{...services.AddOpenTelemetryTracing((builder) => builder.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(this.Configuration.GetValue<string>("Zipkin:ServiceName"))).AddAspNetCoreInstrumentation().AddZipkinExporter(zipkinOptions =>{zipkinOptions.Endpoint = new Uri(Configuration.GetValue<string>("Zipkin:Endpoint"));}));
}
修改WeatherForecastController.cs,访问WebApplication2:
[HttpGet]
public async Task<string> Get()
{var response = await _httpClientFactory.CreateClient().GetStringAsync("http://localhost:5200/WeatherForecast");return response;
}
WebApplication2
引用Nuget包,包括预发行版:
OpenTelemetry.Extensions.Hosting
OpenTelemetry.Instrumentation.AspNetCore
OpenTelemetry.Exporter.Zipkin
Grpc.Net.Client
OpenTelemetry.Instrumentation.GrpcNetClient
新增配置,Endpoint对应Zipkin部署地址:
"Zipkin": {"ServiceName": "WebApplication2","Endpoint": "http://192.168.1.11:9411/api/v2/spans"
}
修改Startup.cs:
public void ConfigureServices(IServiceCollection services)
{...services.AddOpenTelemetryTracing((builder) => builder.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(this.Configuration.GetValue<string>("Zipkin:ServiceName"))).AddGrpcClientInstrumentation().AddAspNetCoreInstrumentation().AddZipkinExporter(zipkinOptions =>{zipkinOptions.Endpoint = new Uri(Configuration.GetValue<string>("Zipkin:Endpoint"));}));
}
修改WeatherForecastController.cs,访问GrpcService1:
[HttpGet]
public async Task<string> Get()
{var channel = GrpcChannel.ForAddress("http://localhost:5300");var client = new Greeter.GreeterClient(channel);var response = await client.SayHelloAsync(new HelloRequest { Name = "World" });return response.Message;
}
GrpcService1
引用Nuget包,包括预发行版:
OpenTelemetry.Extensions.Hosting
OpenTelemetry.Instrumentation.AspNetCore
OpenTelemetry.Exporter.Zipkin
新增配置,Endpoint对应Zipkin部署地址:
"Zipkin": {"ServiceName": "GrpcService1","Endpoint": "http://192.168.1.11:9411/api/v2/spans"
}
修改Startup.cs:
private IConfiguration Configuration { get; }public Startup(IConfiguration configuration)
{Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{...services.AddOpenTelemetryTracing((builder) => builder.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(this.Configuration.GetValue<string>("Zipkin:ServiceName"))).AddAspNetCoreInstrumentation().AddZipkinExporter(zipkinOptions =>{zipkinOptions.Endpoint = new Uri(Configuration.GetValue<string>("Zipkin:Endpoint"));}));
}
运行效果
启动3个应用程序:
WebApplication1 http://localhost/5100
WebApplication2 http://localhost/5200
GrpcService1 http://localhost/5300
访问WebApplication1后,即可在Zipkin上查看调用链:
结论
使用OpenTelemetry搭配Zipkin,可以轻松实现监控WebAPI/gRPC调用链。