bevel

package module
v1.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2016 License: Apache-2.0 Imports: 8 Imported by: 0

README

bevel

Build Status

An angle on Business Event Logger in Golang.

bevel is a simple and extensible module that provides a generalised approach to logging business events in a larger application. Events can be vehicled to any number of writers (file, Kafka topic, etc).

Examples are supplied to get started in minutes.

An application only needs to create a message for the business event and post it to the bevel bus. The bus will relay the messages to the registered writers. That's all it takes!

By design, bevel can be used as an event loop dispatcher: the listeners are the bevel writers and the dispatcher is the bevel Manager (where events are sent to for disptaching).

Installation

To install, use the standard Go installation procedure:

go get github.com/seborama/bevel

You can pick a specific major release for compatibility. For example, to use a v1.x release, use this command:

go get gopkg.in/seborama/bevel.v1

And your source code would use this import:

import "gopkg.in/seborama/bevel.v1"

Documentation

The code documentation can be found on godoc.

Project contents

  • Bevel:
    • Listener
    • WriterPool
  • Writers:
    • ConsoleBEWriter - An example Console writer.
    • KafkaBEWriter - An example Kafka writer.

High level architecture

Messages are posted to the Manager's listener loop.

The listener passes messages to each of the registered Writers.

Writers are then free to process and persist messages as they please.

                           Manager                             Writers
                                                         ___________________
                                        Write(Message)  |                   |
                                     />>>>>>>>>>>>>>>>>>|  KafkaBEWriter    |
                                   //                   |___________________|
                         __________||                    ___________________
         Post(Message)  |            |  Write(Message)  |                   |
Message  >>>>>>>>>>>>>  |  Listener  |>>>>>>>>>>>>>>>>>>|  ConsoleBEWriter  |
                        |____________|                  |___________________|
                                   ||                    ___________________
                                   \\   Write(Message)  |                   |
                                     \>>>>>>>>>>>>>>>>>>|  Other BE Writer  |
                                                        |___________________|

Usage

For a simple example of usage, please see main_test.go.

The example defines a CounterMsg that acts as a business event.

Step 1 - Create a Message structure

To get started, we create a message structure to hold our information about the Business Event.

Our message must embed the StandardMessage structure as demonstrated below.

StandardMessage implements Message, an interface that is consumed by Writer implementations.

type CounterMsg struct {
    bevel.StandardMessage
    Counter int
}

In this example, our CounterMsg simply holds a Counter.

Step 2 - Ignite the event listener

We now need to create a listener to receive our CounterMsg (which is a Message implementor).

    bem := bevel.StartNewListener(&bevel.ConsoleBEWriter{})
    defer func() {
        bem.Done()
    }()

This function is at the heart of bevel, the Business Events Logger and performs these actions:

  1. It registers the supplied Writer (in this instance a simple Console Writer called) in the WriterPool.
  2. It creates a Manager and starts the Manager's listener.
  3. Finally, it returns the Manager for our use.

In addition to running the main Business Event listener, a Manager offer convenient services:

  • To Post() messages to the listener.
  • To instruct the Manager to terminate gracefully - via Done().
  • To add more Writer's to the Manager's WriterPool - via AddWriter().
Step 3 - Optionally register more Writers

We can optionally add all the Writer's we wish our listener to write messages to.

Writers are flexible and may:

  • use different persistence: log files, databases, message queues (RabbitMQ, Kafka, etc).
  • define the data format: CSV, plain text, table columns, queues, etc.
  • filter what messages they wish to persist, from a criteria based on the contents of the message: importance, source/type, etc.
Step 4 - Start generating messages!

We're now ready to send a few Message's to our listener:

    for i := 1; i <= 5; i++ {
        m := CounterMsg{
            StandardMessage: bevel.StandardMessage{
                EventName:         "test_event",
                CreatedTSUnixNano: time.Now().UnixNano(),
            },
            Counter: i,
        }

        bem.Post(m)
    }

The key method to note here is Post().

Step 5 - When we're done, let the listener know via the Manager

