-
Notifications
You must be signed in to change notification settings - Fork 582
/
operations.go
131 lines (117 loc) · 2.96 KB
/
operations.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
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package fetcher
import (
"encoding/json"
"io"
"net/http"
"net/url"
"os"
"github.com/wavesoftware/go-ensure"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"knative.dev/eventing/test/upgrade/prober/wathola/config"
"knative.dev/eventing/test/upgrade/prober/wathola/receiver"
)
// New will create a new fetcher
func New() Fetcher {
ml := &memoryLogger{
Execution: &Execution{
Logs: make([]LogEntry, 0),
Report: nil,
},
}
config.Log = newLogger(ml)
config.ReadIfPresent()
return &fetcher{
out: os.Stdout,
log: ml,
}
}
// FetchReport will try to fetch report from remote receiver service and dump
// it on the output as JSON.
func (f *fetcher) FetchReport() {
err := f.fetchReport()
if err != nil {
config.Log.Error(err)
}
f.ensureStatePrinted()
}
func (f *fetcher) fetchReport() error {
u, err := url.Parse(config.Instance.Forwarder.Target)
if err != nil {
return err
}
u.Path = "/report"
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return err
}
req.Header.Add("content-type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
body, err := io.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil {
return err
}
state := &receiver.Report{
Thrown: receiver.Thrown{
Unexpected: []string{},
Duplicated: []string{},
Missing: []string{},
Unavailable: []string{},
},
}
err = json.Unmarshal(body, state)
if err != nil {
return err
}
f.log.Report = state
return nil
}
func (f *fetcher) ensureStatePrinted() {
bytes, err := json.MarshalIndent(f.log.Execution, "", " ")
ensure.NoError(err)
_, err = f.out.Write(bytes)
ensure.NoError(err)
_, err = f.out.Write([]byte("\n"))
ensure.NoError(err)
}
func (m *memoryLogger) Write(p []byte) (int, error) {
le := LogEntry{}
err := json.Unmarshal(p, &le)
if err != nil {
return 0, err
}
m.Logs = append(m.Logs, le)
return len(p), nil
}
func (m *memoryLogger) Sync() error {
return nil
}
func newLogger(ws zapcore.WriteSyncer) *zap.SugaredLogger {
encoderCfg := zapcore.EncoderConfig{
MessageKey: "msg",
TimeKey: "dt",
LevelKey: "level",
NameKey: "logger",
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.RFC3339NanoTimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
}
core := zapcore.NewCore(zapcore.NewJSONEncoder(encoderCfg), ws, zap.DebugLevel)
return zap.New(core).Sugar()
}