-
Notifications
You must be signed in to change notification settings - Fork 8
/
monitor.go
67 lines (63 loc) · 1.6 KB
/
monitor.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 bqtail
import (
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/viant/bqtail/mon"
"github.com/viant/bqtail/shared"
"github.com/viant/toolbox"
"log"
"net/http"
)
//Monitor cloud function entry point
func Monitor(w http.ResponseWriter, r *http.Request) {
if r.ContentLength > 0 {
defer func() {
_ = r.Body.Close()
}()
}
err := checkBqTailPerformance(w, r)
if err != nil {
log.Print(err)
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func checkBqTailPerformance(writer http.ResponseWriter, httpRequest *http.Request) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%v", r)
}
}()
request := &mon.Request{}
if httpRequest.ContentLength > 0 {
if err = json.NewDecoder(httpRequest.Body).Decode(&request); err != nil {
return errors.Wrapf(err, "failed to decode %T", request)
}
} else {
if err := httpRequest.ParseForm(); err == nil {
if len(httpRequest.Form) > 0 {
request.IncludeDone = toolbox.AsBoolean(httpRequest.Form.Get("IncludeDone"))
request.Recency = httpRequest.Form.Get("Recency")
request.DestBucket = httpRequest.Form.Get("DestBucket")
request.DestPath = httpRequest.Form.Get("DestPath")
}
}
}
if request.Recency == "" {
request.Recency = "1hour"
}
ctx := context.Background()
service, err := mon.Singleton(ctx, shared.ConfigEnvKey)
if err != nil {
return err
}
response := service.Check(ctx, request)
writer.Header().Set("Content-Type", "application/json")
if err = json.NewEncoder(writer).Encode(response); err != nil {
return err
}
return err
}