Skip to content

Commit af86fe1

Browse files
added healthcheck for the admin ui
1 parent 69e8306 commit af86fe1

10 files changed

Lines changed: 75 additions & 9 deletions

File tree

ECommerce.AdminUI/ECommerce.AdminUI.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
2-
2+
33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
</PropertyGroup>
8-
8+
99
<ItemGroup>
10+
<PackageReference Include="prometheus-net" Version="8.0.1" />
11+
<PackageReference Include="prometheus-net.AspNetCore" Version="8.0.1" />
1012
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.12.0" />
1113
</ItemGroup>
1214

ECommerce.AdminUI/Program.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using Prometheus;
2+
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
3+
14
var builder = WebApplication.CreateBuilder(args);
25

36
// Log all environment variables to help with debugging
@@ -13,6 +16,9 @@
1316
Console.WriteLine($"TokenServiceUrl from config: {builder.Configuration["TokenServiceUrl"] ?? "not set"}");
1417
Console.WriteLine("==========================");
1518

19+
// Add Prometheus metrics
20+
builder.Services.AddHealthChecks();
21+
1622
// Add services to the container.
1723
builder.Services.AddRazorPages()
1824
.AddMvcOptions(options =>
@@ -88,6 +94,7 @@
8894
app.UseHsts();
8995
}
9096

97+
// Add Prometheus metrics endpoint
9198
app.UseHttpsRedirection();
9299
app.UseStaticFiles();
93100

@@ -96,6 +103,11 @@
96103

97104
app.UseAuthorization();
98105

106+
// Configure metrics endpoint before mapping controllers
107+
app.UseMetricServer("/metrics"); // This exposes the metrics endpoint
108+
app.UseHttpMetrics(); // Collect HTTP metrics
109+
99110
app.MapRazorPages();
111+
app.MapHealthChecks("/health");
100112

101113
app.Run();

ECommerce.AdminUI/k8s/overlays/dev/configmap.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ kind: ConfigMap
33
metadata:
44
name: admin-ui-config
55
data:
6-
ModularMonolithApiUrl: "http://dev-modularmonolith:8080/modulith"
7-
TokenServiceUrl: "http://dev-modularmonolith:8080"
6+
ModularMonolithApiUrl: "http://modularmonolith:8080/modulith"
7+
TokenServiceUrl: "http://modularmonolith:8080"
88

ECommerce.AdminUI/k8s/overlays/dev/kustomization.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ kind: Kustomization
44
resources:
55
- ../../base
66

7-
namePrefix: dev-
7+
#namePrefix: dev-
88

99
replicas:
1010
- name: admin-ui
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: monitoring.coreos.com/v1
2+
kind: ServiceMonitor
3+
metadata:
4+
name: admin-ui
5+
labels:
6+
app: admin-ui
7+
release: prometheus # Ensure this matches your Prometheus operator labels
8+
spec:
9+
selector:
10+
matchLabels:
11+
app: admin-ui
12+
endpoints:
13+
- port: http # Changed from targetPort: 8080 to port: http to match the service port name
14+
interval: 30s
15+
path: /metrics
16+
scheme: http
17+
namespaceSelector:
18+
matchNames:
19+
- default

ECommerceApp/ECommerceApp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
</PackageReference>
1717
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
1818
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" />
19+
<PackageReference Include="prometheus-net.AspNetCore" Version="8.2.1" />
1920
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.9.0" />
2021
</ItemGroup>
2122

ECommerceApp/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.AspNetCore.Authentication.JwtBearer;
1414
using Microsoft.IdentityModel.Tokens;
1515
using Microsoft.OpenApi.Models;
16+
using Prometheus;
1617

1718
namespace ECommerceApp;
1819

@@ -81,6 +82,9 @@ public static async Task Main(string[] args)
8182

8283
var app = builder.Build();
8384

85+
// Enable Prometheus metrics
86+
app.UseHttpMetrics();
87+
8488
// Log ASPNETCORE_PATHBASE value at startup
8589
var logger = app.Logger;
8690
var pathBase = app.Configuration["ASPNETCORE_PATHBASE"] ?? Environment.GetEnvironmentVariable("ASPNETCORE_PATHBASE");
@@ -114,6 +118,9 @@ public static async Task Main(string[] args)
114118
});
115119
}
116120

121+
// Expose /metrics endpoint for Prometheus
122+
app.MapMetrics();
123+
117124
await BusinessEventsModule.InitializeDefaultSchemasAsync(app.Services);
118125

119126
// After builder.Build():

ECommerceApp/k8s/service.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ apiVersion: v1
22
kind: Service
33
metadata:
44
name: modularmonolith
5+
labels:
6+
app: modularmonolith # This label is crucial for ServiceMonitor discovery
57
spec:
68
type: ClusterIP
79
selector:
810
app: modularmonolith
911
ports:
10-
- protocol: TCP
12+
- name: http
13+
protocol: TCP
1114
port: 8080
1215
targetPort: 8080
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: monitoring.coreos.com/v1
2+
kind: ServiceMonitor
3+
metadata:
4+
name: modularmonolith
5+
labels:
6+
app: modularmonolith
7+
release: prometheus # Ensure this matches your Prometheus operator labels
8+
spec:
9+
selector:
10+
matchLabels:
11+
app: modularmonolith
12+
endpoints:
13+
- targetPort: 8080
14+
interval: 30s
15+
path: /metrics
16+
scheme: http
17+
namespaceSelector:
18+
matchNames:
19+
- default

Tiltfile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ k8s_yaml([
2929
'ECommerceApp/k8s/service.yaml',
3030
'ECommerce.DatabaseMigrator/k8s/db-connection-sealedsecrets.yaml',
3131
'ECommerce.DatabaseMigrator/k8s/migrate-job.yaml',
32+
'EcommerceApp/k8s/servicemonitor.yaml',
3233
])
3334

3435
# Use kustomize for AdminUI
@@ -38,19 +39,21 @@ k8s_yaml(kustomize('ECommerce.AdminUI/k8s/overlays/dev'))
3839
k8s_resource(
3940
'modularmonolith', # This should match the name in your deployment.yaml
4041
port_forwards=['8080:8080'],
41-
labels=["app"]
42+
labels=["app"],
43+
trigger_mode=TRIGGER_MODE_MANUAL
4244
)
4345

4446
k8s_resource(
45-
'dev-admin-ui', # Updated to match the namePrefix in kustomize
47+
'admin-ui',
4648
port_forwards=['8081:8080'],
4749
labels=["app"],
4850
trigger_mode=TRIGGER_MODE_MANUAL
4951
)
5052

5153
k8s_resource(
5254
'db-migrator', # This should match the name in migrate-job.yaml
53-
labels=["migrations"]
55+
labels=["migrations"],
56+
trigger_mode=TRIGGER_MODE_MANUAL
5457
)
5558

5659
# Enable file watching for live updates

0 commit comments

Comments
 (0)