forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster_diag.go
128 lines (107 loc) · 3.48 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
package gocb
import (
"encoding/json"
"time"
"github.com/couchbase/gocbcore/v10"
"github.com/google/uuid"
)
// EndPointDiagnostics represents a single entry in a diagnostics report.
type EndPointDiagnostics struct {
Type ServiceType
ID string
Local string
Remote string
LastActivity time.Time
State EndpointState
Namespace string
}
// DiagnosticsResult encapsulates the results of a Diagnostics operation.
type DiagnosticsResult struct {
ID string
Services map[string][]EndPointDiagnostics
sdk string
State ClusterState
}
type jsonDiagnosticEntry struct {
ID string `json:"id,omitempty"`
LastActivityUs uint64 `json:"last_activity_us,omitempty"`
Remote string `json:"remote,omitempty"`
Local string `json:"local,omitempty"`
State string `json:"state,omitempty"`
Details string `json:"details,omitempty"`
Namespace string `json:"namespace,omitempty"`
}
type jsonDiagnosticReport struct {
Version int16 `json:"version"`
SDK string `json:"sdk,omitempty"`
ID string `json:"id,omitempty"`
Services map[string][]jsonDiagnosticEntry `json:"services"`
State string `json:"state"`
}
// MarshalJSON generates a JSON representation of this diagnostics report.
func (report *DiagnosticsResult) MarshalJSON() ([]byte, error) {
jsonReport := jsonDiagnosticReport{
Version: 2,
SDK: report.sdk,
ID: report.ID,
Services: make(map[string][]jsonDiagnosticEntry),
State: clusterStateToString(report.State),
}
for _, serviceType := range report.Services {
for _, service := range serviceType {
serviceStr := serviceTypeToString(service.Type)
stateStr := endpointStateToString(service.State)
jsonReport.Services[serviceStr] = append(jsonReport.Services[serviceStr], jsonDiagnosticEntry{
ID: service.ID,
LastActivityUs: uint64(time.Since(service.LastActivity).Nanoseconds()),
Remote: service.Remote,
Local: service.Local,
State: stateStr,
Details: "",
Namespace: service.Namespace,
})
}
}
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.
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(gocbcore.DiagnosticsOptions{})
if err != nil {
return nil, err
}
report := &DiagnosticsResult{
ID: opts.ReportID,
Services: make(map[string][]EndPointDiagnostics),
sdk: Identifier(),
State: ClusterState(agentReport.State),
}
report.Services["kv"] = make([]EndPointDiagnostics, 0)
for _, conn := range agentReport.MemdConns {
state := EndpointState(conn.State)
report.Services["kv"] = append(report.Services["kv"], EndPointDiagnostics{
Type: ServiceTypeKeyValue,
State: state,
Local: conn.LocalAddr,
Remote: conn.RemoteAddr,
LastActivity: conn.LastActivity,
Namespace: conn.Scope,
ID: conn.ID,
})
}
return report, nil
}