Ejemplo n.º 1
0
        internal static object Run(string zipkinUri)
        {
            // connect to the server
            var connection = ConnectionMultiplexer.Connect("localhost:6379");

            // Configure exporter to export traces to Zipkin
            using var tracerFactory = TracerFactory.Create(builder => builder
                                                           .UseZipkin(o =>
            {
                o.ServiceName = "redis-test";
                o.Endpoint    = new Uri(zipkinUri);
            })
                                                           .AddInstrumentation(t =>
            {
                var instrumentation = new StackExchangeRedisCallsInstrumentation(t);
                connection.RegisterProfiler(instrumentation.GetProfilerSessionsFactory());
                return(instrumentation);
            }));
            var tracer = tracerFactory.GetTracer("redis-test");

            // select a database (by default, DB = 0)
            var db = connection.GetDatabase();

            // Create a scoped span. It will end automatically when using statement ends
            using (tracer.StartActiveSpan("Main", out _))
            {
                Console.WriteLine("About to do a busy work");
                for (var i = 0; i < 10; i++)
                {
                    DoWork(db, tracer);
                }
            }

            return(null);
        }
        public async Task ProfilerSessionsHandleMultipleSpans()
        {
            var connectionOptions = new ConfigurationOptions
            {
                AbortOnConnectFail = false,
            };

            connectionOptions.EndPoints.Add("localhost:6379");

            var connection = ConnectionMultiplexer.Connect(connectionOptions);

            using var instrumentation = new StackExchangeRedisCallsInstrumentation(connection, new StackExchangeRedisCallsInstrumentationOptions());
            var profilerFactory = instrumentation.GetProfilerSessionsFactory();

            // start a root level activity
            using Activity rootActivity = new Activity("Parent")
                                          .SetParentId(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.Recorded)
                                          .Start();

            Assert.NotNull(rootActivity.Id);

            // get an initial profiler from root activity
            Activity.Current = rootActivity;
            ProfilingSession profiler0 = profilerFactory();

            // expect different result from synchronous child activity
            ProfilingSession profiler1;

            using (Activity.Current = new Activity("Child-Span-1").SetParentId(rootActivity.Id).Start())
            {
                profiler1 = profilerFactory();
                Assert.NotSame(profiler0, profiler1);
            }

            Activity.Current = rootActivity;

            // expect different result from asynchronous child activity
            using (Activity.Current = new Activity("Child-Span-2").SetParentId(rootActivity.Id).Start())
            {
                // lose async context on purpose
                await Task.Delay(100).ConfigureAwait(false);

                ProfilingSession profiler2 = profilerFactory();
                Assert.NotSame(profiler0, profiler2);
                Assert.NotSame(profiler1, profiler2);
            }

            Activity.Current = rootActivity;

            // ensure same result back in root activity
            ProfilingSession profiles3 = profilerFactory();

            Assert.Same(profiler0, profiles3);
        }
Ejemplo n.º 3
0
        public async void ProfilerSessionUsesTheSameDefault()
        {
            var connectionOptions = new ConfigurationOptions
            {
                AbortOnConnectFail = false,
            };

            connectionOptions.EndPoints.Add("localhost:6379");

            var connection = ConnectionMultiplexer.Connect(connectionOptions);

            using var instrumentation = new StackExchangeRedisCallsInstrumentation(connection, new StackExchangeRedisCallsInstrumentationOptions());
            var profilerFactory    = instrumentation.GetProfilerSessionsFactory();
            var first              = profilerFactory();
            var second             = profilerFactory();
            ProfilingSession third = null;
            await Task.Delay(1).ContinueWith((t) => { third = profilerFactory(); });

            Assert.Equal(first, second);
            Assert.Equal(second, third);
        }
        public void CheckCacheIsFlushedProperly()
        {
            var connectionOptions = new ConfigurationOptions
            {
                AbortOnConnectFail = false,
            };

            connectionOptions.EndPoints.Add("localhost:6379");

            var connection = ConnectionMultiplexer.Connect(connectionOptions);

            using var instrumentation = new StackExchangeRedisCallsInstrumentation(connection, new StackExchangeRedisCallsInstrumentationOptions());
            var profilerFactory = instrumentation.GetProfilerSessionsFactory();

            // start a root level activity
            using Activity rootActivity = new Activity("Parent")
                                          .SetParentId(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.Recorded)
                                          .Start();

            Assert.NotNull(rootActivity.Id);

            // get an initial profiler from root activity
            Activity.Current = rootActivity;
            ProfilingSession profiler0 = profilerFactory();

            // expect different result from synchronous child activity
            ProfilingSession profiler1;

            using (Activity.Current = new Activity("Child-Span-1").SetParentId(rootActivity.Id).Start())
            {
                profiler1 = profilerFactory();
                Assert.NotSame(profiler0, profiler1);
            }

            rootActivity.Stop();
            rootActivity.Dispose();

            instrumentation.Flush();
            Assert.Empty(instrumentation.Cache);
        }