73 lines
1.7 KiB
Go
73 lines
1.7 KiB
Go
package v1
|
|
|
|
import (
|
|
"context"
|
|
|
|
"golang.org/x/exp/slog"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
rpcv1 "code.icb4dc0.de/buildr/buildr/internal/generated/rpc/v1"
|
|
)
|
|
|
|
var _ slog.Handler = (*GrpcExecutorHandler)(nil)
|
|
|
|
func NewGrpcExecutorHandler(sender StreamSender[*rpcv1.ExecuteTaskResponse]) *GrpcExecutorHandler {
|
|
return &GrpcExecutorHandler{
|
|
Level: slog.LevelInfo,
|
|
sender: sender,
|
|
}
|
|
}
|
|
|
|
type GrpcExecutorHandler struct {
|
|
sender StreamSender[*rpcv1.ExecuteTaskResponse]
|
|
Level slog.Level
|
|
group string
|
|
attributes []slog.Attr
|
|
}
|
|
|
|
func (g GrpcExecutorHandler) Enabled(_ context.Context, level slog.Level) bool {
|
|
return g.Level <= level
|
|
}
|
|
|
|
func (g GrpcExecutorHandler) Handle(_ context.Context, record slog.Record) error {
|
|
taskLog := rpcv1.TaskLog{
|
|
Time: timestamppb.New(record.Time.UTC()),
|
|
Message: record.Message,
|
|
Level: uint32(record.Level),
|
|
Attributes: make([]*rpcv1.TaskLog_LogAttribute, 0, record.NumAttrs()),
|
|
}
|
|
|
|
record.Attrs(func(attr slog.Attr) {
|
|
taskLog.Attributes = append(taskLog.Attributes, &rpcv1.TaskLog_LogAttribute{
|
|
Key: attr.Key,
|
|
Value: attr.Value.String(),
|
|
})
|
|
})
|
|
|
|
resp := rpcv1.ExecuteTaskResponse{
|
|
Envelope: &rpcv1.ExecuteTaskResponse_TaskLog{
|
|
TaskLog: &taskLog,
|
|
},
|
|
}
|
|
|
|
return g.sender.Send(&resp)
|
|
}
|
|
|
|
func (g GrpcExecutorHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
|
all := make([]slog.Attr, len(g.attributes)+len(attrs))
|
|
copy(all, g.attributes)
|
|
copy(all[len(g.attributes):], attrs)
|
|
|
|
return GrpcExecutorHandler{
|
|
sender: g.sender,
|
|
Level: g.Level,
|
|
group: g.group,
|
|
attributes: all,
|
|
}
|
|
}
|
|
|
|
func (g GrpcExecutorHandler) WithGroup(name string) slog.Handler {
|
|
g.group = name
|
|
return g
|
|
}
|