-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send fractions of a second to Fluentd
Previously the timestamp did have a a precision of one second. The fraction got lost. The timestamp is now of the EventTime time according to specification: https://github.com/fluent/fluentd/wiki/Forward-Protocol-Specification-v1#eventtime-ext-format EventTime has nanosecond precision, but .NET only supports 100 nanoseconds (ticks). Fixes #12 Signed-off-by: Edwin Engelen <edwin@engelen.name>
- Loading branch information
1 parent
d0e932a
commit 69eddfa
Showing
2 changed files
with
54 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
|
||
namespace NLog.Targets | ||
{ | ||
using MsgPack; | ||
|
||
internal static class PackerExtensions | ||
{ | ||
private const int nanoSecondsPerSecond = 1 * 1000 * 1000 * 1000; | ||
private const double ticksToNanoSecondsFactor = (double)nanoSecondsPerSecond / TimeSpan.TicksPerSecond; | ||
private static readonly long unixEpochTicks = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks; | ||
|
||
/// <summary> | ||
/// Write according to Fluend EventTime specification. | ||
/// </summary> | ||
/// <remarks> | ||
/// Specification: https://github.com/fluent/fluentd/wiki/Forward-Protocol-Specification-v1#eventtime-ext-format" | ||
/// </remarks> | ||
public static Packer PackEventTime(this Packer that, DateTime value) | ||
{ | ||
DateTimeToEpoch(value, out var secondsFromEpoch, out var nanoSeconds); | ||
|
||
that.PackExtendedTypeValue( | ||
0x0, | ||
new[] | ||
{ | ||
(byte) ((ulong) (secondsFromEpoch >> 24) & (ulong) byte.MaxValue), | ||
(byte) ((ulong) (secondsFromEpoch >> 16) & (ulong) byte.MaxValue), | ||
(byte) ((ulong) (secondsFromEpoch >> 8) & (ulong) byte.MaxValue), | ||
(byte) ((ulong) secondsFromEpoch & (ulong) byte.MaxValue), | ||
(byte) ((ulong) (nanoSeconds >> 24) & (ulong) byte.MaxValue), | ||
(byte) ((ulong) (nanoSeconds >> 16) & (ulong) byte.MaxValue), | ||
(byte) ((ulong) (nanoSeconds >> 8) & (ulong) byte.MaxValue), | ||
(byte) ((ulong) nanoSeconds & (ulong) byte.MaxValue), | ||
}); | ||
|
||
return that; | ||
} | ||
|
||
private static void DateTimeToEpoch(DateTime value, out uint secondsFromEpoch, out uint nanoSeconds) | ||
{ | ||
var fromEpochTicks = value.ToUniversalTime().Ticks - unixEpochTicks; | ||
secondsFromEpoch = (uint)(fromEpochTicks / TimeSpan.TicksPerSecond); | ||
nanoSeconds = (uint)((fromEpochTicks - secondsFromEpoch * TimeSpan.TicksPerSecond) * ticksToNanoSecondsFactor); | ||
} | ||
} | ||
} |