lifecycle

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2019 License: Apache-2.0 Imports: 16 Imported by: 0

README

Choria Lifecycle Events

This package create and view Choria Lifecycle Events

These lifecycle events are published to the choria.lifecycle.event.<type>.<component> topic structure of the middleware and contains small JSON documents that informs listeners about significant life cycle events of Choria components.

GoDoc CircleCI

## Status

This project is versioned using SemVer and have reached version 1.0.0, it's in use by several Choria projects and will follow SemVer rules in future.

CloudEvents Version 1.0

Optional support for receiving and producing events in the CloudEvents format is supported.

Received events are automatically handled, to publish an event in CloudEvents format set event.SetFormat(lifecycle.CloudEventV1Format) before publishing it. To convert an event to CloudEvent format use lifecycle.ToCloudEventV1(event).

Supported Events

Event Description
Startup Event to emit when components start, requires Identity(), Component() and Version() options
Shutdown Event to emit when components shut down, requires Identity() and Component() options
Provisioned Event to emit after provisioning of a component, requires Identity() and Component() options
Alive Event to emit at regular intervals indicating it's still functional, requires Identity(), Component() and Version() options
Sample Events
Schemas

Event Schemas are stored in the Choria Schemas repository.

Startup
{
    "protocol":"io.choria.lifecycle.v1.startup",
    "id":"01e72410-d734-4611-9485-8c6a2dd2579b",
    "identity":"c1.example.net",
    "version":"0.6.0",
    "timestamp":1535369537,
    "component":"server"
}
Shutdown
{
    "protocol":"io.choria.lifecycle.v1.shutdown",
    "id":"01e72410-d734-4611-9485-8c6a2dd2579b",
    "identity":"c1.example.net",
    "component":"server",
    "timestamp":1535369536
}
Provisioned
{
    "protocol":"io.choria.lifecycle.v1.provisioned",
    "id":"01e72410-d734-4611-9485-8c6a2dd2579b",
    "identity":"c1.example.net",
    "component":"server",
    "timestamp":1535369536
}
Alive
{
    "protocol":"io.choria.lifecycle.v1.alive",
    "id":"01e72410-d734-4611-9485-8c6a2dd2579b",
    "identity":"c1.example.net",
    "version":"0.6.0",
    "timestamp":1535369537,
    "component":"server"
}

Viewing events

In a shell configured as a Choria Client run choria tool event to view events in real time. You can also install the CLI found on our releases page and do lifecycle view.

These events do not traverse Federation borders, so you have to view them in the network you care to observe. You can though configure a Choria Adapter to receive them and adapt them onto a NATS Stream from where you can replicate them to other data centers.

Emitting an event

event, err := lifecycle.New(lifecycle.Startup, lifecycle.Identity("my.identity"), lifecycle.Component("my_app"), lifecycle.Version("0.0.1"))
panicIfErr(err)

// conn is a Choria connector
err = lifecycle.PublishEvent(event, conn)

If you are emitting lifecycle.Shutdown events right before exiting be sure to call conn.Close() so the buffers are flushed prior to shutdown.

Receiving events

These events are used to orchestrate associated tools like the Provisioning Server that listens for these events and immediately add a new node to the provisioning queue.

To receive startup events for the server:

events := make(chan *choria.ConnectorMessage, 1000)

// conn is a choria framework connector
// fw is the choria framework
err = conn.QueueSubscribe(ctx, fw.NewRequestID(), "choria.lifecycle.event.startup.server", "", events)
panicIfError(err)

for {
    select {
    case e := <-events:
        event, err := lifecycle.NewFromJSON(e.Data)
        if err != nil {
            continue
        }

        fmt.Printf("Received a startup from %s", event.Identity())
    case <-ctx.Done():
        return
    }
}

Tallying component versions

In large dynamic fleets it's hard to keep track of counts and versions of nodes. A tool is included that can observe a running network and gather versions of a specific component. The results are exposed as Prometheus metrics.

lifecycle tally --component server --port 8080 --prefix lifecycle_tally

For this to work it uses the normal Choria client configuration to connect to the right middleware using TLS and listen there, you'll .

