forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket_diag.go
112 lines (94 loc) · 2.9 KB
/
bucket_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
package gocb
import (
"encoding/json"
"time"
"github.com/google/uuid"
)
// 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 "?"
}
// DiagnosticEntry represents a single entry in a diagnostics report.
type DiagnosticEntry struct {
Service ServiceType
State DiagConnState
LocalAddr string
RemoteAddr string
LastActivity time.Time
}
// DiagnosticReport encapsulates the results of a Diagnostics operation.
type DiagnosticReport struct {
ConfigRev int64
Services []DiagnosticEntry
}
type jsonDiagnosticEntry struct {
State string `json:"state"`
Remote string `json:"remote"`
Local string `json:"local"`
LastActivityUs uint64 `json:"last_activity_us"`
}
type jsonDiagnosticReport struct {
Version int `json:"version"`
Id string `json:"id"`
ConfigRev int `json:"config_rev"`
Sdk string `json:"sdk"`
Services map[string][]jsonDiagnosticEntry `json:"services"`
}
// MarshalJSON generates a JSON representation of this diagnostics report.
func (report *DiagnosticReport) MarshalJSON() ([]byte, error) {
jsonReport := jsonDiagnosticReport{
Version: 1,
Id: uuid.New().String(),
Services: make(map[string][]jsonDiagnosticEntry),
}
for _, service := range report.Services {
serviceStr := diagServiceString(service.Service)
stateStr := diagStateString(service.State)
jsonReport.Services[serviceStr] = append(jsonReport.Services[serviceStr], jsonDiagnosticEntry{
State: stateStr,
Remote: service.RemoteAddr,
Local: service.LocalAddr,
LastActivityUs: uint64(time.Now().Sub(service.LastActivity).Nanoseconds()),
})
}
return json.Marshal(&jsonReport)
}
// Diagnostics returns information about the internal state of the SDK.
//
// Experimental: This API is subject to change at any time.
func (bucket *Bucket) Diagnostics() (*DiagnosticReport, error) {
agentReport, err := bucket.client.Diagnostics()
if err != nil {
return nil, err
}
report := &DiagnosticReport{
ConfigRev: agentReport.ConfigRev,
}
for _, conn := range agentReport.MemdConns {
state := DiagStateDisconnected
if conn.LocalAddr != "" {
state = DiagStateOk
}
report.Services = append(report.Services, DiagnosticEntry{
Service: MemdService,
State: state,
LocalAddr: conn.LocalAddr,
RemoteAddr: conn.RemoteAddr,
LastActivity: conn.LastActivity,
})
}
return report, nil
}