Skip to content

Commit c965fe6

Browse files
committed
Send LogEvent timestamp as "time" in Http Collector json
Per splunk documentation, the log event time (that will later be parsed as the timestamp of the event by splunk) should be sent in the `time` property of the event metadata envelope and should be formated as epoch <sec>.<ms> Sending the event timestamp in in the pre-defined `time` metadata property is important as it alleviates the event processing demands in splunk. As stated in Splunk Docs: >The HTTP Event Collector endpoint extracts the events from the HTTP >request and parses them before sending them to indexers. Because the >event data format, as described in this topic, is pre-determined, Splunk >Enterprise is able to parse your data quickly, and then sends it to be >indexed. This results in improved data throughput and reduced event >processing time compared to other methods of getting data in. For more information, see Http Collector Protocol documentation (http://dev.splunk.com/view/SP-CAAAE6P)
1 parent 78c6b97 commit c965fe6

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

src/Serilog.Sinks.Splunk.FullNetFx/Serilog.Sinks.Splunk.FullNetFx.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
<Compile Include="..\Serilog.Sinks.Splunk\LoggerConfigurationSplunkPCLExtensions.cs">
6666
<Link>LoggerConfigurationSplunkPCLExtensions.cs</Link>
6767
</Compile>
68+
<Compile Include="..\Serilog.Sinks.Splunk\Sinks\Splunk\Epoch.cs">
69+
<Link>Sinks\Splunk\Epoch.cs</Link>
70+
</Compile>
6871
<Compile Include="..\Serilog.Sinks.Splunk\Sinks\Splunk\EventCollectorClient.cs">
6972
<Link>Sinks\Splunk\EventCollectorClient.cs</Link>
7073
</Compile>

src/Serilog.Sinks.Splunk/Serilog.Sinks.Splunk.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<Compile Include="..\..\assets\CommonAssemblyInfo.cs">
5555
<Link>Properties\CommonAssemblyInfo.cs</Link>
5656
</Compile>
57+
<Compile Include="Sinks\Splunk\Epoch.cs" />
5758
<Compile Include="Sinks\Splunk\EventCollectorClient.cs" />
5859
<Compile Include="Sinks\Splunk\EventCollectorRequest.cs" />
5960
<Compile Include="Sinks\Splunk\RepeatAction.cs" />
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Serilog.Sinks.Splunk
2+
{
3+
using System;
4+
5+
internal static class EpochExtensions
6+
{
7+
private static DateTimeOffset Epoch = new DateTimeOffset(1970,1,1,0,0,0,TimeSpan.Zero);
8+
9+
public static double ToEpoch(this DateTimeOffset value)
10+
{
11+
// From Splunk HTTP Collector Protocol
12+
// The default time format is epoch time format, in the format <sec>.<ms>.
13+
// For example, 1433188255.500 indicates 1433188255 seconds and 500 milliseconds after epoch,
14+
// or Monday, June 1, 2015, at 7:50:55 PM GMT.
15+
// See: http://dev.splunk.com/view/SP-CAAAE6P
16+
17+
return Math.Round((value - Epoch).TotalSeconds, 3, MidpointRounding.AwayFromZero);
18+
}
19+
}
20+
}

src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorRequest.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ internal class SplunkEvent
88
{
99
private string _payload;
1010

11-
internal SplunkEvent(string logEvent, string source, string sourceType, string host, string index)
11+
internal SplunkEvent(string logEvent, string source, string sourceType, string host, string index, double time)
1212
{
1313
_payload = string.Empty;
1414

@@ -32,6 +32,11 @@ internal SplunkEvent(string logEvent, string source, string sourceType, string h
3232
jsonPayLoad = jsonPayLoad + @",""index"":""" + index + @"""";
3333
}
3434

35+
if (time > 0)
36+
{
37+
jsonPayLoad = jsonPayLoad + @",""time"":" + time;
38+
}
39+
3540
jsonPayLoad = jsonPayLoad + "}";
3641
_payload = jsonPayLoad;
3742
}

src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorSink.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ private async Task Send(IEnumerable<LogEvent> events)
176176
_jsonFormatter.Format(logEvent, sw);
177177

178178
var serialisedEvent = sw.ToString();
179-
180-
var splunkEvent = new SplunkEvent(serialisedEvent, _source, _sourceType, _host, _index);
179+
180+
var splunkEvent = new SplunkEvent(serialisedEvent, _source, _sourceType, _host, _index, logEvent.Timestamp.ToEpoch());
181181

182182
allEvents = $"{allEvents}{splunkEvent.Payload}";
183183
}

0 commit comments

Comments
 (0)