Example #1
0
        /// <summary>
        /// This program hosts a Web API that has a controller decorated with .a PerfIt filter and then sends an HTTP request to instrument
        /// There is a Zipkin ServerTraceHandler to pick up headers from request and inject headers to the response.
        /// Zipkin emitter has a console dispatcher which outputs spans to the console.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            string baseAddress   = "http://localhost:34543/";
            var    configuration = new HttpSelfHostConfiguration(baseAddress);

            configuration.Routes.Add("def", new HttpRoute("api/{controller}"));
            configuration.MessageHandlers.Add(new ServerTraceHandler("server-test")); // adding Zipkin handler to inject headers
            var server = new HttpSelfHostServer(configuration);

            server.OpenAsync().Wait();

            // zipkin emitter
            var emitter = new SimpleEmitter();

            emitter.RegisterDispatcher(new ConsoleDispatcher());

            // hook to the filter and add a tracer
            PerfItRuntime.InstrumentorCreated += (sender, e) =>
            {
                if (e.Info.CategoryName == "server-test")
                {
                    e.Instrumentor.Tracers.Add("Console", new ServerTracer(emitter));
                }
            };

            // handler
            var handler = new PerfitClientDelegatingHandler("client-test", new ClientTracer(emitter))
            {
                InnerHandler    = new ClientTraceHandler("client-test", new HttpClientHandler()),
                PublishCounters = false
            };

            var client = new HttpClient(handler);
            var result = client.GetAsync(baseAddress + "api/test").Result;

            Console.WriteLine(result.Content.ReadAsStringAsync().Result);

            // notice Zipkin headers in the request as a result of ClientTraceHandler
            Console.BackgroundColor = ConsoleColor.Yellow;
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine(result.RequestMessage.Headers.ToString());
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(result.Headers.ToString());
            Console.ResetColor();
            result.EnsureSuccessStatusCode();

            Console.Read();
        }
Example #2
0
        /// <summary>
        /// This is sample whereby a .NET Core MVC API is hosted with a controller that is decorated with PerfIt filter.
        /// A Zipkin emitter is created with a Console dispatcher so that all Zipkin traces are sent to Console.
        /// A Zipkin tracer gets added by hooking into PerfItRuntime.InstrumentorCreated.
        /// On the other hand, we have HttpClient which gets a PerfIt handler with a ClientTraceHandler which injects Zipkin headers.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            // server
            var h = BuildWebHost(args);

            h.Start();

            // zipkin emitter
            var emitter = new SimpleEmitter();

            emitter.RegisterDispatcher(new ConsoleDispatcher());

            // hook to the filter and add a tracer
            PerfItRuntime.InstrumentorCreated += (sender, e) =>
            {
                if (e.Info.CategoryName == "server-test")
                {
                    e.Instrumentor.Tracers.Add("Console", new ServerTracer(emitter));
                }
            };

            // handler
            var handler = new PerfitClientDelegatingHandler("client-test", new ClientTracer(emitter))
            {
                InnerHandler = new ClientTraceHandler("client-test", new HttpClientHandler())
            };

            var client = new HttpClient(handler);
            var result = client.GetAsync(baseAddress + "api/test").Result;

            Console.WriteLine(result.Content.ReadAsStringAsync().Result);

            // notice Zipkin headers in the request as a result of ClientTraceHandler
            Console.BackgroundColor = ConsoleColor.Yellow;
            Console.ForegroundColor = ConsoleColor.DarkMagenta;
            Console.WriteLine(result.RequestMessage.Headers.ToString());
            Console.ResetColor();
            result.EnsureSuccessStatusCode();

            Console.Read();

            h.StopAsync().Wait();
        }