Skip to content

sdjournal: adds a follow with fields #151

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 25 additions & 0 deletions sdjournal/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,31 @@ func (j *Journal) GetData(field string) (string, error) {
return msg, nil
}

// EnumerateData may be used to iterate through all data fields used in the opened journal files
// the order of the returned field names is not defined.
func (j *Journal) EnumerateData() (string, error) {
var d unsafe.Pointer
var l C.size_t

j.mu.Lock()
r := C.sd_journal_enumerate_data(j.cjournal, &d, &l)
j.mu.Unlock()

if r < 0 {
return "", fmt.Errorf("failed to read message: %d", r)
}

msg := C.GoStringN((*C.char)(d), C.int(l))
return msg, nil
}

//RestartData resets the field name enumeration index to the beginning of the list.
func (j *Journal) RestartData() {
j.mu.Lock()
C.sd_journal_restart_data(j.cjournal)
j.mu.Unlock()
}

// GetDataValue gets the data object associated with a specific field from the
// current journal entry, returning only the value of the object.
func (j *Journal) GetDataValue(field string) (string, error) {
Expand Down
79 changes: 79 additions & 0 deletions sdjournal/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
package sdjournal

import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"strings"
"time"
)

Expand Down Expand Up @@ -188,6 +190,83 @@ process:
return
}

func (r *JournalReader) FollowWithFields(until <-chan time.Time, fields map[string]struct{}, writer io.Writer) (err error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kinda copied from the Follow() function. But I feel it's better to just pass a time, then create a channel inside the function. (e.g. untilCh := time.After(until))
cc @jonboulle ?

No need for a change in this PR though.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of passing in the channel, because it allows me more flexibility when I write my programs with this library. For example, I am working on a daemon that forwards journald logs to an external logging service over http. The only reason I would ever want it killed is if a signal interrupt were given, so to do this I send a time.Now() from my signal handler into this channel to stop the program gracefully.

Copy link
Contributor

@yifan-gu yifan-gu Apr 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@faiq OK, sounds fair.

enc := json.NewEncoder(writer)
journal:
for {
kvMap := make(map[string]string)
select {
case <-until:
return ErrExpired
default:
var err error
var c int

// Advance the journal cursor
c, err = r.journal.Next()

// An unexpected error
if err != nil {
return err
}

// We have a new journal entry go over the fields
// get the data for what we care about and return
r.journal.RestartData()
if c > 0 {
fields:
Copy link
Contributor

@yifan-gu yifan-gu Apr 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe change to another name, because fields is already taken an variable here, so it feels a little bit confusing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea.

for {
s, err := r.journal.EnumerateData()
if err != nil || len(s) == 0 {
break fields
}
s = s[:len(s)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing.

The EnumerateData call allows us to walk over all the different fields in the journal file. Now that I think about it, this can likely be done with multiple calls to GetData as well. Which would likely make it more efficient

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@faiq Good idea.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually in retrospect, i think this method should be called FollowWithAllFields because many people end up using custom fields in their journal entries. This allows the user to have the option to filter or not.

I think the best way of doing so would be taking out the fields map[string]struct{} parameter to the function

arr := strings.SplitN(s, "=", 2)
if _, ok := fields[arr[0]]; ok {
kvMap[arr[0]] = arr[1]
}

}
if err := enc.Encode(kvMap); err != nil {
return err
}
}
}

// we're at the tail, so wait for new events or time out.
// holds journal events to process. tightly bounded for now unless there's a
// reason to unblock the journal watch routine more quickly
events := make(chan int, 1)
pollDone := make(chan bool, 1)
go func() {
for {
select {
case <-pollDone:
return
default:
events <- r.journal.Wait(time.Duration(1) * time.Second)
}
}
}()

select {
case <-until:
pollDone <- true
return ErrExpired
case e := <-events:
pollDone <- true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@faiq I know this is copied from Follow(), but it doesn't feel right to me.
e.g. if there is no journal events in 1 second, then events r.journal.Wait() returns, and feed to the channel events in the above goroutine.
Then here, before we test the events, we notify the pollDone channel, causes the goroutine to return.

switch e {
case SD_JOURNAL_NOP, SD_JOURNAL_APPEND, SD_JOURNAL_INVALIDATE:
// TODO: need to account for any of these?
default:
log.Printf("Received unknown event: %d\n", e)
}
continue journal
}
}
return
}

// buildMessage returns a string representing the current journal entry in a simple format which
// includes the entry timestamp and MESSAGE field.
func (r *JournalReader) buildMessage() (string, error) {
Expand Down