Browse code

Use a regex to match labels

Signed-off-by: Robin THONI <robin@rthoni.com>

Robin THONI authored on 2019/01/17 06:52:22
Showing 12 changed files
... ...
@@ -189,6 +189,7 @@ func ValidateLogOpt(cfg map[string]string) error {
189 189
 		case "env":
190 190
 		case "env-regex":
191 191
 		case "labels":
192
+		case "labels-regex":
192 193
 		case "tag":
193 194
 		case addressKey:
194 195
 		case bufferLimitKey:
... ...
@@ -18,14 +18,15 @@ import (
18 18
 const (
19 19
 	name = "gcplogs"
20 20
 
21
-	projectOptKey  = "gcp-project"
22
-	logLabelsKey   = "labels"
23
-	logEnvKey      = "env"
24
-	logEnvRegexKey = "env-regex"
25
-	logCmdKey      = "gcp-log-cmd"
26
-	logZoneKey     = "gcp-meta-zone"
27
-	logNameKey     = "gcp-meta-name"
28
-	logIDKey       = "gcp-meta-id"
21
+	projectOptKey     = "gcp-project"
22
+	logLabelsKey      = "labels"
23
+	logLabelsRegexKey = "labels-regex"
24
+	logEnvKey         = "env"
25
+	logEnvRegexKey    = "env-regex"
26
+	logCmdKey         = "gcp-log-cmd"
27
+	logZoneKey        = "gcp-meta-zone"
28
+	logNameKey        = "gcp-meta-name"
29
+	logIDKey          = "gcp-meta-id"
29 30
 )
30 31
 
31 32
 var (
... ...
@@ -210,7 +211,7 @@ func New(info logger.Info) (logger.Logger, error) {
210 210
 func ValidateLogOpts(cfg map[string]string) error {
211 211
 	for k := range cfg {
212 212
 		switch k {
213
-		case projectOptKey, logLabelsKey, logEnvKey, logEnvRegexKey, logCmdKey, logZoneKey, logNameKey, logIDKey:
213
+		case projectOptKey, logLabelsKey, logLabelsRegexKey, logEnvKey, logEnvRegexKey, logCmdKey, logZoneKey, logNameKey, logIDKey:
214 214
 		default:
215 215
 			return fmt.Errorf("%q is not a valid option for the gcplogs driver", k)
216 216
 		}
... ...
@@ -207,6 +207,7 @@ func ValidateLogOpt(cfg map[string]string) error {
207 207
 		case "gelf-address":
208 208
 		case "tag":
209 209
 		case "labels":
210
+		case "labels-regex":
210 211
 		case "env":
211 212
 		case "env-regex":
212 213
 		case "gelf-compression-level":
... ...
@@ -98,6 +98,7 @@ func TestUDPValidateLogOpt(t *testing.T) {
98 98
 		"gelf-address":           "udp://127.0.0.1:12201",
99 99
 		"tag":                    "testtag",
100 100
 		"labels":                 "testlabel",
101
+		"labels-regex":           "testlabel-regex",
101 102
 		"env":                    "testenv",
102 103
 		"env-regex":              "testenv-regex",
103 104
 		"gelf-compression-level": "9",
... ...
@@ -90,6 +90,7 @@ func validateLogOpt(cfg map[string]string) error {
90 90
 	for key := range cfg {
91 91
 		switch key {
92 92
 		case "labels":
93
+		case "labels-regex":
93 94
 		case "env":
94 95
 		case "env-regex":
95 96
 		case "tag":
... ...
@@ -156,6 +156,7 @@ func ValidateLogOpt(cfg map[string]string) error {
156 156
 		case "max-size":
157 157
 		case "compress":
158 158
 		case "labels":
159
+		case "labels-regex":
159 160
 		case "env":
160 161
 		case "env-regex":
161 162
 		case "tag":
... ...
@@ -277,12 +277,12 @@ func TestJSONFileLoggerWithLabelsEnv(t *testing.T) {
277 277
 	}
278 278
 	defer os.RemoveAll(tmp)
279 279
 	filename := filepath.Join(tmp, "container.log")
280
-	config := map[string]string{"labels": "rack,dc", "env": "environ,debug,ssl", "env-regex": "^dc"}
280
+	config := map[string]string{"labels": "rack,dc", "labels-regex": "^loc", "env": "environ,debug,ssl", "env-regex": "^dc"}
281 281
 	l, err := New(logger.Info{
282 282
 		ContainerID:     cid,
283 283
 		LogPath:         filename,
284 284
 		Config:          config,
285
-		ContainerLabels: map[string]string{"rack": "101", "dc": "lhr"},
285
+		ContainerLabels: map[string]string{"rack": "101", "dc": "lhr", "location": "here"},
286 286
 		ContainerEnv:    []string{"environ=production", "debug=false", "port=10001", "ssl=true", "dc_region=west"},
287 287
 	})
288 288
 	if err != nil {
... ...
@@ -308,6 +308,7 @@ func TestJSONFileLoggerWithLabelsEnv(t *testing.T) {
308 308
 	expected := map[string]string{
309 309
 		"rack":      "101",
310 310
 		"dc":        "lhr",
311
+		"location":  "here",
311 312
 		"environ":   "production",
312 313
 		"debug":     "false",
313 314
 		"ssl":       "true",
... ...
@@ -100,6 +100,7 @@ func ValidateLogOpt(cfg map[string]string) error {
100 100
 		case "env":
101 101
 		case "env-regex":
102 102
 		case "labels":
103
+		case "labels-regex":
103 104
 		case "tag":
104 105
 		case key:
105 106
 		default:
... ...
@@ -41,6 +41,22 @@ func (info *Info) ExtraAttributes(keyMod func(string) string) (map[string]string
41 41
 		}
42 42
 	}
43 43
 
44
+	labelsRegex, ok := info.Config["labels-regex"]
45
+	if ok && len(labels) > 0 {
46
+		re, err := regexp.Compile(labelsRegex)
47
+		if err != nil {
48
+			return nil, err
49
+		}
50
+		for k, v := range info.ContainerLabels {
51
+			if re.MatchString(k) {
52
+				if keyMod != nil {
53
+					k = keyMod(k)
54
+				}
55
+				extra[k] = v
56
+			}
57
+		}
58
+	}
59
+
44 60
 	envMapping := make(map[string]string)
45 61
 	for _, e := range info.ContainerEnv {
46 62
 		if kv := strings.SplitN(e, "=", 2); len(kv) == 2 {
... ...
@@ -44,6 +44,7 @@ const (
44 44
 	envKey                        = "env"
45 45
 	envRegexKey                   = "env-regex"
46 46
 	labelsKey                     = "labels"
47
+	labelsRegexKey                = "labels-regex"
47 48
 	tagKey                        = "tag"
48 49
 )
49 50
 
... ...
@@ -565,6 +566,7 @@ func ValidateLogOpt(cfg map[string]string) error {
565 565
 		case envKey:
566 566
 		case envRegexKey:
567 567
 		case labelsKey:
568
+		case labelsRegexKey:
568 569
 		case tagKey:
569 570
 		default:
570 571
 			return fmt.Errorf("unknown log opt '%s' for %s log driver", key, driverName)
... ...
@@ -189,6 +189,7 @@ func ValidateLogOpt(cfg map[string]string) error {
189 189
 		case "env":
190 190
 		case "env-regex":
191 191
 		case "labels":
192
+		case "labels-regex":
192 193
 		case "syslog-address":
193 194
 		case "syslog-facility":
194 195
 		case "syslog-tls-ca-cert":
... ...
@@ -137,6 +137,7 @@ func TestValidateLogOpt(t *testing.T) {
137 137
 		"env":                    "http://127.0.0.1",
138 138
 		"env-regex":              "abc",
139 139
 		"labels":                 "labelA",
140
+		"labels-regex":           "def",
140 141
 		"syslog-address":         "udp://1.2.3.4:1111",
141 142
 		"syslog-facility":        "daemon",
142 143
 		"syslog-tls-ca-cert":     "/etc/ca-certificates/custom/ca.pem",