containerd/events/poster.go
Stephen J Day e620833c9e
events: implemenet transaction event API
With this changeset, we provide an implementation for a transaction
event API. The goal is to support the emission of events with
transactions and sub-transactions, allowing a process post events that
may be rolled back later due to an error in the entire process. When
journaled, a consumer will be able to commit and rollback with the same
behavior as the producer.

Events are left to full definition by each component. We may require
more structure in the future to ensure consistency but we need more use
case before making decisions in that direction.

Events may be organized by a topic. A topic defines a single stream of
messages, that could be associated with a specific component. The topic
defines a grouped stream that may be compacted as one.

Only the contextual API is implemented here. After using, we will make a
determination to see how useful this across the board.

Signed-off-by: Stephen J Day <stephen.day@docker.com>
2016-12-05 14:53:13 -08:00

55 lines
952 B
Go

package events
import (
"context"
"github.com/Sirupsen/logrus"
"github.com/docker/containerd/log"
)
var (
G = GetPoster
)
// Poster posts the event.
type Poster interface {
Post(event Event)
}
type posterKey struct{}
func GetPoster(ctx context.Context) Poster {
poster := ctx.Value(ctx)
if poster == nil {
logger := log.G(ctx)
tx, _ := getTx(ctx)
topic := getTopic(ctx)
// likely means we don't have a configured event system. Just return
// the default poster, which merely logs events.
return posterFunc(func(event Event) {
fields := logrus.Fields{"event": event}
if topic != "" {
fields["topic"] = topic
}
if tx != nil {
fields["tx.id"] = tx.id
if tx.parent != nil {
fields["tx.parent.id"] = tx.parent.id
}
}
logger.WithFields(fields).Info("event posted")
})
}
return poster.(Poster)
}
type posterFunc func(event Event)
func (fn posterFunc) Post(event Event) {
fn(event)
}