Ejemplo n.º 1
0
        public async Task Logging_Trace_FromHeader_Implicit()
        {
            string traceId = s_traceIdFactory.NextId();
            ulong  spanId  = s_spanIdFactory.NextId();
            string testId  = IdGenerator.FromGuid();

            using (var server = GetTestServer <NoBufferWarningLoggerTestApplication>())
                using (var client = server.CreateClient())
                {
                    client.DefaultRequestHeaders.Add(TraceHeaderContext.TraceHeader,
                                                     TraceHeaderContext.Create(traceId, spanId, true).ToString());
                    await client.GetAsync($"/Main/Critical/{testId}");
                }

            _fixture.AddValidator(testId, results =>
            {
                // We only have one log entry.
                LogEntry entry = Assert.Single(results);

                // And the resource name of the trace associated to it points to the trace
                // we specified on the header.
                Assert.Contains(TestEnvironment.GetTestProjectId(), entry.Trace);
                Assert.Contains(traceId, entry.Trace);

                // Let's get our trace.
                var trace = s_tracePolling.GetTrace(traceId);
                Assert.NotNull(trace);

                // The span associated to our entry needs to be part of that trace.
                // (We created this span on the middleware to encompass the whole request)
                var entrySpan = Assert.Single(trace.Spans, s => EntryData.SpanIdToHex(s.SpanId) == entry.SpanId);
                // And its parent needs to be the span specified in the header
                Assert.Equal(spanId, entrySpan.ParentSpanId);
            });
        }
Ejemplo n.º 2
0
        public async Task Trace_Header()
        {
            string traceId = _traceIdFactory.NextId();
            ulong  spanId  = _spanIdFactory.NextId();

            var uri           = $"/Trace/{nameof(TraceController.Trace)}/{_testId}";
            var childSpanName = EntryData.GetMessage(nameof(TraceController.Trace), _testId);

            using (var server = new TestServer(new WebHostBuilder().UseStartup <TraceTestNoBufferLowQpsApplication>()))
                using (var client = server.CreateClient())
                {
                    var header = TraceHeaderContext.Create(traceId, spanId, shouldTrace: true);
                    client.DefaultRequestHeaders.Add(TraceHeaderContext.TraceHeader, header.ToString());

                    var response = await client.GetAsync(uri);

                    var trace = _polling.GetTrace(uri, _startTime);

                    TraceEntryVerifiers.AssertParentChildSpan(trace, uri, childSpanName);

                    Assert.Equal(traceId, trace.TraceId);
                    var parentSpan = trace.Spans.First(s => s.Name == uri);
                    Assert.Equal(spanId, parentSpan.ParentSpanId);

                    Assert.True(response.Headers.Contains(TraceHeaderContext.TraceHeader));
                    var returnedHeader = response.Headers.GetValues(TraceHeaderContext.TraceHeader).Single();
                    var headerContext  = TraceHeaderContext.FromHeader(returnedHeader);
                    Assert.Equal(traceId, headerContext.TraceId);
                    Assert.Equal(spanId, headerContext.SpanId);
                    Assert.True(headerContext.ShouldTrace);
                }
        }
Ejemplo n.º 3
0
        public async Task Trace_Header(
            Action <IWebHostBuilder> testServerConfigurator,
            Action <HttpRequestHeaders, string, ulong?, bool?> traceContextEmitter,
            Action <HttpResponseMessage, string, ulong?, bool?> assertTraceContext)
        {
            string traceId = _traceIdFactory.NextId();
            ulong  spanId  = _spanIdFactory.NextId();

            var uri           = $"/Trace/{nameof(TraceController.Trace)}/{_testId}";
            var childSpanName = EntryData.GetMessage(nameof(TraceController.Trace), _testId);

            using var server = GetTestServer <TraceTestNoBufferLowQpsApplication>(testServerConfigurator);
            using var client = server.CreateClient();
            traceContextEmitter(client.DefaultRequestHeaders, traceId, spanId, true);

            var response = await client.GetAsync(uri);

            var trace = _polling.GetTrace(uri, _startTime);

            TraceEntryVerifiers.AssertParentChildSpan(trace, uri, childSpanName);

            Assert.Equal(traceId, trace.TraceId);
            var parentSpan = trace.Spans.First(s => s.Name == uri);

            Assert.Equal(spanId, parentSpan.ParentSpanId);

            assertTraceContext(response, traceId, spanId, true);
        }