Skip to content

Publish and Subscribe Audit Events

Vijay Nalawade edited this page Aug 8, 2019 · 4 revisions

With many contributions being added to the OSS, it is very useful to gather events for tracing/monitoring purposes. In OSS 0.5.7, we added new mechanism that can allow contributions to publish events or subscribe to events.

Publish Event

Any contribution can publish an event as shown below:

import "github.com/project-flogo/core/engine/event"

.....
event.PostEvent(<EVENT_TYPE>, <EVENT_DATA>)

For better performance, ensure that at-least one listener is registered for given event type by calling event.HasListener(<EVENT_TYPE>) before constructing event data.

To turn off events, set FLOGO_PUBLISH_AUDIT_EVENTS=false

Reference Implementation(Flow action)

https://github.com/project-flogo/flow/tree/master/support/event/flowevents.go https://github.com/project-flogo/flow/tree/master/instance/instance.go https://github.com/project-flogo/flow/tree/master/instance/taskinst.go

Register Event Listener

You can register a listener for the set of event types as shown below:

package mylistener
import github.com/project-flogo/core/engine/event

func init() {
	event.RegisterEventListener(&MyListener{name: "Listener1"}, []string{<EVENT_TYPE1>,<EVENT_TYPE2>})
}

// event.EventListener implementation
type MyListener struct {
	name   string
}

func (ls *MyListener) Name() string {
	return ls.name
}

func (ls *MyListener) HandleEvent(evt *core.EventContext) error {
        switch evt.GetType() {
	case <EVENT_TYPE1>:
	case <EVENT_TYPE2>:
	}
	return nil
}

Example

In below example, listener is registered for Flow and Task events published by FLOW action.

package main

import (
	core "github.com/project-flogo/core/engine/event"
	"github.com/project-flogo/core/support/log"
	flow "github.com/project-flogo/flow/support/event"
)

func init() {
	core.RegisterEventListener(&MyListener{name: "Listener1"}, []string{flow.FLOW_EVENT_TYPE, flow.TASK_EVENT_TYPE})
}

type SmapleListener struct {
	name   string
}

func (ls *SmapleListener) Name() string {
	return ls.name
}

func (ls *SmapleListener) HandleEvent(evt *core.EventContext) error {
	// Handle flow and task events
	switch t := evt.GetEvent().(type) {
	case flow.FlowEvent:
		logger.Infof("Name: %s, ID: %s, Status: %s ", t.FlowName(), t.FlowID(), t.FlowStatus())
	case flow.TaskEvent:
		logger.Infof("Name: %s, FID: %s, Status: %s", t.TaskName(), t.FlowID(), t.TaskStatus())
	}
	return nil
}