public void TestActiveSpanAutoReference()
        {
            InMemoryReporter reporter = new InMemoryReporter();
            Tracer           tracer   = new Tracer.Builder("test")
                                        .WithReporter(reporter)
                                        .WithSampler(new ConstSampler(true))
                                        .Build();

            using (IScope parent = tracer.BuildSpan("parent").StartActive(finishSpanOnDispose: true))
            {
                tracer.BuildSpan("child").StartActive(finishSpanOnDispose: true).Dispose();
            }
            Assert.Equal(2, reporter.GetSpans().Count);

            Span childSpan  = reporter.GetSpans()[0];
            Span parentSpan = reporter.GetSpans()[1];

            Assert.Equal("child", childSpan.OperationName);
            Assert.Single(childSpan.GetReferences());
            Assert.Equal("parent", parentSpan.OperationName);
            Assert.Empty(parentSpan.GetReferences());
            Assert.Equal(References.ChildOf, childSpan.GetReferences()[0].Type);
            Assert.Equal(parentSpan.Context, childSpan.GetReferences()[0].Context);
            Assert.Equal(parentSpan.Context.TraceId, childSpan.Context.TraceId);
            Assert.Equal(parentSpan.Context.SpanId, childSpan.Context.ParentId);
        }
        public void TestNoAutoRefWithExistingRefs()
        {
            InMemoryReporter reporter = new InMemoryReporter();
            Tracer           tracer   = new Tracer.Builder("test")
                                        .WithReporter(reporter)
                                        .WithSampler(new ConstSampler(true))
                                        .Build();

            ISpan initialSpan = tracer.BuildSpan("initial").Start();

            using (IScope parent = tracer.BuildSpan("parent").StartActive(finishSpanOnDispose: true))
            {
                tracer.BuildSpan("child").AsChildOf(initialSpan.Context).StartActive(finishSpanOnDispose: true).Dispose();
            }

            initialSpan.Finish();

            Assert.Equal(3, reporter.GetSpans().Count);

            Span childSpan  = reporter.GetSpans()[0];
            Span parentSpan = reporter.GetSpans()[1];
            Span initSpan   = reporter.GetSpans()[2];

            Assert.Empty(initSpan.GetReferences());
            Assert.Empty(parentSpan.GetReferences());

            Assert.Equal(initSpan.Context.TraceId, childSpan.Context.TraceId);
            Assert.Equal(initSpan.Context.SpanId, childSpan.Context.ParentId);

            Assert.Equal(0L, initSpan.Context.ParentId);
            Assert.Equal(0L, parentSpan.Context.ParentId);
        }
        public void SpanBuilder_ShouldUseFlagsFromParent()
        {
            var tracer = new Tracer.Builder("service")
                         .WithSampler(new ConstSampler(true))
                         .Build();

            var parentSpan = (IJaegerCoreSpan)tracer.BuildSpan("parent").Start();

            Assert.True(parentSpan.Context.Flags.HasFlag(ContextFlags.Sampled));

            var childSpan = (IJaegerCoreSpan)tracer.BuildSpan("child").AsChildOf(parentSpan).Start();

            Assert.Equal(parentSpan.Context.Flags, childSpan.Context.Flags);
        }
Exemplo n.º 4
0
        private static void Main(string[] args)
        {
            //Reference https://github.com/jaegertracing/jaeger-client-csharp
            var          loggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); });
            const string serviceName   = "initExampleService";

            var reporter = new LoggingReporter(loggerFactory);
            var sampler  = new ConstSampler(true);
            var tracer   = new Tracer.Builder(serviceName)
                           .WithLoggerFactory(loggerFactory)
                           .WithReporter(reporter)
                           .WithSampler(sampler)
                           .Build();

            const string operationName = "Get::api/values";
            var          spanBuilder   = tracer.BuildSpan(operationName);

            using (var scope = spanBuilder.StartActive(true))
            {
                var logData = new List <KeyValuePair <string, object> >
                {
                    new KeyValuePair <string, object>("handling number of events", 6),
                    new KeyValuePair <string, object>("using legacy system", false)
                };
                scope.Span.Log(DateTimeOffset.Now, logData);
            }

            Console.ReadLine();
        }
