Add context.RequestID to event stream

This PR adds a "request ID" to each event generated, the 'docker events'
stream now looks like this:

```
2015-09-10T15:02:50.000000000-07:00 [reqid: c01e3534ddca] de7c5d4ca927253cf4e978ee9c4545161e406e9b5a14617efb52c658b249174a: (from ubuntu) create
```
Note the `[reqID: c01e3534ddca]` part, that's new.

Each HTTP request will generate its own unique ID. So, if you do a
`docker build` you'll see a series of events all with the same reqID.
This allow for log processing tools to determine which events are all related
to the same http request.

I didn't propigate the context to all possible funcs in the daemon,
I decided to just do the ones that needed it in order to get the reqID
into the events. I'd like to have people review this direction first, and
if we're ok with it then I'll make sure we're consistent about when
we pass around the context - IOW, make sure that all funcs at the same level
have a context passed in even if they don't call the log funcs - this will
ensure we're consistent w/o passing it around for all calls unnecessarily.

ping @icecrime @calavera @crosbymichael

Signed-off-by: Doug Davis <dug@us.ibm.com>
Upstream-commit: 26b1064967d9fcefd4c35f60e96bf6d7c9a3b5f8
Component: engine
This commit is contained in:
Doug Davis
2015-09-10 15:01:18 -07:00
parent 65bd47ac3a
commit bf44c732da
68 changed files with 737 additions and 565 deletions

View File

@ -17,6 +17,7 @@ import (
"github.com/docker/docker/autogen/dockerversion"
"github.com/docker/docker/cli"
"github.com/docker/docker/cliconfig"
"github.com/docker/docker/context"
"github.com/docker/docker/daemon"
"github.com/docker/docker/daemon/logger"
"github.com/docker/docker/opts"
@ -150,6 +151,11 @@ func getGlobalFlag() (globalFlag *flag.Flag) {
// CmdDaemon is the daemon command, called the raw arguments after `docker daemon`.
func (cli *DaemonCli) CmdDaemon(args ...string) error {
// This may need to be made even more global - it all depends
// on whether we want the CLI to have a context object too.
// For now we'll leave it as a daemon-side object only.
ctx := context.Background()
// warn from uuid package when running the daemon
uuid.Loggerf = logrus.Warnf
@ -224,7 +230,7 @@ func (cli *DaemonCli) CmdDaemon(args ...string) error {
serverConfig.TLSConfig = tlsConfig
}
api := apiserver.New(serverConfig)
api := apiserver.New(ctx, serverConfig)
// The serve API routine never exits unless an error occurs
// We need to start it as a goroutine and wait on it so
@ -245,7 +251,7 @@ func (cli *DaemonCli) CmdDaemon(args ...string) error {
cli.TrustKeyPath = commonFlags.TrustKey
registryService := registry.NewService(cli.registryOptions)
d, err := daemon.NewDaemon(cli.Config, registryService)
d, err := daemon.NewDaemon(ctx, cli.Config, registryService)
if err != nil {
if pfile != nil {
if err := pfile.Remove(); err != nil {
@ -260,14 +266,14 @@ func (cli *DaemonCli) CmdDaemon(args ...string) error {
logrus.WithFields(logrus.Fields{
"version": dockerversion.VERSION,
"commit": dockerversion.GITCOMMIT,
"execdriver": d.ExecutionDriver().Name(),
"graphdriver": d.GraphDriver().String(),
"execdriver": d.ExecutionDriver(ctx).Name(),
"graphdriver": d.GraphDriver(ctx).String(),
}).Info("Docker daemon")
signal.Trap(func() {
api.Close()
<-serveAPIWait
shutdownDaemon(d, 15)
shutdownDaemon(ctx, d, 15)
if pfile != nil {
if err := pfile.Remove(); err != nil {
logrus.Error(err)
@ -277,12 +283,12 @@ func (cli *DaemonCli) CmdDaemon(args ...string) error {
// after the daemon is done setting up we can tell the api to start
// accepting connections with specified daemon
api.AcceptConnections(d)
api.AcceptConnections(ctx, d)
// Daemon is fully initialized and handling API traffic
// Wait for serve API to complete
errAPI := <-serveAPIWait
shutdownDaemon(d, 15)
shutdownDaemon(ctx, d, 15)
if errAPI != nil {
if pfile != nil {
if err := pfile.Remove(); err != nil {
@ -297,10 +303,10 @@ func (cli *DaemonCli) CmdDaemon(args ...string) error {
// shutdownDaemon just wraps daemon.Shutdown() to handle a timeout in case
// d.Shutdown() is waiting too long to kill container or worst it's
// blocked there
func shutdownDaemon(d *daemon.Daemon, timeout time.Duration) {
func shutdownDaemon(ctx context.Context, d *daemon.Daemon, timeout time.Duration) {
ch := make(chan struct{})
go func() {
d.Shutdown()
d.Shutdown(ctx)
close(ch)
}()
select {