public async Task <ActionResult <string> > Get(string name, CancellationToken ct) { using (IScope scope = _tracer.BuildActiveSpan("Hello-get")) { ISpan span = scope.Span; span.LogMessage("baggage", span.GetBaggageItem("baggage")); // Here you can have TaskCanceledException, which will be handled by ExceptionHandlingMiddleware. await Task.Delay(_random.Next(100, 250), ct); HttpStatusCode selectedStatusCode = _httpStatusCodes[_random.Next(_httpStatusCodes.Length)]; span.LogMessage("name", name); span.Log($"Selected status code: {selectedStatusCode}"); // --> Return OK. if (selectedStatusCode == HttpStatusCode.OK) { return(Ok($"Hello {name}.")); } // --> Delay. if (selectedStatusCode == HttpStatusCode.RequestTimeout) { try { // If your method do not accept token in the argument, you can check it here beforehand. ct.ThrowIfCancellationRequested(); await Task.Delay(5000, ct); } catch (OperationCanceledException) { span.Log("The operation was canceled."); return(NoContent()); } // The timeout policy cancel this call earlier, so you won't see this line. span.Log($"After the delay."); } if (selectedStatusCode == HttpStatusCode.InternalServerError) { throw new Exception("Throw exception for test purpose."); } // --> Other returns. return(new ContentResult { StatusCode = (int)selectedStatusCode, Content = $"Selected status code: {selectedStatusCode}" }); } }
public async Task SayHello(string helloTo) { using (IScope scope = _tracer.BuildActiveSpan("Call-web-app")) // BuildActiveSpan: From common extensions. { ISpan span = scope.Span; span.SetBaggageItem("baggage", "baggage-value"); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, $"Hello/{helloTo}"); request.Headers.InjectTracing( // InjectTracing: From common extensions. request.Method.Method, $"{_httpClient.BaseAddress}{request.RequestUri}"); HttpResponseMessage response = null; try { response = await _httpClient.SendAsync(request); } catch (Exception ex) { span.LogError(ex, "Failed to call say hello."); // LogError: From common extensions. Console.WriteLine(ex.Message); return; } span.SetTag(Tags.HttpStatus, (int)response.StatusCode); string responseString = await response.Content.ReadAsStringAsync(); span.LogMessage("response", responseString); // LogMessage: From common extensions. Console.WriteLine(responseString); } }