Exemplo n.º 5
0
        public void TestDefaultConstructor()
        {
            var tracer = new Tracer.Builder("name").Build();

            Assert.IsType <RemoteReporter>(tracer.Reporter);
            // no exception
            tracer.BuildSpan("foo").Start().Finish();
        }
        public void SpanBuilder_ShouldDefaultFlagsToZeroWhenNotSampled()
        {
            var tracer = new Tracer.Builder("service")
                         .WithSampler(new ConstSampler(false))
                         .Build();

            var builtSpan = (IJaegerCoreSpan)tracer.BuildSpan("foo").Start();

            Assert.Equal(ContextFlags.None, builtSpan.Context.Flags);
        }
        public void TestActiveSpanAutoFinishOnClose()
        {
            InMemoryReporter reporter = new InMemoryReporter();
            Tracer           tracer   = new Tracer.Builder("test")
                                        .WithReporter(reporter)
                                        .WithSampler(new ConstSampler(true))
                                        .Build();

            tracer.BuildSpan("parent").StartActive(finishSpanOnDispose: true).Dispose();
            Assert.Single(reporter.GetSpans());
        }
        public void CanBeUsedWithMetrics()
        {
            NoopMetricsFactory metricsFactory = NoopMetricsFactory.Instance;
            Tracer             tracer         =
                new Tracer.Builder("metricsFactoryTest")
                .WithReporter(new NoopReporter())
                .WithSampler(new ConstSampler(true))
                .WithMetrics(new MetricsImpl(metricsFactory))
                .Build();

            tracer.BuildSpan("theoperation").Start();
        }
        public void ShouldFallbackWhenInjectingWithFaultyCodec()
        {
            Tracer tracer = new Tracer.Builder("TracerResiliencyTestService")
                            .WithReporter(new InMemoryReporter())
                            .WithSampler(new ConstSampler(true))
                            .RegisterCodec(BuiltinFormats.TextMap, new FaultyCodec())
                            .Build();

            ISpanContext context = tracer.BuildSpan("test-span").Start().Context;

            tracer.Inject(context, BuiltinFormats.TextMap, new NoopTextMap());
        }
        public void TestActiveSpanPropagation()
        {
            Tracer tracer = new Tracer.Builder("test")
                            .WithReporter(new InMemoryReporter())
                            .WithSampler(new ConstSampler(true))
                            .Build();

            using (IScope parent = tracer.BuildSpan("parent").StartActive(finishSpanOnDispose: true))
            {
                Assert.Equal(parent, tracer.ScopeManager.Active);
            }
        }
        public void TestIgnoreActiveSpan()
        {
            InMemoryReporter reporter = new InMemoryReporter();
            Tracer           tracer   = new Tracer.Builder("test")
                                        .WithReporter(reporter)
                                        .WithSampler(new ConstSampler(true))
                                        .Build();

            using (IScope parent = tracer.BuildSpan("parent").StartActive(finishSpanOnDispose: true))
            {
                tracer.BuildSpan("child").IgnoreActiveSpan().StartActive(finishSpanOnDispose: true).Dispose();
            }
            Assert.Equal(2, reporter.GetSpans().Count);

            Span childSpan  = reporter.GetSpans()[0];
            Span parentSpan = reporter.GetSpans()[1];

            Assert.Empty(reporter.GetSpans()[0].GetReferences());
            Assert.Empty(reporter.GetSpans()[1].GetReferences());
            Assert.NotEqual(parentSpan.Context.TraceId, childSpan.Context.TraceId);
            Assert.Equal(0L, childSpan.Context.ParentId);
        }
