forked from kubeshop/testkube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logsserver.go
72 lines (57 loc) · 1.58 KB
/
logsserver.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
package logs
import (
"fmt"
"go.uber.org/zap"
"github.com/kubeshop/testkube/pkg/log"
"github.com/kubeshop/testkube/pkg/logs/pb"
"github.com/kubeshop/testkube/pkg/logs/repository"
"github.com/kubeshop/testkube/pkg/logs/state"
)
func NewLogsServer(repo repository.Factory, state state.Interface) *LogsServer {
return &LogsServer{
state: state,
repoFactory: repo,
log: log.DefaultLogger.With("service", "logs-grpc-server"),
}
}
type LogsServer struct {
pb.UnimplementedLogsServiceServer
repoFactory repository.Factory
state state.Interface
log *zap.SugaredLogger
traceMessages bool
}
func (s LogsServer) Logs(req *pb.LogRequest, stream pb.LogsService_LogsServer) error {
ctx := stream.Context()
log := s.log.With("execution_id", req.ExecutionId)
// get state of current log stream (pending or finished)
st, err := s.state.Get(ctx, req.ExecutionId)
if err != nil {
return err
}
// get valid repository based on state
repo, err := s.repoFactory.GetRepository(st)
if err != nil {
return err
}
log.Debugw("starting sending log stream", "repo", fmt.Sprintf("%T", repo), "state", st)
// stream logs from repository through GRPC channel
ch, err := repo.Get(ctx, req.ExecutionId)
if err != nil {
return err
}
for l := range ch {
if s.traceMessages {
log.Debugw("sending log chunk", "log", l)
}
if err := stream.Send(pb.MapResponseToPB(l)); err != nil {
return err
}
}
log.Debugw("log stream finished")
return nil
}
func (s *LogsServer) WithMessageTracing(enabled bool) *LogsServer {
s.traceMessages = enabled
return s
}