Browse code

add scheme to fluentd-address

Signed-off-by: Akira Koyasu <mail@akirakoyasu.net>

- add scheme to fluentd-address
- define a new type `location`
- use `errors.Wrapf`

Akira Koyasu authored on 2016/08/28 23:24:56
Showing 1 changed files
... ...
@@ -6,6 +6,7 @@ import (
6 6
 	"fmt"
7 7
 	"math"
8 8
 	"net"
9
+	"net/url"
9 10
 	"strconv"
10 11
 	"strings"
11 12
 	"time"
... ...
@@ -13,8 +14,10 @@ import (
13 13
 	"github.com/Sirupsen/logrus"
14 14
 	"github.com/docker/docker/daemon/logger"
15 15
 	"github.com/docker/docker/daemon/logger/loggerutils"
16
+	"github.com/docker/docker/pkg/urlutil"
16 17
 	"github.com/docker/go-units"
17 18
 	"github.com/fluent/fluent-logger-golang/fluent"
19
+	"github.com/pkg/errors"
18 20
 )
19 21
 
20 22
 type fluentd struct {
... ...
@@ -25,9 +28,17 @@ type fluentd struct {
25 25
 	extra         map[string]string
26 26
 }
27 27
 
28
+type location struct {
29
+	protocol string
30
+	host     string
31
+	port     int
32
+	path     string
33
+}
34
+
28 35
 const (
29 36
 	name = "fluentd"
30 37
 
38
+	defaultProtocol    = "tcp"
31 39
 	defaultHost        = "127.0.0.1"
32 40
 	defaultPort        = 24224
33 41
 	defaultBufferLimit = 1024 * 1024
... ...
@@ -60,7 +71,7 @@ func init() {
60 60
 // the context. The supported context configuration variable is
61 61
 // fluentd-address.
62 62
 func New(ctx logger.Context) (logger.Logger, error) {
63
-	host, port, err := parseAddress(ctx.Config[addressKey])
63
+	loc, err := parseAddress(ctx.Config[addressKey])
64 64
 	if err != nil {
65 65
 		return nil, err
66 66
 	}
... ...
@@ -107,12 +118,14 @@ func New(ctx logger.Context) (logger.Logger, error) {
107 107
 	}
108 108
 
109 109
 	fluentConfig := fluent.Config{
110
-		FluentPort:   port,
111
-		FluentHost:   host,
112
-		BufferLimit:  bufferLimit,
113
-		RetryWait:    retryWait,
114
-		MaxRetry:     maxRetries,
115
-		AsyncConnect: asyncConnect,
110
+		FluentPort:       loc.port,
111
+		FluentHost:       loc.host,
112
+		FluentNetwork:    loc.protocol,
113
+		FluentSocketPath: loc.path,
114
+		BufferLimit:      bufferLimit,
115
+		RetryWait:        retryWait,
116
+		MaxRetry:         maxRetries,
117
+		AsyncConnect:     asyncConnect,
116 118
 	}
117 119
 
118 120
 	logrus.WithField("container", ctx.ContainerID).WithField("config", fluentConfig).
... ...
@@ -172,29 +185,65 @@ func ValidateLogOpt(cfg map[string]string) error {
172 172
 		}
173 173
 	}
174 174
 
175
-	if _, _, err := parseAddress(cfg["fluentd-address"]); err != nil {
175
+	if _, err := parseAddress(cfg["fluentd-address"]); err != nil {
176 176
 		return err
177 177
 	}
178 178
 
179 179
 	return nil
180 180
 }
181 181
 
182
-func parseAddress(address string) (string, int, error) {
182
+func parseAddress(address string) (*location, error) {
183 183
 	if address == "" {
184
-		return defaultHost, defaultPort, nil
184
+		return &location{
185
+			protocol: defaultProtocol,
186
+			host:     defaultHost,
187
+			port:     defaultPort,
188
+			path:     "",
189
+		}, nil
190
+	}
191
+
192
+	protocol := defaultProtocol
193
+	givenAddress := address
194
+	if urlutil.IsTransportURL(address) {
195
+		url, err := url.Parse(address)
196
+		if err != nil {
197
+			return nil, errors.Wrapf(err, "invalid fluentd-address %s", givenAddress)
198
+		}
199
+		// unix and unixgram socket
200
+		if url.Scheme == "unix" || url.Scheme == "unixgram" {
201
+			return &location{
202
+				protocol: url.Scheme,
203
+				host:     "",
204
+				port:     0,
205
+				path:     url.Path,
206
+			}, nil
207
+		}
208
+		// tcp|udp
209
+		protocol = url.Scheme
210
+		address = url.Host
185 211
 	}
186 212
 
187 213
 	host, port, err := net.SplitHostPort(address)
188 214
 	if err != nil {
189 215
 		if !strings.Contains(err.Error(), "missing port in address") {
190
-			return "", 0, fmt.Errorf("invalid fluentd-address %s: %s", address, err)
216
+			return nil, errors.Wrapf(err, "invalid fluentd-address %s", givenAddress)
191 217
 		}
192
-		return host, defaultPort, nil
218
+		return &location{
219
+			protocol: protocol,
220
+			host:     host,
221
+			port:     defaultPort,
222
+			path:     "",
223
+		}, nil
193 224
 	}
194 225
 
195 226
 	portnum, err := strconv.Atoi(port)
196 227
 	if err != nil {
197
-		return "", 0, fmt.Errorf("invalid fluentd-address %s: %s", address, err)
228
+		return nil, errors.Wrapf(err, "invalid fluentd-address %s", givenAddress)
198 229
 	}
199
-	return host, portnum, nil
230
+	return &location{
231
+		protocol: protocol,
232
+		host:     host,
233
+		port:     portnum,
234
+		path:     "",
235
+	}, nil
200 236
 }