Exemplo n.º 12
0
        public void TestSpanNotSampled()
        {
            Tracer tracer = new Tracer.Builder("fo")
                            .WithReporter(reporter)
                            .WithSampler(new ConstSampler(false))
                            .Build();
            ISpan foo = tracer.BuildSpan("foo")
                        .Start();

            foo.Log(new Dictionary <string, object>())
            .Finish();
            Assert.Empty(reporter.GetSpans());
        }
        public void EmptyValueForTag()
        {
            InMemoryMetricsFactory metricsFactory = new InMemoryMetricsFactory();
            Tracer tracer = new Tracer.Builder("metricsFactoryTest")
                            .WithReporter(new NoopReporter())
                            .WithSampler(new ConstSampler(true))
                            .WithMetrics(new MetricsImpl(metricsFactory))
                            .Build();

            tracer.BuildSpan("theoperation").Start();
            Assert.Equal(-1, metricsFactory.GetCounter("jaeger:started_spans", "sampled"));
            Assert.Equal(-1, metricsFactory.GetCounter("jaeger:started_spans", ""));
        }
        public void CanBeUsedWithMetrics()
        {
            InMemoryMetricsFactory metricsFactory = new InMemoryMetricsFactory();
            Tracer tracer = new Tracer.Builder("metricsFactoryTest")
                            .WithReporter(new NoopReporter())
                            .WithSampler(new ConstSampler(true))
                            .WithMetrics(new MetricsImpl(metricsFactory))
                            .Build();

            tracer.BuildSpan("theoperation").Start();
            Assert.Equal(1, metricsFactory.GetCounter("jaeger:started_spans", "sampled=y"));
            Assert.Equal(0, metricsFactory.GetCounter("jaeger:started_spans", "sampled=n"));
            Assert.Equal(1, metricsFactory.GetCounter("jaeger:traces", "sampled=y,state=started"));
            Assert.Equal(0, metricsFactory.GetCounter("jaeger:traces", "sampled=n,state=started"));
        }
Exemplo n.º 15
0
        public void TestTraceId64Bit()
        {
            var tracer = new Tracer.Builder("name")
                         .Build();

            // no exception
            var span = tracer.BuildSpan("foo").Start();

            span.Finish();

            var spanContext = span.Context as SpanContext;

            Assert.NotNull(spanContext);
            Assert.True(spanContext.TraceId.High == 0);
        }
        public void TestActiveSpanNotAutoFinishOnClose()
        {
            InMemoryReporter reporter = new InMemoryReporter();
            Tracer           tracer   = new Tracer.Builder("test")
                                        .WithReporter(reporter)
                                        .WithSampler(new ConstSampler(true))
                                        .Build();

            IScope scope = tracer.BuildSpan("parent").StartActive(finishSpanOnDispose: false);
            Span   span  = (Span)scope.Span;

            scope.Dispose();
            Assert.Empty(reporter.GetSpans());
            span.Finish();
            Assert.Single(reporter.GetSpans());
        }
