控制台程序输出:
info: Jaeger.Reporters.LoggingReporter[0]Spanreported: c587bd888e8f1c19:2e3273568e6e373b:c587bd888e8f1c19:1-format-stringinfo: ConsoleApp1.Hello[0]Hello, Thistrace! info: Jaeger.Reporters.LoggingReporter[0]Spanreported: c587bd888e8f1c19:f0416a0130d58924:c587bd888e8f1c19:1-print-helloinfo: Jaeger.Reporters.LoggingReporter[0]Spanreported: c587bd888e8f1c19:c587bd888e8f1c19:0000000000000000:1-say-hello接着 , 我们可以将 Formating 改成:
privatestringFormatString( stringhelloTo ) {using( varscope = _tracer.BuildSpan( "format-string").StartActive( true)) {usingWebClient webClient = newWebClient; varurl = $"http://localhost:8081/api/format/ {helloTo}" ; varhelloString = webClient.DownloadString(url); varspan = scope.Span .SetTag(Tags.SpanKind, Tags.SpanKindClient).SetTag(Tags.HttpMethod, "GET") .SetTag(Tags.HttpUrl, url);vardictionary = newDictionary< string, string>; _tracer.Inject(span.Context, BuiltinFormats.HttpHeaders, newTextMapInjectAdapter(dictionary)); foreach( varentry indictionary) webClient.Headers.Add(entry.Key, entry.Value);returnhelloString; }}
SetTag 可以设置标签 , 我们为本次请求到 Web 的 Span , 设置一个标签 , 并且存储请求的 URL 。
varspan = scope.Span .SetTag(Tags.SpanKind, Tags.SpanKindClient).SetTag(Tags.HttpMethod, "GET") .SetTag(Tags.HttpUrl, url);通过 Inject 将上下文信息注入 。
_ tracer.Inject( span.Context, BuiltinFormats.HttpHeaders, newTextMapInjectAdapter( dictionary)); 这些配置规范 , 可以到 https://github.com/opentracing/specification/blob/master/semantic_conventions.md 了解 。
在ASP.NET Core 中跟踪:
在上面 , 我们实现了 Client 在不同进程的追踪 , 但是还没有实现在 Server 中跟踪 , 我们可以修改 Startup.cs 中的代码 , 将以下代码替换进去:
using Jaeger;using Jaeger.Samplers;using Microsoft.AspNetCore.Builder;using Microsoft.Extensions.Configuration;using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Logging;using OpenTracing.Util;using System;namespaceWebApplication1 {publicclassStartup {privatestaticreadonly ILoggerFactory loggerFactory = LoggerFactory.Create( builder=> builder.AddConsole); privatestaticreadonly Lazy<Tracer> Tracer = newLazy<Tracer> ( ( ) => {returnInitTracer( "webService", loggerFactory); }); privatestaticTracerInitTracer( stringserviceName, ILoggerFactory loggerFactory ) {varsamplerConfiguration= newConfiguration. SamplerConfiguration( loggerFactory) . WithType( ConstSampler.Type) . WithParam( 1);
varreporterConfiguration= newConfiguration. ReporterConfiguration( loggerFactory) . WithLogSpans( true);
return( Tracer) newConfiguration( serviceName, loggerFactory) . WithSampler( samplerConfiguration) . WithReporter( reporterConfiguration) . GetTracer; }publicStartup( IConfiguration configuration) {Configuration= configuration; }
publicIConfigurationConfiguration{ get; }
// Thismethodgetscalledbytheruntime. Usethismethodtoaddservicestothecontainer. publicvoidConfigureServices( IServiceCollection services) {services. AddMvc;
GlobalTracer. Register( Tracer.Value); services. AddOpenTracing; }
特别声明:本站内容均来自网友提供或互联网,仅供参考,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。