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

Use a regex to match labels

Signed-off-by: Robin THONI <robin@rthoni.com>
Robin THONI пре 6 година
родитељ
комит
bc709991b3

+ 1 - 0
daemon/logger/fluentd/fluentd.go

@@ -189,6 +189,7 @@ func ValidateLogOpt(cfg map[string]string) error {
 		case "env":
 		case "env":
 		case "env-regex":
 		case "env-regex":
 		case "labels":
 		case "labels":
+		case "labels-regex":
 		case "tag":
 		case "tag":
 		case addressKey:
 		case addressKey:
 		case bufferLimitKey:
 		case bufferLimitKey:

+ 10 - 9
daemon/logger/gcplogs/gcplogging.go

@@ -18,14 +18,15 @@ import (
 const (
 const (
 	name = "gcplogs"
 	name = "gcplogs"
 
 
-	projectOptKey  = "gcp-project"
-	logLabelsKey   = "labels"
-	logEnvKey      = "env"
-	logEnvRegexKey = "env-regex"
-	logCmdKey      = "gcp-log-cmd"
-	logZoneKey     = "gcp-meta-zone"
-	logNameKey     = "gcp-meta-name"
-	logIDKey       = "gcp-meta-id"
+	projectOptKey     = "gcp-project"
+	logLabelsKey      = "labels"
+	logLabelsRegexKey = "labels-regex"
+	logEnvKey         = "env"
+	logEnvRegexKey    = "env-regex"
+	logCmdKey         = "gcp-log-cmd"
+	logZoneKey        = "gcp-meta-zone"
+	logNameKey        = "gcp-meta-name"
+	logIDKey          = "gcp-meta-id"
 )
 )
 
 
 var (
 var (
@@ -210,7 +211,7 @@ func New(info logger.Info) (logger.Logger, error) {
 func ValidateLogOpts(cfg map[string]string) error {
 func ValidateLogOpts(cfg map[string]string) error {
 	for k := range cfg {
 	for k := range cfg {
 		switch k {
 		switch k {
-		case projectOptKey, logLabelsKey, logEnvKey, logEnvRegexKey, logCmdKey, logZoneKey, logNameKey, logIDKey:
+		case projectOptKey, logLabelsKey, logLabelsRegexKey, logEnvKey, logEnvRegexKey, logCmdKey, logZoneKey, logNameKey, logIDKey:
 		default:
 		default:
 			return fmt.Errorf("%q is not a valid option for the gcplogs driver", k)
 			return fmt.Errorf("%q is not a valid option for the gcplogs driver", k)
 		}
 		}

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

@@ -207,6 +207,7 @@ func ValidateLogOpt(cfg map[string]string) error {
 		case "gelf-address":
 		case "gelf-address":
 		case "tag":
 		case "tag":
 		case "labels":
 		case "labels":
+		case "labels-regex":
 		case "env":
 		case "env":
 		case "env-regex":
 		case "env-regex":
 		case "gelf-compression-level":
 		case "gelf-compression-level":

+ 1 - 0
daemon/logger/gelf/gelf_test.go

@@ -98,6 +98,7 @@ func TestUDPValidateLogOpt(t *testing.T) {
 		"gelf-address":           "udp://127.0.0.1:12201",
 		"gelf-address":           "udp://127.0.0.1:12201",
 		"tag":                    "testtag",
 		"tag":                    "testtag",
 		"labels":                 "testlabel",
 		"labels":                 "testlabel",
+		"labels-regex":           "testlabel-regex",
 		"env":                    "testenv",
 		"env":                    "testenv",
 		"env-regex":              "testenv-regex",
 		"env-regex":              "testenv-regex",
 		"gelf-compression-level": "9",
 		"gelf-compression-level": "9",

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

@@ -90,6 +90,7 @@ func validateLogOpt(cfg map[string]string) error {
 	for key := range cfg {
 	for key := range cfg {
 		switch key {
 		switch key {
 		case "labels":
 		case "labels":
+		case "labels-regex":
 		case "env":
 		case "env":
 		case "env-regex":
 		case "env-regex":
 		case "tag":
 		case "tag":

+ 1 - 0
daemon/logger/jsonfilelog/jsonfilelog.go

@@ -156,6 +156,7 @@ func ValidateLogOpt(cfg map[string]string) error {
 		case "max-size":
 		case "max-size":
 		case "compress":
 		case "compress":
 		case "labels":
 		case "labels":
+		case "labels-regex":
 		case "env":
 		case "env":
 		case "env-regex":
 		case "env-regex":
 		case "tag":
 		case "tag":

+ 3 - 2
daemon/logger/jsonfilelog/jsonfilelog_test.go

@@ -277,12 +277,12 @@ func TestJSONFileLoggerWithLabelsEnv(t *testing.T) {
 	}
 	}
 	defer os.RemoveAll(tmp)
 	defer os.RemoveAll(tmp)
 	filename := filepath.Join(tmp, "container.log")
 	filename := filepath.Join(tmp, "container.log")
-	config := map[string]string{"labels": "rack,dc", "env": "environ,debug,ssl", "env-regex": "^dc"}
+	config := map[string]string{"labels": "rack,dc", "labels-regex": "^loc", "env": "environ,debug,ssl", "env-regex": "^dc"}
 	l, err := New(logger.Info{
 	l, err := New(logger.Info{
 		ContainerID:     cid,
 		ContainerID:     cid,
 		LogPath:         filename,
 		LogPath:         filename,
 		Config:          config,
 		Config:          config,
-		ContainerLabels: map[string]string{"rack": "101", "dc": "lhr"},
+		ContainerLabels: map[string]string{"rack": "101", "dc": "lhr", "location": "here"},
 		ContainerEnv:    []string{"environ=production", "debug=false", "port=10001", "ssl=true", "dc_region=west"},
 		ContainerEnv:    []string{"environ=production", "debug=false", "port=10001", "ssl=true", "dc_region=west"},
 	})
 	})
 	if err != nil {
 	if err != nil {
@@ -308,6 +308,7 @@ func TestJSONFileLoggerWithLabelsEnv(t *testing.T) {
 	expected := map[string]string{
 	expected := map[string]string{
 		"rack":      "101",
 		"rack":      "101",
 		"dc":        "lhr",
 		"dc":        "lhr",
+		"location":  "here",
 		"environ":   "production",
 		"environ":   "production",
 		"debug":     "false",
 		"debug":     "false",
 		"ssl":       "true",
 		"ssl":       "true",

+ 1 - 0
daemon/logger/logentries/logentries.go

@@ -100,6 +100,7 @@ func ValidateLogOpt(cfg map[string]string) error {
 		case "env":
 		case "env":
 		case "env-regex":
 		case "env-regex":
 		case "labels":
 		case "labels":
+		case "labels-regex":
 		case "tag":
 		case "tag":
 		case key:
 		case key:
 		default:
 		default:

+ 16 - 0
daemon/logger/loginfo.go

@@ -41,6 +41,22 @@ func (info *Info) ExtraAttributes(keyMod func(string) string) (map[string]string
 		}
 		}
 	}
 	}
 
 
+	labelsRegex, ok := info.Config["labels-regex"]
+	if ok && len(labels) > 0 {
+		re, err := regexp.Compile(labelsRegex)
+		if err != nil {
+			return nil, err
+		}
+		for k, v := range info.ContainerLabels {
+			if re.MatchString(k) {
+				if keyMod != nil {
+					k = keyMod(k)
+				}
+				extra[k] = v
+			}
+		}
+	}
+
 	envMapping := make(map[string]string)
 	envMapping := make(map[string]string)
 	for _, e := range info.ContainerEnv {
 	for _, e := range info.ContainerEnv {
 		if kv := strings.SplitN(e, "=", 2); len(kv) == 2 {
 		if kv := strings.SplitN(e, "=", 2); len(kv) == 2 {

+ 2 - 0
daemon/logger/splunk/splunk.go

@@ -44,6 +44,7 @@ const (
 	envKey                        = "env"
 	envKey                        = "env"
 	envRegexKey                   = "env-regex"
 	envRegexKey                   = "env-regex"
 	labelsKey                     = "labels"
 	labelsKey                     = "labels"
+	labelsRegexKey                = "labels-regex"
 	tagKey                        = "tag"
 	tagKey                        = "tag"
 )
 )
 
 
@@ -565,6 +566,7 @@ func ValidateLogOpt(cfg map[string]string) error {
 		case envKey:
 		case envKey:
 		case envRegexKey:
 		case envRegexKey:
 		case labelsKey:
 		case labelsKey:
+		case labelsRegexKey:
 		case tagKey:
 		case tagKey:
 		default:
 		default:
 			return fmt.Errorf("unknown log opt '%s' for %s log driver", key, driverName)
 			return fmt.Errorf("unknown log opt '%s' for %s log driver", key, driverName)

+ 1 - 0
daemon/logger/syslog/syslog.go

@@ -189,6 +189,7 @@ func ValidateLogOpt(cfg map[string]string) error {
 		case "env":
 		case "env":
 		case "env-regex":
 		case "env-regex":
 		case "labels":
 		case "labels":
+		case "labels-regex":
 		case "syslog-address":
 		case "syslog-address":
 		case "syslog-facility":
 		case "syslog-facility":
 		case "syslog-tls-ca-cert":
 		case "syslog-tls-ca-cert":

+ 1 - 0
daemon/logger/syslog/syslog_test.go

@@ -137,6 +137,7 @@ func TestValidateLogOpt(t *testing.T) {
 		"env":                    "http://127.0.0.1",
 		"env":                    "http://127.0.0.1",
 		"env-regex":              "abc",
 		"env-regex":              "abc",
 		"labels":                 "labelA",
 		"labels":                 "labelA",
+		"labels-regex":           "def",
 		"syslog-address":         "udp://1.2.3.4:1111",
 		"syslog-address":         "udp://1.2.3.4:1111",
 		"syslog-facility":        "daemon",
 		"syslog-facility":        "daemon",
 		"syslog-tls-ca-cert":     "/etc/ca-certificates/custom/ca.pem",
 		"syslog-tls-ca-cert":     "/etc/ca-certificates/custom/ca.pem",