1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
using System.Text.Json.Serialization;
using Enmarcha.Backend;
using Enmarcha.Backend.Configuration;
using Enmarcha.Backend.Services;
using Enmarcha.Backend.Services.Geocoding;
using Enmarcha.Backend.Services.Processors;
using Enmarcha.Backend.Services.Providers;
using Microsoft.AspNetCore.WebUtilities;
using OpenTelemetry.Logs;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<AppConfiguration>(builder.Configuration.GetSection("App"));
var appConfig = builder.Configuration.GetSection("App").Get<AppConfiguration>();
var otelConfig = appConfig?.OpenTelemetry;
builder.Logging.AddOpenTelemetry(options =>
{
options.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("Enmarcha.Backend"));
options.IncludeFormattedMessage = true;
options.IncludeScopes = true;
if (otelConfig?.Endpoint != null)
{
options.AddOtlpExporter(exporterOptions =>
{
exporterOptions.Endpoint = new Uri(otelConfig.Endpoint);
exporterOptions.Headers = otelConfig.Headers;
});
}
#if DEBUG
options.AddOtlpExporter(exporterOptions =>
{
exporterOptions.Endpoint = new Uri("http://localhost:17011");
});
#endif
});
builder.Services.AddOpenTelemetry()
.WithTracing(tracing =>
{
tracing
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("Enmarcha.Backend"))
.AddSource(Telemetry.Source.Name)
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation(options =>
{
options.EnrichWithHttpRequestMessage = (activity, req) =>
{
var host = req.RequestUri?.Host;
if (host == null) return;
// Set default peer service to host
activity.SetTag("peer.service", host);
activity.SetTag("server.address", host);
if (host == "api.geoapify.com")
{
activity.SetTag("peer.service", "Geoapify");
var query = QueryHelpers.ParseQuery(req.RequestUri!.Query);
if (query.ContainsKey("apiKey"))
{
var uriBuilder = new UriBuilder(req.RequestUri);
var newQuery = query.ToDictionary(x => x.Key, x => x.Value.ToString());
newQuery["apiKey"] = "REDACTED";
uriBuilder.Query = string.Join("&", newQuery.Select(x => $"{x.Key}={x.Value}"));
activity.SetTag("http.url", uriBuilder.ToString());
}
}
else if (host.Contains("tussa.org"))
{
activity.SetTag("peer.service", "TUSSA");
}
else if (host.Contains("itranvias.com"))
{
activity.SetTag("peer.service", "Tranvías Coruña");
}
else if (host.Contains("vigo.org"))
{
activity.SetTag("peer.service", "Vitrasa");
}
else if (appConfig?.OpenTripPlannerBaseUrl != null && req.RequestUri!.ToString().StartsWith(appConfig.OpenTripPlannerBaseUrl))
{
activity.SetTag("peer.service", "OpenTripPlanner");
}
};
})
.SetSampler(new TraceIdRatioBasedSampler(0.75));
if (otelConfig?.Endpoint != null)
{
tracing.AddOtlpExporter(exporterOptions =>
{
exporterOptions.Endpoint = new Uri(otelConfig.Endpoint);
exporterOptions.Headers = otelConfig.Headers;
});
}
#if DEBUG
tracing.AddOtlpExporter(exporterOptions =>
{
exporterOptions.Endpoint = new Uri("http://localhost:17011");
});
#endif
});
builder.Services
.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
builder.Services.AddHttpClient();
builder.Services.AddMemoryCache();
builder.Services.AddSingleton<XuntaFareProvider>();
builder.Services.AddSingleton<ShapeTraversalService>();
builder.Services.AddSingleton<FeedService>();
builder.Services.AddSingleton<FareService>();
builder.Services.AddScoped<IArrivalsProcessor, VitrasaRealTimeProcessor>();
builder.Services.AddScoped<IArrivalsProcessor, CorunaRealTimeProcessor>();
builder.Services.AddScoped<IArrivalsProcessor, SantiagoRealTimeProcessor>();
builder.Services.AddScoped<IArrivalsProcessor, CtagShuttleRealTimeProcessor>();
builder.Services.AddScoped<IArrivalsProcessor, VigoUsageProcessor>();
builder.Services.AddScoped<IArrivalsProcessor, FilterAndSortProcessor>();
builder.Services.AddScoped<IArrivalsProcessor, NextStopsProcessor>();
builder.Services.AddScoped<IArrivalsProcessor, MarqueeProcessor>();
builder.Services.AddScoped<IArrivalsProcessor, ShapeProcessor>();
builder.Services.AddScoped<IArrivalsProcessor, FeedConfigProcessor>();
builder.Services.AddScoped<ArrivalsPipeline>();
// builder.Services.AddKeyedScoped<IGeocodingService, NominatimGeocodingService>("Nominatim");
builder.Services.AddHttpClient<IGeocodingService, GeoapifyGeocodingService>();
builder.Services.AddHttpClient<OtpService>();
builder.Services.AddHttpClient<Enmarcha.Sources.TranviasCoruna.CorunaRealtimeEstimatesProvider>();
builder.Services.AddHttpClient<Enmarcha.Sources.Tussa.SantiagoRealtimeEstimatesProvider>();
builder.Services.AddHttpClient<Enmarcha.Sources.CtagShuttle.CtagShuttleRealtimeEstimatesProvider>();
builder.Services.AddHttpClient<Costasdev.VigoTransitApi.VigoTransitApiClient>();
var app = builder.Build();
app.Use(async (context, next) =>
{
if (context.Request.Headers.TryGetValue("X-Session-Id", out var sessionId))
{
System.Diagnostics.Activity.Current?.SetTag("session.id", sessionId.ToString());
}
await next();
});
app.MapControllers();
app.Run();
|