Skip to content

Commit

Permalink
Add source to OutgoingEvent
Browse files Browse the repository at this point in the history
This patch adds the Source field to the OutgoingEvent, so that
an operator can see which host an event came from.

Fixes: #23
Fixes: #22

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
alexellis committed Sep 9, 2019
1 parent 52a6f9c commit 0d02cc8
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pkg/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
type OutboundEvent struct {
Topic string `json:"topic,omitempty"`
Category string `json:"category,omitempty"`
Source string `json:"source"`

UserName string `json:"userName,omitempty"`
CreatedTime time.Time `json:"createdTime,omitempty"`
Expand Down Expand Up @@ -54,8 +55,10 @@ func Stream(ctx context.Context, c *vim25.Client, controller *types.Controller)
eventsPerPage := int32(1)
tail := true
force := true
source := c.URL().Host

recv := makeRecv(controller, m, source)

recv := makeRecv(controller, m)
err := m.Events(ctx, managedTypes, eventsPerPage, tail, force, recv)
if err != nil {
return errors.Wrap(err, "error connecting to event-stream")
Expand All @@ -64,14 +67,14 @@ func Stream(ctx context.Context, c *vim25.Client, controller *types.Controller)
}

// makeRecv returns a event handler function called by the event manager on each event
func makeRecv(controller *types.Controller, m *event.Manager) func(managedObjectReference vtypes.ManagedObjectReference, baseEvent []vtypes.BaseEvent) error {
func makeRecv(controller *types.Controller, m *event.Manager, source string) func(managedObjectReference vtypes.ManagedObjectReference, baseEvent []vtypes.BaseEvent) error {
return func(managedObjectReference vtypes.ManagedObjectReference, baseEvent []vtypes.BaseEvent) error {
log.Printf("Object %v", managedObjectReference)

for i, event := range baseEvent {
log.Printf("Event [%d] %v", i, event)

topic, message, err := handleEvent(event, m)
topic, message, err := handleEvent(event, m, source)
if err != nil {
log.Printf("error handling event: %s", err.Error())
continue
Expand All @@ -85,7 +88,7 @@ func makeRecv(controller *types.Controller, m *event.Manager) func(managedObject
}
}

func handleEvent(event vtypes.BaseEvent, m *event.Manager) (string, string, error) {
func handleEvent(event vtypes.BaseEvent, m *event.Manager, source string) (string, string, error) {
// Sanity check to avoid nil pointer exception
if event == nil {
return "", "", errors.New("event must not be nil")
Expand Down Expand Up @@ -114,6 +117,7 @@ func handleEvent(event vtypes.BaseEvent, m *event.Manager) (string, string, erro
CreatedTime: createdTime,
ObjectName: name,
ManagedObjectReference: ref,
Source: source,
})
if err != nil {
return "", "", errors.Wrap(err, "error marshaling outboundevent")
Expand Down

0 comments on commit 0d02cc8

Please sign in to comment.