This will listen on port 8080 for /metrics, it will observe events from the server component and expose metrics as below:

Metric Description
lifecycle_tally_good_events Events processed successfully
lifecycle_tally_process_errors The number of events received that failed to process
lifecycle_tally_event_types The number of events received by type
lifecycle_tally_versions Gauge indicating the number of running components by version
lifecycle_tally_maintenance_time Time spent doing regular maintenance on the stored data
lifecycle_tally_processing_time The time taken to process events

Additionally this tool can also watch Choria Autonomous Agent events, today it supports transition events only:

Metric Description
lifecycle_tally_machine_transition Information about transition events handled by Choria Autonomous Agents

Here the prefix - lifecycle_tally - is what would be the default if you didn't specify --prefix.

Documentation

Overview

Package lifecycle provides events that services in the Choria eco system emit during startup, shutdown, provisioning and general running.

These events can be used by other tools to react to events or monitor the running of a Chroia network.

A library to view the events received from the network and one to create a running tally of the count and versions of nodes on your network.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EventTypeNames

func EventTypeNames() []string

EventTypeNames produce a list of valid event type names

func PublishEvent

func PublishEvent(e Event, conn PublishConnector) error

PublishEvent publishes an event

func ToCloudEventV1 added in v1.1.0

func ToCloudEventV1(e Event) cloudevents.Event

ToCloudEventV1 converts an event to a CloudEvent version 1

func View

func View(ctx context.Context, opt *ViewOptions) error

View connects and stream events to Output

func WriteEvents

func WriteEvents(ctx context.Context, opt *ViewOptions) error

WriteEvents views the event stream to the output

Types

type AliveEvent

type AliveEvent struct {
	Version string `json:"version"`
	// contains filtered or unexported fields
}

AliveEvent is a io.choria.lifecycle.v1.alive event

In addition to the usually required fields it requires a Version() specified when producing this type of event

func (*AliveEvent) Component

func (e *AliveEvent) Component() string

Component is the component that produced the event

func (*AliveEvent) Format added in v1.1.0

func (e *AliveEvent) Format() Format

Format retrieves the encoding format for the event

func (*AliveEvent) ID

func (e *AliveEvent) ID() string

ID is the v4 uuid of this message

func (*AliveEvent) Identity

func (e *AliveEvent) Identity() string

Identity sets the identity for the event

func (*AliveEvent) Protocol added in v1.1.0

func (e *AliveEvent) Protocol() string

Protocol retrieves the event protocol

func (*AliveEvent) SetComponent

func (e *AliveEvent) SetComponent(c string)

SetComponent sets the component for the event

func (*AliveEvent) SetFormat added in v1.1.0

func (e *AliveEvent) SetFormat(f Format)

SetFormat sets the encoding format for the event

func (*AliveEvent) SetIdentity

func (e *AliveEvent) SetIdentity(i string)

SetIdentity sets the identity for the event

func (*AliveEvent) SetVersion

func (e *AliveEvent) SetVersion(v string)

SetVersion sets the version for the event

func (*AliveEvent) String

func (e *AliveEvent) String() string

String is text suitable to display on the console etc

func (*AliveEvent) Target

func (e *AliveEvent) Target() (string, error)

Target is where to publish the event to

func (*AliveEvent) TimeStamp added in v1.1.0

func (e *AliveEvent) TimeStamp() time.Time

Time retrieves the event time

func (*AliveEvent) Type

func (e *AliveEvent) Type() Type

Type is the type of event

func (*AliveEvent) TypeString

func (e *AliveEvent) TypeString() string

TypeString the string representation of the event type

type ComponentEvent

type ComponentEvent interface {
	SetComponent(string)
}

ComponentEvent is an event that has a component

type Event

type Event interface {
	Protocol() string
	Target() (string, error)
	String() string
	Type() Type
	TypeString() string
	SetIdentity(string)
	Component() string
	Identity() string
	ID() string
	Format() Format
	SetFormat(Format)
	TimeStamp() time.Time
}

Event is event that can be published to the network

