Exemplo n.º 1
0
        public static IWebHostBuilder ConfigureTracing(this IWebHostBuilder webHostBuilder, IConfiguration configuration)
        {
            webHostBuilder.ConfigureServices(services =>
            {
                var tracingConfig = new TracingConfiguration();
                configuration.GetSection("Tracing").Bind(tracingConfig);

                if (!tracingConfig.EnableOpenTracing)
                {
                    return;
                }

                services.AddOpenTracing();

                var isJaeger = tracingConfig.Tracer.ToLower() == "jaeger";
                var useAgent = tracingConfig.Tracer.ToLower().Contains("agent");

                if (isJaeger || useAgent)
                {
                    services.AddJaeger(configuration, useAgent);
                }
                else
                {
                    services.AddDataDog(configuration);
                }
            });

            return(webHostBuilder);
        }
Exemplo n.º 2
0
        public static IServiceCollection AddDataDog(this IServiceCollection services, IConfiguration configuration)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            services.AddSingleton <ITracer>(serviceProvider =>
            {
                var tracingConfig = new TracingConfiguration();
                configuration.GetSection("Tracing").Bind(tracingConfig);

                var datadogUrl  = $"http://{tracingConfig.DataDogUrl}:8126";
                var serviceName = Assembly.GetEntryAssembly().GetName().Name;

                var tracer = Datadog.Trace.OpenTracing.OpenTracingTracerFactory.CreateTracer(new Uri(datadogUrl), serviceName);
                GlobalTracer.Register(tracer);

                return(tracer);
            });

            // Prevent endless loops when OpenTracing is tracking HTTP requests to opentracing.
            services.Configure <HttpHandlerDiagnosticOptions>(options =>
            {
                options.IgnorePatterns.Add(request => request.RequestUri.ToString().ToLower().Contains("/api/traces"));
                options.IgnorePatterns.Add(request => request.RequestUri.ToString().ToLower().EndsWith("hc"));
                options.IgnorePatterns.Add(request => request.RequestUri.ToString().ToLower().Contains("datadoghq"));
            });

            return(services);
        }
Exemplo n.º 3
0
        public static IWebHostBuilder ConfigureTracing(this IWebHostBuilder webHostBuilder, IConfiguration configuration)
        {
            webHostBuilder.ConfigureServices(services =>
            {
                var tracingConfig = new TracingConfiguration();
                configuration.GetSection("Tracing").Bind(tracingConfig);


                if (tracingConfig.EnableOpenTracing)
                {
                    services.AddJaeger();
                    services.AddOpenTracing();
                }
            });

            return(webHostBuilder);
        }
        public static IServiceCollection AddJaeger(this IServiceCollection services, IConfiguration configuration, bool useAgent = true)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            services.AddSingleton <ITracer>(serviceProvider =>
            {
                var tracingConfig = new TracingConfiguration();
                configuration.GetSection("Tracing").Bind(tracingConfig);

                string serviceName           = tracingConfig.ServiceName ?? Assembly.GetEntryAssembly().GetName().Name;
                ILoggerFactory loggerFactory = serviceProvider.GetRequiredService <ILoggerFactory>();
                ITracer tracer;

                //HACK TIME
                var useLocalSettigns = tracingConfig.JaegerUrl?.Contains("localhost");

                if (!useAgent && useLocalSettigns)
                {
                    //use lcoalhost defaults.
                    ISampler sampler = new ConstSampler(sample: true);

                    tracer = new Tracer.Builder(serviceName)
                             .WithLoggerFactory(loggerFactory)
                             .WithSampler(sampler)
                             .Build();

                    GlobalTracer.Register(tracer);

                    return(tracer);
                }

                //ok we will try and connect to an external jaeger setup
                if (useAgent)
                {
                    //use the agent
                    Environment.SetEnvironmentVariable("JAEGER_AGENT_HOST", tracingConfig.JaegerAgent);
                    Environment.SetEnvironmentVariable("JAEGER_AGENT_PORT", "6831");
                }

                else
                {
                    //use the collector
                    var jaegerUri = $"http://{tracingConfig.JaegerUrl}:14268/api/traces";
                    Environment.SetEnvironmentVariable("JAEGER_ENDPOINT", jaegerUri);
                }

                Environment.SetEnvironmentVariable("JAEGER_SERVICE_NAME", serviceName);
                Environment.SetEnvironmentVariable("JAEGER_SAMPLER_TYPE", "const");

                var config = Jaeger.Configuration.FromEnv(loggerFactory);
                tracer     = config.GetTracer();
                GlobalTracer.Register(tracer);

                return(tracer);
            });

            // Prevent endless loops when OpenTracing is tracking HTTP requests to Jaeger.
            services.Configure <HttpHandlerDiagnosticOptions>(options =>
            {
                options.IgnorePatterns.Add(request => request.RequestUri.ToString().ToLower().Contains("/api/traces"));
                options.IgnorePatterns.Add(request => request.RequestUri.ToString().ToLower().EndsWith("hc"));
                options.IgnorePatterns.Add(request => request.RequestUri.ToString().ToLower().Contains("datadoghq"));
            });

            return(services);
        }