census-instrumentation / opencensus-csharp

Distributed tracing and stats collecting framework
https://opencensus.io
Apache License 2.0
139 stars 32 forks source link

ZipkinExporter will be in an infinite loop when I use ZipkinExporter to Export Tracing data #149

Closed czs1993 closed 5 years ago

czs1993 commented 5 years ago

step1: I created a Web Application(.Net Core)[WebOpenCensus] ,created a logged in Controller,and then configured in the startup class according to the configuration example(https://github.com/census-instrumentation/opencensus-csharp/blob/develop/PROJECT_DESCRIPTION.md) ;

 public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        /********OpenCensus configuration******/
        services.AddSingleton<HttpClient>();
        services.AddSingleton<ITracer>(Tracing.Tracer);
        services.AddSingleton<ISampler>(Samplers.AlwaysSample);
        services.AddSingleton<RequestsCollectorOptions>(new RequestsCollectorOptions());
        services.AddSingleton<RequestsCollector>();
        services.AddSingleton<DependenciesCollectorOptions>(new DependenciesCollectorOptions());
        services.AddSingleton<DependenciesCollector>();
        services.AddSingleton<IExportComponent>(Tracing.ExportComponent);
        services.AddSingleton<IPropagationComponent>(new DefaultPropagationComponent());
        services.AddSingleton<ZipkinTraceExporter>((p) =>
        {
            var exportComponent = p.GetService<IExportComponent>();
            return new ZipkinTraceExporter(new ZipkinTraceExporterOptions()
            {
                Endpoint = new Uri("https://localhost:44399/Index.ashx"), //receiving url
                ServiceName = "OpenCensus_Core",
            },exportComponent);
        });
    }

     public void Configure(IApplicationBuilder app, IHostingEnvironment env, ZipkinTraceExporter agentExporter, IApplicationLifetime applicationLifetime)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        } 
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy(); 
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Login}/{action=Index}");
        });
        /********OpenCensus configuration******/
        var collector = app.ApplicationServices.GetService<RequestsCollector>();
        var depCollector = app.ApplicationServices.GetService<DependenciesCollector>(); 
        agentExporter.Start(); 
        applicationLifetime.ApplicationStopping.Register(agentExporter.Stop); 
    }

Step 2: Create An Empty Web Application(.NET Framework),and then created a Generic Handler(index.ashx) to receive Tracing data, and the received data is written into the txt file: public class Index : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        Stream stream = context.Request.InputStream;
        if (stream.Length > 0)
        {
            byte[] spanByutes = new byte[stream.Length];
            stream.Read(spanByutes, 0, spanByutes.Length);
            var spanString = Encoding.UTF8.GetString(spanByutes);
            StreamWriter sw = File.AppendText(@"D:\Code\Csharp\spanInfo.txt");
            sw.WriteLine(spanString);
            sw.Flush();
            sw.Close();
        }
        context.Response.ContentType = "text/plain";
        context.Response.Write("ok!");
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

}

step 3: Turn on the receiver and listen for ZipkinTracerExporter Export data; https://localhost:44399/Index.ashx; step4: Turn on the Web Application(WebOpenCensus),Tracking call information;

problem:

The ZipkinTracerExporter will Fall into a cycle. Because the ZipkinTracerExporter HttpPost function is tracked.