-
Notifications
You must be signed in to change notification settings - Fork 28
/
stats_stream.go
164 lines (139 loc) · 4.57 KB
/
stats_stream.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2023-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package cbft
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/couchbase/cbgt"
"github.com/couchbase/cbgt/rest"
log "github.com/couchbase/clog"
)
type statsStreamHandler struct {
mgr *cbgt.Manager
}
func NewStatsStreamHandler(mgr *cbgt.Manager) *statsStreamHandler {
return &statsStreamHandler{mgr: mgr}
}
type statsStreamChunk struct {
Stats map[string]interface{} `json:"stats"`
Rebalance bool `json:"rebalance"`
}
func (h *statsStreamHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
cn, ok := w.(http.CloseNotifier)
if !ok {
http.NotFound(w, req)
return
}
flusher, ok := w.(http.Flusher)
if !ok {
http.NotFound(w, req)
return
}
w.Header().Set("Transfer-Encoding", "chunked")
w.WriteHeader(http.StatusOK)
flusher.Flush()
enc := json.NewEncoder(w)
tickerCh := time.NewTicker(time.Second).C
nsStatsToStream := []string{
"batch_bytes_added",
"batch_bytes_removed",
"curr_batches_blocked_by_herder",
"num_batches_introduced",
"num_bytes_used_ram",
"num_gocbcore_dcp_agents",
"num_gocbcore_stats_agents",
"pct_cpu_gc",
"tot_batches_merged",
"tot_batches_new",
"tot_bleve_dest_closed",
"tot_bleve_dest_opened",
"tot_queryreject_on_memquota",
"tot_rollback_full",
"tot_rollback_partial",
"total_gc",
"total_queries_rejected_by_herder",
"utilization:billableUnitsRate",
"utilization:cpuPercent",
"utilization:diskBytes",
"utilization:memoryBytes",
}
serverlessStatsToStream := []string{
"limits:billableUnitsRate",
"limits:diskBytes",
"limits:memoryBytes",
"resourceUnderUtilizationWaterMark",
"resourceUtilizationHighWaterMark",
"resourceUtilizationLowWaterMark",
}
for {
select {
case <-cn.CloseNotify():
return
case <-tickerCh:
stats := make(map[string]interface{})
rd := getRecentInfo()
if rd.err != nil {
rest.ShowError(w, req, fmt.Sprintf("could not retrieve defs: %v", rd.err), http.StatusInternalServerError)
return
}
nsIndexStats, err := gatherIndexesStats(h.mgr, rd, false)
if err != nil {
rest.ShowError(w, req, fmt.Sprintf("error in retrieving defs: %v", err), http.StatusInternalServerError)
return
}
if ServerlessMode {
for statType, nsStats := range nsIndexStats {
if statType == "regulatorStats" {
for key, value := range nsStats {
if key == "total_units_metered" {
stats[key] = value
} else if bucketStats, ok := value.(*regulatorStats); ok {
stats[key+":total_RUs_metered"] = bucketStats.TotalRUsMetered
stats[key+":total_WUs_metered"] = bucketStats.TotalWUsMetered
stats[key+":total_metering_errs"] = bucketStats.TotalMeteringErrs
stats[key+":total_read_ops_capped"] = bucketStats.TotalReadOpsCapped
stats[key+":total_read_ops_rejected"] = bucketStats.TotalReadOpsRejected
stats[key+":total_write_ops_rejected"] = bucketStats.TotalWriteOpsRejected
stats[key+":total_write_throttle_seconds"] = bucketStats.TotalWriteThrottleSeconds
stats[key+":total_read_ops_metering_errs"] = bucketStats.TotalCheckQuotaReadErrs
stats[key+":total_write_ops_metering_errs"] = bucketStats.TotalCheckQuotaWriteErrs
stats[key+":total_ops_timed_out_while_metering"] = bucketStats.TotalOpsTimedOutWhileMetering
stats[key+":total_batch_limiting_timeouts"] = bucketStats.TotalBatchLimitingTimeOuts
stats[key+":total_batch_rejection_backoff_time_ms"] = bucketStats.TotalBatchRejectionBackoffTime
stats[key+":total_check_access_rejects"] = bucketStats.TotCheckAccessOpsRejects
stats[key+":total_check_access_errs"] = bucketStats.TotCheckAccessErrs
}
}
}
}
for _, stat := range serverlessStatsToStream {
stats[stat] = nsIndexStats[""][stat]
}
}
for _, stat := range nsStatsToStream {
stats[stat] = nsIndexStats[""][stat]
}
rebalance, err := rest.CheckRebalanceStatus(h.mgr)
if err != nil {
log.Warnf("Error getting rebalance status: %v", err)
}
m := statsStreamChunk{
Stats: stats,
Rebalance: rebalance,
}
err = enc.Encode(m)
if err != nil {
log.Warnf("Error encoding stats stream message into json: %v", err)
return
}
flusher.Flush()
}
}
}