예제 #1
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);
                }
        }
예제 #2
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);
        }
        public async Task Traces_Outgoing()
        {
            var uri      = $"/TraceSamples/{nameof(TraceSamplesController.TraceOutgoing)}/{_testId}";
            var response = await _client.GetAsync(uri);

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

            TraceEntryVerifiers.AssertParentChildSpan(trace, uri, "https://weather.com/");
            TraceEntryVerifiers.AssertSpanLabelsContains(
                trace.Spans.First(s => s.Name == uri), TraceEntryData.HttpGetSuccessLabels);
            Assert.False(response.Headers.Contains(TraceHeaderContext.TraceHeader));
        }
        public async Task Traces_Block()
        {
            var uri      = $"/TraceSamples/{nameof(TraceSamplesController.TraceHelloWorld)}/{_testId}";
            var response = await _client.GetAsync(uri);

            var trace = s_tracePolling.GetTrace(uri, Timestamp.FromDateTime(_startTime));

            TraceEntryVerifiers.AssertParentChildSpan(trace, uri, _testId);
            TraceEntryVerifiers.AssertSpanLabelsContains(
                trace.Spans.First(s => s.Name == uri), TraceEntryData.HttpGetSuccessLabels);
            Assert.False(response.Headers.Contains(TraceHeaderContext.TraceHeader));
        }
예제 #5
0
        public async Task Traces_MethodInjection()
        {
            var uri      = $"/TraceSamplesMethod/{nameof(TraceSamplesMethodController.TraceHelloWorld)}/{_testId}";
            var response = await _client.GetAsync(uri);

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

            TraceEntryVerifiers.AssertParentChildSpan(trace, uri, _testId);
            TraceEntryVerifiers.AssertSpanLabelsContains(
                trace.Spans.First(s => s.Name == uri), TraceEntryData.HttpGetSuccessLabels);
            Assert.False(response.Headers.Contains(TraceHeaderContext.TraceHeader));
        }
        public async Task Traces_Outgoing()
        {
            Skip.If(TestEnvironment.IsWindows(), "https://github.com/googleapis/google-cloud-dotnet/issues/5425");
            var uri      = $"/TraceSamples/{nameof(TraceSamplesController.TraceOutgoing)}/{_testId}";
            var response = await _client.GetAsync(uri);

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

            TraceEntryVerifiers.AssertParentChildSpan(trace, uri, "https://weather.com/");
            TraceEntryVerifiers.AssertSpanLabelsContains(
                trace.Spans.First(s => s.Name == uri), TraceEntryData.HttpGetSuccessLabels);
            Assert.False(response.Headers.Contains(TraceHeaderContext.TraceHeader));
        }
예제 #7
0
        public async Task Trace_OutGoing(string methodName)
        {
            var uri              = $"/Trace/{methodName}/{_testId}";
            var childSpanName    = EntryData.GetMessage(methodName, _testId);
            var outgoingSpanName = "https://google.com/";

            using var server = GetTestServer <TraceTestNoBufferHighQpsApplication>();
            using var client = server.CreateClient();
            await client.GetAsync(uri);

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

            TraceEntryVerifiers.AssertParentChildSpan(trace, uri, childSpanName, outgoingSpanName);
        }
예제 #8
0
        public async Task Trace_OutGoingClientFactory(Action <IWebHostBuilder> testServerConfigurator)
        {
            var uri              = $"/Trace/{nameof(TraceController.TraceOutgoingClientFactory)}/{_testId}";
            var childSpanName    = EntryData.GetMessage(nameof(TraceController.TraceOutgoingClientFactory), _testId);
            var outgoingSpanName = "https://google.com/";

            using var server = GetTestServer <TraceTestNoBufferHighQpsApplication>(testServerConfigurator);
            using var client = server.CreateClient();
            await client.GetAsync(uri);

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

            TraceEntryVerifiers.AssertParentChildSpan(trace, uri, childSpanName, outgoingSpanName);
        }
        public async Task Traces_OutgoingClientFactory()
        {
            var uri = $"/TraceSamples/{nameof(TraceSamplesController.TraceOutgoingClientFactory)}/{_testId}";

            using var server = GetTestServer <TraceClientFactoryTestApplication.Startup>();
            using var client = server.CreateClient();
            var response = await client.GetAsync(uri);

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

            TraceEntryVerifiers.AssertParentChildSpan(trace, uri, "http://weather.com/");
            TraceEntryVerifiers.AssertSpanLabelsContains(
                trace.Spans.First(s => s.Name == uri), TraceEntryData.HttpGetSuccessLabels);
            Assert.False(response.Headers.Contains(TraceHeaderContext.TraceHeader));
        }
