diff --git a/README.md b/README.md index 876add3..b515c55 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,10 @@ The following configuration settings are supported: log level are read and pushed to CloudWatch. For more information about priority levels, look at https://www.freedesktop.org/software/systemd/man/journalctl.html +* `unit`: (Optional) The name of a systemd unit to filter to. This has a behaviour similar to + `journalctl -u `. If this is specified, only journal messages originating from this unit + will be pushed to CloudWatch. + * `log_stream`: (Optional) The name of the cloudwatch log stream to write logs into. This defaults to the EC2 instance id. Each running instance of this application (along with any other applications writing logs into the same log group) must have a unique `log_stream` value. If the given log stream diff --git a/config.go b/config.go index 9f3a2b2..b6eeaa5 100644 --- a/config.go +++ b/config.go @@ -24,6 +24,7 @@ type Config struct { LogStreamName string LogPriority Priority StateFilename string + Unit string JournalDir string BufferSize int } @@ -36,6 +37,7 @@ type fileConfig struct { LogPriority string `hcl:"log_priority"` StateFilename string `hcl:"state_file"` JournalDir string `hcl:"journal_dir"` + Unit string `hcl:"unit"` BufferSize int `hcl:"buffer_size"` } @@ -127,6 +129,7 @@ func LoadConfig(filename string) (*Config, error) { config.StateFilename = fConfig.StateFilename config.JournalDir = fConfig.JournalDir + config.Unit = fConfig.Unit if fConfig.BufferSize != 0 { config.BufferSize = fConfig.BufferSize @@ -153,7 +156,6 @@ func (c *Config) NewAWSSession() *awsSession.Session { return awsSession.New(config) } - /* * Expand variables of the form $Foo or ${Foo} in the user provided config * from the EC2Metadata Instance Identity Document @@ -167,12 +169,12 @@ func expandFileConfig(config *fileConfig, metaClient *ec2metadata.EC2Metadata) { // struct extracting the string fields and their values into the vars map data, err := metaClient.GetInstanceIdentityDocument() if err == nil { - metadata := reflect.ValueOf( data ) + metadata := reflect.ValueOf(data) for i := 0; i < metadata.NumField(); i++ { field := metadata.Field(i) ftype := metadata.Type().Field(i) - if (field.Type() != reflect.TypeOf("")) { + if field.Type() != reflect.TypeOf("") { continue } vars[ftype.Name] = fmt.Sprintf("%v", field.Interface()) @@ -199,8 +201,8 @@ func expandFileConfig(config *fileConfig, metaClient *ec2metadata.EC2Metadata) { return val } // Unknown key => empty string - return "" - } else if (strings.HasPrefix(varname, "env.")) { + return "" + } else if strings.HasPrefix(varname, "env.") { return os.Getenv(strings.TrimPrefix(varname, "env.")) } else { // Unknown prefix => empty string @@ -213,7 +215,6 @@ func expandFileConfig(config *fileConfig, metaClient *ec2metadata.EC2Metadata) { } } - // Modified version of os.Expand() that only expands ${name} and not $name func expandBraceVars(s string, mapping func(string) string) string { buf := make([]byte, 0, 2*len(s)) @@ -223,10 +224,10 @@ func expandBraceVars(s string, mapping func(string) string) string { if s[j] == '$' && j+3 < len(s) && s[j+1] == '{' { buf = append(buf, s[i:j]...) idx := strings.Index(s[j+2:], "}") - if (idx >= 0) { + if idx >= 0 { // We have a full ${name} string buf = append(buf, mapping(s[j+2:j+2+idx])...) - j += 2+idx + j += 2 + idx } else { // We ran out of string (unclosed ${) return string(buf) @@ -236,6 +237,3 @@ func expandBraceVars(s string, mapping func(string) string) string { } return string(buf) + s[i:] } - - - diff --git a/journal.go b/journal.go index 35797a5..c6094a9 100644 --- a/journal.go +++ b/journal.go @@ -1,12 +1,17 @@ package main import ( - "github.com/coreos/go-systemd/sdjournal" "strconv" + + "github.com/coreos/go-systemd/sdjournal" ) func AddLogFilters(journal *sdjournal.Journal, config *Config) { + if unit := config.Unit; unit != "" { + journal.AddMatch(sdjournal.SD_JOURNAL_FIELD_SYSTEMD_UNIT + "=" + unit) + } + // Add Priority Filters if config.LogPriority < DEBUG { for p, _ := range PriorityJSON {