File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 1+ using Prometheus ;
2+ using Microsoft . AspNetCore . Diagnostics . HealthChecks ;
3+
14var builder = WebApplication . CreateBuilder ( args ) ;
25
36// Log all environment variables to help with debugging
1316Console . WriteLine ( $ "TokenServiceUrl from config: { builder . Configuration [ "TokenServiceUrl" ] ?? "not set" } ") ;
1417Console . WriteLine ( "==========================" ) ;
1518
19+ // Add Prometheus metrics
20+ builder . Services . AddHealthChecks ( ) ;
21+
1622// Add services to the container.
1723builder . Services . AddRazorPages ( )
1824 . AddMvcOptions ( options =>
8894 app . UseHsts ( ) ;
8995}
9096
97+ // Add Prometheus metrics endpoint
9198app . UseHttpsRedirection ( ) ;
9299app . UseStaticFiles ( ) ;
93100
96103
97104app . 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+
99110app . MapRazorPages ( ) ;
111+ app . MapHealthChecks ( "/health" ) ;
100112
101113app . Run ( ) ;
Original file line number Diff line number Diff line change @@ -3,6 +3,6 @@ kind: ConfigMap
33metadata :
44 name : admin-ui-config
55data :
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
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ kind: Kustomization
44resources :
55- ../../base
66
7- namePrefix : dev-
7+ # namePrefix: dev-
88
99replicas :
1010- name : admin-ui
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 1313using Microsoft . AspNetCore . Authentication . JwtBearer ;
1414using Microsoft . IdentityModel . Tokens ;
1515using Microsoft . OpenApi . Models ;
16+ using Prometheus ;
1617
1718namespace 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():
Original file line number Diff line number Diff line change @@ -2,11 +2,14 @@ apiVersion: v1
22kind : Service
33metadata :
44 name : modularmonolith
5+ labels :
6+ app : modularmonolith # This label is crucial for ServiceMonitor discovery
57spec :
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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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'))
3839k8s_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
4446k8s_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
5153k8s_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
You can’t perform that action at this time.
0 commit comments