Skip to content

Commit

Permalink
Fixed nanosecond to second conversions in target.
Browse files Browse the repository at this point in the history
  • Loading branch information
corentinaltepe committed May 30, 2022
1 parent c9ec8b0 commit 7c39a22
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/NLog.Loki.gRPC/LokiTarget.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -49,12 +50,14 @@ protected override async Task WriteAsyncTask(LogEventInfo logEvent, Cancellation
{
var @event = GetLokiEvent(logEvent);
var stream = new StreamAdapter { Labels = FormatLabels(@event.Labels.Labels) };
var nano = UnixDateTimeConverter.ToUnixTimeNs(@event.Timestamp);
stream.Entries.Add(new EntryAdapter()
{
Line = @event.Line,
Timestamp = new Google.Protobuf.WellKnownTypes.Timestamp
{
Seconds = UnixDateTimeConverter.ToUnixTimeNs(@event.Timestamp) / 1000000000
Seconds = nano / 1_000_000_000,
Nanos = Convert.ToInt32(nano % 1_000_000_000),
}
});

Expand All @@ -78,14 +81,18 @@ protected override async Task WriteAsyncTask(IList<LogEventInfo> logEvents, Canc
orderedEvents = gp.OrderBy(e => e.Timestamp);

foreach(var @event in gp)
{
var nano = UnixDateTimeConverter.ToUnixTimeNs(@event.Timestamp);
stream.Entries.Add(new EntryAdapter()
{
Line = @event.Line,
Timestamp = new Google.Protobuf.WellKnownTypes.Timestamp
{
Seconds = UnixDateTimeConverter.ToUnixTimeNs(@event.Timestamp)
Seconds = nano / 1_000_000_000,
Nanos = Convert.ToInt32(nano % 1_000_000_000),
}
});
}
return stream;
});

Expand Down

0 comments on commit 7c39a22

Please sign in to comment.