Skip to content

Commit d3d35ae

Browse files
grussorussomatnar
andcommitted
Refactored Prometheus integration
Co-authored-by: Matteo Nardelli <matnar@gmail.com>
1 parent 18417e5 commit d3d35ae

6 files changed

Lines changed: 32 additions & 37 deletions

File tree

cmd/serverledge/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func main() {
5959
}
6060
node.NodeIdentifier = myKey
6161

62-
go metrics.Init()
62+
metrics.Init()
6363

6464
if config.GetBool(config.TRACING_ENABLED, false) {
6565
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)

internal/api/server.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"github.com/serverledge-faas/serverledge/internal/metrics"
78
"log"
89
"net/http"
910
"os"
@@ -29,6 +30,14 @@ func StartAPIServer(e *echo.Echo) {
2930
e.GET("/function", GetFunctions)
3031
e.GET("/poll/:reqId", PollAsyncResult)
3132
e.GET("/status", GetServerStatus)
33+
34+
if config.GetBool(config.METRICS_ENABLED, false) {
35+
e.GET("/metrics", func(c echo.Context) error {
36+
metrics.ScrapingHandler.ServeHTTP(c.Response(), c.Request())
37+
return nil
38+
})
39+
}
40+
3241
// Workflow routes
3342
e.POST("/workflow/invoke/:workflow", InvokeWorkflow)
3443
e.POST("/workflow/resume/:workflow", ResumeWorkflow)

internal/config/keys.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const LISTEN_UDP_PORT = "registry.udp.port"
5959
// enable metrics system
6060
const METRICS_ENABLED = "metrics.enabled"
6161

62-
const METRICS_PROMETHEUS_HOST = "metrics.prometheus.host"
62+
// HTTP port used for Prometheus scraping
6363
const METRICS_PROMETHEUS_PORT = "metrics.prometheus.port"
6464

6565
// Scheduling policy to use

internal/metrics/metrics.go

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,20 @@ import (
1515

1616
var Enabled bool
1717
var registry = prometheus.NewRegistry()
18-
var nodeIdentifier string
18+
var ScrapingHandler http.Handler = nil
19+
var durationBuckets = []float64{0.002, 0.005, 0.010, 0.02, 0.03, 0.05, 0.1, 0.15, 0.3, 0.6, 1.0}
20+
21+
var (
22+
CompletedInvocations = promauto.NewCounterVec(prometheus.CounterOpts{
23+
Name: "completed_total",
24+
Help: "Number of completed function invocations",
25+
}, []string{"node", "function"})
26+
ExecutionTimes = promauto.NewHistogramVec(prometheus.HistogramOpts{
27+
Name: "execution_time",
28+
Help: "Function duration",
29+
Buckets: durationBuckets,
30+
}, []string{"node", "function"})
31+
)
1932

2033
func Init() {
2134
if config.GetBool(config.METRICS_ENABLED, false) {
@@ -26,43 +39,16 @@ func Init() {
2639
return
2740
}
2841

29-
nodeIdentifier = node.NodeIdentifier
30-
registerGlobalMetrics()
42+
registry.MustRegister(CompletedInvocations)
43+
registry.MustRegister(ExecutionTimes)
3144

32-
handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{
45+
ScrapingHandler = promhttp.HandlerFor(registry, promhttp.HandlerOpts{
3346
EnableOpenMetrics: true})
34-
http.Handle("/metrics", handler)
35-
err := http.ListenAndServe(":2112", nil)
36-
if err != nil {
37-
log.Printf("Listen and serve terminated with error: %s\n", err)
38-
return
39-
}
4047
}
4148

42-
// Global metrics
43-
var (
44-
CompletedInvocations = promauto.NewCounterVec(prometheus.CounterOpts{
45-
Name: "sedge_completed_total",
46-
Help: "The total number of completed function invocations",
47-
}, []string{"node", "function"})
48-
ExecutionTimes = promauto.NewHistogramVec(prometheus.HistogramOpts{
49-
Name: "sedge_exectime",
50-
Help: "Function duration",
51-
Buckets: durationBuckets,
52-
},
53-
[]string{"node", "function"})
54-
)
55-
56-
var durationBuckets = []float64{0.002, 0.005, 0.010, 0.02, 0.03, 0.05, 0.1, 0.15, 0.3, 0.6, 1.0}
57-
5849
func AddCompletedInvocation(funcName string) {
59-
CompletedInvocations.With(prometheus.Labels{"function": funcName, "node": nodeIdentifier}).Inc()
50+
CompletedInvocations.With(prometheus.Labels{"function": funcName, "node": node.NodeIdentifier}).Inc()
6051
}
6152
func AddFunctionDurationValue(funcName string, duration float64) {
62-
ExecutionTimes.With(prometheus.Labels{"function": funcName, "node": nodeIdentifier}).Observe(duration)
63-
}
64-
65-
func registerGlobalMetrics() {
66-
registry.MustRegister(CompletedInvocations)
67-
registry.MustRegister(ExecutionTimes)
53+
ExecutionTimes.With(prometheus.Labels{"function": funcName, "node": node.NodeIdentifier}).Observe(duration)
6854
}

internal/test/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func testStartServerledge(isInCloud bool, outboundIp string) (*registration.Regi
6464

6565
node.NodeIdentifier = myKey
6666

67-
go metrics.Init()
67+
metrics.Init()
6868

6969
e := echo.New()
7070

prometheus.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ scrape_configs:
1111
# scheme defaults to 'http'.
1212

1313
static_configs:
14-
- targets: ["172.17.0.1:2112"]
14+
- targets: ["172.17.0.1:1323"]
1515

1616

0 commit comments

Comments
 (0)