-
Notifications
You must be signed in to change notification settings - Fork 47
/
EventCollectorSink.cs
214 lines (199 loc) · 9.91 KB
/
EventCollectorSink.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2016 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using Serilog.Core;
using Serilog.Debugging;
using Serilog.Events;
using Serilog.Formatting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace Serilog.Sinks.Splunk
{
/// <summary>
/// A sink to log to the Event Collector available in Splunk 6.3
/// </summary>
public class EventCollectorSink : IBatchedLogEventSink
{
internal const int DefaultQueueLimit = 100000;
private readonly string _splunkHost;
private readonly string _uriPath;
private readonly ITextFormatter _jsonFormatter;
private readonly EventCollectorClient _httpClient;
/// <summary>
/// Taken from Splunk.Logging.Common
/// </summary>
private static readonly HttpStatusCode[] HttpEventCollectorApplicationErrors =
{
HttpStatusCode.Forbidden,
HttpStatusCode.MethodNotAllowed,
HttpStatusCode.BadRequest
};
/// <summary>
/// Creates a new instance of the sink
/// </summary>
/// <param name="splunkHost">The host of the Splunk instance with the Event collector configured</param>
/// <param name="eventCollectorToken">The token to use when authenticating with the event collector</param>
/// <param name="formatProvider">The format provider used when rendering the message</param>
/// <param name="renderTemplate">Whether to render the message template</param>
/// <param name="renderMessage">Include "RenderedMessage" parameter in output JSON message.</param>
/// <param name="subSecondPrecision">Timestamp sub-second precision. Splunk props.conf setup is required.</param>
public EventCollectorSink(
string splunkHost,
string eventCollectorToken,
IFormatProvider formatProvider = null,
bool renderTemplate = true,
bool renderMessage = true,
SubSecondPrecision subSecondPrecision = SubSecondPrecision.Milliseconds)
: this(
splunkHost,
eventCollectorToken,
null, null, null, null, null,
formatProvider,
renderTemplate,
renderMessage,
subSecondPrecision: subSecondPrecision)
{
}
/// <summary>
/// Creates a new instance of the sink
/// </summary>
/// <param name="splunkHost">The host of the Splunk instance with the Event collector configured</param>
/// <param name="eventCollectorToken">The token to use when authenticating with the event collector</param>
/// <param name="uriPath">Change the default endpoint of the Event Collector e.g. services/collector/event</param>
/// <param name="formatProvider">The format provider used when rendering the message</param>
/// <param name="renderTemplate">Whether to render the message template</param>
/// <param name="index">The Splunk index to log to</param>
/// <param name="source">The source of the event</param>
/// <param name="sourceType">The source type of the event</param>
/// <param name="host">The host of the event</param>
/// <param name="messageHandler">The handler used to send HTTP requests</param>
/// <param name="renderMessage">Include "RenderedMessage" parameter in output JSON message.</param>
/// <param name="subSecondPrecision">Timestamp sub-second precision. Splunk props.conf setup is required.</param>
public EventCollectorSink(
string splunkHost,
string eventCollectorToken,
string uriPath,
string source,
string sourceType,
string host,
string index,
IFormatProvider formatProvider = null,
bool renderTemplate = true,
bool renderMessage = true,
HttpMessageHandler messageHandler = null,
SubSecondPrecision subSecondPrecision = SubSecondPrecision.Milliseconds)
: this(
splunkHost,
eventCollectorToken,
uriPath,
new SplunkJsonFormatter(renderTemplate, renderMessage, formatProvider, source, sourceType, host, index, subSecondPrecision: subSecondPrecision),
messageHandler)
{
}
/// <summary>
/// Creates a new instance of the sink with Customfields
/// </summary>
/// <param name="splunkHost">The host of the Splunk instance with the Event collector configured</param>
/// <param name="eventCollectorToken">The token to use when authenticating with the event collector</param>
/// <param name="uriPath">Change the default endpoint of the Event Collector e.g. services/collector/event</param>
/// <param name="formatProvider">The format provider used when rendering the message</param>
/// <param name="renderTemplate">Whether to render the message template</param>
/// <param name="index">The Splunk index to log to</param>
/// <param name="fields">Add extra CustomExtraFields for Splunk to index</param>
/// <param name="source">The source of the event</param>
/// <param name="sourceType">The source type of the event</param>
/// <param name="host">The host of the event</param>
/// <param name="messageHandler">The handler used to send HTTP requests</param>
/// <param name="renderMessage">Include "RenderedMessage" parameter in output JSON message.</param>
/// <param name="subSecondPrecision">Timestamp sub-second precision. Splunk props.conf setup is required.</param>
public EventCollectorSink(
string splunkHost,
string eventCollectorToken,
string uriPath,
string source,
string sourceType,
string host,
string index,
CustomFields fields,
IFormatProvider formatProvider = null,
bool renderTemplate = true,
bool renderMessage = true,
HttpMessageHandler messageHandler = null,
SubSecondPrecision subSecondPrecision = SubSecondPrecision.Milliseconds)
// TODO here is the jsonformatter creation. We must make way to test output of jsonformatter.
: this(
splunkHost,
eventCollectorToken,
uriPath,
new SplunkJsonFormatter(renderTemplate, renderMessage, formatProvider, source, sourceType, host, index, fields, subSecondPrecision: subSecondPrecision),
messageHandler)
{
}
/// <summary>
/// Creates a new instance of the sink
/// </summary>
/// <param name="splunkHost">The host of the Splunk instance with the Event collector configured</param>
/// <param name="eventCollectorToken">The token to use when authenticating with the event collector</param>
/// <param name="uriPath">Change the default endpoint of the Event Collector e.g. services/collector/event</param>
/// <param name="jsonFormatter">The text formatter used to render log events into a JSON format for consumption by Splunk</param>
/// <param name="messageHandler">The handler used to send HTTP requests</param>
public EventCollectorSink(
string splunkHost,
string eventCollectorToken,
string uriPath,
ITextFormatter jsonFormatter,
HttpMessageHandler messageHandler = null)
{
_uriPath = uriPath;
_splunkHost = splunkHost;
_jsonFormatter = jsonFormatter;
_httpClient = messageHandler != null
? new EventCollectorClient(eventCollectorToken, messageHandler)
: new EventCollectorClient(eventCollectorToken);
}
/// <inheritdoc />
public virtual Task OnEmptyBatchAsync() => Task.CompletedTask;
/// <inheritdoc />
public virtual async Task EmitBatchAsync(IReadOnlyCollection<LogEvent> batch)
{
var allEvents = new StringWriter();
foreach (var logEvent in batch)
{
_jsonFormatter.Format(logEvent, allEvents);
}
var request = new EventCollectorRequest(_splunkHost, allEvents.ToString(), _uriPath);
var response = await _httpClient.SendAsync(request).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
//Application Errors sent via HTTP Event Collector
if (HttpEventCollectorApplicationErrors.Any(x => x == response.StatusCode))
{
// By not throwing an exception here the PeriodicBatchingSink will assume the batch succeeded and not send it again.
SelfLog.WriteLine(
"A status code of {0} was received when attempting to send to {1}. The event has been discarded and will not be placed back in the queue.",
response.StatusCode.ToString(), _splunkHost);
}
else
{
// EnsureSuccessStatusCode will throw an exception and the PeriodicBatchingSink will catch/log the exception and retry the batch.
response.EnsureSuccessStatusCode();
}
}
}
}
}