-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial event sink implementation
- Loading branch information
Showing
12 changed files
with
498 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package eventlog | ||
|
||
import ( | ||
cloudevents "github.com/cloudevents/sdk-go/v2" | ||
"github.com/jsiebens/ionscale/internal/domain" | ||
"math/big" | ||
) | ||
|
||
const ( | ||
tailnetCreated = "ionscale.tailnet.created" | ||
tailnetDeleted = "ionscale.tailnet.deleted" | ||
nodeCreated = "ionscale.node.created" | ||
) | ||
|
||
func TailnetCreated(tailnet *domain.Tailnet, actor *domain.User) cloudevents.Event { | ||
data := &EventData{ | ||
Tailnet: &Target{ID: idToStr(tailnet.ID), Name: tailnet.Name}, | ||
Target: &Target{ID: idToStr(tailnet.ID), Name: tailnet.Name}, | ||
Actor: system, | ||
} | ||
|
||
if actor != nil { | ||
data.Actor = Actor{ID: idToStr(actor.ID), Name: actor.Name} | ||
} | ||
|
||
event := cloudevents.NewEvent() | ||
event.SetType(tailnetCreated) | ||
_ = event.SetData(cloudevents.ApplicationJSON, data) | ||
|
||
return event | ||
} | ||
|
||
func MachineCreated(machine *domain.Machine, actor *domain.User) cloudevents.Event { | ||
data := &EventData{ | ||
Tailnet: &Target{ID: idToStr(machine.Tailnet.ID), Name: machine.Tailnet.Name}, | ||
Target: &Target{ID: idToStr(machine.ID), Name: machine.CompleteName(), Addresses: machine.IPs()}, | ||
Actor: UserToActor(actor), | ||
} | ||
|
||
event := cloudevents.NewEvent() | ||
event.SetType(nodeCreated) | ||
_ = event.SetData(cloudevents.ApplicationJSON, data) | ||
|
||
return event | ||
} | ||
|
||
func UserToActor(actor *domain.User) Actor { | ||
if actor == nil { | ||
return system | ||
} | ||
|
||
switch actor.UserType { | ||
case domain.UserTypePerson: | ||
return Actor{ID: idToStr(actor.ID), Name: actor.Name} | ||
default: | ||
return system | ||
} | ||
} | ||
|
||
type EventData struct { | ||
Tailnet *Target `json:"tailnet,omitempty"` | ||
Target *Target `json:"target,omitempty"` | ||
Actor Actor `json:"actor"` | ||
} | ||
|
||
type Target struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
Addresses []string `json:"addresses,omitempty"` | ||
} | ||
|
||
type Actor struct { | ||
ID string `json:"id,omitempty"` | ||
Name string `json:"name"` | ||
} | ||
|
||
func idToStr(id uint64) string { | ||
return big.NewInt(int64(id)).Text(10) | ||
} | ||
|
||
var system = Actor{ID: "", Name: "ionscale system"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package eventlog | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
cloudevents "github.com/cloudevents/sdk-go/v2" | ||
"github.com/hashicorp/go-multierror" | ||
"github.com/jsiebens/ionscale/internal/config" | ||
"github.com/jsiebens/ionscale/internal/util" | ||
"go.uber.org/zap" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
"time" | ||
) | ||
|
||
const ( | ||
stdout = "/dev/stdout" | ||
stderr = "/dev/stderr" | ||
devnull = "/dev/null" | ||
) | ||
|
||
type Events []cloudevents.Event | ||
|
||
func (e *Events) Add(event cloudevents.Event) { | ||
x := append(*e, event) | ||
*e = x | ||
} | ||
|
||
type Eventer interface { | ||
Send(ctx context.Context, events ...cloudevents.Event) error | ||
} | ||
|
||
type eventer struct { | ||
source string | ||
sinks []sink | ||
} | ||
|
||
func (e *eventer) Send(ctx context.Context, events ...cloudevents.Event) error { | ||
groupID := util.NextIDString() | ||
now := time.Now() | ||
|
||
for _, event := range events { | ||
event.SetSource(e.source) | ||
event.SetID(util.NextIDString()) | ||
event.SetTime(now) | ||
event.SetExtension("eventGroupID", groupID) | ||
} | ||
|
||
var r *multierror.Error | ||
for _, s := range e.sinks { | ||
r = multierror.Append(r, s.process(ctx, events...)) | ||
} | ||
|
||
return r.ErrorOrNil() | ||
} | ||
|
||
type sink interface { | ||
process(context.Context, ...cloudevents.Event) error | ||
} | ||
|
||
var ( | ||
_globalMu sync.RWMutex | ||
_globalE Eventer = &eventer{} | ||
) | ||
|
||
func Configure(c *config.Config) error { | ||
var sinks []sink | ||
|
||
if c.Events.Log.Enabled { | ||
sinks = append(sinks, &zapSink{logger: zap.L().Named("events").WithOptions(zap.AddCallerSkip(3))}) | ||
} | ||
|
||
if c.Events.File.Enabled { | ||
switch c.Events.File.Path { | ||
case devnull: | ||
// ignore | ||
case stderr: | ||
sinks = append(sinks, &writerSink{w: os.Stderr}) | ||
case stdout: | ||
sinks = append(sinks, &writerSink{w: os.Stdout}) | ||
default: | ||
abs, err := filepath.Abs(c.Events.File.Path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
sinks = append(sinks, &fileSink{ | ||
path: abs, | ||
fileName: c.Events.File.FileName, | ||
maxBytes: c.Events.File.MaxBytes, | ||
maxDuration: c.Events.File.MaxDuration, | ||
maxFiles: c.Events.File.MaxFiles, | ||
}) | ||
} | ||
} | ||
|
||
_globalMu.Lock() | ||
defer _globalMu.Unlock() | ||
_globalE = &eventer{ | ||
source: c.ServerUrl, | ||
sinks: sinks, | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func Send(ctx context.Context, events ...cloudevents.Event) { | ||
_globalMu.RLock() | ||
l := _globalE | ||
_globalMu.RUnlock() | ||
|
||
if err := l.Send(ctx, events...); err != nil { | ||
zap.L().Error("error while processing event", zap.Error(err)) | ||
} | ||
} | ||
|
||
func writeJSONLine(w io.Writer, events ...cloudevents.Event) (int, error) { | ||
var payload bytes.Buffer | ||
|
||
for _, event := range events { | ||
eventJson, err := event.MarshalJSON() | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
payload.Write(eventJson) | ||
payload.Write([]byte("\n")) | ||
} | ||
|
||
return w.Write(payload.Bytes()) | ||
} |
Oops, something went wrong.