Преглед изворни кода

Merge pull request #28759 from miaoyq/optimization-context-name1

Optimize the function 'Context.Name()' and replace 'Context.Container…
Sebastiaan van Stijn пре 8 година
родитељ
комит
e026f80a4d

+ 1 - 1
daemon/logger/context.go

@@ -92,7 +92,7 @@ func (ctx *Context) FullID() string {
 
 
 // Name returns the ContainerName without a preceding '/'.
 // Name returns the ContainerName without a preceding '/'.
 func (ctx *Context) Name() string {
 func (ctx *Context) Name() string {
-	return ctx.ContainerName[1:]
+	return strings.TrimPrefix(ctx.ContainerName, "/")
 }
 }
 
 
 // ImageID returns the ContainerImageID shortened to 12 characters.
 // ImageID returns the ContainerImageID shortened to 12 characters.

+ 1 - 9
daemon/logger/etwlogs/etwlogs_windows.go

@@ -61,7 +61,7 @@ func New(ctx logger.Context) (logger.Logger, error) {
 	logrus.Debugf("logging driver etwLogs configured for container: %s.", ctx.ContainerID)
 	logrus.Debugf("logging driver etwLogs configured for container: %s.", ctx.ContainerID)
 
 
 	return &etwLogs{
 	return &etwLogs{
-		containerName: fixContainerName(ctx.ContainerName),
+		containerName: ctx.Name(),
 		imageName:     ctx.ContainerImageName,
 		imageName:     ctx.ContainerImageName,
 		containerID:   ctx.ContainerID,
 		containerID:   ctx.ContainerID,
 		imageID:       ctx.ContainerImageID,
 		imageID:       ctx.ContainerImageID,
@@ -99,14 +99,6 @@ func createLogMessage(etwLogger *etwLogs, msg *logger.Message) string {
 		msg.Line)
 		msg.Line)
 }
 }
 
 
-// fixContainerName removes the initial '/' from the container name.
-func fixContainerName(cntName string) string {
-	if len(cntName) > 0 && cntName[0] == '/' {
-		cntName = cntName[1:]
-	}
-	return cntName
-}
-
 func registerETWProvider() error {
 func registerETWProvider() error {
 	mu.Lock()
 	mu.Lock()
 	defer mu.Unlock()
 	defer mu.Unlock()

+ 1 - 5
daemon/logger/gelf/gelf.go

@@ -5,7 +5,6 @@
 package gelf
 package gelf
 
 
 import (
 import (
-	"bytes"
 	"compress/flate"
 	"compress/flate"
 	"encoding/json"
 	"encoding/json"
 	"fmt"
 	"fmt"
@@ -54,9 +53,6 @@ func New(ctx logger.Context) (logger.Logger, error) {
 		return nil, fmt.Errorf("gelf: cannot access hostname to set source field")
 		return nil, fmt.Errorf("gelf: cannot access hostname to set source field")
 	}
 	}
 
 
-	// remove trailing slash from container name
-	containerName := bytes.TrimLeft([]byte(ctx.ContainerName), "/")
-
 	// parse log tag
 	// parse log tag
 	tag, err := loggerutils.ParseLogTag(ctx, loggerutils.DefaultTemplate)
 	tag, err := loggerutils.ParseLogTag(ctx, loggerutils.DefaultTemplate)
 	if err != nil {
 	if err != nil {
@@ -65,7 +61,7 @@ func New(ctx logger.Context) (logger.Logger, error) {
 
 
 	extra := map[string]interface{}{
 	extra := map[string]interface{}{
 		"_container_id":   ctx.ContainerID,
 		"_container_id":   ctx.ContainerID,
-		"_container_name": string(containerName),
+		"_container_name": ctx.Name(),
 		"_image_id":       ctx.ContainerImageID,
 		"_image_id":       ctx.ContainerImageID,
 		"_image_name":     ctx.ContainerImageName,
 		"_image_name":     ctx.ContainerImageName,
 		"_command":        ctx.Command(),
 		"_command":        ctx.Command(),

+ 1 - 7
daemon/logger/journald/journald.go

@@ -62,12 +62,6 @@ func New(ctx logger.Context) (logger.Logger, error) {
 	if !journal.Enabled() {
 	if !journal.Enabled() {
 		return nil, fmt.Errorf("journald is not enabled on this host")
 		return nil, fmt.Errorf("journald is not enabled on this host")
 	}
 	}
-	// Strip a leading slash so that people can search for
-	// CONTAINER_NAME=foo rather than CONTAINER_NAME=/foo.
-	name := ctx.ContainerName
-	if name[0] == '/' {
-		name = name[1:]
-	}
 
 
 	// parse log tag
 	// parse log tag
 	tag, err := loggerutils.ParseLogTag(ctx, loggerutils.DefaultTemplate)
 	tag, err := loggerutils.ParseLogTag(ctx, loggerutils.DefaultTemplate)
@@ -78,7 +72,7 @@ func New(ctx logger.Context) (logger.Logger, error) {
 	vars := map[string]string{
 	vars := map[string]string{
 		"CONTAINER_ID":      ctx.ContainerID[:12],
 		"CONTAINER_ID":      ctx.ContainerID[:12],
 		"CONTAINER_ID_FULL": ctx.ContainerID,
 		"CONTAINER_ID_FULL": ctx.ContainerID,
-		"CONTAINER_NAME":    name,
+		"CONTAINER_NAME":    ctx.Name(),
 		"CONTAINER_TAG":     tag,
 		"CONTAINER_TAG":     tag,
 	}
 	}
 	extraAttrs := ctx.ExtraAttributes(sanitizeKeyMod)
 	extraAttrs := ctx.ExtraAttributes(sanitizeKeyMod)