Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow filtering to a single systemd unit #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <unit>`. 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
Expand Down
20 changes: 9 additions & 11 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Config struct {
LogStreamName string
LogPriority Priority
StateFilename string
Unit string
JournalDir string
BufferSize int
}
Expand All @@ -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"`
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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())
Expand All @@ -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
Expand All @@ -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))
Expand All @@ -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)
Expand All @@ -236,6 +237,3 @@ func expandBraceVars(s string, mapping func(string) string) string {
}
return string(buf) + s[i:]
}



7 changes: 6 additions & 1 deletion journal.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down