File size: 6,324 Bytes
d6f631f | 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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | package flushhandler
import (
"context"
"errors"
"fmt"
"log/slog"
"sync"
"sync/atomic"
"time"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"github.com/openmeterio/openmeter/openmeter/sink/models"
)
const (
defaultFlushChanSize = 1000
defaultCallbackTimeout = 30 * time.Second
)
type FlushEventHandlerOptions struct {
Name string
Callback FlushCallback
Logger *slog.Logger
MetricMeter metric.Meter
DrainTimeout time.Duration
CallbackTimeout time.Duration
}
var _ FlushEventHandler = (*flushEventHandler)(nil)
type flushEventHandler struct {
name string
events chan []models.SinkMessage
eventsClose func()
stopChan chan struct{}
stopChanClose func()
drainDone chan struct{}
drainDoneClose func()
callback FlushCallback
callbackTimeout time.Duration
drainTimeout time.Duration
metrics *metrics
logger *slog.Logger
isShutdown atomic.Bool
mu sync.Mutex
}
func NewFlushEventHandler(opts FlushEventHandlerOptions) (FlushEventHandler, error) {
// validate options
if opts.Name == "" {
return nil, errors.New("name is required")
}
if opts.Callback == nil {
return nil, errors.New("callback is required")
}
if opts.Logger == nil {
return nil, errors.New("logger is required")
}
if opts.MetricMeter == nil {
return nil, errors.New("metric meter is required")
}
if opts.CallbackTimeout == 0 {
opts.CallbackTimeout = defaultCallbackTimeout
}
if opts.DrainTimeout == 0 {
opts.DrainTimeout = defaultCallbackTimeout
}
// construct underlying object
metrics, err := newMetrics(opts.Name, opts.MetricMeter)
if err != nil {
return nil, err
}
events := make(chan []models.SinkMessage, defaultFlushChanSize)
eventsClose := sync.OnceFunc(func() {
close(events)
})
stopChan := make(chan struct{})
stopChanClose := sync.OnceFunc(func() {
close(stopChan)
})
drainDone := make(chan struct{})
drainDoneClose := sync.OnceFunc(func() {
close(drainDone)
})
return &flushEventHandler{
callback: opts.Callback,
callbackTimeout: opts.CallbackTimeout,
drainTimeout: opts.DrainTimeout,
name: opts.Name,
events: events,
eventsClose: eventsClose,
stopChan: stopChan,
stopChanClose: stopChanClose,
drainDone: drainDone,
drainDoneClose: drainDoneClose,
metrics: metrics,
logger: opts.Logger,
}, nil
}
func (f *flushEventHandler) Close() error {
if f.isShutdown.Swap(true) {
return nil
}
// Close control channel
f.stopChanClose()
// Acquire lock to avoid closing events channel while there is an ongoing OnFlushSuccess operation
f.mu.Lock()
defer f.mu.Unlock()
// Close events channel in order to avoid readers getting blocked
f.eventsClose()
return nil
}
func (f *flushEventHandler) Start(ctx context.Context) error {
go f.start(ctx)
return nil
}
func (f *flushEventHandler) start(ctx context.Context) {
defer f.drainDoneClose()
if f.isShutdown.Load() {
f.logger.ErrorContext(ctx, "failed to start flush event handler as it is already shut down")
return
}
// Capture the trace span from the start context so callbacks can be linked
// to the parent trace even though they use context.Background() for cancellation isolation.
parentSpan := trace.SpanFromContext(ctx)
for !f.isShutdown.Load() {
select {
case event := <-f.events:
if err := f.invokeCallbackWithTimeout(parentSpan, event); err != nil {
f.logger.ErrorContext(ctx, "failed to invoke callback", "error", err)
}
case <-ctx.Done():
_ = f.Close()
case <-f.stopChan:
_ = f.Close()
}
}
// let's drain the queue using a new context, as the parent context is already canceled
drainContext, cancel := context.WithTimeout(context.Background(), f.drainTimeout)
defer cancel()
// Attach trace context to drain context so drain callbacks are also linked to the parent trace.
drainContext = trace.ContextWithSpan(drainContext, parentSpan)
// NOTE: this will block if the events channel is not closed
for event := range f.events {
if err := f.invokeCallback(drainContext, event); err != nil {
f.logger.ErrorContext(ctx, "failed to invoke callback", "error", err)
}
}
}
func (f *flushEventHandler) invokeCallbackWithTimeout(parentSpan trace.Span, events []models.SinkMessage) error {
// We are using a background context here, as if the parent context is canceled, we still want to
// allow the callbacks to call external systems. In exchange we are limiting the work with a timeout.
ctx, cancel := context.WithTimeout(context.Background(), f.callbackTimeout)
defer cancel()
// Propagate trace context so callback spans are linked to the parent trace.
ctx = trace.ContextWithSpan(ctx, parentSpan)
return f.invokeCallback(ctx, events)
}
func (f *flushEventHandler) invokeCallback(ctx context.Context, events []models.SinkMessage) error {
startTime := time.Now()
if err := f.callback(ctx, events); err != nil {
f.metrics.eventsFailed.Add(ctx, 1)
return err
}
f.metrics.eventProcessingTime.Record(ctx, time.Since(startTime).Milliseconds())
f.metrics.eventsProcessed.Add(ctx, 1)
return nil
}
func (f *flushEventHandler) OnFlushSuccess(ctx context.Context, event []models.SinkMessage) error {
if f.isShutdown.Load() {
return errors.New("handler is shutting down")
}
f.mu.Lock()
defer f.mu.Unlock()
select {
case <-f.stopChan:
return fmt.Errorf("handler is shutting down")
case f.events <- event:
f.metrics.eventsReceived.Add(ctx, 1)
case <-ctx.Done():
f.metrics.eventsFailed.Add(ctx, 1)
return fmt.Errorf("context canceled handler: %s", f.name)
default:
f.logger.ErrorContext(ctx, "flush handler: work queue full, callback might be hanging", "event", event, "name", f.name)
f.metrics.eventChannelFull.Add(ctx, 1)
select {
case <-f.stopChan:
return fmt.Errorf("handler is shutting down")
case f.events <- event:
f.metrics.eventsReceived.Add(ctx, 1)
case <-ctx.Done():
f.metrics.eventsFailed.Add(ctx, 1)
return fmt.Errorf("context canceled handler: %s", f.name)
}
}
return nil
}
func (f *flushEventHandler) WaitForDrain(ctx context.Context) error {
select {
case <-f.drainDone:
return nil
case <-ctx.Done():
return fmt.Errorf("context canceled while wainting for drain in handler %s", f.name)
}
}
|