Skip to content

Commit

Permalink
add the quotetext endpoint that makes an http request to test nested …
Browse files Browse the repository at this point in the history
…traces
  • Loading branch information
rgl committed Jul 9, 2024
1 parent 34873c1 commit b087305
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 1 deletion.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"otlp",
"Petroski",
"Pravin",
"quotetext",
"Sipley",
"Skroob",
"Spaceballs",
Expand Down
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: <version>-<trace-id>-<parent-id-aka-span-id>-<trace-flags>
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: <version>-<trace-id>-<parent-id-aka-span-id>-<trace-flags>
http \
--verbose \
http://localhost:8000/quotetext \
traceparent:00-20000000000000000000000000000000-1000000000000000-01 \
tracestate:x.client.state=example

# open aspire dashboard (metrics/traces/logs).
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions quotes/Controllers/QuoteTextController.cs
Original file line number Diff line number Diff line change
@@ -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<QuoteController> _logger;
private readonly IHttpClientFactory _httpClientFactory;

public QuoteTextController(ILogger<QuoteController> logger, IHttpClientFactory httpClientFactory)
{
_logger = logger;
_httpClientFactory = httpClientFactory;
}

[HttpGet(Name = "GetQuoteText")]
public async Task<string> GetQuoteText([FromQuery] string? opsi)
{
_logger.LogInformation("At GetQuoteText");

var activity = HttpContext.Features.Get<IHttpActivityFeature>()?.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<Quote>(requestUrl);

if (quote == null)
{
throw new ApplicationException("failed to get quote");
}

return $"{quote.Text} -- {quote.Author}";
}
}
12 changes: 12 additions & 0 deletions quotes/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -32,6 +43,7 @@
.AddAspNetCoreInstrumentation()
.AddOtlpExporter())
.WithTracing(tracing => tracing
.AddHttpClientInstrumentation()
.AddAspNetCoreInstrumentation()
.AddOtlpExporter()
.AddConsoleExporter());
Expand Down
1 change: 1 addition & 0 deletions quotes/Quotes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.ResourceDetectors.Container" Version="1.0.0-beta.7" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
Expand Down

0 comments on commit b087305

Please sign in to comment.