예제 #10
0
        public async Task Trace_Exception(Action <IWebHostBuilder> testServerConfigurator)
        {
            var uri           = $"/Trace/{nameof(TraceController.ThrowException)}/{_testId}";
            var childSpanName = EntryData.GetMessage(nameof(TraceController.ThrowException), _testId);

            using var server = GetTestServer <TraceTestNoBufferHighQpsApplication>(testServerConfigurator);
            using var client = server.CreateClient();
            await Assert.ThrowsAnyAsync <Exception>(() => client.GetAsync(uri));

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

            TraceEntryVerifiers.AssertParentChildSpan(trace, uri, childSpanName);
            TraceEntryVerifiers.AssertContainsStackTrace(trace.Spans.First(s => s.Name == uri),
                                                         nameof(TraceController), nameof(TraceController.ThrowException));
        }
예제 #11
0
        private static async Task TestTrace(string testId, DateTimeOffset startTime, HttpClient client)
        {
            var uri           = $"/Trace/{nameof(TraceController.Trace)}/{testId}";
            var childSpanName = EntryData.GetMessage(nameof(TraceController.Trace), testId);

            var response = await client.GetAsync(uri);

            var trace = TraceEntryPolling.Default.GetTrace(uri, startTime);

            TraceEntryVerifiers.AssertParentChildSpan(trace, uri, childSpanName);
            TraceEntryVerifiers.AssertSpanLabelsContains(
                trace.Spans.First(s => s.Name == uri), TraceEntryData.HttpGetSuccessLabels);

            Assert.False(response.Headers.Contains(TraceHeaderContext.TraceHeader));
        }
예제 #12
0
        public async Task Trace_OutGoing()
        {
            // We don't test custom trace context with TraceHeaderPropagatingHandler because it's
            // pre IHttpClientFactory and has associated lifetime issues.
            var uri              = $"/Trace/{nameof(TraceController.TraceOutgoing)}/{_testId}";
            var childSpanName    = EntryData.GetMessage(nameof(TraceController.TraceOutgoing), _testId);
            var outgoingSpanName = "https://google.com/";

            using var server = GetTestServer <TraceTestNoBufferHighQpsApplication>();
            using var client = server.CreateClient();
            await client.GetAsync(uri);

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

            TraceEntryVerifiers.AssertParentChildSpan(trace, uri, childSpanName, outgoingSpanName);
        }
예제 #13
0
        public async Task Trace_WebServerHosted_Outgoing()
        {
            var service          = $"/api/Trace/TraceOutgoing/{_testId}";
            var childSpanName    = EntryData.GetMessage("TraceOutgoing", _testId);
            var outgoingSpanName = "https://google.com/";

            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.BaseAddress = PublishedServiceBaseAddress;
                await httpClient.GetAsync(service);
            }

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

            TraceEntryVerifiers.AssertParentChildSpan(trace, service, childSpanName, outgoingSpanName);
        }
