fix: add more traces
This commit is contained in:
parent
c144c96a98
commit
5ab185df21
|
@ -44,6 +44,9 @@ func New(es *es.EventStore) (*Resolver, error) {
|
||||||
|
|
||||||
// Posts is the resolver for the events field.
|
// Posts is the resolver for the events field.
|
||||||
func (r *Resolver) Posts(ctx context.Context, streamID string, paging *PageInput) (*Connection, error) {
|
func (r *Resolver) Posts(ctx context.Context, streamID string, paging *PageInput) (*Connection, error) {
|
||||||
|
ctx, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
r.Mresolver_posts.Add(ctx, 1)
|
r.Mresolver_posts.Add(ctx, 1)
|
||||||
|
|
||||||
lis, err := r.es.Read(ctx, streamID, paging.GetIdx(0), paging.GetCount(30))
|
lis, err := r.es.Read(ctx, streamID, paging.GetIdx(0), paging.GetCount(30))
|
||||||
|
@ -53,6 +56,7 @@ func (r *Resolver) Posts(ctx context.Context, streamID string, paging *PageInput
|
||||||
|
|
||||||
edges := make([]Edge, 0, len(lis))
|
edges := make([]Edge, 0, len(lis))
|
||||||
for i := range lis {
|
for i := range lis {
|
||||||
|
span.AddEvent(fmt.Sprint("post ", i, " of ", len(lis)))
|
||||||
e := lis[i]
|
e := lis[i]
|
||||||
m := e.EventMeta()
|
m := e.EventMeta()
|
||||||
|
|
||||||
|
@ -89,6 +93,9 @@ func (r *Resolver) Posts(ctx context.Context, streamID string, paging *PageInput
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resolver) PostAdded(ctx context.Context, streamID string, after int64) (<-chan *PostEvent, error) {
|
func (r *Resolver) PostAdded(ctx context.Context, streamID string, after int64) (<-chan *PostEvent, error) {
|
||||||
|
ctx, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
r.Mresolver_post_added.Add(ctx, 1)
|
r.Mresolver_post_added.Add(ctx, 1)
|
||||||
|
|
||||||
es := r.es.EventStream()
|
es := r.es.EventStream()
|
||||||
|
|
|
@ -8,6 +8,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Init(ctx context.Context, name string) (context.Context, func() error) {
|
func Init(ctx context.Context, name string) (context.Context, func() error) {
|
||||||
|
ctx, span := Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
stop := [3]func() error{
|
stop := [3]func() error{
|
||||||
initLogger(name),
|
initLogger(name),
|
||||||
}
|
}
|
||||||
|
@ -20,8 +23,10 @@ func Init(ctx context.Context, name string) (context.Context, func() error) {
|
||||||
log.Println("flushing logs...")
|
log.Println("flushing logs...")
|
||||||
errs := make([]error, len(stop))
|
errs := make([]error, len(stop))
|
||||||
for i, fn := range stop {
|
for i, fn := range stop {
|
||||||
|
if fn != nil {
|
||||||
errs[i] = fn()
|
errs[i] = fn()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
log.Println("all stopped.")
|
log.Println("all stopped.")
|
||||||
return multierr.Combine(errs...)
|
return multierr.Combine(errs...)
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,6 +39,9 @@ const AppendOnly = es.AppendOnly
|
||||||
const AllEvents = es.AllEvents
|
const AllEvents = es.AllEvents
|
||||||
|
|
||||||
func Init(ctx context.Context) error {
|
func Init(ctx context.Context) error {
|
||||||
|
_, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
m := logz.Meter(ctx)
|
m := logz.Meter(ctx)
|
||||||
var err, errs error
|
var err, errs error
|
||||||
|
|
||||||
|
@ -80,8 +83,14 @@ func (d *diskStore) Open(ctx context.Context, dsn string) (driver.Driver, error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c, err := cache.NewWithEvict(CachSize, func(ctx context.Context, s string, l *lockedWal) {
|
c, err := cache.NewWithEvict(CachSize, func(ctx context.Context, s string, l *lockedWal) {
|
||||||
|
_, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
l.Modify(ctx, func(w *wal.Log) error {
|
l.Modify(ctx, func(w *wal.Log) error {
|
||||||
// logz.Mdisk_evict.Add(ctx, 1)
|
_, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
|
d.Mdisk_evict.Add(ctx, 1)
|
||||||
|
|
||||||
err := w.Close()
|
err := w.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -102,9 +111,15 @@ func (d *diskStore) Open(ctx context.Context, dsn string) (driver.Driver, error)
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
func (ds *diskStore) EventLog(ctx context.Context, streamID string) (driver.EventLog, error) {
|
func (ds *diskStore) EventLog(ctx context.Context, streamID string) (driver.EventLog, error) {
|
||||||
|
_, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
el := &eventLog{streamID: streamID}
|
el := &eventLog{streamID: streamID}
|
||||||
|
|
||||||
return el, ds.openlogs.Modify(ctx, func(openlogs *openlogs) error {
|
return el, ds.openlogs.Modify(ctx, func(openlogs *openlogs) error {
|
||||||
|
_, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
if events, ok := openlogs.logs.Get(streamID); ok {
|
if events, ok := openlogs.logs.Get(streamID); ok {
|
||||||
el.events = *events
|
el.events = *events
|
||||||
return nil
|
return nil
|
||||||
|
@ -128,10 +143,16 @@ type eventLog struct {
|
||||||
var _ driver.EventLog = (*eventLog)(nil)
|
var _ driver.EventLog = (*eventLog)(nil)
|
||||||
|
|
||||||
func (es *eventLog) Append(ctx context.Context, events event.Events, version uint64) (uint64, error) {
|
func (es *eventLog) Append(ctx context.Context, events event.Events, version uint64) (uint64, error) {
|
||||||
|
_, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
event.SetStreamID(es.streamID, events...)
|
event.SetStreamID(es.streamID, events...)
|
||||||
|
|
||||||
var count uint64
|
var count uint64
|
||||||
err := es.events.Modify(ctx, func(l *wal.Log) error {
|
err := es.events.Modify(ctx, func(l *wal.Log) error {
|
||||||
|
_, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
last, err := l.LastIndex()
|
last, err := l.LastIndex()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -145,6 +166,8 @@ func (es *eventLog) Append(ctx context.Context, events event.Events, version uin
|
||||||
|
|
||||||
batch := &wal.Batch{}
|
batch := &wal.Batch{}
|
||||||
for i, e := range events {
|
for i, e := range events {
|
||||||
|
span.AddEvent(fmt.Sprintf("append event %d of %d", i, len(events)))
|
||||||
|
|
||||||
b, err = event.MarshalText(e)
|
b, err = event.MarshalText(e)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -162,9 +185,15 @@ func (es *eventLog) Append(ctx context.Context, events event.Events, version uin
|
||||||
return count, err
|
return count, err
|
||||||
}
|
}
|
||||||
func (es *eventLog) Read(ctx context.Context, pos, count int64) (event.Events, error) {
|
func (es *eventLog) Read(ctx context.Context, pos, count int64) (event.Events, error) {
|
||||||
|
_, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
var events event.Events
|
var events event.Events
|
||||||
|
|
||||||
err := es.events.Modify(ctx, func(stream *wal.Log) error {
|
err := es.events.Modify(ctx, func(stream *wal.Log) error {
|
||||||
|
_, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
first, err := stream.FirstIndex()
|
first, err := stream.FirstIndex()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -186,6 +215,8 @@ func (es *eventLog) Read(ctx context.Context, pos, count int64) (event.Events, e
|
||||||
|
|
||||||
events = make([]event.Event, math.Abs(count))
|
events = make([]event.Event, math.Abs(count))
|
||||||
for i := range events {
|
for i := range events {
|
||||||
|
span.AddEvent(fmt.Sprintf("read event %d of %d", i, len(events)))
|
||||||
|
|
||||||
// ---
|
// ---
|
||||||
var b []byte
|
var b []byte
|
||||||
b, err = stream.Read(start)
|
b, err = stream.Read(start)
|
||||||
|
@ -219,6 +250,9 @@ func (es *eventLog) Read(ctx context.Context, pos, count int64) (event.Events, e
|
||||||
return events, err
|
return events, err
|
||||||
}
|
}
|
||||||
func (es *eventLog) FirstIndex(ctx context.Context) (uint64, error) {
|
func (es *eventLog) FirstIndex(ctx context.Context) (uint64, error) {
|
||||||
|
_, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
var idx uint64
|
var idx uint64
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
@ -230,6 +264,9 @@ func (es *eventLog) FirstIndex(ctx context.Context) (uint64, error) {
|
||||||
return idx, err
|
return idx, err
|
||||||
}
|
}
|
||||||
func (es *eventLog) LastIndex(ctx context.Context) (uint64, error) {
|
func (es *eventLog) LastIndex(ctx context.Context) (uint64, error) {
|
||||||
|
_, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
var idx uint64
|
var idx uint64
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/sour-is/ev/internal/logz"
|
||||||
"github.com/sour-is/ev/pkg/es"
|
"github.com/sour-is/ev/pkg/es"
|
||||||
"github.com/sour-is/ev/pkg/es/driver"
|
"github.com/sour-is/ev/pkg/es/driver"
|
||||||
"github.com/sour-is/ev/pkg/es/event"
|
"github.com/sour-is/ev/pkg/es/event"
|
||||||
|
@ -26,19 +27,31 @@ const AppendOnly = es.AppendOnly
|
||||||
const AllEvents = es.AllEvents
|
const AllEvents = es.AllEvents
|
||||||
|
|
||||||
func Init(ctx context.Context) {
|
func Init(ctx context.Context) {
|
||||||
|
ctx, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
es.Register(ctx, "mem", &memstore{})
|
es.Register(ctx, "mem", &memstore{})
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ driver.Driver = (*memstore)(nil)
|
var _ driver.Driver = (*memstore)(nil)
|
||||||
|
|
||||||
func (memstore) Open(_ context.Context, name string) (driver.Driver, error) {
|
func (memstore) Open(ctx context.Context, name string) (driver.Driver, error) {
|
||||||
|
_, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
s := &state{streams: make(map[string]*locker.Locked[event.Events])}
|
s := &state{streams: make(map[string]*locker.Locked[event.Events])}
|
||||||
return &memstore{locker.New(s)}, nil
|
return &memstore{locker.New(s)}, nil
|
||||||
}
|
}
|
||||||
func (m *memstore) EventLog(ctx context.Context, streamID string) (driver.EventLog, error) {
|
func (m *memstore) EventLog(ctx context.Context, streamID string) (driver.EventLog, error) {
|
||||||
|
ctx, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
el := &eventLog{streamID: streamID}
|
el := &eventLog{streamID: streamID}
|
||||||
|
|
||||||
err := m.state.Modify(ctx, func(state *state) error {
|
err := m.state.Modify(ctx, func(state *state) error {
|
||||||
|
_, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
l, ok := state.streams[streamID]
|
l, ok := state.streams[streamID]
|
||||||
if !ok {
|
if !ok {
|
||||||
l = locker.New(&event.Events{})
|
l = locker.New(&event.Events{})
|
||||||
|
@ -57,15 +70,23 @@ var _ driver.EventLog = (*eventLog)(nil)
|
||||||
|
|
||||||
// Append implements driver.EventStore
|
// Append implements driver.EventStore
|
||||||
func (m *eventLog) Append(ctx context.Context, events event.Events, version uint64) (uint64, error) {
|
func (m *eventLog) Append(ctx context.Context, events event.Events, version uint64) (uint64, error) {
|
||||||
|
ctx, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
event.SetStreamID(m.streamID, events...)
|
event.SetStreamID(m.streamID, events...)
|
||||||
|
|
||||||
return uint64(len(events)), m.events.Modify(ctx, func(stream *event.Events) error {
|
return uint64(len(events)), m.events.Modify(ctx, func(stream *event.Events) error {
|
||||||
|
_, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
last := uint64(len(*stream))
|
last := uint64(len(*stream))
|
||||||
if version != AppendOnly && version != last {
|
if version != AppendOnly && version != last {
|
||||||
return fmt.Errorf("current version wrong %d != %d", version, last)
|
return fmt.Errorf("current version wrong %d != %d", version, last)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range events {
|
for i := range events {
|
||||||
|
span.AddEvent(fmt.Sprintf("read event %d of %d", i, len(events)))
|
||||||
|
|
||||||
pos := last + uint64(i) + 1
|
pos := last + uint64(i) + 1
|
||||||
event.SetPosition(events[i], pos)
|
event.SetPosition(events[i], pos)
|
||||||
*stream = append(*stream, events[i])
|
*stream = append(*stream, events[i])
|
||||||
|
@ -77,9 +98,15 @@ func (m *eventLog) Append(ctx context.Context, events event.Events, version uint
|
||||||
|
|
||||||
// Read implements driver.EventStore
|
// Read implements driver.EventStore
|
||||||
func (es *eventLog) Read(ctx context.Context, pos int64, count int64) (event.Events, error) {
|
func (es *eventLog) Read(ctx context.Context, pos int64, count int64) (event.Events, error) {
|
||||||
|
ctx, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
var events event.Events
|
var events event.Events
|
||||||
|
|
||||||
err := es.events.Modify(ctx, func(stream *event.Events) error {
|
err := es.events.Modify(ctx, func(stream *event.Events) error {
|
||||||
|
_, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
first := stream.First().EventMeta().Position
|
first := stream.First().EventMeta().Position
|
||||||
last := stream.Last().EventMeta().Position
|
last := stream.Last().EventMeta().Position
|
||||||
// ---
|
// ---
|
||||||
|
@ -94,6 +121,7 @@ func (es *eventLog) Read(ctx context.Context, pos int64, count int64) (event.Eve
|
||||||
|
|
||||||
events = make([]event.Event, math.Abs(count))
|
events = make([]event.Event, math.Abs(count))
|
||||||
for i := range events {
|
for i := range events {
|
||||||
|
span.AddEvent(fmt.Sprintf("read event %d of %d", i, math.Abs(count)))
|
||||||
// ---
|
// ---
|
||||||
events[i] = (*stream)[start-1]
|
events[i] = (*stream)[start-1]
|
||||||
// ---
|
// ---
|
||||||
|
@ -122,12 +150,18 @@ func (es *eventLog) Read(ctx context.Context, pos int64, count int64) (event.Eve
|
||||||
|
|
||||||
// FirstIndex for the streamID
|
// FirstIndex for the streamID
|
||||||
func (m *eventLog) FirstIndex(ctx context.Context) (uint64, error) {
|
func (m *eventLog) FirstIndex(ctx context.Context) (uint64, error) {
|
||||||
|
_, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
events, err := m.events.Copy(ctx)
|
events, err := m.events.Copy(ctx)
|
||||||
return events.First().EventMeta().Position, err
|
return events.First().EventMeta().Position, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// LastIndex for the streamID
|
// LastIndex for the streamID
|
||||||
func (m *eventLog) LastIndex(ctx context.Context) (uint64, error) {
|
func (m *eventLog) LastIndex(ctx context.Context) (uint64, error) {
|
||||||
|
_, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
events, err := m.events.Copy(ctx)
|
events, err := m.events.Copy(ctx)
|
||||||
return events.Last().EventMeta().Position, err
|
return events.Last().EventMeta().Position, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package streamer
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/sour-is/ev/internal/logz"
|
||||||
"github.com/sour-is/ev/pkg/es"
|
"github.com/sour-is/ev/pkg/es"
|
||||||
"github.com/sour-is/ev/pkg/es/driver"
|
"github.com/sour-is/ev/pkg/es/driver"
|
||||||
"github.com/sour-is/ev/pkg/es/event"
|
"github.com/sour-is/ev/pkg/es/event"
|
||||||
|
@ -19,6 +20,9 @@ type streamer struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(ctx context.Context) *streamer {
|
func New(ctx context.Context) *streamer {
|
||||||
|
ctx, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
return &streamer{state: locker.New(&state{subscribers: map[string][]*subscription{}})}
|
return &streamer{state: locker.New(&state{subscribers: map[string][]*subscription{}})}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,9 +39,15 @@ func (s *streamer) Unwrap() driver.Driver {
|
||||||
var _ driver.Driver = (*streamer)(nil)
|
var _ driver.Driver = (*streamer)(nil)
|
||||||
|
|
||||||
func (s *streamer) Open(ctx context.Context, dsn string) (driver.Driver, error) {
|
func (s *streamer) Open(ctx context.Context, dsn string) (driver.Driver, error) {
|
||||||
|
ctx, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
return s.up.Open(ctx, dsn)
|
return s.up.Open(ctx, dsn)
|
||||||
}
|
}
|
||||||
func (s *streamer) EventLog(ctx context.Context, streamID string) (driver.EventLog, error) {
|
func (s *streamer) EventLog(ctx context.Context, streamID string) (driver.EventLog, error) {
|
||||||
|
ctx, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
l, err := s.up.EventLog(ctx, streamID)
|
l, err := s.up.EventLog(ctx, streamID)
|
||||||
return &wrapper{streamID, l, s}, err
|
return &wrapper{streamID, l, s}, err
|
||||||
}
|
}
|
||||||
|
@ -45,6 +55,9 @@ func (s *streamer) EventLog(ctx context.Context, streamID string) (driver.EventL
|
||||||
var _ driver.EventStream = (*streamer)(nil)
|
var _ driver.EventStream = (*streamer)(nil)
|
||||||
|
|
||||||
func (s *streamer) Subscribe(ctx context.Context, streamID string, start int64) (driver.Subscription, error) {
|
func (s *streamer) Subscribe(ctx context.Context, streamID string, start int64) (driver.Subscription, error) {
|
||||||
|
ctx, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
events, err := s.up.EventLog(ctx, streamID)
|
events, err := s.up.EventLog(ctx, streamID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -62,9 +75,18 @@ func (s *streamer) Subscribe(ctx context.Context, streamID string, start int64)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
func (s *streamer) Send(ctx context.Context, streamID string, events event.Events) error {
|
func (s *streamer) Send(ctx context.Context, streamID string, events event.Events) error {
|
||||||
|
ctx, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
return s.state.Modify(ctx, func(state *state) error {
|
return s.state.Modify(ctx, func(state *state) error {
|
||||||
|
ctx, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
for _, sub := range state.subscribers[streamID] {
|
for _, sub := range state.subscribers[streamID] {
|
||||||
err := sub.position.Modify(ctx, func(position *position) error {
|
err := sub.position.Modify(ctx, func(position *position) error {
|
||||||
|
_, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
position.size = int64(events.Last().EventMeta().Position - uint64(position.idx))
|
position.size = int64(events.Last().EventMeta().Position - uint64(position.idx))
|
||||||
|
|
||||||
if position.wait != nil {
|
if position.wait != nil {
|
||||||
|
@ -83,10 +105,16 @@ func (s *streamer) Send(ctx context.Context, streamID string, events event.Event
|
||||||
|
|
||||||
func (s *streamer) delete(streamID string, sub *subscription) func(context.Context) error {
|
func (s *streamer) delete(streamID string, sub *subscription) func(context.Context) error {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
|
ctx, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
if err := ctx.Err(); err != nil {
|
if err := ctx.Err(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return s.state.Modify(ctx, func(state *state) error {
|
return s.state.Modify(ctx, func(state *state) error {
|
||||||
|
_, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
lis := state.subscribers[streamID]
|
lis := state.subscribers[streamID]
|
||||||
for i := range lis {
|
for i := range lis {
|
||||||
if lis[i] == sub {
|
if lis[i] == sub {
|
||||||
|
@ -110,10 +138,16 @@ type wrapper struct {
|
||||||
var _ driver.EventLog = (*wrapper)(nil)
|
var _ driver.EventLog = (*wrapper)(nil)
|
||||||
|
|
||||||
func (w *wrapper) Read(ctx context.Context, pos int64, count int64) (event.Events, error) {
|
func (w *wrapper) Read(ctx context.Context, pos int64, count int64) (event.Events, error) {
|
||||||
|
ctx, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
return w.up.Read(ctx, pos, count)
|
return w.up.Read(ctx, pos, count)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *wrapper) Append(ctx context.Context, events event.Events, version uint64) (uint64, error) {
|
func (w *wrapper) Append(ctx context.Context, events event.Events, version uint64) (uint64, error) {
|
||||||
|
ctx, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
i, err := w.up.Append(ctx, events, version)
|
i, err := w.up.Append(ctx, events, version)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return i, err
|
return i, err
|
||||||
|
@ -122,10 +156,16 @@ func (w *wrapper) Append(ctx context.Context, events event.Events, version uint6
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *wrapper) FirstIndex(ctx context.Context) (uint64, error) {
|
func (w *wrapper) FirstIndex(ctx context.Context) (uint64, error) {
|
||||||
|
ctx, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
return w.up.FirstIndex(ctx)
|
return w.up.FirstIndex(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *wrapper) LastIndex(ctx context.Context) (uint64, error) {
|
func (w *wrapper) LastIndex(ctx context.Context) (uint64, error) {
|
||||||
|
ctx, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
return w.up.LastIndex(ctx)
|
return w.up.LastIndex(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,14 +185,24 @@ type subscription struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *subscription) Recv(ctx context.Context) bool {
|
func (s *subscription) Recv(ctx context.Context) bool {
|
||||||
|
ctx, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
var wait func(context.Context) bool
|
var wait func(context.Context) bool
|
||||||
|
|
||||||
err := s.position.Modify(ctx, func(position *position) error {
|
err := s.position.Modify(ctx, func(position *position) error {
|
||||||
|
_, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
if position.size == es.AllEvents {
|
if position.size == es.AllEvents {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if position.size == 0 {
|
if position.size == 0 {
|
||||||
position.wait = make(chan struct{})
|
position.wait = make(chan struct{})
|
||||||
wait = func(ctx context.Context) bool {
|
wait = func(ctx context.Context) bool {
|
||||||
|
ctx, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-position.wait:
|
case <-position.wait:
|
||||||
|
|
||||||
|
@ -178,8 +228,14 @@ func (s *subscription) Recv(ctx context.Context) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
func (s *subscription) Events(ctx context.Context) (event.Events, error) {
|
func (s *subscription) Events(ctx context.Context) (event.Events, error) {
|
||||||
|
ctx, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
var events event.Events
|
var events event.Events
|
||||||
return events, s.position.Modify(ctx, func(position *position) error {
|
return events, s.position.Modify(ctx, func(position *position) error {
|
||||||
|
ctx, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
events, err = s.events.Read(ctx, position.idx, position.size)
|
events, err = s.events.Read(ctx, position.idx, position.size)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -193,5 +249,8 @@ func (s *subscription) Events(ctx context.Context) (event.Events, error) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
func (s *subscription) Close(ctx context.Context) error {
|
func (s *subscription) Close(ctx context.Context) error {
|
||||||
|
ctx, span := logz.Span(ctx)
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
return s.unsub(ctx)
|
return s.unsub(ctx)
|
||||||
}
|
}
|
||||||
|
|
|
@ -111,6 +111,7 @@ func (s *service) get(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
func (s *service) post(w http.ResponseWriter, r *http.Request) {
|
func (s *service) post(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
|
|
||||||
ctx, span := logz.Span(ctx)
|
ctx, span := logz.Span(ctx)
|
||||||
defer span.End()
|
defer span.End()
|
||||||
|
|
||||||
|
@ -127,7 +128,8 @@ func (s *service) post(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
b, err := io.ReadAll(io.LimitReader(r.Body, 64*1024))
|
b, err := io.ReadAll(io.LimitReader(r.Body, 64*1024))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
span.RecordError(err)
|
||||||
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -142,9 +144,10 @@ func (s *service) post(w http.ResponseWriter, r *http.Request) {
|
||||||
Payload: b,
|
Payload: b,
|
||||||
Tags: fields(tags),
|
Tags: fields(tags),
|
||||||
})
|
})
|
||||||
_, err = s.es.Append(r.Context(), "post-"+name, events)
|
|
||||||
|
_, err = s.es.Append(ctx, "post-"+name, events)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
span.RecordError(err)
|
||||||
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -155,13 +158,14 @@ func (s *service) post(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
m := events.First().EventMeta()
|
m := events.First().EventMeta()
|
||||||
log.Print("POST topic=", name, " tags=", tags, " idx=", m.Position, " id=", m.EventID)
|
span.AddEvent(fmt.Sprint("POST topic=", name, " tags=", tags, " idx=", m.Position, " id=", m.EventID))
|
||||||
|
// log.Print("POST topic=", name, " tags=", tags, " idx=", m.Position, " id=", m.EventID)
|
||||||
|
|
||||||
w.WriteHeader(http.StatusAccepted)
|
w.WriteHeader(http.StatusAccepted)
|
||||||
if strings.Contains(r.Header.Get("Accept"), "application/json") {
|
if strings.Contains(r.Header.Get("Accept"), "application/json") {
|
||||||
w.Header().Add("Content-Type", "application/json")
|
w.Header().Add("Content-Type", "application/json")
|
||||||
if err = encodeJSON(w, first, events...); err != nil {
|
if err = encodeJSON(w, first, events...); err != nil {
|
||||||
log.Print(err)
|
span.RecordError(err)
|
||||||
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -169,6 +173,7 @@ func (s *service) post(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
span.AddEvent("finish response")
|
||||||
|
|
||||||
w.Header().Add("Content-Type", "text/plain")
|
w.Header().Add("Content-Type", "text/plain")
|
||||||
fmt.Fprintf(w, "OK %d %s", m.Position, m.EventID)
|
fmt.Fprintf(w, "OK %d %s", m.Position, m.EventID)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user