Skip to content

Commit

Permalink
Merge pull request #12 in GITPLAY/splunk-library-dotnetlogging-battle…
Browse files Browse the repository at this point in the history
…cat from bugfix/DVPL-6421-rename-http-input-in-.net-logging to develop

* commit '2497887cf7e859d40c66818a9b481ae4cabb3c30':
  Refactoring http input -> http event collector
  • Loading branch information
oizmerly committed Jun 18, 2015
2 parents b95416c + 2497887 commit 3b724ba
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
namespace Splunk.Logging
{
/// <summary>
/// HttpInputEventInfo is a wrapper container for .NET events information.
/// An instance of HttpInputEventInfo can be easily serialized into json
/// HttpEventCollectorEventInfo is a wrapper container for .NET events information.
/// An instance of HttpEventCollectorEventInfo can be easily serialized into json
/// format using JsonConvert.SerializeObject.
/// </summary>
public class HttpInputEventInfo
public class HttpEventCollectorEventInfo
{
#region metadata

/// <summary>
/// Common metadata tags that can be specified by HTTP input logger.
/// Common metadata tags that can be specified by HTTP event collector logger.
/// </summary>
public const string MetadataTimeTag = "time";
public const string MetadataIndexTag = "index";
Expand Down Expand Up @@ -149,14 +149,14 @@ internal LoggerEvent(string id, string severity, string message, object data) :
public LoggerEvent Event { get; private set; }

/// <summary>
/// HttpInputEventInfo c-or.
/// HttpEventCollectorEventInfo c-or.
/// </summary>
/// <param name="id">Event id.</param>
/// <param name="severity">Event severity info.</param>
/// <param name="message">Event message text.</param>
/// <param name="data">Event auxiliary data.</param>
/// <param name="metadata">Logger metadata.</param>
public HttpInputEventInfo(
public HttpEventCollectorEventInfo(
string id, string severity, string message, object data,
Metadata metadata)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
namespace Splunk.Logging
{
/// <summary>
/// HTTP input exception. This class is used when an HTTP input client is
/// HTTP event collector exception. This class is used when an HTTP event collector client is
/// unable to send events to the server;
/// </summary>
public class HttpInputException : Exception
public class HttpEventCollectorException : Exception
{
/// <summary>
/// HTTP status code.
Expand All @@ -51,24 +51,24 @@ public class HttpInputException : Exception
public HttpResponseMessage Response { get; private set; }

/// <summary>
/// List of events that caused the problem. This value is set by HttpInputSender.
/// List of events that caused the problem. This value is set by HttpEventCollectorSender.
/// </summary>
public List<HttpInputEventInfo> Events { get; set; }
public List<HttpEventCollectorEventInfo> Events { get; set; }

/// <summary>
/// HTTP input exception container.
/// HTTP event collector exception container.
/// </summary>
/// <param name="code">HTTP status code.</param>
/// <param name="webException">Exception thrown by HTTP client when sending the data.</param>
/// <param name="reply">Splunk server reply.</param>
/// <param name="response">HTTP response.</param>
/// <param name="events">List of events that caused the problem.</param>
public HttpInputException(
public HttpEventCollectorException(
HttpStatusCode code,
Exception webException = null,
string reply = null,
HttpResponseMessage response = null,
List<HttpInputEventInfo> events = null)
List<HttpEventCollectorEventInfo> events = null)
{
this.StatusCode = code;
this.WebException = webException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@
namespace Splunk.Logging
{
/// <summary>
/// HTTP input middleware plug in that implements a simple resend policy.
/// HTTP event collector middleware plug in that implements a simple resend policy.
/// When HTTP post reply isn't an application error we try to resend the data.
/// Usage:
/// <code>
/// trace.listeners.Add(new HttpInputTraceListener(
/// trace.listeners.Add(new HttpEventCollectorTraceListener(
/// uri: new Uri("https://localhost:8089"),
/// token: "E6099437-3E1F-4793-90AB-0E5D9438A918",
/// new HttpInputResendMiddleware(10).Plugin // retry 10 times
/// new HttpEventCollectorResendMiddleware(10).Plugin // retry 10 times
/// );
/// </code>
/// </summary>
public class HttpInputResendMiddleware
public class HttpEventCollectorResendMiddleware
{
// List of HTTP input server application error statuses. These statuses
// List of HTTP event collector server application error statuses. These statuses
// indicate non-transient problems that cannot be fixed by resending the
// data.
private static readonly HttpStatusCode[] HttpInputApplicationErrors =
private static readonly HttpStatusCode[] HttpEventCollectorApplicationErrors =
{
HttpStatusCode.Forbidden,
HttpStatusCode.MethodNotAllowed,
Expand All @@ -55,21 +55,21 @@ public class HttpInputResendMiddleware
/// <param name="retriesOnError">
/// Max number of retries before reporting an error.
/// </param>
public HttpInputResendMiddleware(int retriesOnError)
public HttpEventCollectorResendMiddleware(int retriesOnError)
{
this.retriesOnError = retriesOnError;
}

/// <summary>
/// Callback that should be used as middleware in HttpInputSender
/// Callback that should be used as middleware in HttpEventCollectorSender
/// </summary>
/// <param name="request"></param>
/// <param name="next"></param>
/// <returns></returns>
public async Task<HttpResponseMessage> Plugin(
string token,
List<HttpInputEventInfo> events,
HttpInputSender.HttpInputHandler next)
List<HttpEventCollectorEventInfo> events,
HttpEventCollectorSender.HttpEventCollectorHandler next)
{
HttpResponseMessage response = null;
HttpStatusCode statusCode = HttpStatusCode.OK;
Expand All @@ -89,9 +89,9 @@ public async Task<HttpResponseMessage> Plugin(
webException = null;
break;
}
else if (Array.IndexOf(HttpInputApplicationErrors, statusCode) >= 0)
else if (Array.IndexOf(HttpEventCollectorApplicationErrors, statusCode) >= 0)
{
// HTTP input application error detected - resend wouldn't help
// HTTP event collector application error detected - resend wouldn't help
// in this case. Record server reply and break.
if (response.Content != null)
{
Expand All @@ -116,7 +116,7 @@ public async Task<HttpResponseMessage> Plugin(
}
if (statusCode != HttpStatusCode.OK || webException != null)
{
throw new HttpInputException(
throw new HttpEventCollectorException(
code: statusCode,
webException: webException,
reply: serverReply,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@
namespace Splunk.Logging
{
/// <summary>
/// HTTP input client side implementation that collects, serializes and send
/// events to Splunk HTTP input endpoint. This class shouldn't be used directly
/// HTTP event collector client side implementation that collects, serializes and send
/// events to Splunk HTTP event collector endpoint. This class shouldn't be used directly
/// by user applications.
/// </summary>
/// <remarks>
/// * HttpInputSender is thread safe and Send(...) method may be called from
/// * HttpEventCollectorSender is thread safe and Send(...) method may be called from
/// different threads.
/// * Events are are sending asynchronously and Send(...) method doesn't
/// block the caller code.
/// * HttpInputSender has an ability to plug middleware components that act
/// * HttpEventCollectorSender has an ability to plug middleware components that act
/// before posting data.
/// For example:
/// <code>
/// new HttpInputSender(uri: ..., token: ...,
/// new HttpEventCollectorSender(uri: ..., token: ...,
/// middleware: (request, next) => {
/// // preprocess request
/// var response = next(request); // post data
Expand All @@ -53,54 +53,54 @@ namespace Splunk.Logging
/// )
/// </code>
/// Middleware components can apply additional logic before and after posting
/// the data to Splunk server. See HttpInputResendMiddleware.
/// the data to Splunk server. See HttpEventCollectorResendMiddleware.
/// </remarks>
public class HttpInputSender : IDisposable
public class HttpEventCollectorSender : IDisposable
{
/// <summary>
/// Post request delegate.
/// </summary>
/// <param name="request">HTTP request.</param>
/// <returns>Server HTTP response.</returns>
public delegate Task<HttpResponseMessage> HttpInputHandler(
string token, List<HttpInputEventInfo> events);
public delegate Task<HttpResponseMessage> HttpEventCollectorHandler(
string token, List<HttpEventCollectorEventInfo> events);

/// <summary>
/// HTTP input middleware plugin.
/// HTTP event collector middleware plugin.
/// </summary>
/// <param name="request">HTTP request.</param>
/// <param name="next">A handler that posts data to the server.</param>
/// <returns>Server HTTP response.</returns>
public delegate Task<HttpResponseMessage> HttpInputMiddleware(
string token, List<HttpInputEventInfo> events, HttpInputHandler next);
public delegate Task<HttpResponseMessage> HttpEventCollectorMiddleware(
string token, List<HttpEventCollectorEventInfo> events, HttpEventCollectorHandler next);

private const string HttpContentTypeMedia = "application/json";
private const string HttpInputPath = "/services/receivers/token/event/1.0";
private const string HttpEventCollectorPath = "/services/collector/event/1.0";
private const string AuthorizationHeaderScheme = "Splunk";
private Uri httpInputEndpointUri; // HTTP input endpoint full uri
private HttpInputEventInfo.Metadata metadata; // logger metadata
private Uri httpEventCollectorEndpointUri; // HTTP event collector endpoint full uri
private HttpEventCollectorEventInfo.Metadata metadata; // logger metadata
private string token; // authorization token

// events batching properties and collection
private int batchInterval = 0;
private int batchSizeBytes = 0;
private int batchSizeCount = 0;
private List<HttpInputEventInfo> eventsBatch = new List<HttpInputEventInfo>();
private List<HttpEventCollectorEventInfo> eventsBatch = new List<HttpEventCollectorEventInfo>();
private StringBuilder serializedEventsBatch = new StringBuilder();
private Timer timer;

private HttpClient httpClient = null;
private HttpInputMiddleware middleware = null;
private HttpEventCollectorMiddleware middleware = null;
// counter for bookkeeping the async tasks
private long activeAsyncTasksCount = 0;

/// <summary>
/// On error callbacks.
/// </summary>
public event EventHandler<HttpInputException> OnError = (s, e) => { };
public event EventHandler<HttpEventCollectorException> OnError = (s, e) => { };

/// <param name="uri">Splunk server uri, for example https://localhost:8089.</param>
/// <param name="token">HTTP input authorization token.</param>
/// <param name="token">HTTP event collector authorization token.</param>
/// <param name="metadata">Logger metadata.</param>
/// <param name="batchInterval">Batch interval in milliseconds.</param>
/// <param name="batchSizeBytes">Batch max size.</param>
Expand All @@ -112,12 +112,12 @@ public delegate Task<HttpResponseMessage> HttpInputMiddleware(
/// <remarks>
/// Zero values for the batching params mean that batching is off.
/// </remarks>
public HttpInputSender(
Uri uri, string token, HttpInputEventInfo.Metadata metadata,
public HttpEventCollectorSender(
Uri uri, string token, HttpEventCollectorEventInfo.Metadata metadata,
int batchInterval, int batchSizeBytes, int batchSizeCount,
HttpInputMiddleware middleware)
HttpEventCollectorMiddleware middleware)
{
this.httpInputEndpointUri = new Uri(uri, HttpInputPath);
this.httpEventCollectorEndpointUri = new Uri(uri, HttpEventCollectorPath);
this.batchInterval = batchInterval;
this.batchSizeBytes = batchSizeBytes;
this.batchSizeCount = batchSizeCount;
Expand Down Expand Up @@ -162,8 +162,8 @@ public void Send(
string message = null,
object data = null)
{
HttpInputEventInfo ei =
new HttpInputEventInfo(id, severity, message, data, metadata);
HttpEventCollectorEventInfo ei =
new HttpEventCollectorEventInfo(id, severity, message, data, metadata);
// we use lock serializedEventsBatch to synchronize both
// serializedEventsBatch and serializedEvents
string serializedEventInfo = SerializeEventInfo(ei);
Expand Down Expand Up @@ -212,7 +212,7 @@ public void FlushSync()
/// </summary>
/// <param name="eventInfo"></param>
/// <returns></returns>
public static string SerializeEventInfo(HttpInputEventInfo eventInfo)
public static string SerializeEventInfo(HttpEventCollectorEventInfo eventInfo)
{
return JsonConvert.SerializeObject(eventInfo);
}
Expand All @@ -232,13 +232,13 @@ private void FlushUnlocked()
// the old ones because Flush works in async mode
// and can use use "previous" containers
serializedEventsBatch = new StringBuilder();
eventsBatch = new List<HttpInputEventInfo>();
eventsBatch = new List<HttpEventCollectorEventInfo>();
}

}

private async Task<HttpStatusCode> PostEvents(
List<HttpInputEventInfo> events,
List<HttpEventCollectorEventInfo> events,
String serializedEvents)
{
// encode data
Expand All @@ -248,12 +248,12 @@ private async Task<HttpStatusCode> PostEvents(
try
{
// post data
HttpInputHandler next = (t, e) =>
HttpEventCollectorHandler next = (t, e) =>
{
HttpContent content = new StringContent(serializedEvents, Encoding.UTF8, HttpContentTypeMedia);
return httpClient.PostAsync(httpInputEndpointUri, content);
return httpClient.PostAsync(httpEventCollectorEndpointUri, content);
};
HttpInputHandler postEvents = (t, e) =>
HttpEventCollectorHandler postEvents = (t, e) =>
{
return middleware == null ?
next(t, e) : middleware(t, e, next);
Expand All @@ -264,7 +264,7 @@ private async Task<HttpStatusCode> PostEvents(
{
// record server reply
serverReply = await response.Content.ReadAsStringAsync();
OnError(this, new HttpInputException(
OnError(this, new HttpEventCollectorException(
code: responseCode,
webException: null,
reply: serverReply,
Expand All @@ -273,14 +273,14 @@ private async Task<HttpStatusCode> PostEvents(
));
}
}
catch (HttpInputException e)
catch (HttpEventCollectorException e)
{
e.Events = events;
OnError(this, e);
}
catch (Exception e)
{
OnError(this, new HttpInputException(
OnError(this, new HttpEventCollectorException(
code: responseCode,
webException: e,
reply: serverReply,
Expand Down Expand Up @@ -320,7 +320,7 @@ protected virtual void Dispose(bool disposing)
disposed = true;
}

~HttpInputSender()
~HttpEventCollectorSender()
{
Dispose(false);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Splunk.Logging.Common/Splunk.Logging.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@
<Compile Include="TcpReconnectionPolicy.cs" />
<Compile Include="TcpSocketWriter.cs" />
<Compile Include="Util.cs" />
<Compile Include="HttpInputSender.cs" />
<Compile Include="HttpInputEventInfo.cs" />
<Compile Include="HttpInputException.cs" />
<Compile Include="HttpInputResendMiddleware.cs" />
<Compile Include="HttpEventCollectorSender.cs" />
<Compile Include="HttpEventCollectorEventInfo.cs" />
<Compile Include="HttpEventCollectorException.cs" />
<Compile Include="HttpEventCollectorResendMiddleware.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
Loading

0 comments on commit 3b724ba

Please sign in to comment.