Ejemplo n.º 1
0
        public async Task TraceSingleAsync(Func <IHostBuilder> createHostBuilder)
        {
            IHost host = null;

            try
            {
                host = createHostBuilder().Build();
                await host.StartAsync();

                ITraceContext  traceContext  = new SimpleTraceContext(null, null, true);
                var            tracerFactory = host.Services.GetRequiredService <Func <ITraceContext, IManagedTracer> >();
                IManagedTracer tracer        = tracerFactory(traceContext);
                ContextTracerManager.SetCurrentTracer(tracer);

                using (tracer.StartSpan(_testId))
                {
                    IManagedTracer currentTracer = host.Services.GetRequiredService <IManagedTracer>();
                    currentTracer.RunInSpan(
                        () => Console.WriteLine("Using Cloud Trace from a non ASP.NET Core app"),
                        "testing_tracing");
                }

                var trace = TraceEntryPolling.Default.GetTrace(_testId, _startTime);
                TraceEntryVerifiers.AssertParentChildSpan(trace, _testId, "testing_tracing");
            }
            finally
            {
                if (host is object)
                {
                    await host.StopAsync();
                }
            }
        }
        public void Trace_Simple_Buffer()
        {
            string rootSpanName = EntryData.GetMessage(nameof(Trace_Simple_Buffer), _testId);
            // Create annotations with very large labels to ensure the buffer is flushed.
            string label  = string.Join("", Enumerable.Repeat("1234567890", 1000));
            var    labels = new Dictionary <string, string>
            {
                { "key-one", label },
                { "key-two", label },
                { "key-three", label },
                { "key-four", label },
                { "key-five", label },
            };

            using (var consumer = SizedBufferingConsumer <TraceProto> .Create(_grpcConsumer, MessageSizer <TraceProto> .GetSize, BufferOptions.DefaultBufferSize / 2))
            {
                var tracer = CreateSimpleManagedTracer(consumer);
                using (tracer.StartSpan(rootSpanName))
                {
                    BlockUntilClockTick();
                    tracer.AnnotateSpan(labels);
                }
            }

            TraceProto trace = TraceEntryPolling.Default.GetTrace(rootSpanName, _startTime);

            TraceEntryVerifiers.AssertSingleSpan(trace, rootSpanName);
        }
        public void Trace_MultipleSpans()
        {
            string rootSpanName = EntryData.GetMessage(nameof(Trace_MultipleSpans), _testId);
            var    labels       = new Dictionary <string, string>
            {
                { "annotation-key", "annotation-value" }
            };

            var tracer = CreateSimpleManagedTracer(_grpcConsumer);

            using (tracer.StartSpan(rootSpanName))
            {
                BlockUntilClockTick();
                using (tracer.StartSpan("child-one"))
                {
                    tracer.SetStackTrace(TraceEntryData.CreateStackTrace());
                    BlockUntilClockTick();
                }
                using (tracer.StartSpan("child-two"))
                {
                    BlockUntilClockTick();
                    using (tracer.StartSpan("grandchild-one", StartSpanOptions.Create(SpanKind.RpcClient)))
                    {
                        BlockUntilClockTick();
                    }
                    using (tracer.StartSpan("grandchild-two"))
                    {
                        BlockUntilClockTick();
                        tracer.AnnotateSpan(labels);
                    }
                }
            }

            TraceProto trace = TraceEntryPolling.Default.GetTrace(rootSpanName, _startTime);

            Assert.NotNull(trace);
            Assert.Equal(5, trace.Spans.Count);

            TraceSpan root          = trace.Spans.First(s => s.Name.Equals(rootSpanName));
            TraceSpan childOne      = trace.Spans.First(s => s.Name.Equals("child-one"));
            TraceSpan childTwo      = trace.Spans.First(s => s.Name.Equals("child-two"));
            TraceSpan grandchildOne = trace.Spans.First(s => s.Name.Equals("grandchild-one"));
            TraceSpan grandchildTwo = trace.Spans.First(s => s.Name.Equals("grandchild-two"));

            Assert.Equal(root.SpanId, childOne.ParentSpanId);
            TraceEntryVerifiers.AssertContainsStackTrace(childOne,
                                                         nameof(TraceEntryData.CreateStackTrace), nameof(SimpleManagedTracerTest));

            Assert.Equal(root.SpanId, childTwo.ParentSpanId);

            Assert.Equal(childTwo.SpanId, grandchildOne.ParentSpanId);
            Assert.Equal(TraceSpan.Types.SpanKind.RpcClient, grandchildOne.Kind);

            Assert.Equal(childTwo.SpanId, grandchildTwo.ParentSpanId);
            Assert.Equal(TraceSpan.Types.SpanKind.Unspecified, grandchildTwo.Kind);
            TraceEntryVerifiers.AssertSpanLabelsExact(grandchildTwo, labels);
        }
        public void Trace_Simple()
        {
            string rootSpanName = EntryData.GetMessage(nameof(Trace_Simple), _testId);

            using (CreateSimpleManagedTracer(_grpcConsumer).StartSpan(rootSpanName))
            {
                BlockUntilClockTick();
            }

            TraceProto trace = TraceEntryPolling.Default.GetTrace(rootSpanName, _startTime);

            TraceEntryVerifiers.AssertSingleSpan(trace, rootSpanName);
        }
        public async Task TraceOutGoing()
        {
            string googleUri = "https://google.com/";
            var    spanName  = EntryData.GetMessage(nameof(TraceOutGoing), _testId);

            await TraceOutGoingRequest(spanName, googleUri, false);

            var trace = TraceEntryPolling.Default.GetTrace(spanName, _startTime);

            TraceEntryVerifiers.AssertParentChildSpan(trace, spanName, googleUri);
            TraceEntryVerifiers.AssertSpanLabelsExact(
                trace.Spans.First(s => s.Name == googleUri), TraceEntryData.HttpGetSuccessLabels);
        }
        public async Task TraceOutGoing_HttpError()
        {
            string fakeUri  = "https://google.com/404";
            var    spanName = EntryData.GetMessage(nameof(TraceOutGoing_HttpError), _testId);

            await TraceOutGoingRequest(spanName, fakeUri, false);

            var trace = TraceEntryPolling.Default.GetTrace(spanName, _startTime);

            TraceEntryVerifiers.AssertParentChildSpan(trace, spanName, fakeUri);
            TraceEntryVerifiers.AssertSpanLabelsExact(trace.Spans.Where(s => s.Name == fakeUri).Single(),
                                                      new Dictionary <string, string>
            {
                { TraceLabels.HttpMethod, "GET" },
                { TraceLabels.HttpStatusCode, "404" }
            });
        }
        public void Trace_SimpleStacktrace()
        {
            string rootSpanName = EntryData.GetMessage(nameof(Trace_SimpleStacktrace), _testId);

            var tracer = CreateSimpleManagedTracer(_grpcConsumer);

            using (tracer.StartSpan(rootSpanName))
            {
                BlockUntilClockTick();
                tracer.SetStackTrace(TraceEntryData.CreateStackTrace());
            }

            TraceProto trace = TraceEntryPolling.Default.GetTrace(rootSpanName, _startTime);

            TraceEntryVerifiers.AssertSingleSpan(trace, rootSpanName);
            TraceEntryVerifiers.AssertContainsStackTrace(trace.Spans[0],
                                                         nameof(TraceEntryData.CreateStackTrace), nameof(SimpleManagedTracerTest));
        }
        public async Task TraceOutGoing_Exception()
        {
            string fakeUri  = "http://www.thiscannotpossiblyexist934719238.com/";
            var    spanName = EntryData.GetMessage(nameof(TraceOutGoing_Exception), _testId);

            await TraceOutGoingRequest(spanName, fakeUri, true);

            var trace = TraceEntryPolling.Default.GetTrace(spanName, _startTime);

            TraceEntryVerifiers.AssertParentChildSpan(trace, spanName, fakeUri);
            var span = trace.Spans.Where(s => s.Name == fakeUri).Single();

            TraceEntryVerifiers.AssertSpanLabelsContains(span,
                                                         new Dictionary <string, string>
            {
                { TraceLabels.HttpMethod, "GET" },
            });
            TraceEntryVerifiers.AssertContainsStackTrace(span);
        }
        public void Trace_SimpleAnnotation()
        {
            string rootSpanName = EntryData.GetMessage(nameof(Trace_SimpleAnnotation), _testId);
            var    labels       = new Dictionary <string, string>
            {
                { "annotation-key", "annotation-value" },
                { "some-key", "some-value" }
            };

            var tracer = CreateSimpleManagedTracer(_grpcConsumer);

            using (tracer.StartSpan(rootSpanName))
            {
                BlockUntilClockTick();
                tracer.AnnotateSpan(labels);
            }

            TraceProto trace = TraceEntryPolling.Default.GetTrace(rootSpanName, _startTime);

            TraceEntryVerifiers.AssertSingleSpan(trace, rootSpanName);
            TraceEntryVerifiers.AssertSpanLabelsExact(trace.Spans.First(), labels);
        }