Exemplo n.º 17
0
        public CompositeCodecTests()
        {
            var tracer = new Tracer.Builder("service")
                         .WithReporter(new NoopReporter())
                         .Build();

            Span span = (Span)tracer.BuildSpan("foo").Start();

            _spanContext = span.Context;

            _mockCarrier = Substitute.For <ITextMap>();
            _mockCodec1  = Substitute.For <Codec <ITextMap> >();
            _mockCodec2  = Substitute.For <Codec <ITextMap> >();
            _composite   = new CompositeCodec <ITextMap>(new List <Codec <ITextMap> > {
                _mockCodec1, _mockCodec2
            });
        }
        public void TestTracerTags()
        {
            InMemoryReporter spanReporter = new InMemoryReporter();
            Tracer           tracer       = new Tracer.Builder("x")
                                            .WithReporter(spanReporter)
                                            .WithSampler(new ConstSampler(true))
                                            .WithZipkinSharedRpcSpan()
                                            .WithTag("tracer.tag.str", "y")
                                            .Build();

            Span span = (Span)tracer.BuildSpan("root").Start();

            // span should only contain sampler tags and no tracer tags
            Assert.Equal(2, span.GetTags().Count);
            Assert.True(span.GetTags().ContainsKey("sampler.type"));
            Assert.True(span.GetTags().ContainsKey("sampler.param"));
            Assert.False(span.GetTags().ContainsKey("tracer.tag.str"));
        }
        public void LoggingReporter_ShouldCallLogger()
        {
            var loggerFactory = Substitute.For <ILoggerFactory>();
            var logger        = Substitute.For <MockLogger>();

            loggerFactory.CreateLogger <LoggingReporter>().Returns <ILogger>(logger);

            var reporter = new LoggingReporter(loggerFactory);

            var tracer = new Tracer.Builder("service")
                         .WithReporter(reporter)
                         .WithSampler(new ConstSampler(true))
                         .Build();

            tracer.BuildSpan("foo").Start().Finish();

            loggerFactory.Received(1).CreateLogger <LoggingReporter>();
            logger.Received(1).Log(LogLevel.Information, Arg.Any <string>());
        }
        public void LoggingReporter_CanLogActiveSpan()
        {
            var loggerFactory = Substitute.For <ILoggerFactory>();
            var logger        = Substitute.For <ILogger>();

            loggerFactory.CreateLogger <LoggingReporter>().Returns(logger);

            var reporter = new LoggingReporter(loggerFactory);

            var tracer = new Tracer.Builder("service")
                         .WithReporter(reporter)
                         .WithSampler(new ConstSampler(true))
                         .Build();

            tracer.BuildSpan("foo").StartActive(true).Dispose();

            loggerFactory.Received(1).CreateLogger <LoggingReporter>();
            logger.Received(1).Log(LogLevel.Information, Arg.Any <EventId>(), Arg.Any <object>(), null, Arg.Any <Func <object, Exception, string> >());
        }
        public void SpanBuilder_ShouldBuildFlagsAndTagsFromSampler()
        {
            var sampler     = Substitute.For <ISampler>();
            var samplerTags = new Dictionary <string, object> {
                { "tag.1", "value1" },
                { "tag.2", "value2" }
            };

            sampler.IsSampled(Arg.Any <TraceId>(), Arg.Any <string>())
            .Returns((true, samplerTags));

            var tracer = new Tracer.Builder("service")
                         .WithSampler(sampler)
                         .Build();

            var builtSpan = (IJaegerCoreSpan)tracer.BuildSpan("foo").Start();

            Assert.Equal(ContextFlags.Sampled, builtSpan.Context.Flags);
            Assert.Equal(samplerTags, builtSpan.Tags);
        }
Exemplo n.º 22
0
        public void TestSetAndGetBaggageItem()
        {
            string service = "SamplerTest";
            IBaggageRestrictionManager mgr = Substitute.ForPartsOf <DefaultBaggageRestrictionManager>();
            var tracer = new Tracer.Builder(service)
                         .WithReporter(reporter)
                         .WithSampler(new ConstSampler(true))
                         .WithClock(clock)
                         .WithBaggageRestrictionManager(mgr)
                         .Build();
            var span = (Span)tracer.BuildSpan("some-operation").Start();

            string key   = "key";
            string value = "value";

            mgr.GetRestriction(service, key).Returns(new Restriction(true, 10));
            span.SetBaggageItem(key, "value");
            mgr.Received(1).GetRestriction(service, key);
            Assert.Equal(value, span.GetBaggageItem(key));
        }
