Signed-off-by: Robin THONI <robin@rthoni.com>
| ... | ... |
@@ -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 |
} |
| ... | ... |
@@ -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", |
| ... | ... |
@@ -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", |
| ... | ... |
@@ -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)
|
| ... | ... |
@@ -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", |