-
Notifications
You must be signed in to change notification settings - Fork 139
/
functions_handler.go
143 lines (126 loc) · 3.59 KB
/
functions_handler.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package http
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/go-graphite/carbonapi/carbonapipb"
"github.com/go-graphite/carbonapi/expr/metadata"
"github.com/go-graphite/carbonapi/expr/types"
utilctx "github.com/go-graphite/carbonapi/util/ctx"
"github.com/lomik/zapwriter"
"go.uber.org/zap"
)
func functionsHandler(w http.ResponseWriter, r *http.Request) {
// TODO: Implement helper for specific functions
t0 := time.Now()
username, _, _ := r.BasicAuth()
srcIP, srcPort := splitRemoteAddr(r.RemoteAddr)
accessLogger := zapwriter.Logger("access")
var accessLogDetails = carbonapipb.AccessLogDetails{
Handler: "functions",
Username: username,
URL: r.URL.RequestURI(),
PeerIP: srcIP,
PeerPort: srcPort,
Host: r.Host,
Referer: r.Referer(),
URI: r.RequestURI,
RequestHeaders: utilctx.GetLogHeaders(r.Context()),
}
logAsError := false
defer func() {
deferredAccessLogging(accessLogger, &accessLogDetails, t0, logAsError)
}()
ApiMetrics.Requests.Add(1)
err := r.ParseForm()
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest)+": "+err.Error(), http.StatusBadRequest)
accessLogDetails.HTTPCode = http.StatusBadRequest
accessLogDetails.Reason = err.Error()
logAsError = true
return
}
grouped := false
nativeOnly := false
groupedStr := r.FormValue("grouped")
prettyStr := r.FormValue("pretty")
nativeOnlyStr := r.FormValue("nativeOnly")
var marshaler func(interface{}) ([]byte, error)
if groupedStr == "1" {
grouped = true
}
if prettyStr == "1" {
marshaler = func(v interface{}) ([]byte, error) {
return json.MarshalIndent(v, "", "\t")
}
} else {
marshaler = json.Marshal
}
if nativeOnlyStr == "1" {
nativeOnly = true
}
path := strings.Split(r.URL.EscapedPath(), "/")
function := ""
if len(path) >= 3 {
function = path[2]
}
var b []byte
if !nativeOnly {
metadata.FunctionMD.RLock()
if function != "" {
b, err = marshaler(metadata.FunctionMD.Descriptions[function])
} else if grouped {
b, err = marshaler(metadata.FunctionMD.DescriptionsGrouped)
} else {
b, err = marshaler(metadata.FunctionMD.Descriptions)
}
metadata.FunctionMD.RUnlock()
} else {
metadata.FunctionMD.RLock()
if function != "" {
if !metadata.FunctionMD.Descriptions[function].Proxied {
b, err = marshaler(metadata.FunctionMD.Descriptions[function])
} else {
err = fmt.Errorf("%v is proxied to graphite-web and nativeOnly was specified", function)
}
} else if grouped {
descGrouped := make(map[string]map[string]types.FunctionDescription)
for groupName, description := range metadata.FunctionMD.DescriptionsGrouped {
desc := make(map[string]types.FunctionDescription)
for f, d := range description {
if d.Proxied {
continue
}
desc[f] = d
}
if len(desc) > 0 {
descGrouped[groupName] = desc
}
}
b, err = marshaler(descGrouped)
} else {
desc := make(map[string]types.FunctionDescription)
for f, d := range metadata.FunctionMD.Descriptions {
if d.Proxied {
continue
}
desc[f] = d
}
b, err = marshaler(desc)
}
metadata.FunctionMD.RUnlock()
}
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
accessLogDetails.HTTPCode = http.StatusInternalServerError
accessLogDetails.Reason = err.Error()
logAsError = true
return
}
w.Write(b)
accessLogDetails.Runtime = time.Since(t0).Seconds()
accessLogDetails.HTTPCode = http.StatusOK
accessLogger.Info("request served", zap.Any("data", accessLogDetails))
}