forked from f5devcentral/f5-bigip-rest-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_sample.go
53 lines (45 loc) · 1.8 KB
/
utils_sample.go
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
package main
import (
"context"
"net/http"
"time"
"github.com/f5devcentral/f5-bigip-rest-go/utils"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
// get a new logger instance and set log level and request id for tracing.
slog := utils.NewLog()
uid := uuid.New().String()
slog.WithLevel(utils.LogLevel_Type_DEBUG).WithRequestID(uid)
slog.Debugf("hello there.")
// embed logger into a context for later using.
ctx := context.WithValue(context.TODO(), utils.CtxKey_Logger, slog)
// prometheus usage
prometheus.MustRegister(utils.FunctionDurationTimeCostCount)
prometheus.MustRegister(utils.FunctionDurationTimeCostTotal)
// f5_bigip "github.com/f5devcentral/f5-bigip-rest-go/bigip"
// prometheus.MustRegister(f5_bigip.BIGIPiControlTimeCostCount)
// prometheus.MustRegister(f5_bigip.BIGIPiControlTimeCostTotal)
callMonitoredFunc(ctx)
http.Handle("/metrics", promhttp.Handler())
func() {
slog.Infof("Starting 8080 for prometheus monitoring..")
http.ListenAndServe("0.0.0.0:8080", nil)
}()
// By accessing http://localhost:8080, we can get the following metrics.
// # HELP function_duration_timecost_count time cost count of functions
// # TYPE function_duration_timecost_count gauge
// function_duration_timecost_count{name="main.callMonitoredFunc"} 1
// # HELP function_duration_timecost_total time cost total(in milliseconds) of functions
// # TYPE function_duration_timecost_total gauge
// function_duration_timecost_total{name="main.callMonitoredFunc"} 100
}
// callMonitoredFunc a test function
func callMonitoredFunc(ctx context.Context) {
defer utils.TimeItToPrometheus()()
slog := utils.LogFromContext(ctx)
<-time.After(100 * time.Millisecond)
slog.Debugf("called function promTest")
}