public string Health([FromServices] IManagedTracer tracer) { string message = "/_ah/health"; using (tracer.StartSpan(message)) { Thread.Sleep(10); } return(message); }
public async Task <string> CreateFirstSpan(string id) { string createNoSpanUri = $"/Propagation/{nameof(CreateNoSpan)}/{id}"; string createSecondSpanUri = $"/Propagation/{nameof(CreatesSecondSpan)}/{id}"; string message = EntryData.GetMessage(nameof(CreateFirstSpan), id); // This will guarantee that our requests are to the first server instead // of to a published app in localhost. using (_propagatingHandler.InnerHandler = FirstCallServer.CreateHandler()) using (var client = new HttpClient(_propagatingHandler, false)) using (_tracer.StartSpan(message)) { client.BaseAddress = new Uri("http://localhost"); await client.GetAsync(createNoSpanUri); await client.GetAsync(createSecondSpanUri); } return(message); }
/// <summary>Traces a 10ms sleep.</summary> public string Trace(string id, [FromServices] IManagedTracer tracer) { string message = EntryData.GetMessage(nameof(Trace), id); using (tracer.StartSpan(message)) { Thread.Sleep(10); } return(message); }
/// <summary>Traces a 10ms sleep and throws an exception.</summary> public string ThrowException(string id, [FromServices] IManagedTracer tracer) { // Add a span with the test id to allow for the trace to be found. string message = GetMessage(nameof(ThrowException), id); tracer.StartSpan(message); Thread.Sleep(10); tracer.EndSpan(); throw new DivideByZeroException(); }
/// <summary>Traces a 10ms sleep and adds a stacktrace.</summary> public string TraceStackTrace(string id, [FromServices] IManagedTracer tracer) { string message = GetMessage(nameof(TraceStackTrace), id); tracer.StartSpan(message); Thread.Sleep(10); tracer.SetStackTrace(CreateStackTrace()); tracer.EndSpan(); return(message); }
// This incoming HTTP request should be captured by Trace. public IActionResult Index([FromServices] IManagedTracer tracer) { using (tracer.StartSpan(nameof(Index))) { var traceHeaderHandler = new TraceHeaderPropagatingHandler(() => tracer); var response = TraceOutgoing(traceHeaderHandler); ViewData["text"] = response.Result.ToString(); return(View()); } }
public async Task <string> TraceOutgoing(string id, [FromServices] IManagedTracer tracer, [FromServices] TraceHeaderPropagatingHandler propagatingHandler) { string message = EntryData.GetMessage(nameof(TraceOutgoing), id); using (tracer.StartSpan(message)) using (var httpClient = new HttpClient(propagatingHandler)) { await httpClient.GetAsync("https://google.com/"); } return(message); }
public async Task <string> TraceOutgoingClientFactory(string id, [FromServices] IManagedTracer tracer, [FromServices] IHttpClientFactory httpClientFactory) { string message = EntryData.GetMessage(nameof(TraceOutgoingClientFactory), id); using (tracer.StartSpan(message)) { var httpClient = httpClientFactory.CreateClient("google"); await httpClient.GetAsync(""); } return(message); }
/// <summary>Traces a 10ms sleep and adds an annotation.</summary> public string TraceLabels(string id, [FromServices] IManagedTracer tracer) { string message = GetMessage(nameof(TraceLabels), id); tracer.StartSpan(message); Thread.Sleep(10); tracer.AnnotateSpan(new Dictionary <string, string> { { Label, LabelValue } }); tracer.EndSpan(); return(message); }
/// <summary> /// Handles received HTTP request. For details of the expected request, /// please see Dialogflow fulfillment doc: https://dialogflow.com/docs/fulfillment /// </summary> /// <param name="httpRequest">HTTP request</param> /// <returns>Webhook response</returns> public async Task <WebhookResponse> HandleRequest(HttpRequest httpRequest) { using (_tracer.StartSpan(nameof(DialogflowApp))) { WebhookRequest request; using (var reader = new StreamReader(httpRequest.Body)) { request = jsonParser.Parse <WebhookRequest>(reader); } _logger.LogInformation($"Intent: '{request.QueryResult.Intent.DisplayName}', QueryText: '{request.QueryResult.QueryText}'"); var conversation = GetOrCreateConversation(request.Session); using (_tracer.StartSpan("Conversation")) { return(await conversation.HandleAsync(request)); } } }
/// <summary>Traces a 10ms sleep and adds an annotation.</summary> public string TraceLabels(string id, [FromServices] IManagedTracer tracer) { string message = EntryData.GetMessage(nameof(TraceLabels), id); using (tracer.StartSpan(message)) { Thread.Sleep(10); tracer.AnnotateSpan(new Dictionary <string, string> { { TraceEntryData.TraceLabel, TraceEntryData.TraceLabelValue } }); return(message); } }
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(); } } }
/// <summary> /// Creates a <see cref="TraceHeaderPropagatingHandler"/> and traces the sending of a /// GET request to the given Uri. The trace is wrapped in a parent span. /// </summary> /// <param name="rootSpanName">The name of the root span that will wrap the span /// that traces the request.</param> /// <param name="uri">The Uri to request.</param> /// <param name="exceptionExpected">True if an exception from the request to the Uri is expected.</param> private async Task TraceOutGoingRequest(string rootSpanName, string uri, bool exceptionExpected) { using (_tracer.StartSpan(rootSpanName)) using (var traceHeaderHandler = new TraceHeaderPropagatingHandler(() => _tracer)) using (var httpClient = new HttpClient(traceHeaderHandler)) { try { await httpClient.GetAsync(uri); Assert.False(exceptionExpected); } catch (Exception e) when(exceptionExpected && !(e is Xunit.Sdk.XunitException)) { } } }
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(); } } }
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(); } } }
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(); } } }
/// <summary> /// Handle a DialogFlow conversation request. /// An instance of this class handles just one conversation, /// which is tracked external to this class, using the DialogFlow sessionID. /// </summary> /// <param name="req">Webhook request</param> /// <returns>Webhook response</returns> public async Task <WebhookResponse> HandleAsync(WebhookRequest req) { // Use reflection to find the handler method for the requested DialogFlow intent var intentName = req.QueryResult.Intent.DisplayName; var handler = FindHandler(intentName); if (handler == null) { return(new WebhookResponse { FulfillmentText = $"Sorry, no handler found for intent: {intentName}" }); } try { using (_tracer.StartSpan(intentName)) { // Call the sync handler, if there is one. If not, call the async handler. // Otherwise, it's an error. return(handler.Handle(req) ?? await handler.HandleAsync(req) ?? new WebhookResponse { FulfillmentText = "Error. Handler did not return a valid response." }); } } catch (Exception e) when(req.QueryResult.Intent.DisplayName != "exception.throw") { _exceptionLogger.Log(e); var msg = (e as GoogleApiException)?.Error.Message ?? e.Message; return(new WebhookResponse { FulfillmentText = $"Sorry, there's a problem: {msg}" }); } }
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(); } } }
public async Task <IActionResult> PostAsync( [FromBody] WeatherForecastModel model, CancellationToken cancellationToken) { using (_tracer.StartSpan("save")) { try { //These two lines are for example var traceHeaderHandler = new TraceHeaderPropagatingHandler(() => _tracer); var response = TraceOutgoing(traceHeaderHandler); await _storageLogic.SaveToFileAsync(model, cancellationToken); return(Ok()); } catch (Exception e) { _logger.LogError(e, "Service Error, attention!!!!"); return(StatusCode(StatusCodes.Status500InternalServerError, e.Message)); } } }