-
Notifications
You must be signed in to change notification settings - Fork 1
/
discovery.go
60 lines (49 loc) · 1.32 KB
/
discovery.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
package totem
import (
"context"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
)
func discoverServices(ctx context.Context, ctrl *StreamController) (*ServiceInfo, error) {
reqBytes, _ := proto.Marshal(&DiscoveryRequest{
Initiator: ctrl.uuid,
Visited: []string{},
RemainingHops: -1,
})
ctx, span := Tracer().Start(ctx, "totem.discoverServices")
defer span.End()
lg := ctrl.logger.With(
zap.String("traceID", span.SpanContext().TraceID().String()),
)
lg.Debug("starting service discovery")
respC := ctrl.Request(ctx, &RPC{
ServiceName: "totem.ServerReflection",
MethodName: "ListServices",
Content: &RPC_Request{
Request: reqBytes,
},
})
resp := <-respC
respMsg := resp.GetResponse()
stat := respMsg.GetStatus()
if err := stat.Err(); err != nil {
lg.With(
zap.Error(err),
).Warn("discovery failed")
return nil, err
}
infoMsg := &ServiceInfo{}
if err := proto.Unmarshal(respMsg.GetResponse(), infoMsg); err != nil {
lg.Warn("received bad service info message")
return nil, err
}
span.AddEvent("Results", trace.WithAttributes(
attribute.StringSlice("methods", infoMsg.MethodNames()),
))
lg.With(
zap.Any("methods", infoMsg.MethodNames()),
).Debug("discovery complete")
return infoMsg, nil
}