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

sdjournal: add GetEntry method to retrieve all fields #164

Merged
merged 2 commits into from Jun 7, 2016

Conversation

glerchundi
Copy link
Contributor

@glerchundi glerchundi commented May 26, 2016

Implements the same semantics as the JOURNAL_FOREACH_DATA_RETVAL macro in journal-internal.h and produces a *JournalEntry with every field with its corresponding data in a map[string]string. Also it includes address fields like realtime (__REALTIME_TIMESTAMP), monotonic (__MONOTONIC_TIMESTAMP and cursor (__CURSOR).

It mimics more or less journalctl -o json.

Fixes #142
Supersedes #136

@wrouesnel
Copy link

Looks good to me : should it support JsonEncode to produce a matching output?

@glerchundi
Copy link
Contributor Author

@wrouesnel sounds reasonable but I would vote not to do so. I think json encoding is out of this package scope (although in my use-case would be a really helpful shortcut...)

WDYT on this @lucab @jonboulle?

@lucab
Copy link
Contributor

lucab commented May 30, 2016

Personally, I also think json encoding is out of scope here and easily doable in the consuming application. (I just had a quick glance at code, haven't reviewed yet)

@glerchundi
Copy link
Contributor Author

(I just had a quick glance at code, haven't reviewed yet)

Ok, thanks!

if got != want {
t.Fatalf("Bad result for entry.Fields[\"MESSAGE\"]: got %s, want %s", got, want)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This test is fragile, it will easily pick up messages from other tests and get confused. I think you should instead filter on something a bit more specific (for example introducing a custom field key with a random entry, and matching on that).

Copy link

@tv42 tv42 Jun 3, 2016

Choose a reason for hiding this comment

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

Maybe filter by _PID (though beware concurrent tests).

@lucab
Copy link
Contributor

lucab commented Jun 3, 2016

Some remarks inline. I think the test definitely needs to be reworked, I already saw it failing several times here (it is a pity this can't be run on Travis).

@lucab lucab self-assigned this Jun 3, 2016
@lucab
Copy link
Contributor

lucab commented Jun 3, 2016

@ wrouesnel @wolfeidau: as original supporters of this, do you have any other concerns with it? Does it cover your usecases (minus the json part)?

@wrouesnel
Copy link

None from me!

@glerchundi
Copy link
Contributor Author

@lucab PTAL, i fixed your comments and reworked the test using a custom field with a timestamp as a random string, WDYT?

@lucab
Copy link
Contributor

lucab commented Jun 4, 2016

@glerchundi Thanks, I think it is much better now. I'll take another pass at this, just to double-check the C-casting part. In the meanwhile, would you mind word-wrapping the commit message as described in the contributing page and add a "fixes" line for #142?

@glerchundi glerchundi force-pushed the add-getjournalentry-function branch from 975031c to 559bbee Compare June 5, 2016 08:24
@glerchundi
Copy link
Contributor Author

@lucab done, I referenced the issue in the commit, PTAL whenever you have time, thanks :)

@glerchundi glerchundi force-pushed the add-getjournalentry-function branch from 559bbee to 8bea57f Compare June 5, 2016 08:37
@glerchundi
Copy link
Contributor Author

I reread every comments and splitted this PR in two commits, one for the implementation itself and the other for facilitating access to the fields themselves.

@lucab
Copy link
Contributor

lucab commented Jun 7, 2016

I think you just hit an unrelated bug here, as most of the time the test fails for me with:

--- FAIL: TestJournalGetEntry (0.00s)
        journal_test.go:188: Error reading to journal: EOF

Hardcoding the match filter works, though, so I'm wondering if the bug lies somewhere else in the match subsystem. Is the test working fine on your system? Which systemd version are you running?

@glerchundi
Copy link
Contributor Author

@lucab wow, that's weird, I run the test against two environments, Debian and Ubuntu (both inside virtual machines):

glerchundi@debian:~/.go/src/github.com/coreos/go-systemd$ ./test 
Running tests...
ok      github.com/coreos/go-systemd/activation 6.788s  coverage: 0.0% of statements
?       github.com/coreos/go-systemd/journal    [no test files]
ok      github.com/coreos/go-systemd/login1 0.005s  coverage: 41.9% of statements
ok      github.com/coreos/go-systemd/machine1   0.006s  coverage: 65.0% of statements
ok      github.com/coreos/go-systemd/unit   0.003s  coverage: 93.7% of statements
ok      github.com/coreos/go-systemd/sdjournal  6.948s  coverage: 45.2% of statements
Checking gofmt...
Checking govet...
Success

glerchundi@debian:~/.go/src/github.com/coreos/go-systemd$ uname -a
Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt25-2 (2016-04-08) x86_64 GNU/Linux

glerchundi@debian:~/.go/src/github.com/coreos/go-systemd$ cat /etc/os-release 
PRETTY_NAME="Debian GNU/Linux 8 (jessie)"
NAME="Debian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
ID=debian
HOME_URL="http://www.debian.org/"
SUPPORT_URL="http://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

glerchundi@debian:~/.go/src/github.com/coreos/go-systemd$ systemctl --version
systemd 215
+PAM +AUDIT +SELINUX +IMA +SYSVINIT +LIBCRYPTSETUP +GCRYPT +ACL +XZ -SECCOMP -APPARMOR
glerchundi@ubuntu:~/.go/src/github.com/coreos/go-systemd$ ./test
Running tests...
ok      github.com/coreos/go-systemd/activation 5.222s  coverage: 0.0% of statements
?       github.com/coreos/go-systemd/journal    [no test files]
ok      github.com/coreos/go-systemd/login1 0.005s  coverage: 41.9% of statements
ok      github.com/coreos/go-systemd/machine1   0.009s  coverage: 65.0% of statements
ok      github.com/coreos/go-systemd/unit   0.006s  coverage: 93.7% of statements
ok      github.com/coreos/go-systemd/sdjournal  6.490s  coverage: 45.0% of statements
Checking gofmt...
Checking govet...
Success

glerchundi@ubuntu:~/.go/src/github.com/coreos/go-systemd$ uname -a
Linux ubuntu 4.2.0-30-generic #36-Ubuntu SMP Fri Feb 26 00:58:07 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

glerchundi@ubuntu:~/.go/src/github.com/coreos/go-systemd$ cat /etc/os-release 
NAME="Ubuntu"
VERSION="15.10 (Wily Werewolf)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 15.10"
VERSION_ID="15.10"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"

glerchundi@ubuntu:~/.go/src/github.com/coreos/go-systemd$ systemctl --version
systemd 225
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 +SECCOMP +BLKID -ELFUTILS +KMOD -IDN

And it worked on both...., which systemd version are you using?

@glerchundi glerchundi force-pushed the add-getjournalentry-function branch 3 times, most recently from 8562d27 to ed33ef2 Compare June 7, 2016 15:08
@glerchundi
Copy link
Contributor Author

@lucab can you try if doing some retries, in this case 5 (I made this in the same way your reading follower does), works on your machine? Maybe journald is not emitting the result that fast to the sd_* subsystem and we're facing a race condition here.

@lucab
Copy link
Contributor

lucab commented Jun 7, 2016

@glerchundi yes I think I was hitting the R/W race you describe, just retrying one more time seems to reliably pass the test. I have no better proposals here, so I'll merge it as is.
LGTM

@glerchundi
Copy link
Contributor Author

@lucab ok, wait a minute I slightly changed just for your use case, i'll rever to the last time you reviewed.

@glerchundi glerchundi force-pushed the add-getjournalentry-function branch from ed33ef2 to a443091 Compare June 7, 2016 15:33
@lucab
Copy link
Contributor

lucab commented Jun 7, 2016

@glerchundi ah, no hurry then. I actually think you can leave the retry there, as the test is inherently racy.

@glerchundi
Copy link
Contributor Author

Ah, I reverted already because I'm quite sure once the EOF (r==0) is emitted next wait will fail so if you don't care I think it's better to keep it as simple as possible and think in a better solution, WDYT?

@glerchundi glerchundi force-pushed the add-getjournalentry-function branch from a443091 to 896af86 Compare June 7, 2016 15:41
…elds

Implements the same semantics as the JOURNAL_FOREACH_DATA_RETVAL macro in journal-internal.h and produces a *JournalEntry with every field with its corresponding data in a map[string]string. Also it includes address fields like realtime (__REALTIME_TIMESTAMP), monotonic (__MONOTONIC_TIMESTAMP and cursor (__CURSOR).

It mimics more or less `journalctl -o json`.

Fixes coreos#142
Supersedes coreos#136
As GetEntry outputs every field in a string keyed map, it eases access to a concrete field without hardcoding it.

Ref: https://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html
@glerchundi glerchundi force-pushed the add-getjournalentry-function branch from 896af86 to 7fd54aa Compare June 7, 2016 15:45
@glerchundi
Copy link
Contributor Author

@lucab should be mergeable now, recleaned commits, squashed, rebased and that git hell.

@lucab
Copy link
Contributor

lucab commented Jun 7, 2016

@glerchundi sorry for the confusion 😄. Yes, it is ok to land this as is and then explore other issues/races separately. I just double-checked the commits in this branch and they seem ok now. May I proceed?

@glerchundi
Copy link
Contributor Author

Yes please!

@lucab lucab changed the title sdjournal: add GetEntry method to retrieve all fields plus address fields sdjournal: add GetEntry method to retrieve all fields Jun 7, 2016
@lucab lucab merged commit ff577b2 into coreos:master Jun 7, 2016
@glerchundi
Copy link
Contributor Author

🎉 🎉

Thanks for your time @lucab

@glerchundi glerchundi deleted the add-getjournalentry-function branch June 7, 2016 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants