aboutsummaryrefslogtreecommitdiff
path: root/src/Enmarcha.Backend/Controllers
diff options
context:
space:
mode:
authorAriel Costas Guerrero <ariel@costas.dev>2026-01-25 21:05:33 +0100
committerAriel Costas Guerrero <ariel@costas.dev>2026-01-25 21:06:01 +0100
commitf9b7af64550be1320acc84d60184e8c8ce873b94 (patch)
treed43e995319b4a3856aa929848b9ad807afb1cf86 /src/Enmarcha.Backend/Controllers
parentc89353dede64bd2c21c0a1ebd6b6de6282998326 (diff)
feat: Add OpenTelemetry instrumentation and configuration for enhanced telemetry tracking
Diffstat (limited to 'src/Enmarcha.Backend/Controllers')
-rw-r--r--src/Enmarcha.Backend/Controllers/ArrivalsController.cs6
-rw-r--r--src/Enmarcha.Backend/Controllers/TileController.cs8
-rw-r--r--src/Enmarcha.Backend/Controllers/TransitController.cs18
3 files changed, 29 insertions, 3 deletions
diff --git a/src/Enmarcha.Backend/Controllers/ArrivalsController.cs b/src/Enmarcha.Backend/Controllers/ArrivalsController.cs
index a23c69c..eb147fc 100644
--- a/src/Enmarcha.Backend/Controllers/ArrivalsController.cs
+++ b/src/Enmarcha.Backend/Controllers/ArrivalsController.cs
@@ -46,6 +46,10 @@ public partial class ArrivalsController : ControllerBase
[FromQuery] bool reduced
)
{
+ using var activity = Telemetry.Source.StartActivity("GetArrivals");
+ activity?.SetTag("stop.id", id);
+ activity?.SetTag("reduced", reduced);
+
var tz = TimeZoneInfo.FindSystemTimeZoneById("Europe/Madrid");
var nowLocal = TimeZoneInfo.ConvertTime(DateTime.UtcNow, tz);
var todayLocal = nowLocal.Date;
@@ -65,12 +69,14 @@ public partial class ArrivalsController : ControllerBase
if (responseBody is not { IsSuccess: true } || responseBody.Data?.Stop == null)
{
+ activity?.SetStatus(System.Diagnostics.ActivityStatusCode.Error, "Error fetching stop data from OTP");
LogErrorFetchingStopData(response.StatusCode, await response.Content.ReadAsStringAsync());
return StatusCode(500, "Error fetching stop data");
}
var stop = responseBody.Data.Stop;
_logger.LogInformation("Fetched {Count} arrivals for stop {StopName} ({StopId})", stop.Arrivals.Count, stop.Name, id);
+ activity?.SetTag("arrivals.count", stop.Arrivals.Count);
List<Arrival> arrivals = [];
foreach (var item in stop.Arrivals)
diff --git a/src/Enmarcha.Backend/Controllers/TileController.cs b/src/Enmarcha.Backend/Controllers/TileController.cs
index 4065ecd..3fdedfb 100644
--- a/src/Enmarcha.Backend/Controllers/TileController.cs
+++ b/src/Enmarcha.Backend/Controllers/TileController.cs
@@ -43,12 +43,19 @@ public class TileController : ControllerBase
[HttpGet("stops/{z:int}/{x:int}/{y:int}")]
public async Task<IActionResult> Stops(int z, int x, int y)
{
+ using var activity = Telemetry.Source.StartActivity("GenerateStopsTile");
+ activity?.SetTag("tile.z", z);
+ activity?.SetTag("tile.x", x);
+ activity?.SetTag("tile.y", y);
+
if (z is < 9 or > 20)
{
return BadRequest("Zoom level out of range (9-20)");
}
var cacheHit = _cache.TryGetValue($"stops-tile-{z}-{x}-{y}", out byte[]? cachedTile);
+ activity?.SetTag("cache.hit", cacheHit);
+
if (cacheHit && cachedTile != null)
{
Response.Headers.Append("X-Cache-Hit", "true");
@@ -78,6 +85,7 @@ public class TileController : ControllerBase
if (responseBody is not { IsSuccess: true })
{
+ activity?.SetStatus(System.Diagnostics.ActivityStatusCode.Error, "Error fetching stop data from OTP");
_logger.LogError(
"Error fetching stop data, received {StatusCode} {ResponseBody}",
response.StatusCode,
diff --git a/src/Enmarcha.Backend/Controllers/TransitController.cs b/src/Enmarcha.Backend/Controllers/TransitController.cs
index 00e5fb7..4853e66 100644
--- a/src/Enmarcha.Backend/Controllers/TransitController.cs
+++ b/src/Enmarcha.Backend/Controllers/TransitController.cs
@@ -38,14 +38,19 @@ public class TransitController : ControllerBase
[HttpGet("routes")]
public async Task<ActionResult<List<RouteDto>>> GetRoutes([FromQuery] string[] feeds)
{
+ using var activity = Telemetry.Source.StartActivity("GetRoutes");
if (feeds.Length == 0)
{
feeds = ["tussa", "vitrasa", "tranvias", "feve"];
}
+ activity?.SetTag("feeds", string.Join(",", feeds));
var serviceDate = DateTime.Now.ToString("yyyy-MM-dd");
var cacheKey = $"routes_{string.Join("_", feeds)}_{serviceDate}";
- if (_cache.TryGetValue(cacheKey, out List<RouteDto>? cachedRoutes))
+ var cacheHit = _cache.TryGetValue(cacheKey, out List<RouteDto>? cachedRoutes);
+ activity?.SetTag("cache.hit", cacheHit);
+
+ if (cacheHit && cachedRoutes != null)
{
return Ok(cachedRoutes);
}
@@ -71,6 +76,7 @@ public class TransitController : ControllerBase
}
catch (Exception e)
{
+ activity?.SetStatus(System.Diagnostics.ActivityStatusCode.Error, e.Message);
_logger.LogError(e, "Error fetching routes");
return StatusCode(500, "An error occurred while fetching routes.");
}
@@ -79,10 +85,16 @@ public class TransitController : ControllerBase
[HttpGet("routes/{id}")]
public async Task<ActionResult<RouteDetailsDto>> GetRouteDetails(string id)
{
+ using var activity = Telemetry.Source.StartActivity("GetRouteDetails");
+ activity?.SetTag("route.id", id);
+
var serviceDate = DateTime.Now.ToString("yyyy-MM-dd");
var cacheKey = $"route_details_{id}_{serviceDate}";
- if (_cache.TryGetValue(cacheKey, out RouteDetailsDto? cachedDetails))
+ var cacheHit = _cache.TryGetValue(cacheKey, out RouteDetailsDto? cachedDetails);
+ activity?.SetTag("cache.hit", cacheHit);
+
+ if (cacheHit && cachedDetails != null)
{
return Ok(cachedDetails);
}
@@ -104,7 +116,7 @@ public class TransitController : ControllerBase
}
catch (Exception e)
{
- _logger.LogError(e, "Error fetching route details for {Id}", id);
+ activity?.SetStatus(System.Diagnostics.ActivityStatusCode.Error, e.Message); _logger.LogError(e, "Error fetching route details for {Id}", id);
return StatusCode(500, "An error occurred while fetching route details.");
}
}