Browse code

Remove `docker/` prefix from log messages tag.

This fix tries to address the issue raised in #22358 where syslog's
message tag always starts with `docker/` and can not be removed
by changing the log tag templates.

The issue is that syslog driver hardcodes `path.Base(os.Args[0])`
as the prefix, which is the binary file name of the daemon (`dockerd`).
This could be an issue for certain situations (e.g., #22358) where
user may prefer not to have a dedicated prefix in syslog messages.
There is no way to override this behavior in the current verison of
the docker.

This fix tries to address this issue without making changes in the
default behavior of the syslog driver. An additional
`{{.DaemonName}}` has been introduced in the syslog tag. This is
assigned as the `docker` when daemon starts. The default log tag
template has also been changed from
`path.Base(os.Args[0]) + "/{{.ID}}"` to `{{.DaemonName}}/{{.ID}}`.
Therefore, there is no behavior changes when log-tag is not provided.

In order to be consistent, the default log tag for fluentd has been
changed from `docker.{{.ID}}` to `{{DaemonName}}.{{.ID}}` as well.

The documentation for log-tag has been updated to reflect this change.

Additional test cases have been added to cover changes in this fix.

This fix fixes #22358.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

Yong Tang authored on 2016/04/28 11:46:54
Showing 6 changed files
... ...
@@ -315,6 +315,7 @@ func (container *Container) StartLogger(cfg containertypes.LogConfig) (logger.Lo
315 315
 		ContainerCreated:    container.Created,
316 316
 		ContainerEnv:        container.Config.Env,
317 317
 		ContainerLabels:     container.Config.Labels,
318
+		DaemonName:          "docker",
318 319
 	}
319 320
 
320 321
 	// Set logging file for "json-logger"
... ...
@@ -20,6 +20,7 @@ type Context struct {
20 20
 	ContainerEnv        []string
21 21
 	ContainerLabels     map[string]string
22 22
 	LogPath             string
23
+	DaemonName          string
23 24
 }
24 25
 
25 26
 // ExtraAttributes returns the user-defined extra attributes (labels,
... ...
@@ -65,7 +65,7 @@ func New(ctx logger.Context) (logger.Logger, error) {
65 65
 		return nil, err
66 66
 	}
67 67
 
68
-	tag, err := loggerutils.ParseLogTag(ctx, "docker.{{.ID}}")
68
+	tag, err := loggerutils.ParseLogTag(ctx, "{{.DaemonName}}.{{.ID}}")
69 69
 	if err != nil {
70 70
 		return nil, err
71 71
 	}
... ...
@@ -18,6 +18,12 @@ func TestParseLogTag(t *testing.T) {
18 18
 	assertTag(t, e, tag, "test-image/test-container/container-ab")
19 19
 }
20 20
 
21
+func TestParseLogTagEmptyTag(t *testing.T) {
22
+	ctx := buildContext(map[string]string{})
23
+	tag, e := ParseLogTag(ctx, "{{.DaemonName}}/{{.ID}}")
24
+	assertTag(t, e, tag, "test-dockerd/container-ab")
25
+}
26
+
21 27
 // Helpers
22 28
 
23 29
 func buildContext(cfg map[string]string) logger.Context {
... ...
@@ -27,6 +33,7 @@ func buildContext(cfg map[string]string) logger.Context {
27 27
 		ContainerImageID:   "image-abcdefghijklmnopqrstuvwxyz01234567890",
28 28
 		ContainerImageName: "test-image",
29 29
 		Config:             cfg,
30
+		DaemonName:         "test-dockerd",
30 31
 	}
31 32
 }
32 33
 
... ...
@@ -10,7 +10,6 @@ import (
10 10
 	"net"
11 11
 	"net/url"
12 12
 	"os"
13
-	"path"
14 13
 	"strconv"
15 14
 	"strings"
16 15
 	"time"
... ...
@@ -91,7 +90,7 @@ func rfc5424microformatterWithAppNameAsTag(p syslog.Priority, hostname, tag, con
91 91
 // the context. Supported context configuration variables are
92 92
 // syslog-address, syslog-facility, syslog-format.
93 93
 func New(ctx logger.Context) (logger.Logger, error) {
94
-	tag, err := loggerutils.ParseLogTag(ctx, "{{.ID}}")
94
+	tag, err := loggerutils.ParseLogTag(ctx, "{{.DaemonName}}/{{.ID}}")
95 95
 	if err != nil {
96 96
 		return nil, err
97 97
 	}
... ...
@@ -111,17 +110,15 @@ func New(ctx logger.Context) (logger.Logger, error) {
111 111
 		return nil, err
112 112
 	}
113 113
 
114
-	logTag := path.Base(os.Args[0]) + "/" + tag
115
-
116 114
 	var log *syslog.Writer
117 115
 	if proto == secureProto {
118 116
 		tlsConfig, tlsErr := parseTLSConfig(ctx.Config)
119 117
 		if tlsErr != nil {
120 118
 			return nil, tlsErr
121 119
 		}
122
-		log, err = syslog.DialWithTLSConfig(proto, address, facility, logTag, tlsConfig)
120
+		log, err = syslog.DialWithTLSConfig(proto, address, facility, tag, tlsConfig)
123 121
 	} else {
124
-		log, err = syslog.Dial(proto, address, facility, logTag)
122
+		log, err = syslog.Dial(proto, address, facility, tag)
125 123
 	}
126 124
 
127 125
 	if err != nil {
... ...
@@ -30,6 +30,7 @@ Docker supports some special template markup you can use when specifying a tag's
30 30
 | `{{.ImageID}}`     | The first 12 characters of the container's image id. |
31 31
 | `{{.ImageFullID}}` | The container's full image identifier.               |
32 32
 | `{{.ImageName}}`   | The name of the image used by the container.         |
33
+| `{{.DaemonName}}`  | The name of the docker program (`docker`).           |
33 34
 
34 35
 For example, specifying a `--log-opt tag="{{.ImageName}}/{{.Name}}/{{.ID}}"` value yields `syslog` log lines like:
35 36