-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
log driver - add ability to interpolate container context into the lo…
…g tag field Signed-off-by: Philip Monroe <[email protected]>
- Loading branch information
1 parent
5cbcbfc
commit 3be7146
Showing
13 changed files
with
292 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package logger | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// Context provides enough information for a logging driver to do its function. | ||
type Context struct { | ||
Config map[string]string | ||
ContainerID string | ||
ContainerName string | ||
ContainerEntrypoint string | ||
ContainerArgs []string | ||
ContainerImageID string | ||
ContainerImageName string | ||
ContainerCreated time.Time | ||
LogPath string | ||
} | ||
|
||
// Hostname returns the hostname from the underlying OS. | ||
func (ctx *Context) Hostname() (string, error) { | ||
hostname, err := os.Hostname() | ||
if err != nil { | ||
return "", fmt.Errorf("logger: can not resolve hostname: %v", err) | ||
} | ||
return hostname, nil | ||
} | ||
|
||
// Command returns the command that the container being logged was | ||
// started with. The Entrypoint is prepended to the container | ||
// arguments. | ||
func (ctx *Context) Command() string { | ||
terms := []string{ctx.ContainerEntrypoint} | ||
for _, arg := range ctx.ContainerArgs { | ||
terms = append(terms, arg) | ||
} | ||
command := strings.Join(terms, " ") | ||
return command | ||
} | ||
|
||
// ID Returns the Container ID shortened to 12 characters. | ||
func (ctx *Context) ID() string { | ||
return ctx.ContainerID[:12] | ||
} | ||
|
||
// FullID is an alias of ContainerID. | ||
func (ctx *Context) FullID() string { | ||
return ctx.ContainerID | ||
} | ||
|
||
// Name returns the ContainerName without a preceding '/'. | ||
func (ctx *Context) Name() string { | ||
return ctx.ContainerName[1:] | ||
} | ||
|
||
// ImageID returns the ContainerImageID shortened to 12 characters. | ||
func (ctx *Context) ImageID() string { | ||
return ctx.ContainerImageID[:12] | ||
} | ||
|
||
// ImageFullID is an alias of ContainerID. | ||
func (ctx *Context) ImageFullID() string { | ||
return ctx.ContainerImageID | ||
} | ||
|
||
// ImageName is an alias of ContainerImageName | ||
func (ctx *Context) ImageName() string { | ||
return ctx.ContainerImageName | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package loggerutils | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"text/template" | ||
|
||
"github.com/Sirupsen/logrus" | ||
"github.com/docker/docker/daemon/logger" | ||
) | ||
|
||
// ParseLogTag generates a context aware tag for consistency across different | ||
// log drivers based on the context of the running container. | ||
func ParseLogTag(ctx logger.Context, defaultTemplate string) (string, error) { | ||
tagTemplate := lookupTagTemplate(ctx, defaultTemplate) | ||
|
||
tmpl, err := template.New("log-tag").Parse(tagTemplate) | ||
if err != nil { | ||
return "", err | ||
} | ||
buf := new(bytes.Buffer) | ||
if err := tmpl.Execute(buf, &ctx); err != nil { | ||
return "", err | ||
} | ||
|
||
return buf.String(), nil | ||
} | ||
|
||
func lookupTagTemplate(ctx logger.Context, defaultTemplate string) string { | ||
tagTemplate := ctx.Config["tag"] | ||
|
||
deprecatedConfigs := []string{"syslog-tag", "gelf-tag", "fluentd-tag"} | ||
for i := 0; tagTemplate == "" && i < len(deprecatedConfigs); i++ { | ||
cfg := deprecatedConfigs[i] | ||
if ctx.Config[cfg] != "" { | ||
tagTemplate = ctx.Config[cfg] | ||
logrus.Warn(fmt.Sprintf("Using log tag from deprecated log-opt '%s'. Please use: --log-opt tag=\"%s\"", cfg, tagTemplate)) | ||
} | ||
} | ||
|
||
if tagTemplate == "" { | ||
tagTemplate = defaultTemplate | ||
} | ||
|
||
return tagTemplate | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package loggerutils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/docker/docker/daemon/logger" | ||
) | ||
|
||
func TestParseLogTagDefaultTag(t *testing.T) { | ||
ctx := buildContext(map[string]string{}) | ||
tag, e := ParseLogTag(ctx, "{{.ID}}") | ||
assertTag(t, e, tag, ctx.ID()) | ||
} | ||
|
||
func TestParseLogTag(t *testing.T) { | ||
ctx := buildContext(map[string]string{"tag": "{{.ImageName}}/{{.Name}}/{{.ID}}"}) | ||
tag, e := ParseLogTag(ctx, "{{.ID}}") | ||
assertTag(t, e, tag, "test-image/test-container/container-ab") | ||
} | ||
|
||
func TestParseLogTagSyslogTag(t *testing.T) { | ||
ctx := buildContext(map[string]string{"syslog-tag": "{{.ImageName}}/{{.Name}}/{{.ID}}"}) | ||
tag, e := ParseLogTag(ctx, "{{.ID}}") | ||
assertTag(t, e, tag, "test-image/test-container/container-ab") | ||
} | ||
|
||
func TestParseLogTagGelfTag(t *testing.T) { | ||
ctx := buildContext(map[string]string{"gelf-tag": "{{.ImageName}}/{{.Name}}/{{.ID}}"}) | ||
tag, e := ParseLogTag(ctx, "{{.ID}}") | ||
assertTag(t, e, tag, "test-image/test-container/container-ab") | ||
} | ||
|
||
func TestParseLogTagFluentdTag(t *testing.T) { | ||
ctx := buildContext(map[string]string{"fluentd-tag": "{{.ImageName}}/{{.Name}}/{{.ID}}"}) | ||
tag, e := ParseLogTag(ctx, "{{.ID}}") | ||
assertTag(t, e, tag, "test-image/test-container/container-ab") | ||
} | ||
|
||
// Helpers | ||
|
||
func buildContext(cfg map[string]string) logger.Context { | ||
return logger.Context{ | ||
ContainerID: "container-abcdefghijklmnopqrstuvwxyz01234567890", | ||
ContainerName: "/test-container", | ||
ContainerImageID: "image-abcdefghijklmnopqrstuvwxyz01234567890", | ||
ContainerImageName: "test-image", | ||
Config: cfg, | ||
} | ||
} | ||
|
||
func assertTag(t *testing.T, e error, tag string, expected string) { | ||
if e != nil { | ||
t.Fatalf("Error generating tag: %q", e) | ||
} | ||
if tag != expected { | ||
t.Fatalf("Wrong tag: %q, should be %q", tag, expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.