This was already prepared via defer above, at step 2.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnableToSendMessage = errors.New("unable to send message")

ErrUnableToSendMessage occurs when the message could not be sent to the external queue (Apache Kafka, etc).

View Source
var ErrUnableToTranscodeMessage = errors.New("unable to transcode message")

ErrUnableToTranscodeMessage occurs when the content of the message could not be transcoded by Write() for output.

Functions

This section is empty.

Types

type Closer

type Closer interface {
	Close() error
}

Closer is an interface that defines the operations that Closer implementors must adhere to.

type ConsoleBEWriter

type ConsoleBEWriter struct{}

ConsoleBEWriter is a simple Writer implementation..

func (*ConsoleBEWriter) Write

func (bew *ConsoleBEWriter) Write(m Message) error

Write outputs the contents of Message to the console.

type Counter

type Counter struct {
	// contains filtered or unexported fields
}

Counter is used to count the number of messages that the Listener has processed since it was started.

func (*Counter) Get

func (c *Counter) Get() uint64

Get is a getter for the value of msgCounter.

func (*Counter) Inc

func (c *Counter) Inc()

Inc is a thread-safe incrementer of the value of msgCounter.

type KafkaBEWriter

type KafkaBEWriter struct {
	// contains filtered or unexported fields
}

KafkaBEWriter is an Apache Kafka mock Writer implementation..

func (*KafkaBEWriter) Close

func (bew *KafkaBEWriter) Close() error

Close shuts down the producer and flushes any messages it may have buffered. You must call this function before a producer object passes out of scope, as it may otherwise leak memory. You must call this before calling Close on the underlying client.

func (*KafkaBEWriter) Write

func (bew *KafkaBEWriter) Write(m Message) error

Write outputs the contents of Message to a Kafka topic.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager holds the properties needed by the business event logger. It is the receiver of utility methods for consumers.

func StartNewListener

func StartNewListener(w Writer) *Manager

StartNewListener creates a new business event bus and adds a Writer to the WriterPool.

func (*Manager) AddWriter

func (bem *Manager) AddWriter(w Writer)

AddWriter adds a Writer to the WriterPool.

func (*Manager) Done

func (bem *Manager) Done()

Done sends the listener a message to terminate.

func (*Manager) Post

func (bem *Manager) Post(m Message)

Post sends a Message to the business event message bus for ingestion by all Writer's in the WriterPool.

func (*Manager) String

func (bem *Manager) String() string

String implements Stringer.

type Message

type Message interface{}

Message is a high level interface that all Business Events must implement.

type MessageEnvelop

type MessageEnvelop struct {
	ProcessedTSUnixNano int64
	Message             Message
}

MessageEnvelop wraps Message with additional info.

type StandardMessage

type StandardMessage struct {
	EventName         string
	CreatedTSUnixNano int64
}

StandardMessage is the first implementor of Message. This represents acts as a header for all Message's.

type Writer

type Writer interface {
	Write(Message) error
}

Writer is an interface that defines the operations that Writer implementors must adhere to. This is used by WriterPool.Write() to write the message contents.

func NewKafkaBEWriter

func NewKafkaBEWriter(addrs []string, config *sarama.Config, topic string) Writer

NewKafkaBEWriter creates a new KafkaBEWriter using the given broker addresses and configuration.

type WriterPool

type WriterPool struct {
	sync.Mutex
	// contains filtered or unexported fields
}

WriterPool is a thread-safe pool of Writer's.

func NewWriterPool

func NewWriterPool() *WriterPool

NewWriterPool creates a new WriterPool.

func (*WriterPool) AddWriter

func (bewp *WriterPool) AddWriter(w Writer)

AddWriter adds a Writer to the pool of Writer's.

func (*WriterPool) String

func (bewp *WriterPool) String() string

String is an implementation of Stringer.

func (*WriterPool) Write

func (bewp *WriterPool) Write(m Message)

Writer iterates through the pool of Writer's and and calls Write() on each one of them in a thread-safe manner. Thread-safety is achieved independently per Writer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL