Browse code

Merge pull request #31672 from dperny/service-logs-formatting

Service logs formatting

Brian Goff authored on 2017/03/14 08:08:55
Showing 2 changed files
... ...
@@ -7,7 +7,6 @@ import (
7 7
 
8 8
 	"github.com/docker/docker/api/types/swarm"
9 9
 	"github.com/docker/docker/client"
10
-	"github.com/docker/docker/pkg/stringid"
11 10
 )
12 11
 
13 12
 // IDResolver provides ID to Name resolution.
... ...
@@ -27,7 +26,7 @@ func New(client client.APIClient, noResolve bool) *IDResolver {
27 27
 }
28 28
 
29 29
 func (r *IDResolver) get(ctx context.Context, t interface{}, id string) (string, error) {
30
-	switch t := t.(type) {
30
+	switch t.(type) {
31 31
 	case swarm.Node:
32 32
 		node, _, err := r.client.NodeInspectWithRaw(ctx, id)
33 33
 		if err != nil {
... ...
@@ -46,25 +45,6 @@ func (r *IDResolver) get(ctx context.Context, t interface{}, id string) (string,
46 46
 			return id, nil
47 47
 		}
48 48
 		return service.Spec.Annotations.Name, nil
49
-	case swarm.Task:
50
-		// If the caller passes the full task there's no need to do a lookup.
51
-		if t.ID == "" {
52
-			var err error
53
-
54
-			t, _, err = r.client.TaskInspectWithRaw(ctx, id)
55
-			if err != nil {
56
-				return id, nil
57
-			}
58
-		}
59
-		taskID := stringid.TruncateID(t.ID)
60
-		if t.ServiceID == "" {
61
-			return taskID, nil
62
-		}
63
-		service, err := r.Resolve(ctx, swarm.Service{}, t.ServiceID)
64
-		if err != nil {
65
-			return "", err
66
-		}
67
-		return fmt.Sprintf("%s.%d.%s", service, t.Slot, taskID), nil
68 49
 	default:
69 50
 		return "", fmt.Errorf("unsupported type")
70 51
 	}
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"bytes"
5 5
 	"fmt"
6 6
 	"io"
7
+	"strconv"
7 8
 	"strings"
8 9
 
9 10
 	"golang.org/x/net/context"
... ...
@@ -13,16 +14,19 @@ import (
13 13
 	"github.com/docker/docker/cli"
14 14
 	"github.com/docker/docker/cli/command"
15 15
 	"github.com/docker/docker/cli/command/idresolver"
16
+	"github.com/docker/docker/client"
16 17
 	"github.com/docker/docker/pkg/stdcopy"
18
+	"github.com/docker/docker/pkg/stringid"
17 19
 	"github.com/spf13/cobra"
18 20
 )
19 21
 
20 22
 type logsOptions struct {
21 23
 	noResolve  bool
24
+	noTrunc    bool
25
+	noTaskIDs  bool
22 26
 	follow     bool
23 27
 	since      string
24 28
 	timestamps bool
25
-	details    bool
26 29
 	tail       string
27 30
 
28 31
 	service string
... ...
@@ -44,10 +48,11 @@ func newLogsCommand(dockerCli *command.DockerCli) *cobra.Command {
44 44
 
45 45
 	flags := cmd.Flags()
46 46
 	flags.BoolVar(&opts.noResolve, "no-resolve", false, "Do not map IDs to Names")
47
+	flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Do not truncate output")
48
+	flags.BoolVar(&opts.noTaskIDs, "no-task-ids", false, "Do not include task IDs")
47 49
 	flags.BoolVarP(&opts.follow, "follow", "f", false, "Follow log output")
48 50
 	flags.StringVar(&opts.since, "since", "", "Show logs since timestamp")
49 51
 	flags.BoolVarP(&opts.timestamps, "timestamps", "t", false, "Show timestamps")
50
-	flags.BoolVar(&opts.details, "details", false, "Show extra details provided to logs")
51 52
 	flags.StringVar(&opts.tail, "tail", "all", "Number of lines to show from the end of the logs")
52 53
 	return cmd
53 54
 }
... ...
@@ -62,30 +67,96 @@ func runLogs(dockerCli *command.DockerCli, opts *logsOptions) error {
62 62
 		Timestamps: opts.timestamps,
63 63
 		Follow:     opts.follow,
64 64
 		Tail:       opts.tail,
65
-		Details:    opts.details,
66 65
 	}
67 66
 
68 67
 	client := dockerCli.Client()
68
+
69
+	service, _, err := client.ServiceInspectWithRaw(ctx, opts.service)
70
+	if err != nil {
71
+		return err
72
+	}
73
+
69 74
 	responseBody, err := client.ServiceLogs(ctx, opts.service, options)
70 75
 	if err != nil {
71 76
 		return err
72 77
 	}
73 78
 	defer responseBody.Close()
74 79
 
75
-	resolver := idresolver.New(client, opts.noResolve)
80
+	var replicas uint64
81
+	padding := 1
82
+	if service.Spec.Mode.Replicated != nil && service.Spec.Mode.Replicated.Replicas != nil {
83
+		// if replicas are initialized, figure out if we need to pad them
84
+		replicas = *service.Spec.Mode.Replicated.Replicas
85
+		padding = len(strconv.FormatUint(replicas, 10))
86
+	}
87
+
88
+	taskFormatter := newTaskFormatter(client, opts, padding)
76 89
 
77
-	stdout := &logWriter{ctx: ctx, opts: opts, r: resolver, w: dockerCli.Out()}
78
-	stderr := &logWriter{ctx: ctx, opts: opts, r: resolver, w: dockerCli.Err()}
90
+	stdout := &logWriter{ctx: ctx, opts: opts, f: taskFormatter, w: dockerCli.Out()}
91
+	stderr := &logWriter{ctx: ctx, opts: opts, f: taskFormatter, w: dockerCli.Err()}
79 92
 
80 93
 	// TODO(aluzzardi): Do an io.Copy for services with TTY enabled.
81 94
 	_, err = stdcopy.StdCopy(stdout, stderr, responseBody)
82 95
 	return err
83 96
 }
84 97
 
98
+type taskFormatter struct {
99
+	client  client.APIClient
100
+	opts    *logsOptions
101
+	padding int
102
+
103
+	r     *idresolver.IDResolver
104
+	cache map[logContext]string
105
+}
106
+
107
+func newTaskFormatter(client client.APIClient, opts *logsOptions, padding int) *taskFormatter {
108
+	return &taskFormatter{
109
+		client:  client,
110
+		opts:    opts,
111
+		padding: padding,
112
+		r:       idresolver.New(client, opts.noResolve),
113
+		cache:   make(map[logContext]string),
114
+	}
115
+}
116
+
117
+func (f *taskFormatter) format(ctx context.Context, logCtx logContext) (string, error) {
118
+	if cached, ok := f.cache[logCtx]; ok {
119
+		return cached, nil
120
+	}
121
+
122
+	nodeName, err := f.r.Resolve(ctx, swarm.Node{}, logCtx.nodeID)
123
+	if err != nil {
124
+		return "", err
125
+	}
126
+
127
+	serviceName, err := f.r.Resolve(ctx, swarm.Service{}, logCtx.serviceID)
128
+	if err != nil {
129
+		return "", err
130
+	}
131
+
132
+	task, _, err := f.client.TaskInspectWithRaw(ctx, logCtx.taskID)
133
+	if err != nil {
134
+		return "", err
135
+	}
136
+
137
+	taskName := fmt.Sprintf("%s.%d", serviceName, task.Slot)
138
+	if !f.opts.noTaskIDs {
139
+		if f.opts.noTrunc {
140
+			taskName += fmt.Sprintf(".%s", task.ID)
141
+		} else {
142
+			taskName += fmt.Sprintf(".%s", stringid.TruncateID(task.ID))
143
+		}
144
+	}
145
+	padding := strings.Repeat(" ", f.padding-len(strconv.FormatInt(int64(task.Slot), 10)))
146
+	formatted := fmt.Sprintf("%s@%s%s", taskName, nodeName, padding)
147
+	f.cache[logCtx] = formatted
148
+	return formatted, nil
149
+}
150
+
85 151
 type logWriter struct {
86 152
 	ctx  context.Context
87 153
 	opts *logsOptions
88
-	r    *idresolver.IDResolver
154
+	f    *taskFormatter
89 155
 	w    io.Writer
90 156
 }
91 157
 
... ...
@@ -102,7 +173,7 @@ func (lw *logWriter) Write(buf []byte) (int, error) {
102 102
 		return 0, fmt.Errorf("invalid context in log message: %v", string(buf))
103 103
 	}
104 104
 
105
-	taskName, nodeName, err := lw.parseContext(string(parts[contextIndex]))
105
+	logCtx, err := lw.parseContext(string(parts[contextIndex]))
106 106
 	if err != nil {
107 107
 		return 0, err
108 108
 	}
... ...
@@ -115,8 +186,11 @@ func (lw *logWriter) Write(buf []byte) (int, error) {
115 115
 		}
116 116
 
117 117
 		if i == contextIndex {
118
-			// TODO(aluzzardi): Consider constant padding.
119
-			output = append(output, []byte(fmt.Sprintf("%s@%s    |", taskName, nodeName))...)
118
+			formatted, err := lw.f.format(lw.ctx, logCtx)
119
+			if err != nil {
120
+				return 0, err
121
+			}
122
+			output = append(output, []byte(fmt.Sprintf("%s    |", formatted))...)
120 123
 		} else {
121 124
 			output = append(output, part...)
122 125
 		}
... ...
@@ -129,35 +203,42 @@ func (lw *logWriter) Write(buf []byte) (int, error) {
129 129
 	return len(buf), nil
130 130
 }
131 131
 
132
-func (lw *logWriter) parseContext(input string) (string, string, error) {
132
+func (lw *logWriter) parseContext(input string) (logContext, error) {
133 133
 	context := make(map[string]string)
134 134
 
135 135
 	components := strings.Split(input, ",")
136 136
 	for _, component := range components {
137 137
 		parts := strings.SplitN(component, "=", 2)
138 138
 		if len(parts) != 2 {
139
-			return "", "", fmt.Errorf("invalid context: %s", input)
139
+			return logContext{}, fmt.Errorf("invalid context: %s", input)
140 140
 		}
141 141
 		context[parts[0]] = parts[1]
142 142
 	}
143 143
 
144
-	taskID, ok := context["com.docker.swarm.task.id"]
144
+	nodeID, ok := context["com.docker.swarm.node.id"]
145 145
 	if !ok {
146
-		return "", "", fmt.Errorf("missing task id in context: %s", input)
147
-	}
148
-	taskName, err := lw.r.Resolve(lw.ctx, swarm.Task{}, taskID)
149
-	if err != nil {
150
-		return "", "", err
146
+		return logContext{}, fmt.Errorf("missing node id in context: %s", input)
151 147
 	}
152 148
 
153
-	nodeID, ok := context["com.docker.swarm.node.id"]
149
+	serviceID, ok := context["com.docker.swarm.service.id"]
154 150
 	if !ok {
155
-		return "", "", fmt.Errorf("missing node id in context: %s", input)
151
+		return logContext{}, fmt.Errorf("missing service id in context: %s", input)
156 152
 	}
157
-	nodeName, err := lw.r.Resolve(lw.ctx, swarm.Node{}, nodeID)
158
-	if err != nil {
159
-		return "", "", err
153
+
154
+	taskID, ok := context["com.docker.swarm.task.id"]
155
+	if !ok {
156
+		return logContext{}, fmt.Errorf("missing task id in context: %s", input)
160 157
 	}
161 158
 
162
-	return taskName, nodeName, nil
159
+	return logContext{
160
+		nodeID:    nodeID,
161
+		serviceID: serviceID,
162
+		taskID:    taskID,
163
+	}, nil
164
+}
165
+
166
+type logContext struct {
167
+	nodeID    string
168
+	serviceID string
169
+	taskID    string
163 170
 }