generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprimaryHandler.go
105 lines (89 loc) · 2.64 KB
/
primaryHandler.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
// SPDX-FileCopyrightText: 2020 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
import (
"io"
"net/http"
"strconv"
"sync"
"time"
"github.com/go-kit/kit/metrics"
"github.com/go-kit/kit/metrics/provider"
"github.com/go-kit/log"
vegeta "github.com/tsenart/vegeta/lib"
"github.com/xmidt-org/webpa-common/v2/logging" // nolint: staticcheck
"github.com/xmidt-org/webpa-common/v2/xmetrics" // nolint: staticcheck
)
type Measures struct {
TimeInMemory metrics.Histogram
}
// App used for logging and saving durations
type App struct {
logger log.Logger
measures *Measures
attacker *vegeta.Attacker
counter int
maxRoutines int
mutex *sync.Mutex
queryURL string
queryExpression string
metricsURL string
sleepTime time.Duration
sleepTimeAfter time.Duration
prometheusAuth string
timeoutPrometheus time.Duration
webhookURLs []string
}
const (
TimeInMemory = "queue_empty_duration"
)
func Metrics() []xmetrics.Metric {
return []xmetrics.Metric{
{
Name: TimeInMemory,
Help: "The duration it takes to empty queue in Caduceus.",
Type: "histogram",
Namespace: "xmidt",
Subsystem: "caduceator",
Buckets: []float64{0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0},
},
}
}
func NewMeasures(p provider.Provider) *Measures {
return &Measures{
TimeInMemory: p.NewHistogram(TimeInMemory, 10),
}
}
func (m *Measures) TrackTime(length time.Duration) {
m.TimeInMemory.Observe(length.Seconds())
}
func (app *App) receiveEvents(writer http.ResponseWriter, req *http.Request) {
time.Sleep(app.sleepTime)
_, err := io.ReadAll(req.Body)
req.Body.Close()
if err != nil {
logging.Error(app.logger).Log(logging.MessageKey(), "Could not read request body", logging.ErrorKey(), err.Error())
writer.WriteHeader(http.StatusBadRequest)
return
}
writer.WriteHeader(http.StatusAccepted)
}
func (app *App) receiveCutoff(writer http.ResponseWriter, req *http.Request) {
cutoffTime := time.Now()
logging.Info(app.logger).Log(logging.MessageKey(), "time caduceus queue is full: "+cutoffTime.String())
logging.Info(app.logger).Log(logging.MessageKey(), "counter: "+strconv.Itoa(app.counter))
logging.Info(app.logger).Log(logging.MessageKey(), "max routines: "+strconv.Itoa(app.maxRoutines))
app.mutex.Lock()
if app.maxRoutines == 0 {
app.counter++
go app.calculateDuration(cutoffTime)
app.mutex.Unlock()
} else if app.counter <= app.maxRoutines {
if app.counter == app.maxRoutines {
app.attacker.Stop()
}
app.counter++
go app.calculateDuration(cutoffTime)
app.mutex.Unlock()
}
}