-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathmisc.go
More file actions
72 lines (58 loc) · 1.46 KB
/
misc.go
File metadata and controls
72 lines (58 loc) · 1.46 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"fmt"
"log/slog"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/webdevops/go-common/log/slogger"
)
func buildContextLoggerFromRequest(r *http.Request) *slogger.Logger {
var logParams = []any{}
for name, value := range r.URL.Query() {
logParams = append(logParams, slog.Any(name, value))
}
contextLogger := logger.With(
slog.Group(
"request",
slog.String("path", r.URL.Path),
slog.Group("param", logParams...),
),
)
return contextLogger
}
func getPrometheusTimeout(r *http.Request, defaultTimeout float64) (timeout float64, err error) {
// If a timeout is configured via the Prometheus header, add it to the request.
if v := r.Header.Get("X-Prometheus-Scrape-Timeout-Seconds"); v != "" {
timeout, err = strconv.ParseFloat(v, 64)
if err != nil {
return
}
}
if timeout == 0 {
timeout = defaultTimeout
}
return
}
func paramsGetRequired(params url.Values, name string) (value string, err error) {
value = params.Get(name)
if value == "" {
err = fmt.Errorf("parameter \"%v\" is missing", name)
}
return
}
func paramsGetList(params url.Values, name string) (list []string, err error) {
for _, v := range params[name] {
list = append(list, strings.Split(v, ",")...)
}
return
}
func paramsGetListRequired(params url.Values, name string) (list []string, err error) {
list, err = paramsGetList(params, name)
if len(list) == 0 {
err = fmt.Errorf("parameter \"%v\" is missing", name)
return
}
return
}