BrunoBonacci / mulog

μ/log is a micro-logging library that logs events and data, not words!
https://cljdoc.org/d/com.brunobonacci/mulog/
Apache License 2.0
490 stars 48 forks source link

timestamp accuracy #76

Closed wactbprot closed 3 years ago

wactbprot commented 3 years ago

Hello,

thank you for this great project! I have a brief question: Is it possible to configure higher timestamp accuracy to maintain logging order?

Thank you in advance Thomas

BrunoBonacci commented 3 years ago

Hi Thomas,

the timestamp uses the wall-clock ([System.currentTimeMillis()](https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#currentTimeMillis())).

If you are looking at ordering across applications in different systems (distributed logging), then I would suggest implementing Lamport Clocks.

If you are interested in the relative order of events within the same JVM, then the easiest option is to sort by :mulog/trace-id. The trace-id is a Flake. The high-order bits of a Flake use a monotonic clock base and the low-order bits are random.

The monotonic clock has nanoseconds precision, however, its value can only be estimated. The advantage of the monotonic clock is that while the wall-clock ([System.currentTimeMillis()](https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#currentTimeMillis())) will accelerate and slow down and sometimes jump back or forward (reset), the monotonic timer is guaranteed to always increase. The monotonic time-source depends on the specific hardware you use and its precision it is hardware dependent. The Flake has a homomorphic property in respect to the ordering so you can just compare two Flakes for the relative order of the events.

I hope this makes sense, if you need more info I'll be happy to help.

Bruno

wactbprot commented 3 years ago

That makes perfect sense. Thank you for your kind and patient reply.

Regards Thomas