From b087305310c63f9d719a8f4c71722ba523193fb5 Mon Sep 17 00:00:00 2001 From: Rui Lopes Date: Tue, 9 Jul 2024 18:50:18 +0100 Subject: [PATCH] add the quotetext endpoint that makes an http request to test nested traces --- .vscode/settings.json | 1 + README.md | 24 ++++++++++++- docker-compose.yml | 1 + quotes/Controllers/QuoteTextController.cs | 43 +++++++++++++++++++++++ quotes/Program.cs | 12 +++++++ quotes/Quotes.csproj | 1 + 6 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 quotes/Controllers/QuoteTextController.cs diff --git a/.vscode/settings.json b/.vscode/settings.json index 40c567b..a3fe4a8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,6 +10,7 @@ "otlp", "Petroski", "Pravin", + "quotetext", "Sipley", "Skroob", "Spaceballs", diff --git a/README.md b/README.md index ee51846..2f7630b 100644 --- a/README.md +++ b/README.md @@ -45,10 +45,32 @@ http \ # make a request that includes a parent trace. # NB the dotnet trace id will be set to the traceparent trace id. # NB the tracestate does not seem to be stored or propagated anywhere. +# NB traceparent syntax: --- http \ --verbose \ http://localhost:8000/quote \ - traceparent:00-f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1-2f2f2f2f2f2f2f2f-01 \ + traceparent:00-10000000000000000000000000000000-1000000000000000-01 \ + tracestate:x.client.state=example + +# make a request to quotetext, which in turn, makes a nested request to quote. +http \ + --verbose \ + http://localhost:8000/quotetext + +# make a failing request to quotetext, which in turn, makes a nested failing +# request to quote. +http \ + --verbose \ + http://localhost:8000/quotetext?opsi=opsi + +# make a request that includes a parent trace. +# NB the dotnet trace id will be set to the traceparent trace id. +# NB the tracestate does not seem to be stored or propagated anywhere. +# NB traceparent syntax: --- +http \ + --verbose \ + http://localhost:8000/quotetext \ + traceparent:00-20000000000000000000000000000000-1000000000000000-01 \ tracestate:x.client.state=example # open aspire dashboard (metrics/traces/logs). diff --git a/docker-compose.yml b/docker-compose.yml index cc9c7da..f0e442c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -22,6 +22,7 @@ services: - OTEL_EXPORTER_OTLP_ENDPOINT=http://aspire-dashboard:18889 - OTEL_EXPORTER_OTLP_PROTOCOL=grpc - ASPNETCORE_URLS=http://+:8000 + - QUOTES_BASE_URL=http://quotes:8000 ports: # http api. # http://localhost:8000 diff --git a/quotes/Controllers/QuoteTextController.cs b/quotes/Controllers/QuoteTextController.cs new file mode 100644 index 0000000..22431df --- /dev/null +++ b/quotes/Controllers/QuoteTextController.cs @@ -0,0 +1,43 @@ +namespace Quotes.Controllers; + +using Microsoft.AspNetCore.Http.Features; +using Microsoft.AspNetCore.Mvc; + +[ApiController] +[Route("[controller]")] +public class QuoteTextController : ControllerBase +{ + private readonly ILogger _logger; + private readonly IHttpClientFactory _httpClientFactory; + + public QuoteTextController(ILogger logger, IHttpClientFactory httpClientFactory) + { + _logger = logger; + _httpClientFactory = httpClientFactory; + } + + [HttpGet(Name = "GetQuoteText")] + public async Task GetQuoteText([FromQuery] string? opsi) + { + _logger.LogInformation("At GetQuoteText"); + + var activity = HttpContext.Features.Get()?.Activity; + + activity?.SetTag("x.foo", "bar"); + + _logger.LogInformation("Current Activity Id={activityId} TraceId={traceId} SpanId={spanId}", activity?.Id, activity?.TraceId, activity?.SpanId); + + var requestUrl = string.IsNullOrEmpty(opsi) ? "quote" : $"quote?opsi={Uri.EscapeDataString(opsi)}"; + + using var client = _httpClientFactory.CreateClient("quotes"); + + var quote = await client.GetFromJsonAsync(requestUrl); + + if (quote == null) + { + throw new ApplicationException("failed to get quote"); + } + + return $"{quote.Text} -- {quote.Author}"; + } +} diff --git a/quotes/Program.cs b/quotes/Program.cs index d998456..bc15f18 100644 --- a/quotes/Program.cs +++ b/quotes/Program.cs @@ -11,6 +11,17 @@ builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +builder.Services.AddHttpClient( + "quotes", + client => + { + var quotesBaseUrl = Environment.GetEnvironmentVariable("QUOTES_BASE_URL"); + if (string.IsNullOrEmpty(quotesBaseUrl)) + { + throw new ApplicationException("the QUOTES_BASE_URL environment variable must be defined"); + } + client.BaseAddress = new Uri(quotesBaseUrl); + }); // configure telemetry. if (Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT") != null) @@ -32,6 +43,7 @@ .AddAspNetCoreInstrumentation() .AddOtlpExporter()) .WithTracing(tracing => tracing + .AddHttpClientInstrumentation() .AddAspNetCoreInstrumentation() .AddOtlpExporter() .AddConsoleExporter()); diff --git a/quotes/Quotes.csproj b/quotes/Quotes.csproj index 792ceb7..b5f3aa9 100644 --- a/quotes/Quotes.csproj +++ b/quotes/Quotes.csproj @@ -14,6 +14,7 @@ +