-
Notifications
You must be signed in to change notification settings - Fork 104
/
diagnosticsprovider_core.go
135 lines (111 loc) · 3.34 KB
/
diagnosticsprovider_core.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
package gocb
import (
"context"
"github.com/google/uuid"
"time"
"github.com/couchbase/gocbcore/v10"
)
type diagnosticsProviderCoreProvider interface {
Diagnostics(opts gocbcore.DiagnosticsOptions) (*gocbcore.DiagnosticInfo, error)
Ping(ctx context.Context, opts gocbcore.PingOptions) (*gocbcore.PingResult, error)
}
type diagnosticsProviderCore struct {
provider diagnosticsProviderCoreProvider
tracer *tracerWrapper
timeouts TimeoutsConfig
}
func (d *diagnosticsProviderCore) Diagnostics(opts *DiagnosticsOptions) (*DiagnosticsResult, error) {
if opts == nil {
opts = &DiagnosticsOptions{}
}
if opts.ReportID == "" {
opts.ReportID = uuid.New().String()
}
agentReport, err := d.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
}
func (d *diagnosticsProviderCore) Ping(opts *PingOptions) (*PingResult, error) {
span := d.tracer.createSpan(opts.ParentSpan, "ping", "kv")
defer span.End()
services := opts.ServiceTypes
gocbcoreServices := make([]gocbcore.ServiceType, len(services))
for i, svc := range services {
gocbcoreServices[i] = gocbcore.ServiceType(svc)
}
coreopts := gocbcore.PingOptions{
ServiceTypes: gocbcoreServices,
TraceContext: span.Context(),
}
now := time.Now()
timeout := opts.Timeout
if timeout == 0 {
coreopts.KVDeadline = now.Add(d.timeouts.KVTimeout)
coreopts.CapiDeadline = now.Add(d.timeouts.ViewTimeout)
coreopts.N1QLDeadline = now.Add(d.timeouts.QueryTimeout)
coreopts.CbasDeadline = now.Add(d.timeouts.AnalyticsTimeout)
coreopts.FtsDeadline = now.Add(d.timeouts.SearchTimeout)
coreopts.MgmtDeadline = now.Add(d.timeouts.ManagementTimeout)
} else {
coreopts.KVDeadline = now.Add(timeout)
coreopts.CapiDeadline = now.Add(timeout)
coreopts.N1QLDeadline = now.Add(timeout)
coreopts.CbasDeadline = now.Add(timeout)
coreopts.FtsDeadline = now.Add(timeout)
coreopts.MgmtDeadline = now.Add(timeout)
}
id := opts.ReportID
if id == "" {
id = uuid.New().String()
}
result, err := d.provider.Ping(opts.Context, coreopts)
if err != nil {
return nil, err
}
reportSvcs := make(map[ServiceType][]EndpointPingReport)
for svcType, svc := range result.Services {
st := ServiceType(svcType)
svcs := make([]EndpointPingReport, len(svc))
for i, rep := range svc {
var errStr string
if rep.Error != nil {
errStr = rep.Error.Error()
}
svcs[i] = EndpointPingReport{
ID: rep.ID,
Remote: rep.Endpoint,
State: PingState(rep.State),
Error: errStr,
Namespace: rep.Scope,
Latency: rep.Latency,
}
}
reportSvcs[st] = svcs
}
return &PingResult{
ID: id,
sdk: Identifier() + " " + "gocbcore/" + gocbcore.Version(),
Services: reportSvcs,
}, nil
}