Skip to content

Commit 812d43f

Browse files
Yanqiang Miaoruncom
authored andcommitted
Optimize the function 'Context.Name()' and replace 'Context.ContainerName' that need to remove slash with 'Context.Name()'.
Signed-off-by: Yanqiang Miao <miao.yanqiang@zte.com.cn> update Signed-off-by: Yanqiang Miao <miao.yanqiang@zte.com.cn> update Signed-off-by: Yanqiang Miao <miao.yanqiang@zte.com.cn>
1 parent 6c336e4 commit 812d43f

4 files changed

Lines changed: 4 additions & 22 deletions

File tree

daemon/logger/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (ctx *Context) FullID() string {
9292

9393
// Name returns the ContainerName without a preceding '/'.
9494
func (ctx *Context) Name() string {
95-
return ctx.ContainerName[1:]
95+
return strings.TrimPrefix(ctx.ContainerName, "/")
9696
}
9797

9898
// ImageID returns the ContainerImageID shortened to 12 characters.

daemon/logger/etwlogs/etwlogs_windows.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func New(ctx logger.Context) (logger.Logger, error) {
6161
logrus.Debugf("logging driver etwLogs configured for container: %s.", ctx.ContainerID)
6262

6363
return &etwLogs{
64-
containerName: fixContainerName(ctx.ContainerName),
64+
containerName: ctx.Name(),
6565
imageName: ctx.ContainerImageName,
6666
containerID: ctx.ContainerID,
6767
imageID: ctx.ContainerImageID,
@@ -99,14 +99,6 @@ func createLogMessage(etwLogger *etwLogs, msg *logger.Message) string {
9999
msg.Line)
100100
}
101101

102-
// fixContainerName removes the initial '/' from the container name.
103-
func fixContainerName(cntName string) string {
104-
if len(cntName) > 0 && cntName[0] == '/' {
105-
cntName = cntName[1:]
106-
}
107-
return cntName
108-
}
109-
110102
func registerETWProvider() error {
111103
mu.Lock()
112104
defer mu.Unlock()

daemon/logger/gelf/gelf.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package gelf
66

77
import (
8-
"bytes"
98
"compress/flate"
109
"encoding/json"
1110
"fmt"
@@ -54,9 +53,6 @@ func New(ctx logger.Context) (logger.Logger, error) {
5453
return nil, fmt.Errorf("gelf: cannot access hostname to set source field")
5554
}
5655

57-
// remove trailing slash from container name
58-
containerName := bytes.TrimLeft([]byte(ctx.ContainerName), "/")
59-
6056
// parse log tag
6157
tag, err := loggerutils.ParseLogTag(ctx, loggerutils.DefaultTemplate)
6258
if err != nil {
@@ -65,7 +61,7 @@ func New(ctx logger.Context) (logger.Logger, error) {
6561

6662
extra := map[string]interface{}{
6763
"_container_id": ctx.ContainerID,
68-
"_container_name": string(containerName),
64+
"_container_name": ctx.Name(),
6965
"_image_id": ctx.ContainerImageID,
7066
"_image_name": ctx.ContainerImageName,
7167
"_command": ctx.Command(),

daemon/logger/journald/journald.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,6 @@ func New(ctx logger.Context) (logger.Logger, error) {
6262
if !journal.Enabled() {
6363
return nil, fmt.Errorf("journald is not enabled on this host")
6464
}
65-
// Strip a leading slash so that people can search for
66-
// CONTAINER_NAME=foo rather than CONTAINER_NAME=/foo.
67-
name := ctx.ContainerName
68-
if name[0] == '/' {
69-
name = name[1:]
70-
}
7165

7266
// parse log tag
7367
tag, err := loggerutils.ParseLogTag(ctx, loggerutils.DefaultTemplate)
@@ -78,7 +72,7 @@ func New(ctx logger.Context) (logger.Logger, error) {
7872
vars := map[string]string{
7973
"CONTAINER_ID": ctx.ContainerID[:12],
8074
"CONTAINER_ID_FULL": ctx.ContainerID,
81-
"CONTAINER_NAME": name,
75+
"CONTAINER_NAME": ctx.Name(),
8276
"CONTAINER_TAG": tag,
8377
}
8478
extraAttrs := ctx.ExtraAttributes(sanitizeKeyMod)

0 commit comments

Comments
 (0)