func New

func New(t Type, opts ...Option) (Event, error)

New creates a new event

func NewFromJSON

func NewFromJSON(j []byte) (event Event, err error)

NewFromJSON creates an event from the event JSON

type Format added in v1.1.0

type Format int

Format is the event format used for transporting events

const (
	// UnknownFormat is a unknown format message
	UnknownFormat Format = iota

	// ChoriaFormat is classical ChoriaFormat lifecycle events in its own package
	ChoriaFormat

	// CloudEventV1Format is a classical Choria lifecycle event carried within a version 1.0 CloudEvent
	CloudEventV1Format
)

func EventFormatFromJSON added in v1.1.0

func EventFormatFromJSON(j []byte) Format

EventFormatFromJSON inspects the JSON data and tries to determine the format from it's content

type Framework added in v1.1.0

type Framework interface {
	NewConnector(ctx context.Context, servers func() (srvcache.Servers, error), name string, logger *logrus.Entry) (conn choria.Connector, err error)
	Certname() string
	Logger(name string) *logrus.Entry
	NewRequestID() (string, error)
	MiddlewareServers() (servers srvcache.Servers, err error)
}

type Option

type Option func(e interface{}) error

Option configures events

func Component

func Component(component string) Option

Component set the component for events

func Identity

func Identity(identity string) Option

Identity sets the identity for events

func Version

func Version(version string) Option

Version set the version for events

type ProvisionedEvent

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

ProvisionedEvent is a io.choria.lifecycle.v1.provisioned event

func (*ProvisionedEvent) Component

func (e *ProvisionedEvent) Component() string

Component is the component that produced the event

func (*ProvisionedEvent) Format added in v1.1.0

func (e *ProvisionedEvent) Format() Format

Format retrieves the encoding format for the event

func (*ProvisionedEvent) ID

func (e *ProvisionedEvent) ID() string

ID is the v4 uuid of this message

func (*ProvisionedEvent) Identity

func (e *ProvisionedEvent) Identity() string

Identity sets the identity for the event

func (*ProvisionedEvent) Protocol added in v1.1.0

func (e *ProvisionedEvent) Protocol() string

Protocol retrieves the event protocol

func (*ProvisionedEvent) SetComponent

func (e *ProvisionedEvent) SetComponent(c string)

SetComponent sets the component for the event

func (*ProvisionedEvent) SetFormat added in v1.1.0

func (e *ProvisionedEvent) SetFormat(f Format)

SetFormat sets the encoding format for the event

func (*ProvisionedEvent) SetIdentity

func (e *ProvisionedEvent) SetIdentity(i string)

SetIdentity sets the identity for the event

func (*ProvisionedEvent) String

func (e *ProvisionedEvent) String() string

String is text suitable to display on the console etc

func (*ProvisionedEvent) Target

func (e *ProvisionedEvent) Target() (string, error)

Target is where to publish the event to

func (*ProvisionedEvent) TimeStamp added in v1.1.0

func (e *ProvisionedEvent) TimeStamp() time.Time

Time retrieves the event time

func (*ProvisionedEvent) Type

func (e *ProvisionedEvent) Type() Type

Type is the type of event

func (*ProvisionedEvent) TypeString

func (e *ProvisionedEvent) TypeString() string

TypeString the string representation of the event type

type PublishConnector

type PublishConnector interface {
	PublishRaw(target string, data []byte) error
}

PublishConnector is a connection to the middleware

type ShutdownEvent

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

ShutdownEvent is a io.choria.lifecycle.v1.shutdown event

func (*ShutdownEvent) Component

func (e *ShutdownEvent) Component() string

Component is the component that produced the event

func (*ShutdownEvent) Format added in v1.1.0

func (e *ShutdownEvent) Format() Format

Format retrieves the encoding format for the event

func (*ShutdownEvent) ID

func (e *ShutdownEvent) ID() string

ID is the v4 uuid of this message

func (*ShutdownEvent) Identity

func (e *ShutdownEvent) Identity() string

Identity sets the identity for the event

func (*ShutdownEvent) Protocol added in v1.1.0

