-
Notifications
You must be signed in to change notification settings - Fork 104
/
cluster_diag.go
197 lines (168 loc) · 4.59 KB
/
cluster_diag.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package gocb
import (
"encoding/json"
"time"
"github.com/google/uuid"
"github.com/couchbase/gocbcore/v8"
)
type diagnosticsProvider interface {
Diagnostics() (*gocbcore.DiagnosticInfo, error)
}
func diagServiceString(service ServiceType) string {
switch service {
case MgmtService:
return "mgmt"
case KeyValueService:
return "kv"
case CapiService:
return "views"
case QueryService:
return "query"
case SearchService:
return "search"
case AnalyticsService:
return "analytics"
}
return ""
}
func diagStringService(service string) ServiceType {
switch service {
case "mgmt":
return MgmtService
case "kv":
return KeyValueService
case "views":
return CapiService
case "query":
return QueryService
case "search":
return SearchService
case "analytics":
return AnalyticsService
}
return ServiceType(0)
}
// DiagConnState represents the state of a connection in a diagnostics report.
type DiagConnState int
const (
// DiagStateOk indicates that the connection state is ok.
DiagStateOk = DiagConnState(0)
// DiagStateDisconnected indicates that the connection is disconnected.
DiagStateDisconnected = DiagConnState(1)
)
func diagStateString(state DiagConnState) string {
switch state {
case DiagStateOk:
return "ok"
case DiagStateDisconnected:
return "disconnected"
}
return ""
}
// EndPointDiagnostics represents a single entry in a diagnostics report.
type EndPointDiagnostics struct {
Type ServiceType
State DiagConnState
Local string
Remote string
LastActivity time.Time
Scope string
ID string
}
// DiagnosticsResult encapsulates the results of a Diagnostics operation.
type DiagnosticsResult struct {
ID string
Version int64
SDK string
Services map[string][]EndPointDiagnostics
}
type jsonDiagnosticEntry struct {
State string `json:"state"`
Remote string `json:"remote"`
Local string `json:"local"`
LastActivityUs uint64 `json:"last_activity_us"`
Scope string `json:"scope,omitempty"`
ID string `json:"id"`
}
type jsonDiagnosticReport struct {
Version int64 `json:"version"`
ID string `json:"id"`
SDK string `json:"sdk"`
Services map[string][]jsonDiagnosticEntry `json:"services"`
}
// MarshalJSON generates a JSON representation of this diagnostics report.
func (report *DiagnosticsResult) MarshalJSON() ([]byte, error) {
jsonReport := jsonDiagnosticReport{
Version: 1,
ID: report.ID,
Services: make(map[string][]jsonDiagnosticEntry),
SDK: report.SDK,
}
for _, serviceType := range report.Services {
for _, service := range serviceType {
serviceStr := diagServiceString(service.Type)
if serviceStr == "" {
serviceStr = "unknown"
}
stateStr := diagStateString(service.State)
if stateStr == "" {
stateStr = "unknown"
}
jsonReport.Services[serviceStr] = append(jsonReport.Services[serviceStr], jsonDiagnosticEntry{
State: stateStr,
Remote: service.Remote,
Local: service.Local,
LastActivityUs: uint64(time.Now().Sub(service.LastActivity).Nanoseconds()),
Scope: service.Scope,
ID: service.ID,
})
}
}
return json.Marshal(&jsonReport)
}
// DiagnosticsOptions are the options that are available for use with the Diagnostics operation.
type DiagnosticsOptions struct {
ReportID string
}
// Diagnostics returns information about the internal state of the SDK.
//
// Volatile: This API is subject to change at any time.
func (c *Cluster) Diagnostics(opts *DiagnosticsOptions) (*DiagnosticsResult, error) {
if opts == nil {
opts = &DiagnosticsOptions{}
}
if opts.ReportID == "" {
opts.ReportID = uuid.New().String()
}
provider, err := c.getDiagnosticsProvider()
if err != nil {
return nil, err
}
agentReport, err := provider.Diagnostics()
if err != nil {
return nil, err
}
report := &DiagnosticsResult{
ID: opts.ReportID,
Version: agentReport.ConfigRev,
SDK: Identifier(),
Services: make(map[string][]EndPointDiagnostics),
}
report.Services["kv"] = make([]EndPointDiagnostics, 0)
for _, conn := range agentReport.MemdConns {
state := DiagStateDisconnected
if conn.LocalAddr != "" {
state = DiagStateOk
}
report.Services["kv"] = append(report.Services["kv"], EndPointDiagnostics{
Type: KeyValueService,
State: state,
Local: conn.LocalAddr,
Remote: conn.RemoteAddr,
LastActivity: conn.LastActivity,
Scope: conn.Scope,
ID: conn.ID,
})
}
return report, nil
}