简体   繁体   English

.NET 核心中的 OpenTracing 不依赖于特定的解决方案库

[英]OpenTracing in .NET core without taking dependency on specific solution library

For logging in our microservice applications we simply log to stdout/console and the docker logging driver handles and redirects these logs somewhere eg gelf/logstash, fluentd, etc. Basically, we're following 12 factor guidelines for logging.为了登录我们的微服务应用程序,我们只需登录到 stdout/console,docker 日志驱动程序处理这些日志并将这些日志重定向到某个地方,例如 gelf/logstash、fluentd 等。基本上,我们遵循12 因素准则进行日志记录。 This means that developers working on the application code don't need to know anything about the underlying logging solution (eg Elasticsearch, Graylog, Splunk, etc) - it's entirely an ops/configuration concern.这意味着处理应用程序代码的开发人员不需要了解有关底层日志记录解决方案(例如 Elasticsearch、Graylog、Splunk 等)的任何信息——这完全是一个操作/配置问题。 In theory we should be able to change the underlying logging solution without any code changes.从理论上讲,我们应该能够在不更改任何代码的情况下更改底层日志记录解决方案。

I'd like something similar for traces and my research has led me to OpenTracing.我想要类似的痕迹,我的研究使我使用了 OpenTracing。 Developers shouldn't need to know the underlying tracing solution (eg Jaeger, Zipkin, Elastic APM, etc) and as per logging;开发人员不需要了解底层跟踪解决方案(例如 Jaeger、Zipkin、Elastic APM 等)和日志记录; in theory we should be able to change the underlying tracing solution without any code changes.理论上,我们应该能够在不更改任何代码的情况下更改底层跟踪解决方案。

I've successfully got a .NET core POC sending traces to Jaeger using the opentracing/opentracing-csharp and jaegertracing/jaeger-client-csharp libraries.我已经成功地使用opentracing/opentracing-csharpjaegertracing/jaeger-client-csharp库将跟踪发送到 Jaeger 的 .NET 核心 POC。

I'm still trying to fully get my head around OpenTracing, but I'm wondering if there's a way to send traces to an OpenTracing compliant API without having to take a hard dependency on a particular solution such as Jaeger (ie the jaeger-client-csharp library).我仍在尝试完全了解 OpenTracing,但我想知道是否有一种方法可以将跟踪发送到符合 OpenTracing 的 API,而不必对特定解决方案(例如 Jaeger)(即 jaeger-client -csharp 库)。 Based on my understanding OpenTracing is just a standard.根据我的理解,OpenTracing 只是一个标准。 Shouldn't I just be able to configure an OpenTracing endpoint with some sampling options without needing the jaeger-client-csharp library?我不应该能够在不需要 jaeger-client-csharp 库的情况下使用一些采样选项配置 OpenTracing 端点吗? Or is it that the jaeger-client-csharp is not actually Jaeger specific and can actually send traces to any OpenTracing API?还是 jaeger-client-csharp 实际上并不是 Jaeger 特定的,并且实际上可以将跟踪发送到任何 OpenTracing API?

Example configuration shown below, which uses jaeger client library:示例配置如下所示,它使用 jaeger 客户端库:

services.AddOpenTracing();

if (appSettings.TracerEnabled)
{
   services.AddSingleton(serviceProvider =>
   {
      var loggerFactory = new LoggerFactory();
      var config = Jaeger.Configuration.FromEnv(loggerFactory);

      var tracer = config.GetTracer();

      GlobalTracer.Register(tracer);

      return tracer;
   });
}

OpenTracing is a set of standard APIs that consistently model and describe the behavior of distributed systems ) OpenTracing 是一组标准 API,可以一致地建模和描述分布式系统的行为

OpenTracing did not describe how to collect, report, store or represent the data of interrelated traces and spans. OpenTracing 没有描述如何收集、报告、存储或表示相互关联的跟踪和跨度的数据。 It is implementation details (such as jaeger or wavefront ).它是实现细节(例如jaegerwavefront )。