예제 #14
0
        public async Task Trace_StackTrace()
        {
            var uri           = $"/Trace/{nameof(TraceController.TraceStackTrace)}/{_testId}";
            var childSpanName = EntryData.GetMessage(nameof(TraceController.TraceStackTrace), _testId);

            using var server = GetTestServer <TraceTestNoBufferHighQpsApplication>();
            using var client = server.CreateClient();
            await client.GetAsync(uri);

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

            TraceEntryVerifiers.AssertParentChildSpan(trace, uri, childSpanName);
            TraceEntryVerifiers.AssertContainsStackTrace(trace.Spans.First(s => s.Name == childSpanName),
                                                         nameof(TraceController), nameof(TraceController.TraceStackTrace),
                                                         nameof(TraceEntryData), nameof(TraceEntryData.CreateStackTrace));
        }
예제 #15
0
        public async Task TraceIncomingAsync()
        {
            IHost host = null;
            // Hides that we use different classes so that we can have multiple CreateHostBuilder methods.
            Func <IHostBuilder> CreateHostBuilder = DefaultHostBuilder.CreateHostBuilder;

            try
            {
                // Sample: Start
                host = CreateHostBuilder().Build();
                await host.StartAsync();

                // End sample

                object request = null;
                // Sample: IncomingContext
                ITraceContext  traceContext  = GetTraceContextFromIncomingRequest(request);
                var            tracerFactory = host.Services.GetRequiredService <Func <ITraceContext, IManagedTracer> >();
                IManagedTracer tracer        = tracerFactory(traceContext);
                ContextTracerManager.SetCurrentTracer(tracer);
                // End sample

                // Let's just start a span with the test ID so we can find it faster.
                // But we don't show this in sample code.
                using (tracer.StartSpan(_testId))
                {
                    // Sample: Trace
                    IManagedTracer currentTracer = host.Services.GetRequiredService <IManagedTracer>();
                    using (currentTracer.StartSpan("testing_tracing"))
                    {
                        Console.WriteLine("Using Cloud Trace from a non ASP.NET Core app");
                    }
                    // End sample
                }

                var trace = TraceEntryPolling.Default.GetTrace(_testId, _startTime);
                TraceEntryVerifiers.AssertParentChildSpan(trace, _testId, "testing_tracing");
                Assert.Equal(traceContext.TraceId, trace.TraceId);
            }
            finally
            {
                if (host is object)
                {
                    await host.StopAsync();
                }
            }
        }
예제 #16
0
        public async Task Trace(Action <IWebHostBuilder> testServerConfigurator)
        {
            var uri           = $"/Trace/{nameof(TraceController.Trace)}/{_testId}";
            var childSpanName = EntryData.GetMessage(nameof(TraceController.Trace), _testId);

            using var server = GetTestServer <TraceTestNoBufferHighQpsApplication>(testServerConfigurator);
            using var client = server.CreateClient();
            var response = await client.GetAsync(uri);

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

            TraceEntryVerifiers.AssertParentChildSpan(trace, uri, childSpanName);
            TraceEntryVerifiers.AssertSpanLabelsContains(
                trace.Spans.First(s => s.Name == uri), TraceEntryData.HttpGetSuccessLabels);

            AssertNoTraceHeaders(response);
        }
        public async Task TraceAsync()
        {
            // Naming it like an instance variable so that it looks like that on sample code.
            IHost _host = null;

            try
            {
                // Sample: Start
                _host = CreateHostBuilder().Build();
                await _host.StartAsync();

                // End sample

                // Sample: IncomingContext
                ITraceContext  traceContext  = GetTraceContextFromIncomingRequest();
                var            tracerFactory = _host.Services.GetRequiredService <Func <ITraceContext, IManagedTracer> >();
                IManagedTracer tracer        = tracerFactory(traceContext);
                ContextTracerManager.SetCurrentTracer(tracer);
                // End sample

                // Let's just start a span with the test ID so we can find it faster.
                // But we don't show this in sample code.
                using (tracer.StartSpan(_testId))
                {
                    // Sample: Trace
                    IManagedTracer currentTracer = _host.Services.GetRequiredService <IManagedTracer>();
                    using (currentTracer.StartSpan("standalone_tracing"))
                    {
                        Console.WriteLine("Using Cloud Trace from a non ASP.NET Core app");
                    }
                    // End sample
                }

                var trace = s_polling.GetTrace(_testId, _startTime);
                TraceEntryVerifiers.AssertParentChildSpan(trace, _testId, "standalone_tracing");
                Assert.Equal(traceContext.TraceId, trace.TraceId);
            }
            finally
            {
                if (_host != null)
                {
                    await _host.StopAsync();
                }
            }
        }
