-
Notifications
You must be signed in to change notification settings - Fork 26
/
misc.go
67 lines (53 loc) · 1.49 KB
/
misc.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
stringsCommon "github.com/webdevops/go-common/strings"
"go.uber.org/zap"
)
func buildContextLoggerFromRequest(r *http.Request) *zap.SugaredLogger {
contextLogger := logger.With(zap.String("requestPath", r.URL.Path))
for name, value := range r.URL.Query() {
fieldName := fmt.Sprintf("param%s", stringsCommon.UppercaseFirst(name))
fieldValue := value
contextLogger = contextLogger.With(zap.Any(fieldName, fieldValue))
}
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
}