Exemplo n.º 23
0
        public void TestNoExpandExceptionLogs()
        {
            Tracer tracer = new Tracer.Builder("fo")
                            .WithReporter(reporter)
                            .WithSampler(new ConstSampler(true))
                            .Build();

            Span span = (Span)tracer.BuildSpan("foo").Start();

            Exception ex   = new Exception("ex");
            var       logs = new Dictionary <string, object>();

            logs[LogFields.ErrorObject] = ex;
            span.Log(logs);

            var logData = span.GetLogs();

            Assert.Single(logData);
            Assert.Single(logData[0].Fields);
            Assert.Equal(ex, logData[0].Fields[LogFields.ErrorObject]);
        }
        public void TestDebugCorrelationId()
        {
            Tracer tracer = new Tracer.Builder("test")
                            .WithReporter(new InMemoryReporter())
                            .WithSampler(new ConstSampler(true))
                            .Build();

            var headers = new Dictionary <string, string>();

            headers.Add(Constants.DebugIdHeaderKey, "Coraline");
            ITextMap    carrier     = new TextMapExtractAdapter(headers);
            SpanContext spanContext = (SpanContext)tracer.Extract(BuiltinFormats.TextMap, carrier);

            Assert.True(spanContext.IsDebugIdContainerOnly());
            Assert.Equal("Coraline", spanContext.DebugId);
            Span span = (Span)tracer.BuildSpan("span").AsChildOf(spanContext).Start();

            spanContext = (SpanContext)span.Context;
            Assert.True(spanContext.IsSampled);
            Assert.True(spanContext.IsDebug);
            Assert.Equal("Coraline", span.GetTags()[Constants.DebugIdHeaderKey]);
        }
        public void CompositeReporter_ShouldCallLoggerOnBoth()
        {
            var loggerFactory = Substitute.For <ILoggerFactory>();
            var logger1       = Substitute.For <ILogger>();
            var logger2       = Substitute.For <ILogger>();

            loggerFactory.CreateLogger <LoggingReporter>().Returns(logger1, logger2);

            var reporter1 = new LoggingReporter(loggerFactory);
            var reporter2 = new LoggingReporter(loggerFactory);
            var reporter  = new CompositeReporter(reporter1, reporter2);

            var tracer = new Tracer.Builder("service")
                         .WithReporter(reporter)
                         .WithSampler(new ConstSampler(true))
                         .Build();

            tracer.BuildSpan("foo").Start().Finish();
            loggerFactory.Received(2).CreateLogger <LoggingReporter>();

            logger1.Received(1).Log(LogLevel.Information, Arg.Any <EventId>(), Arg.Any <object>(), null, Arg.Any <Func <object, Exception, string> >());
            logger2.Received(1).Log(LogLevel.Information, Arg.Any <EventId>(), Arg.Any <object>(), null, Arg.Any <Func <object, Exception, string> >());
        }
Exemplo n.º 26
0
        public void TestPublish()
        {
            var cf = new ConnectionFactory()
            {
                Uri = new Uri("amqp://*****:*****@localhost:5672")
            };

            var serviceName = "initExampleService";
            //var transport = new JaegerHttpTransport("192.168.99.100");
            var transport = new JaegerUdpTransport("10.150.28.30", 6831);
            //var zt = new ZipkinJSONTransport(new Uri("http://192.168.99.100:9411"), 2);

            var reporter = new RemoteReporter.Builder(transport).Build();
            //var zreporter = new RemoteReporter.Builder(zt).Build();

            var sampler = new ConstSampler(true);

            var serviceProvider = new ServiceCollection()
                                  .AddLogging() //<-- You were missing this
                                  .BuildServiceProvider();
            //get logger
            var loggerFactory = serviceProvider.GetService <ILoggerFactory>();

            loggerFactory.AddConsole(LogLevel.Trace);
            loggerFactory.AddDebug(LogLevel.Trace);

            var log = loggerFactory.CreateLogger("test");

            log.LogInformation("prova log {0}", "prova");

            var logReporter = new LoggingReporter(loggerFactory);

            var tracer = new Tracer.Builder(serviceName)
                         .WithLoggerFactory(loggerFactory)
                         //.WithTransport(transport)
                         //.WithReporter(logReporter)
                         .WithReporter(reporter)
                         .WithSampler(sampler)
                         .Build();

            using (var span = tracer.BuildSpan("fakeget").StartActive(true))
            {
                var conn  = cf.CreateConnection("testtracing");
                var model = new TracingModel(conn.CreateModel(), tracer);
                // TODO: Add your test code here

                for (int i = 0; i < 25; i++)
                {
                    model.BasicPublish("testexchange", "", false, null, Encoding.UTF8.GetBytes("testmessage"));

                    /*var consumer = new EventingBasicConsumer(model);
                     * consumer.Received += (ch, ea) =>
                     * {
                     *  var body = ea.Body;
                     *  // ... process the message
                     *  model.BasicAck(ea.DeliveryTag, false);
                     * };
                     *
                     * Task.Delay(200).Wait();
                     *
                     * var res = model.BasicConsume("testqueueu", false, consumer);*/
                    Task.Delay(2500).Wait();
                    var res = model.BasicGet("testqueueu", true);
                }
            }
            tracer.Dispose();
            Assert.True(true, "Your first passing test");
        }