예제 #18
0
        public async Task Trace_Label(Action <IWebHostBuilder> testServerConfigurator)
        {
            var uri           = $"/Trace/{nameof(TraceController.TraceLabels)}/{_testId}";
            var childSpanName = EntryData.GetMessage(nameof(TraceController.TraceLabels), _testId);

            using var server = GetTestServer <TraceTestNoBufferHighQpsApplication>(testServerConfigurator);
            using var client = server.CreateClient();
            await client.GetAsync(uri);

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

            TraceEntryVerifiers.AssertParentChildSpan(trace, uri, childSpanName);
            TraceEntryVerifiers.AssertSpanLabelsContains(trace.Spans.First(s => s.Name == childSpanName),
                                                         new Dictionary <string, string>
            {
                { TraceEntryData.TraceLabel, TraceEntryData.TraceLabelValue }
            });
        }
예제 #19
0
        public async Task Trace_WebServerHosted_StackTrace()
        {
            var service       = $"/api/Trace/TraceStackTrace/{_testId}";
            var childSpanName = EntryData.GetMessage("TraceStackTrace", _testId);

            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.BaseAddress = PublishedServiceBaseAddress;
                await httpClient.GetAsync(service);
            }

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

            TraceEntryVerifiers.AssertParentChildSpan(trace, service, childSpanName);
            TraceEntryVerifiers.AssertContainsStackTrace(trace.Spans.First(s => s.Name == childSpanName),
                                                         "TraceController", "TraceStackTrace",
                                                         nameof(TraceEntryData), nameof(TraceEntryData.CreateStackTrace));
        }
예제 #20
0
        public async Task Trace_DummyNameProvider(Action <IWebHostBuilder> testServerConfigurator)
        {
            var uri = $"/Trace/{nameof(TraceController.Trace)}/{_testId}";
            // our dummy provider prefixes with /Dummy
            var expectedTraceName = $"/Dummy{uri}";
            var childSpanName     = EntryData.GetMessage(nameof(TraceController.Trace), _testId);

            using var server = GetTestServer <TraceTestCustomNameProviderNoBufferHighQpsApplication>(testServerConfigurator);
            using var client = server.CreateClient();
            var response = await client.GetAsync(uri);

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

            TraceEntryVerifiers.AssertParentChildSpan(trace, expectedTraceName, childSpanName);
            TraceEntryVerifiers.AssertSpanLabelsContains(
                trace.Spans.First(s => s.Name == expectedTraceName), TraceEntryData.HttpGetSuccessLabels);

            AssertNoTraceHeaders(response);
        }
예제 #21
0
        public async Task Trace()
        {
            var uri           = $"/Trace/{nameof(TraceController.Trace)}/{_testId}";
            var childSpanName = EntryData.GetMessage(nameof(TraceController.Trace), _testId);

            using (var server = new TestServer(new WebHostBuilder().UseStartup <TraceTestNoBufferHighQpsApplication>()))
                using (var client = server.CreateClient())
                {
                    var response = await client.GetAsync(uri);

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

                    TraceEntryVerifiers.AssertParentChildSpan(trace, uri, childSpanName);
                    TraceEntryVerifiers.AssertSpanLabelsContains(
                        trace.Spans.First(s => s.Name == uri), TraceEntryData.HttpGetSuccessLabels);

                    Assert.False(response.Headers.Contains(TraceHeaderContext.TraceHeader));
                }
        }
        private static async Task TestTrace(string testId, DateTime startTime, HttpClient client)
        {
            var uri           = $"/Trace/{nameof(TraceController.Trace)}/{testId}";
            var childSpanName = EntryData.GetMessage(nameof(TraceController.Trace), testId);

            var response = await client.GetAsync(uri);

            // Give the polling a little extra time to find the trace as
            // trace processing can sometimes take time and the default buffer is a
            // timed buffer.
            var polling = new TraceEntryPolling(TimeSpan.FromSeconds(20));
            var trace   = polling.GetTrace(uri, Timestamp.FromDateTime(startTime));

            TraceEntryVerifiers.AssertParentChildSpan(trace, uri, childSpanName);
            TraceEntryVerifiers.AssertSpanLabelsContains(
                trace.Spans.First(s => s.Name == uri), TraceEntryData.HttpGetSuccessLabels);

            Assert.False(response.Headers.Contains(TraceHeaderContext.TraceHeader));
        }
