/
exporter.go
221 lines (186 loc) · 6.39 KB
/
exporter.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright The OpenTelemetry Authors
//
// 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 arrow // import "github.com/f5/otel-arrow-adapter/collector/gen/exporter/otlpexporter/internal/arrow"
import (
"context"
"errors"
"sync"
arrowpb "github.com/f5/otel-arrow-adapter/api/collector/arrow/v1"
arrowRecord "github.com/f5/otel-arrow-adapter/pkg/otel/arrow_record"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"go.opentelemetry.io/collector/component"
)
// Exporter is 1:1 with exporter, isolates arrow-specific
// functionality.
type Exporter struct {
// numStreams is the number of streams that will be used.
numStreams int
// disableDowngrade prevents downgrade from occurring, supports
// forcing Arrow transport.
disableDowngrade bool
// telemetry includes logger, tracer, meter.
telemetry component.TelemetrySettings
// grpcOptions includes options used by the unary RPC methods,
// e.g., WaitForReady.
grpcOptions []grpc.CallOption
// newProducer returns a real (or mock) Producer.
newProducer func() arrowRecord.ProducerAPI
// client uses the exporter's gRPC ClientConn (or is a mock, in tests).
client arrowpb.ArrowStreamServiceClient
// perRPCCredentials derived from the exporter's gRPC auth settings.
perRPCCredentials credentials.PerRPCCredentials
// returning is used to pass broken, gracefully-terminated,
// and otherwise to the stream controller.
returning chan *Stream
// ready prioritizes streams that are ready to send
ready *streamPrioritizer
// cancel cancels the background context of this
// Exporter, used for shutdown.
cancel context.CancelFunc
// wg counts one per active goroutine belonging to all strings
// of this exporter. The wait group has Add(1) called before
// starting goroutines so that they can be properly waited for
// in shutdown(), so the pattern is:
//
// wg.Add(1)
// go func() {
// defer wg.Done()
// ...
// }()
wg sync.WaitGroup
}
// NewExporter configures a new Exporter.
func NewExporter(
numStreams int,
disableDowngrade bool,
telemetry component.TelemetrySettings,
grpcOptions []grpc.CallOption,
newProducer func() arrowRecord.ProducerAPI,
client arrowpb.ArrowStreamServiceClient,
perRPCCredentials credentials.PerRPCCredentials,
) *Exporter {
return &Exporter{
numStreams: numStreams,
disableDowngrade: disableDowngrade,
telemetry: telemetry,
grpcOptions: grpcOptions,
newProducer: newProducer,
client: client,
perRPCCredentials: perRPCCredentials,
returning: make(chan *Stream, numStreams),
}
}
// Start creates the background context used by all streams and starts
// a stream controller, which initializes the initial set of streams.
func (e *Exporter) Start(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
e.cancel = cancel
e.wg.Add(1)
e.ready = newStreamPrioritizer(ctx, e.numStreams)
go e.runStreamController(ctx)
return nil
}
// runStreamController starts the initial set of streams, then waits for streams to
// terminate one at a time and restarts them. If streams come back with a nil
// client (meaning that OTLP+Arrow was not supported by the endpoint), it will
// not be restarted.
func (e *Exporter) runStreamController(bgctx context.Context) {
defer e.cancel()
defer e.wg.Done()
running := e.numStreams
// Start the initial number of streams
for i := 0; i < running; i++ {
e.wg.Add(1)
go e.runArrowStream(bgctx)
}
for {
select {
case stream := <-e.returning:
if stream.client != nil || e.disableDowngrade {
// The stream closed or broken. Restart it.
e.wg.Add(1)
go e.runArrowStream(bgctx)
continue
}
// Otherwise, the stream never got started. It was
// downgraded and senders will use the standard OTLP path.
running--
// None of the streams were able to connect to
// an Arrow endpoint.
if running == 0 {
e.telemetry.Logger.Info("could not establish arrow streams, downgrading to standard OTLP export")
e.ready.downgrade()
}
case <-bgctx.Done():
// We are shutting down.
return
}
}
}
// runArrowStream begins one gRPC stream using a child of the background context.
// If the stream connection is successful, this goroutine starts another goroutine
// to call writeStream() and performs readStream() itself. When the stream shuts
// down this call synchronously waits for and unblocks the consumers.
func (e *Exporter) runArrowStream(ctx context.Context) {
producer := e.newProducer()
stream := newStream(producer, e.ready, e.telemetry, e.perRPCCredentials)
defer func() {
if err := producer.Close(); err != nil {
e.telemetry.Logger.Error("arrow producer close:", zap.Error(err))
}
e.wg.Done()
e.returning <- stream
}()
stream.run(ctx, e.client, e.grpcOptions)
}
// SendAndWait tries to send using an Arrow stream. The results are:
//
// (true, nil): Arrow send: success at consumer
// (false, nil): Arrow is not supported by the server, caller expected to fallback.
// (true, non-nil): Arrow send: server response may be permanent or allow retry.
// (false, non-nil): Context timeout prevents retry.
//
// consumer should fall back to standard OTLP, (true, nil)
func (e *Exporter) SendAndWait(ctx context.Context, data interface{}) (bool, error) {
for {
var stream *Stream
var err error
select {
case <-ctx.Done():
err = ctx.Err()
case stream = <-e.ready.readyChannel():
}
if err != nil {
return false, err // a Context error
}
if stream == nil {
return false, nil // a downgraded connection
}
err = stream.SendAndWait(ctx, data)
if err != nil && errors.Is(err, ErrStreamRestarting) {
continue // an internal retry
}
// result from arrow server (may be nil, may be
// permanent, etc.)
return true, err
}
}
// Shutdown returns when all Arrow-associated goroutines have returned.
func (e *Exporter) Shutdown(ctx context.Context) error {
e.cancel()
e.wg.Wait()
return nil
}