-
Notifications
You must be signed in to change notification settings - Fork 250
/
Copy pathProgram.cs
108 lines (84 loc) · 3.79 KB
/
Program.cs
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
using Steeltoe.Common;
using Steeltoe.Configuration.CloudFoundry;
using Steeltoe.Management.Endpoint;
using Steeltoe.Management.Endpoint.Actuators.All;
using Steeltoe.Management.Endpoint.SpringBootAdminClient;
using Steeltoe.Management.Prometheus;
using Steeltoe.Management.Tracing;
using Steeltoe.Samples.ActuatorWeb;
using Steeltoe.Samples.ActuatorWeb.CustomActuators.LocalTime;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
// Steeltoe: Add Cloud Foundry configuration provider and options classes.
builder.AddCloudFoundryConfiguration();
// Steeltoe: Add all actuators.
builder.Services.AddAllActuators();
// Steeltoe: Add Prometheus and specify an authorization policy for use outside Cloud Foundry.
if (!Platform.IsCloudFoundry)
{
builder.Services.ConfigureActuatorAuth();
builder.Services.ConfigureActuatorEndpoints(configureEndpoints =>
{
configureEndpoints.RequireAuthorization("actuator.read");
});
// Steeltoe: Map Steeltoe-configured OpenTelemetry Prometheus exporter with authorization middleware in the pipeline.
builder.Services.AddPrometheusActuator(true, configurePrometheusPipeline =>
{
configurePrometheusPipeline.UseAuthorization();
});
}
else
{
// Steeltoe: Map Steeltoe-configured OpenTelemetry Prometheus exporter.
builder.Services.AddPrometheusActuator();
}
// Steeltoe: Add custom actuator that displays the local server time.
// See appsettings.Development.json for how the response format can be configured.
builder.Services.AddLocalTimeActuator();
// Steeltoe: Register with Spring Boot Admin.
if (builder.Configuration.GetValue<bool>("UseSpringBootAdmin"))
{
builder.Services.AddSpringBootAdminClient();
}
// Steeltoe: Ensure log entries include "[app-name, traceId, spanId]" for log correlation.
builder.Services.AddTracingLogProcessor();
// Steeltoe: Configure OpenTelemetry app instrumentation and export of metrics and traces.
builder.Services.ConfigureOpenTelemetry(builder.Configuration);
// Steeltoe: Register typed HttpClient for communicating with the backend service.
builder.Services.AddHttpClient<ActuatorApiClient>(SetBaseAddress);
WebApplication app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
// Steeltoe: HTTPS redirection affects requests to Actuators, which are listening on HTTP.
// See the note in the README regarding HTTPS and Basic authentication for a more complete explanation.
// app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
// This code is used to limit complexity in the sample. A real application should use Service Discovery.
// To learn more about service discovery, review the documentation at: https://docs.steeltoe.io/api/v4/discovery/
static void SetBaseAddress(IServiceProvider serviceProvider, HttpClient client)
{
var instanceInfo = serviceProvider.GetRequiredService<IApplicationInstanceInfo>();
if (instanceInfo is CloudFoundryApplicationOptions { Uris.Count: > 0 } options)
{
string address = options.Uris.First();
string baseAddress = address.Replace("actuator-web", "actuator-api");
client.BaseAddress = new Uri($"https://{baseAddress}");
}
else
{
client.BaseAddress = new Uri("http://localhost:5140");
}
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
ILogger<Program> logger = loggerFactory.CreateLogger<Program>();
logger.LogInformation("HttpClient BaseAddress set to {BaseAddress}", client.BaseAddress);
}