2016-12-05 22:53:13 +00:00
|
|
|
package events
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2017-01-19 05:57:01 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2017-04-03 20:14:15 +00:00
|
|
|
"github.com/containerd/containerd/log"
|
2016-12-05 22:53:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
G = GetPoster
|
|
|
|
)
|
|
|
|
|
|
|
|
// Poster posts the event.
|
|
|
|
type Poster interface {
|
2016-12-12 22:26:51 +00:00
|
|
|
Post(ctx context.Context, event Event)
|
2016-12-05 22:53:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type posterKey struct{}
|
|
|
|
|
2016-12-12 22:26:51 +00:00
|
|
|
func WithPoster(ctx context.Context, poster Poster) context.Context {
|
|
|
|
return context.WithValue(ctx, posterKey{}, poster)
|
|
|
|
}
|
|
|
|
|
2016-12-05 22:53:13 +00:00
|
|
|
func GetPoster(ctx context.Context) Poster {
|
2016-12-12 22:26:51 +00:00
|
|
|
poster := ctx.Value(posterKey{})
|
2016-12-05 22:53:13 +00:00
|
|
|
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.
|
2016-12-12 22:26:51 +00:00
|
|
|
return posterFunc(func(ctx context.Context, event Event) {
|
2016-12-05 22:53:13 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-12-12 22:26:51 +00:00
|
|
|
type posterFunc func(ctx context.Context, event Event)
|
2016-12-05 22:53:13 +00:00
|
|
|
|
2016-12-12 22:26:51 +00:00
|
|
|
func (fn posterFunc) Post(ctx context.Context, event Event) {
|
|
|
|
fn(ctx, event)
|
2016-12-05 22:53:13 +00:00
|
|
|
}
|