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

Misc Changes #24

Open
wants to merge 4 commits 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
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
GOOS?=linux
GOARCH?=amd64
SOURCES:=$(shell find -type f -name '*.go')

PROGRAM:=journald-cloudwatch-logs

.PHONY: all
all: $(PROGRAM) $(PROGRAM).digests

.PHONY: deps
deps:
go get

$(PROGRAM): $(SOURCES) deps
GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o $@

$(PROGRAM).digests: $(PROGRAM)
sha256sum $< > $@

.PHONY: install
install: build
go install

.PHONY: clean
clean:
-rm $(PROGRAM)
17 changes: 6 additions & 11 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,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 +166,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 +198,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 +212,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 +221,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 +234,3 @@ func expandBraceVars(s string, mapping func(string) string) string {
}
return string(buf) + s[i:]
}



24 changes: 20 additions & 4 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ func (w *Writer) WriteBatch(records []Record) (string, error) {
return nil
}

createGroup := func() error {
request := &cloudwatchlogs.CreateLogGroupInput{
LogGroupName: &w.logGroupName,
}
_, err := w.conn.CreateLogGroup(request)
return err
}

createStream := func() error {
request := &cloudwatchlogs.CreateLogStreamInput{
LogGroupName: &w.logGroupName,
Expand All @@ -74,10 +82,18 @@ func (w *Writer) WriteBatch(records []Record) (string, error) {
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "ResourceNotFoundException" {
// Maybe our log stream doesn't exist yet. We'll try
// to create it and then, if we're successful, try
// writing the events again.
err := createStream()
// Maybe our log group or stream doesn't exist
// yet. We'll try to create it and then, if we're
// successful, try writing the events again.
err := createGroup()
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() != "ResourceAlreadyExistsException" {
return "", fmt.Errorf("Failed to create stream: %s", err)
}
}
}
err = createStream()
if err != nil {
return "", fmt.Errorf("failed to create stream: %s", err)
}
Expand Down