jaeger-client-csharp is very jaeger-specific. jaeger-client-csharp 是非常特定于 jaeger 的。 But there is one exception, called zipkin which in turns is not fully OpenTracing compliant, even it has similar terms.但是有一个例外,叫做zipkin ,它又不完全符合 OpenTracing,即使它有类似的术语。

If you are OK with opentracing-contrib/csharp-netcore (hope you are using this library) then if you want to achieve "no code change" (in target microservice) in order to configure tracing subsystem, you should use some plug-in model.如果你对opentracing-contrib/csharp-netcore 没问题(希望你正在使用这个库)那么如果你想实现“无代码更改”(在目标微服务中)以配置跟踪子系统,你应该使用一些插件模型。

Good news that aspnetcore has concept of hosted startup assemblies , which allow you to configure tracing system.好消息是 aspnetcore 有托管启动程序集的概念,它允许您配置跟踪系统。 So, you can have some library called JaegerStartup where you will implement IHostedStartup like follows:因此,您可以拥有一些名为JaegerStartup库,您将在其中实现 IHostedStartup,如下所示:

public class JaegerStartup : IHostingStartup
{
    public void Configure(IWebHostBuilder builder)
    {
        builder.ConfigureServices((ctx, services) =>
        {
            services.AddOpenTracing();

            if (ctx.Configuration.IsTracerEnabled()) // implement it by observing your section in configuration.
            {
                services.AddSingleton(serviceProvider =>
                {
                    var loggerFactory = new LoggerFactory();
                    var config = Jaeger.Configuration.FromEnv(loggerFactory);

                    var tracer = config.GetTracer();

                    GlobalTracer.Register(tracer);

                    return tracer;
                });
            }
        });
    }
}

When you decide to switch the tracing system - you need to create another library, which can be loaded automatically, and target microservice code will not be touched.当您决定切换跟踪系统时-您需要创建另一个库,该库可以自动加载,并且不会触及目标微服务代码。

I think this should be updated that now we have Opentelemtry dirrect supported by ELastic APM server and also nativ support in NetCore Activity class..我认为这应该更新,现在我们有 ELastic APM 服务器支持的 Opentelemtry dirrect 以及 NetCore Activity 类中的 nativ 支持..

public static partial class ServiceExtension {

        public static IServiceCollection AddTelemerty(
            this IServiceCollection serviceCollection,
            IConfiguration Configuration, IWebHostEnvironment Environment) {

            serviceCollection.AddOpenTelemetryTracing((builder) => {
                // Sources
                builder.AddSource(Sources.DemoSource.Name);

                builder.SetResourceBuilder(ResourceBuilder
                  .CreateDefault()
                    //.AddAttributes( new List<KeyValuePair<String, object>> { 
                    //   new KeyValuePair<String, object>("SomeKey", "This is String Value")
                    //  })
                  .AddService(Environment.ApplicationName));

                builder.AddAspNetCoreInstrumentation(opts => {
                    opts.RecordException = true;
                });

                builder.AddElasticsearchClientInstrumentation();

                builder.AddSqlClientInstrumentation();

                builder.AddHttpClientInstrumentation(opts => opts.RecordException = true);

                if (Uri.TryCreate(Configuration.GetConnectionString("Jaeger"), UriKind.Absolute, out var uri)) {
                    builder.AddJaegerExporter(opts => {
                        opts.AgentHost = uri.Host;
                        opts.AgentPort = uri.Port;
                        opts.BatchExportProcessorOptions = new OpenTelemetry.BatchExportProcessorOptions<Activity>() {
                        };
                    });
                
                    // builder.AddZipkinExporter(opts => {
                    //     opts.Endpoint = new Uri("http://localhost:9412/api/v2/spans");
                    // });
                }
            });

            return serviceCollection;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM