2023-05-30 09:31:34 +00:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-11-20 08:35:34 +00:00
|
|
|
"fmt"
|
2023-05-30 09:31:34 +00:00
|
|
|
|
2024-11-20 08:35:34 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2023-05-30 09:31:34 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
)
|
|
|
|
|
|
|
|
type clientStream struct {
|
|
|
|
originalStream grpc.ClientStream
|
|
|
|
desc *grpc.StreamDesc
|
2024-11-20 08:35:34 +00:00
|
|
|
span trace.Span
|
2023-05-30 09:31:34 +00:00
|
|
|
finished chan<- error
|
|
|
|
done <-chan struct{}
|
|
|
|
}
|
|
|
|
|
2024-11-20 08:35:34 +00:00
|
|
|
func newgRPCClientStream(originalStream grpc.ClientStream, desc *grpc.StreamDesc, span trace.Span, finished chan<- error, done <-chan struct{}) grpc.ClientStream {
|
2023-05-30 09:31:34 +00:00
|
|
|
return &clientStream{
|
|
|
|
originalStream: originalStream,
|
|
|
|
desc: desc,
|
2024-11-20 08:35:34 +00:00
|
|
|
span: span,
|
2023-05-30 09:31:34 +00:00
|
|
|
finished: finished,
|
|
|
|
done: done,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *clientStream) Header() (metadata.MD, error) {
|
|
|
|
md, err := cs.originalStream.Header()
|
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case <-cs.done:
|
|
|
|
case cs.finished <- err:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return md, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *clientStream) Trailer() metadata.MD {
|
|
|
|
return cs.originalStream.Trailer()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *clientStream) CloseSend() error {
|
2024-11-20 08:35:34 +00:00
|
|
|
cs.span.AddEvent("client.stream.close.send.start")
|
2023-05-30 09:31:34 +00:00
|
|
|
err := cs.originalStream.CloseSend()
|
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case <-cs.done:
|
|
|
|
case cs.finished <- err:
|
|
|
|
}
|
|
|
|
}
|
2024-11-20 08:35:34 +00:00
|
|
|
cs.span.AddEvent("client.stream.close.send.finish")
|
2023-05-30 09:31:34 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *clientStream) Context() context.Context {
|
|
|
|
return cs.originalStream.Context()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *clientStream) SendMsg(m any) error {
|
2024-11-20 08:35:34 +00:00
|
|
|
cs.span.AddEvent("client.stream.send.msg.start", trace.WithAttributes(
|
|
|
|
attribute.String("message.type", fmt.Sprintf("%T", m))),
|
|
|
|
)
|
2023-05-30 09:31:34 +00:00
|
|
|
err := cs.originalStream.SendMsg(m)
|
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case <-cs.done:
|
|
|
|
case cs.finished <- err:
|
|
|
|
}
|
|
|
|
}
|
2024-11-20 08:35:34 +00:00
|
|
|
cs.span.AddEvent("client.stream.send.msg.finish", trace.WithAttributes(
|
|
|
|
attribute.String("message.type", fmt.Sprintf("%T", m))),
|
|
|
|
)
|
2023-05-30 09:31:34 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *clientStream) RecvMsg(m any) error {
|
2024-11-20 08:35:34 +00:00
|
|
|
cs.span.AddEvent("client.stream.receive.msg.start", trace.WithAttributes(
|
|
|
|
attribute.String("message.type", fmt.Sprintf("%T", m))),
|
|
|
|
)
|
2023-05-30 09:31:34 +00:00
|
|
|
err := cs.originalStream.RecvMsg(m)
|
|
|
|
if err != nil || !cs.desc.ServerStreams {
|
|
|
|
select {
|
|
|
|
case <-cs.done:
|
|
|
|
case cs.finished <- err:
|
|
|
|
}
|
|
|
|
}
|
2024-11-20 08:35:34 +00:00
|
|
|
cs.span.AddEvent("client.stream.receive.msg.finish", trace.WithAttributes(
|
|
|
|
attribute.String("message.type", fmt.Sprintf("%T", m))),
|
|
|
|
)
|
2023-05-30 09:31:34 +00:00
|
|
|
return err
|
|
|
|
}
|