Skip to content

Commit 17394dd

Browse files
add prometheus integration for node and netcore
1 parent 8bb97ec commit 17394dd

File tree

14 files changed

+910
-27
lines changed

14 files changed

+910
-27
lines changed

prometheus-monitoring/dashboard.json

Lines changed: 758 additions & 0 deletions
Large diffs are not rendered by default.

prometheus-monitoring/docker-compose.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,28 @@ services:
66
container_name: go-application
77
image: go-application
88
ports:
9-
- "80:80"
9+
- "80:5000"
1010
python-application:
1111
build:
1212
context: ./python-application
1313
container_name: python-application
1414
image: python-application
1515
ports:
16-
- "81:80"
16+
- "81:5000"
1717
dotnet-application:
1818
build:
1919
context: ./dotnet-application
2020
container_name: dotnet-application
2121
image: dotnet-application
2222
ports:
23-
- "82:80"
23+
- "82:5000"
2424
nodejs-application:
2525
build:
2626
context: ./nodejs-application
2727
container_name: nodejs-application
2828
image: nodejs-application
2929
ports:
30-
- "83:80"
30+
- "83:5000"
3131
prometheus:
3232
container_name: prometheus-svc
3333
image: prom/prometheus

prometheus-monitoring/dotnet-application/src/Pages/Index.cshtml.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Threading.Tasks;
56
using Microsoft.AspNetCore.Mvc;
67
using Microsoft.AspNetCore.Mvc.RazorPages;
7-
8+
using Prometheus;
89
namespace work.Pages
910
{
1011
public class IndexModel : PageModel
1112
{
13+
private static readonly Counter ProcessedJobCount = Metrics
14+
.CreateCounter("dotnet_request_operations_total", "The total number of processed requests");
1215
public void OnGet()
1316
{
17+
var sw = Stopwatch.StartNew();
18+
19+
sw.Stop();
20+
ProcessedJobCount.Inc();
21+
var histogram =
22+
Metrics
23+
.CreateHistogram(
24+
"dotnet_request_duration_seconds",
25+
"Histogram for the duration in seconds.",
26+
new[] { 0.02, 0.05, 0.1, 0.15, 0.2, 0.5, 0.8, 1 },
27+
"GET",
28+
"/");
29+
30+
histogram
31+
.Observe(sw.Elapsed.TotalSeconds);
1432

1533
}
1634
}

prometheus-monitoring/dotnet-application/src/Startup.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.AspNetCore.Mvc;
1010
using Microsoft.Extensions.Configuration;
1111
using Microsoft.Extensions.DependencyInjection;
12+
using Prometheus;
1213

1314
namespace work
1415
{
@@ -52,7 +53,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
5253
app.UseHttpsRedirection();
5354
app.UseStaticFiles();
5455
app.UseCookiePolicy();
55-
56+
app.UseMetricServer();
5657
app.UseMvc();
5758
}
5859
}

prometheus-monitoring/dotnet-application/src/work.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<ItemGroup>
1212
<PackageReference Include="Microsoft.AspNetCore.App"/>
13+
<PackageReference Include="prometheus-net.AspNetCore" Version="3.1.4"/>
1314
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
1415
</ItemGroup>
1516

prometheus-monitoring/go-application/dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ RUN apk update && apk upgrade && \
2525
WORKDIR /go/src/app
2626
COPY --from=builder /go/src/app/myapp /go/src/app/myapp
2727

28-
EXPOSE 80
28+
EXPOSE 5000
2929

3030
CMD ["./myapp"]

prometheus-monitoring/go-application/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ func main() {
4646
})
4747

4848
http.Handle("/metrics", promhttp.Handler())
49-
http.ListenAndServe(":80", nil)
49+
http.ListenAndServe(":5000", nil)
5050

5151
}

prometheus-monitoring/nodejs-application/src/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"start": "node server.js"
99
},
1010
"dependencies": {
11-
"express": "^4.16.1"
11+
"express": "^4.16.1",
12+
"prom-client" : "11.5.3"
1213
}
1314
}

prometheus-monitoring/nodejs-application/src/server.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,46 @@ const PORT = 5000;
77
const HOST = '0.0.0.0';
88

99
// App
10+
const client = require('prom-client');
11+
const collectDefaultMetrics = client.collectDefaultMetrics;
12+
// Probe every 5th second.
13+
collectDefaultMetrics({ timeout: 5000 });
14+
15+
const counter = new client.Counter({
16+
name: 'node_request_operations_total',
17+
help: 'The total number of processed requests'
18+
});
19+
20+
const histogram = new client.Histogram({
21+
name: 'node_request_duration_seconds',
22+
help: 'Histogram for the duration in seconds.',
23+
buckets: [1, 2, 5, 6, 10]
24+
});
25+
1026
const app = express();
1127
app.get('/', (req, res) => {
28+
29+
//Simulate a sleep
30+
var start = new Date()
31+
var simulateTime = 1000
32+
33+
setTimeout(function(argument) {
34+
// execution time simulated with setTimeout function
35+
var end = new Date() - start
36+
histogram.observe(end / 1000); //convert to seconds
37+
}, simulateTime)
38+
39+
counter.inc();
40+
1241
res.send('Hello world\n');
1342
});
1443

44+
45+
// Metrics endpoint
46+
app.get('/metrics', (req, res) => {
47+
res.set('Content-Type', client.register.contentType)
48+
res.end(client.register.metrics())
49+
})
50+
1551
app.listen(PORT, HOST);
1652
console.log(`Running on http://${HOST}:${PORT}`);

prometheus-monitoring/prometheus.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ scrape_configs:
55
- job_name: my-application
66
honor_labels: true
77
static_configs:
8-
- targets: ['go-application','python-application','dotnet-application','nodejs-application']
8+
- targets: ['go-application:5000','python-application:5000','dotnet-application:5000','nodejs-application:5000']

0 commit comments

Comments
 (0)