Skip to content

Commit

Permalink
Merge pull request moby#27565 from rothrock/env-by-regex
Browse files Browse the repository at this point in the history
[splunk] Use a regex to match environment variables
  • Loading branch information
cpuguy83 authored Mar 31, 2017
2 parents 1ecaed0 + 9758a2a commit 1325f66
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 34 deletions.
6 changes: 5 additions & 1 deletion daemon/logger/fluentd/fluentd.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ func New(info logger.Info) (logger.Logger, error) {
return nil, err
}

extra := info.ExtraAttributes(nil)
extra, err := info.ExtraAttributes(nil)
if err != nil {
return nil, err
}

bufferLimit := defaultBufferLimit
if info.Config[bufferLimitKey] != "" {
Expand Down Expand Up @@ -172,6 +175,7 @@ func ValidateLogOpt(cfg map[string]string) error {
for key := range cfg {
switch key {
case "env":
case "env-regex":
case "labels":
case "tag":
case addressKey:
Expand Down
24 changes: 15 additions & 9 deletions daemon/logger/gcplogs/gcplogging.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ import (
const (
name = "gcplogs"

projectOptKey = "gcp-project"
logLabelsKey = "labels"
logEnvKey = "env"
logCmdKey = "gcp-log-cmd"
logZoneKey = "gcp-meta-zone"
logNameKey = "gcp-meta-name"
logIDKey = "gcp-meta-id"
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"
)

var (
Expand Down Expand Up @@ -133,6 +134,11 @@ func New(info logger.Info) (logger.Logger, error) {
return nil, fmt.Errorf("unable to connect or authenticate with Google Cloud Logging: %v", err)
}

extraAttributes, err := info.ExtraAttributes(nil)
if err != nil {
return nil, err
}

l := &gcplogs{
logger: lg,
container: &containerInfo{
Expand All @@ -141,7 +147,7 @@ func New(info logger.Info) (logger.Logger, error) {
ImageName: info.ContainerImageName,
ImageID: info.ContainerImageID,
Created: info.ContainerCreated,
Metadata: info.ExtraAttributes(nil),
Metadata: extraAttributes,
},
}

Expand Down Expand Up @@ -185,7 +191,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, logCmdKey, logZoneKey, logNameKey, logIDKey:
case projectOptKey, logLabelsKey, logEnvKey, logEnvRegexKey, logCmdKey, logZoneKey, logNameKey, logIDKey:
default:
return fmt.Errorf("%q is not a valid option for the gcplogs driver", k)
}
Expand Down
8 changes: 7 additions & 1 deletion daemon/logger/gelf/gelf.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,17 @@ func New(info logger.Info) (logger.Logger, error) {
"_created": info.ContainerCreated,
}

extraAttrs := info.ExtraAttributes(func(key string) string {
extraAttrs, err := info.ExtraAttributes(func(key string) string {
if key[0] == '_' {
return key
}
return "_" + key
})

if err != nil {
return nil, err
}

for k, v := range extraAttrs {
extra[k] = v
}
Expand Down Expand Up @@ -157,6 +162,7 @@ func ValidateLogOpt(cfg map[string]string) error {
case "tag":
case "labels":
case "env":
case "env-regex":
case "gelf-compression-level":
i, err := strconv.Atoi(val)
if err != nil || i < flate.DefaultCompression || i > flate.BestCompression {
Expand Down
6 changes: 5 additions & 1 deletion daemon/logger/journald/journald.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ func New(info logger.Info) (logger.Logger, error) {
"CONTAINER_NAME": info.Name(),
"CONTAINER_TAG": tag,
}
extraAttrs := info.ExtraAttributes(sanitizeKeyMod)
extraAttrs, err := info.ExtraAttributes(sanitizeKeyMod)
if err != nil {
return nil, err
}
for k, v := range extraAttrs {
vars[k] = v
}
Expand All @@ -89,6 +92,7 @@ func validateLogOpt(cfg map[string]string) error {
switch key {
case "labels":
case "env":
case "env-regex":
case "tag":
default:
return fmt.Errorf("unknown log opt '%s' for journald log driver", key)
Expand Down
7 changes: 6 additions & 1 deletion daemon/logger/jsonfilelog/jsonfilelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ func New(info logger.Info) (logger.Logger, error) {
}

var extra []byte
if attrs := info.ExtraAttributes(nil); len(attrs) > 0 {
attrs, err := info.ExtraAttributes(nil)
if err != nil {
return nil, err
}
if len(attrs) > 0 {
var err error
extra, err = json.Marshal(attrs)
if err != nil {
Expand Down Expand Up @@ -122,6 +126,7 @@ func ValidateLogOpt(cfg map[string]string) error {
case "max-size":
case "labels":
case "env":
case "env-regex":
default:
return fmt.Errorf("unknown log opt '%s' for json-file log driver", key)
}
Expand Down
15 changes: 8 additions & 7 deletions daemon/logger/jsonfilelog/jsonfilelog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,13 @@ 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"}
config := map[string]string{"labels": "rack,dc", "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"},
ContainerEnv: []string{"environ=production", "debug=false", "port=10001", "ssl=true"},
ContainerEnv: []string{"environ=production", "debug=false", "port=10001", "ssl=true", "dc_region=west"},
})
if err != nil {
t.Fatal(err)
Expand All @@ -189,11 +189,12 @@ func TestJSONFileLoggerWithLabelsEnv(t *testing.T) {
t.Fatal(err)
}
expected := map[string]string{
"rack": "101",
"dc": "lhr",
"environ": "production",
"debug": "false",
"ssl": "true",
"rack": "101",
"dc": "lhr",
"environ": "production",
"debug": "false",
"ssl": "true",
"dc_region": "west",
}
if !reflect.DeepEqual(extra, expected) {
t.Fatalf("Wrong log attrs: %q, expected %q", extra, expected)
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 @@ -80,6 +80,7 @@ func ValidateLogOpt(cfg map[string]string) error {
for key := range cfg {
switch key {
case "env":
case "env-regex":
case "labels":
case "tag":
case key:
Expand Down
34 changes: 26 additions & 8 deletions daemon/logger/loginfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package logger
import (
"fmt"
"os"
"regexp"
"strings"
"time"
)
Expand All @@ -26,7 +27,7 @@ type Info struct {
// ExtraAttributes returns the user-defined extra attributes (labels,
// environment variables) in key-value format. This can be used by log drivers
// that support metadata to add more context to a log.
func (info *Info) ExtraAttributes(keyMod func(string) string) map[string]string {
func (info *Info) ExtraAttributes(keyMod func(string) string) (map[string]string, error) {
extra := make(map[string]string)
labels, ok := info.Config["labels"]
if ok && len(labels) > 0 {
Expand All @@ -40,14 +41,15 @@ func (info *Info) ExtraAttributes(keyMod func(string) string) map[string]string
}
}

envMapping := make(map[string]string)
for _, e := range info.ContainerEnv {
if kv := strings.SplitN(e, "=", 2); len(kv) == 2 {
envMapping[kv[0]] = kv[1]
}
}

env, ok := info.Config["env"]
if ok && len(env) > 0 {
envMapping := make(map[string]string)
for _, e := range info.ContainerEnv {
if kv := strings.SplitN(e, "=", 2); len(kv) == 2 {
envMapping[kv[0]] = kv[1]
}
}
for _, l := range strings.Split(env, ",") {
if v, ok := envMapping[l]; ok {
if keyMod != nil {
Expand All @@ -58,7 +60,23 @@ func (info *Info) ExtraAttributes(keyMod func(string) string) map[string]string
}
}

return extra
envRegex, ok := info.Config["env-regex"]
if ok && len(envRegex) > 0 {
re, err := regexp.Compile(envRegex)
if err != nil {
return nil, err
}
for k, v := range envMapping {
if re.MatchString(k) {
if keyMod != nil {
k = keyMod(k)
}
extra[k] = v
}
}
}

return extra, nil
}

// Hostname returns the hostname from the underlying OS.
Expand Down
7 changes: 6 additions & 1 deletion daemon/logger/splunk/splunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
splunkGzipCompressionKey = "splunk-gzip"
splunkGzipCompressionLevelKey = "splunk-gzip-level"
envKey = "env"
envRegexKey = "env-regex"
labelsKey = "labels"
tagKey = "tag"
)
Expand Down Expand Up @@ -235,7 +236,10 @@ func New(info logger.Info) (logger.Logger, error) {
}
}

attrs := info.ExtraAttributes(nil)
attrs, err := info.ExtraAttributes(nil)
if err != nil {
return nil, err
}

var (
postMessagesFrequency = getAdvancedOptionDuration(envVarPostMessagesFrequency, defaultPostMessagesFrequency)
Expand Down Expand Up @@ -538,6 +542,7 @@ func ValidateLogOpt(cfg map[string]string) error {
case splunkGzipCompressionKey:
case splunkGzipCompressionLevelKey:
case envKey:
case envRegexKey:
case labelsKey:
case tagKey:
default:
Expand Down
14 changes: 9 additions & 5 deletions daemon/logger/splunk/splunk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ func TestValidateLogOpt(t *testing.T) {
splunkVerifyConnectionKey: "true",
splunkGzipCompressionKey: "true",
splunkGzipCompressionLevelKey: "1",
envKey: "a",
labelsKey: "b",
tagKey: "c",
envKey: "a",
envRegexKey: "^foo",
labelsKey: "b",
tagKey: "c",
})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -215,8 +216,9 @@ func TestInlineFormatWithNonDefaultOptions(t *testing.T) {
splunkIndexKey: "myindex",
splunkFormatKey: splunkFormatInline,
splunkGzipCompressionKey: "true",
tagKey: "{{.ImageName}}/{{.Name}}",
labelsKey: "a",
tagKey: "{{.ImageName}}/{{.Name}}",
labelsKey: "a",
envRegexKey: "^foo",
},
ContainerID: "containeriid",
ContainerName: "/container_name",
Expand All @@ -225,6 +227,7 @@ func TestInlineFormatWithNonDefaultOptions(t *testing.T) {
ContainerLabels: map[string]string{
"a": "b",
},
ContainerEnv: []string{"foo_finder=bar"},
}

hostname, err := info.Hostname()
Expand Down Expand Up @@ -295,6 +298,7 @@ func TestInlineFormatWithNonDefaultOptions(t *testing.T) {
event["source"] != "stdout" ||
event["tag"] != "container_image_name/container_name" ||
event["attrs"].(map[string]interface{})["a"] != "b" ||
event["attrs"].(map[string]interface{})["foo_finder"] != "bar" ||
len(event) != 4 {
t.Fatalf("Unexpected event in message %v", event)
}
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 @@ -186,6 +186,7 @@ func ValidateLogOpt(cfg map[string]string) error {
for key := range cfg {
switch key {
case "env":
case "env-regex":
case "labels":
case "syslog-address":
case "syslog-facility":
Expand Down

0 comments on commit 1325f66

Please sign in to comment.