Skip to content

Commit

Permalink
Merge pull request moby#38586 from robin-thoni/labels-regex
Browse files Browse the repository at this point in the history
Use a regex to match labels
  • Loading branch information
yongtang committed Apr 29, 2019
2 parents 8a4070a + bc70999 commit 9a2c263
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 11 deletions.
1 change: 1 addition & 0 deletions daemon/logger/fluentd/fluentd.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func ValidateLogOpt(cfg map[string]string) error {
case "env":
case "env-regex":
case "labels":
case "labels-regex":
case "tag":
case addressKey:
case bufferLimitKey:
Expand Down
19 changes: 10 additions & 9 deletions daemon/logger/gcplogs/gcplogging.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import (
const (
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 (
Expand Down Expand Up @@ -210,7 +211,7 @@ func New(info logger.Info) (logger.Logger, error) {
func ValidateLogOpts(cfg map[string]string) error {
for k := range cfg {
switch k {
case projectOptKey, logLabelsKey, logEnvKey, logEnvRegexKey, logCmdKey, logZoneKey, logNameKey, logIDKey:
case projectOptKey, logLabelsKey, logLabelsRegexKey, logEnvKey, logEnvRegexKey, logCmdKey, logZoneKey, logNameKey, logIDKey:
default:
return fmt.Errorf("%q is not a valid option for the gcplogs driver", k)
}
Expand Down
1 change: 1 addition & 0 deletions daemon/logger/gelf/gelf.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ func ValidateLogOpt(cfg map[string]string) error {
case "gelf-address":
case "tag":
case "labels":
case "labels-regex":
case "env":
case "env-regex":
case "gelf-compression-level":
Expand Down
1 change: 1 addition & 0 deletions daemon/logger/gelf/gelf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func TestUDPValidateLogOpt(t *testing.T) {
"gelf-address": "udp://127.0.0.1:12201",
"tag": "testtag",
"labels": "testlabel",
"labels-regex": "testlabel-regex",
"env": "testenv",
"env-regex": "testenv-regex",
"gelf-compression-level": "9",
Expand Down
1 change: 1 addition & 0 deletions daemon/logger/journald/journald.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func validateLogOpt(cfg map[string]string) error {
for key := range cfg {
switch key {
case "labels":
case "labels-regex":
case "env":
case "env-regex":
case "tag":
Expand Down
1 change: 1 addition & 0 deletions daemon/logger/jsonfilelog/jsonfilelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func ValidateLogOpt(cfg map[string]string) error {
case "max-size":
case "compress":
case "labels":
case "labels-regex":
case "env":
case "env-regex":
case "tag":
Expand Down
5 changes: 3 additions & 2 deletions daemon/logger/jsonfilelog/jsonfilelog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,12 @@ func TestJSONFileLoggerWithLabelsEnv(t *testing.T) {
}
defer os.RemoveAll(tmp)
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{
ContainerID: cid,
LogPath: filename,
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"},
})
if err != nil {
Expand All @@ -308,6 +308,7 @@ func TestJSONFileLoggerWithLabelsEnv(t *testing.T) {
expected := map[string]string{
"rack": "101",
"dc": "lhr",
"location": "here",
"environ": "production",
"debug": "false",
"ssl": "true",
Expand Down
1 change: 1 addition & 0 deletions daemon/logger/logentries/logentries.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func ValidateLogOpt(cfg map[string]string) error {
case "env":
case "env-regex":
case "labels":
case "labels-regex":
case "tag":
case key:
default:
Expand Down
16 changes: 16 additions & 0 deletions daemon/logger/loginfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
for _, e := range info.ContainerEnv {
if kv := strings.SplitN(e, "=", 2); len(kv) == 2 {
Expand Down
2 changes: 2 additions & 0 deletions daemon/logger/splunk/splunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
envKey = "env"
envRegexKey = "env-regex"
labelsKey = "labels"
labelsRegexKey = "labels-regex"
tagKey = "tag"
)

Expand Down Expand Up @@ -565,6 +566,7 @@ func ValidateLogOpt(cfg map[string]string) error {
case envKey:
case envRegexKey:
case labelsKey:
case labelsRegexKey:
case tagKey:
default:
return fmt.Errorf("unknown log opt '%s' for %s log driver", key, driverName)
Expand Down
1 change: 1 addition & 0 deletions daemon/logger/syslog/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ func ValidateLogOpt(cfg map[string]string) error {
case "env":
case "env-regex":
case "labels":
case "labels-regex":
case "syslog-address":
case "syslog-facility":
case "syslog-tls-ca-cert":
Expand Down
1 change: 1 addition & 0 deletions daemon/logger/syslog/syslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func TestValidateLogOpt(t *testing.T) {
"env": "http://127.0.0.1",
"env-regex": "abc",
"labels": "labelA",
"labels-regex": "def",
"syslog-address": "udp://1.2.3.4:1111",
"syslog-facility": "daemon",
"syslog-tls-ca-cert": "/etc/ca-certificates/custom/ca.pem",
Expand Down

0 comments on commit 9a2c263

Please sign in to comment.