containerd/events/poster.go
Michael Crosby 4f2b443a27 Rewrite imports for new github org
This rewrites the Go imports after switching to the new github org.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
2017-04-03 14:05:44 -07:00

59 lines
1.2 KiB
Go

package events
import (
"context"
"github.com/Sirupsen/logrus"
"github.com/containerd/containerd/log"
)
var (
G = GetPoster
)
// Poster posts the event.
type Poster interface {
Post(ctx context.Context, event Event)
}
type posterKey struct{}
func WithPoster(ctx context.Context, poster Poster) context.Context {
return context.WithValue(ctx, posterKey{}, poster)
}
func GetPoster(ctx context.Context) Poster {
poster := ctx.Value(posterKey{})
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(ctx context.Context, 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(ctx context.Context, event Event)
func (fn posterFunc) Post(ctx context.Context, event Event) {
fn(ctx, event)
}