func (e *ShutdownEvent) Protocol() string

Protocol retrieves the event protocol

func (*ShutdownEvent) SetComponent

func (e *ShutdownEvent) SetComponent(c string)

SetComponent sets the component for the event

func (*ShutdownEvent) SetFormat added in v1.1.0

func (e *ShutdownEvent) SetFormat(f Format)

SetFormat sets the encoding format for the event

func (*ShutdownEvent) SetIdentity

func (e *ShutdownEvent) SetIdentity(i string)

SetIdentity sets the identity for the event

func (*ShutdownEvent) String

func (e *ShutdownEvent) String() string

String is text suitable to display on the console etc

func (*ShutdownEvent) Target

func (e *ShutdownEvent) Target() (string, error)

Target is where to publish the event to

func (*ShutdownEvent) TimeStamp added in v1.1.0

func (e *ShutdownEvent) TimeStamp() time.Time

Time retrieves the event time

func (*ShutdownEvent) Type

func (e *ShutdownEvent) Type() Type

Type is the type of event

func (*ShutdownEvent) TypeString

func (e *ShutdownEvent) TypeString() string

TypeString the string representation of the event type

type StartupEvent

type StartupEvent struct {
	Version string `json:"version"`
	// contains filtered or unexported fields
}

StartupEvent is a io.choria.lifecycle.v1.startup event

In addition to the usually required fields it requires a Version() specified when producing this type of event

func (*StartupEvent) Component

func (e *StartupEvent) Component() string

Component is the component that produced the event

func (*StartupEvent) Format added in v1.1.0

func (e *StartupEvent) Format() Format

Format retrieves the encoding format for the event

func (*StartupEvent) ID

func (e *StartupEvent) ID() string

ID is the v4 uuid of this message

func (*StartupEvent) Identity

func (e *StartupEvent) Identity() string

Identity sets the identity for the event

func (*StartupEvent) Protocol added in v1.1.0

func (e *StartupEvent) Protocol() string

Protocol retrieves the event protocol

func (*StartupEvent) SetComponent

func (e *StartupEvent) SetComponent(c string)

SetComponent sets the component for the event

func (*StartupEvent) SetFormat added in v1.1.0

func (e *StartupEvent) SetFormat(f Format)

SetFormat sets the encoding format for the event

func (*StartupEvent) SetIdentity

func (e *StartupEvent) SetIdentity(i string)

SetIdentity sets the identity for the event

func (*StartupEvent) SetVersion

func (e *StartupEvent) SetVersion(v string)

SetVersion sets the version for the event

func (*StartupEvent) String

func (e *StartupEvent) String() string

String is text suitable to display on the console etc

func (*StartupEvent) Target

func (e *StartupEvent) Target() (string, error)

Target is where to publish the event to

func (*StartupEvent) TimeStamp added in v1.1.0

func (e *StartupEvent) TimeStamp() time.Time

Time retrieves the event time

func (*StartupEvent) Type

func (e *StartupEvent) Type() Type

Type is the type of event

func (*StartupEvent) TypeString

func (e *StartupEvent) TypeString() string

TypeString the string representation of the event type

type SubscribeConnector

type SubscribeConnector interface {
	QueueSubscribe(ctx context.Context, name string, subject string, group string, output chan *choria.ConnectorMessage) error
	ConnectedServer() string
}

SubscribeConnector is a connection to the middleware

type Type

type Type int

Type is a type of event this system supports

const (

	// Startup is an event components can publish when they start
	Startup Type = iota

	// Shutdown is an event components can publish when they shutdown
	Shutdown

	// Provisioned is an event components can publish post provisioning
	Provisioned

	// Alive is an event components can publish to indicate they are still alive
	Alive
)

type VersionEvent

type VersionEvent interface {
	SetVersion(string)
}

VersionEvent is an event that has a version

type ViewOptions

type ViewOptions struct {
	TypeFilter      string
	ComponentFilter string
	Debug           bool
	Output          io.Writer
	Choria          Framework
	Connector       SubscribeConnector
}

ViewOptions configure the view command

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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