예제 #23
0
        public async Task Trace_OutGoingClientFactory_WithLabels(Action <IWebHostBuilder> testServerConfigurator)
        {
            var uri              = $"/Trace/{nameof(TraceController.TraceOutgoingClientFactoryWithLabels)}/{_testId}";
            var childSpanName    = EntryData.GetMessage(nameof(TraceController.TraceOutgoingClientFactory), _testId);
            var outgoingSpanName = "https://google.com/";

            using var server = GetTestServer <TraceTestNoBufferHighQpsApplication>(builder =>
            {
                testServerConfigurator?.Invoke(builder);
                builder.ConfigureServices(services => services
                                          .AddHttpClient("outgoing_with_labels", c => c.BaseAddress = new Uri("https://google.com/"))
                                          .AddOutgoingGoogleTraceHandler(sp => _customLabels));
            });
            using var client = server.CreateClient();
            await client.GetAsync(uri);

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

            TraceEntryVerifiers.AssertParentChildSpan(trace, uri, childSpanName, outgoingSpanName);
            TraceEntryVerifiers.AssertSpanLabelsContains(trace.Spans.First(span => span.Name == outgoingSpanName), _customLabels);
        }
예제 #24
0
        public async Task Trace_OutGoing()
        {
            // We don't test custom trace context with TraceHeaderPropagatingHandler because it's
            // pre IHttpClientFactory and has associated lifetime issues.
            var uri              = $"/Trace/{nameof(TraceController.TraceOutgoing)}/{_testId}";
            var childSpanName    = EntryData.GetMessage(nameof(TraceController.TraceOutgoing), _testId);
            var outgoingSpanName = "https://google.com/";

            using var server = GetTestServer <TraceTestNoBufferHighQpsApplication>(
                      builder => builder.ConfigureServices(
                          services => services.TryAddSingleton(
#pragma warning disable CS0618 // Type or member is obsolete
                              new TraceHeaderPropagatingHandler(ContextTracerManager.GetCurrentTracer))));
#pragma warning restore CS0618 // Type or member is obsolete
            using var client = server.CreateClient();
            await client.GetAsync(uri);

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

            TraceEntryVerifiers.AssertParentChildSpan(trace, uri, childSpanName, outgoingSpanName);
        }
예제 #25
0
        public async Task Trace_TraceFallbackPredicate(Action <IWebHostBuilder> testServerConfigurator)
        {
            var tracedId            = $"fallback{_testId}";
            var tracedUri           = $"/Trace/{nameof(TraceController.Trace)}/{tracedId}";
            var tracedChildSpanName = EntryData.GetMessage(nameof(TraceController.Trace), tracedId);
            var nonTracedUri        = $"/Trace/{nameof(TraceController.Trace)}/{_testId}";

            using var server = GetTestServer <TraceTestFallbackPredicate>(testServerConfigurator);
            using var client = server.CreateClient();
            var nonTracedTask = client.GetAsync(nonTracedUri);
            var tracedTask    = client.GetAsync(tracedUri);
            await Task.WhenAll(nonTracedTask, tracedTask);

            Assert.Null(TraceEntryPolling.NoEntry.GetTrace(nonTracedUri, _startTime, expectTrace: false));

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

            TraceEntryVerifiers.AssertParentChildSpan(trace, tracedUri, tracedChildSpanName);
            TraceEntryVerifiers.AssertSpanLabelsContains(
                trace.Spans.First(s => s.Name == tracedUri), TraceEntryData.HttpGetSuccessLabels);
        }
예제 #26
0
        public async Task Trace_WebServerHosted()
        {
            var service       = $"/api/Trace/Trace/{_testId}";
            var childSpanName = EntryData.GetMessage("Trace", _testId);

            HttpResponseMessage response;

            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.BaseAddress = PublishedServiceBaseAddress;
                response = await httpClient.GetAsync(service);
            }

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

            TraceEntryVerifiers.AssertParentChildSpan(trace, service, childSpanName);
            TraceEntryVerifiers.AssertSpanLabelsContains(
                trace.Spans.First(s => s.Name == service), TraceEntryData.HttpGetSuccessLabels);

            Assert.False(response.Headers.Contains(TraceHeaderContext.TraceHeader));
        }
예제 #27
0
        public async Task Trace_WebServerHosted_Label()
        {
            var service       = $"/api/Trace/TraceLabels/{_testId}";
            var childSpanName = EntryData.GetMessage("TraceLabels", _testId);

            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.BaseAddress = PublishedServiceBaseAddress;
                await httpClient.GetAsync(service);
            }

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

            TraceEntryVerifiers.AssertParentChildSpan(trace, service, childSpanName);
            TraceEntryVerifiers.AssertSpanLabelsContains(
                trace.Spans.First(s => s.Name == childSpanName),
                new Dictionary <string, string>
            {
                { TraceEntryData.TraceLabel, TraceEntryData.TraceLabelValue }
            });
        }
예제 #28
0
        public async Task TraceSingleAsync()
        {
            IHost host = null;

            try
            {
                host = TroubleshootingHostBuilder.CreateHostBuilder().Build();
                await host.StartAsync();

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

                // Let's just start a span with the test ID so we can find it faster.
                // But we don't show this in sample code.
                using (tracer.StartSpan(_testId))
                {
                    // Sample: RunIn
                    IManagedTracer currentTracer = host.Services.GetRequiredService <IManagedTracer>();
                    currentTracer.RunInSpan(
                        () => Console.WriteLine("Using Cloud Trace from a non ASP.NET Core app"),
                        "testing_tracing");
                    // End sample
                }

                var trace = TraceEntryPolling.Default.GetTrace(_testId, _startTime);
                TraceEntryVerifiers.AssertParentChildSpan(trace, _testId, "testing_tracing");
            }
            finally
            {
                if (host is object)
                {
                    await host.StopAsync();
                }
            }
        }
예제 #29
0
        public async Task TraceOutgoingAsync()
        {
            IHost host = null;

            try
            {
                host = OutgoingHostBuilder.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);

                // Let's just start a span with the test ID so we can find it faster.
                // But we don't show this in sample code.
                using (tracer.StartSpan(_testId))
                {
                    // Sample: TraceOutgoingClientFactory
                    IHttpClientFactory clientFactory = host.Services.GetRequiredService <IHttpClientFactory>();
                    var httpClient = clientFactory.CreateClient("tracesOutgoing");
                    // Any code that makes outgoing requests.
                    var response = await httpClient.GetAsync("http://weather.com/");

                    // End sample
                }

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

                TraceEntryVerifiers.AssertParentChildSpan(trace, _testId, "http://weather.com/");
            }
            finally
            {
                if (host is object)
                {
                    await host.StopAsync();
                }
            }
        }
예제 #30
0
        public async Task TraceSingleAsync()
        {
            IHost host = null;
            // Hides that we use different classes so that we can have multiple CreateHostBuilder methods.
            Func <IHostBuilder> CreateHostBuilder = DefaultHostBuilder.CreateHostBuilder;

            try
            {
                // Sample: Start
                host = CreateHostBuilder().Build();
                await host.StartAsync();

                // End sample

                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();
                }
            }
        }