-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
debug.go
187 lines (159 loc) · 3.99 KB
/
debug.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
// Package handler implements service debug handler embedded in go-micro services
package handler
import (
"context"
"errors"
"io"
"time"
"go-micro.dev/v4/client"
"go-micro.dev/v4/debug/log"
proto "go-micro.dev/v4/debug/proto"
"go-micro.dev/v4/debug/stats"
"go-micro.dev/v4/debug/trace"
)
// NewHandler returns an instance of the Debug Handler.
func NewHandler(c client.Client) *Debug {
return &Debug{
log: log.DefaultLog,
stats: stats.DefaultStats,
trace: trace.DefaultTracer,
}
}
var _ proto.DebugHandler = (*Debug)(nil)
type Debug struct {
// must honor the debug handler
proto.DebugHandler
// the logger for retrieving logs
log log.Log
// the stats collector
stats stats.Stats
// the tracer
trace trace.Tracer
}
func (d *Debug) Health(ctx context.Context, req *proto.HealthRequest, rsp *proto.HealthResponse) error {
rsp.Status = "ok"
return nil
}
func (d *Debug) MessageBus(ctx context.Context, stream proto.Debug_MessageBusStream) error {
for {
_, err := stream.Recv()
if errors.Is(err, io.EOF) {
return nil
} else if err != nil {
return err
}
rsp := proto.BusMsg{
Msg: "Request received!",
}
if err := stream.Send(&rsp); err != nil {
return err
}
}
}
func (d *Debug) Stats(ctx context.Context, req *proto.StatsRequest, rsp *proto.StatsResponse) error {
stats, err := d.stats.Read()
if err != nil {
return err
}
if len(stats) == 0 {
return nil
}
// write the response values
rsp.Timestamp = uint64(stats[0].Timestamp)
rsp.Started = uint64(stats[0].Started)
rsp.Uptime = uint64(stats[0].Uptime)
rsp.Memory = stats[0].Memory
rsp.Gc = stats[0].GC
rsp.Threads = stats[0].Threads
rsp.Requests = stats[0].Requests
rsp.Errors = stats[0].Errors
return nil
}
func (d *Debug) Trace(ctx context.Context, req *proto.TraceRequest, rsp *proto.TraceResponse) error {
traces, err := d.trace.Read(trace.ReadTrace(req.Id))
if err != nil {
return err
}
for _, t := range traces {
var typ proto.SpanType
switch t.Type {
case trace.SpanTypeRequestInbound:
typ = proto.SpanType_INBOUND
case trace.SpanTypeRequestOutbound:
typ = proto.SpanType_OUTBOUND
}
rsp.Spans = append(rsp.Spans, &proto.Span{
Trace: t.Trace,
Id: t.Id,
Parent: t.Parent,
Name: t.Name,
Started: uint64(t.Started.UnixNano()),
Duration: uint64(t.Duration.Nanoseconds()),
Type: typ,
Metadata: t.Metadata,
})
}
return nil
}
func (d *Debug) Log(ctx context.Context, req *proto.LogRequest, stream proto.Debug_LogStream) error {
var options []log.ReadOption
since := time.Unix(req.Since, 0)
if !since.IsZero() {
options = append(options, log.Since(since))
}
count := int(req.Count)
if count > 0 {
options = append(options, log.Count(count))
}
if req.Stream {
// TODO: we need to figure out how to close the log stream
// It seems like when a client disconnects,
// the connection stays open until some timeout expires
// or something like that; that means the map of streams
// might end up leaking memory if not cleaned up properly
lgStream, err := d.log.Stream()
if err != nil {
return err
}
defer lgStream.Stop()
for record := range lgStream.Chan() {
// copy metadata
metadata := make(map[string]string)
for k, v := range record.Metadata {
metadata[k] = v
}
// send record
if err := stream.Send(&proto.Record{
Timestamp: record.Timestamp.Unix(),
Message: record.Message.(string),
Metadata: metadata,
}); err != nil {
return err
}
}
// done streaming, return
return nil
}
// get the log records
records, err := d.log.Read(options...)
if err != nil {
return err
}
// send all the logs downstream
for _, record := range records {
// copy metadata
metadata := make(map[string]string)
for k, v := range record.Metadata {
metadata[k] = v
}
// send record
if err := stream.Send(&proto.Record{
Timestamp: record.Timestamp.Unix(),
Message: record.Message.(string),
Metadata: metadata,
}); err != nil {
return err
}
}
return nil
}