This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
forked from connectrpc/otelconnect-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streaming.go
144 lines (134 loc) · 4.27 KB
/
streaming.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
// Copyright 2022-2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package otelconnect
import (
"context"
"errors"
"io"
"sync"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
"go.opentelemetry.io/otel/trace"
"google.golang.org/protobuf/proto"
)
type streamingState struct {
mu sync.Mutex
protocol string
req *Request
attributeFilter AttributeFilter
omitTraceEvents bool
attributes []attribute.KeyValue
error error
sentCounter int
receivedCounter int
receiveSize metric.Int64Histogram
receivesPerRPC metric.Int64Histogram
sendSize metric.Int64Histogram
sendsPerRPC metric.Int64Histogram
}
func newStreamingState(
req *Request,
attributeFilter AttributeFilter,
omitTraceEvents bool,
attributes []attribute.KeyValue,
receiveSize, receivesPerRPC, sendSize, sendsPerRPC metric.Int64Histogram,
) *streamingState {
attributes = attributeFilter.filter(req, attributes...)
return &streamingState{
protocol: protocolToSemConv(req.Peer.Protocol),
attributeFilter: attributeFilter,
omitTraceEvents: omitTraceEvents,
req: req,
attributes: attributes,
receiveSize: receiveSize,
receivesPerRPC: receivesPerRPC,
sendSize: sendSize,
sendsPerRPC: sendsPerRPC,
}
}
type sendReceiver interface {
Receive(any) error
Send(any) error
}
func (s *streamingState) addAttributes(attributes ...attribute.KeyValue) {
s.attributes = append(s.attributes, s.attributeFilter.filter(s.req, attributes...)...)
}
func (s *streamingState) receive(ctx context.Context, msg any, conn sendReceiver) error {
err := conn.Receive(msg)
s.mu.Lock()
defer s.mu.Unlock()
if errors.Is(err, io.EOF) {
return err
}
if err != nil {
s.error = err
// If error add it to the attributes because the stream is about to terminate.
// If no error don't add anything because status only exists at end of stream.
if statusCode, ok := statusCodeAttribute(s.protocol, err); ok {
s.addAttributes(statusCode)
}
}
protomsg, ok := msg.(proto.Message)
size := proto.Size(protomsg)
if !s.omitTraceEvents {
s.receivedCounter++
s.event(ctx, semconv.MessageTypeReceived, s.receivedCounter, ok, size)
}
s.receiveSize.Record(ctx, int64(size), metric.WithAttributes(s.attributes...))
s.receivesPerRPC.Record(ctx, 1, metric.WithAttributes(s.attributes...))
return err
}
func (s *streamingState) send(ctx context.Context, msg any, conn sendReceiver) error {
err := conn.Send(msg)
s.mu.Lock()
defer s.mu.Unlock()
if errors.Is(err, io.EOF) {
return err
}
if err != nil {
s.error = err
// If error add it to the attributes because the stream is about to terminate.
// If no error don't add anything because status only exists at end of stream.
if statusCode, ok := statusCodeAttribute(s.protocol, err); ok {
s.addAttributes(statusCode)
}
}
protomsg, ok := msg.(proto.Message)
size := proto.Size(protomsg)
if !s.omitTraceEvents {
s.sentCounter++
s.event(ctx, semconv.MessageTypeSent, s.sentCounter, ok, size)
}
s.sendSize.Record(ctx, int64(size), metric.WithAttributes(s.attributes...))
s.sendsPerRPC.Record(ctx, 1, metric.WithAttributes(s.attributes...))
return err
}
func (s *streamingState) event(ctx context.Context, messageType attribute.KeyValue, messageID int, msgOk bool, size int) {
span := trace.SpanFromContext(ctx)
if msgOk {
span.AddEvent("message", trace.WithAttributes(s.attributeFilter.filter(
s.req,
messageType,
semconv.MessageUncompressedSizeKey.Int(size),
semconv.MessageIDKey.Int(messageID),
)...))
} else {
span.AddEvent("message", trace.WithAttributes(s.attributeFilter.filter(
s.req,
messageType,
semconv.MessageIDKey.Int(messageID),
)...))
}
}