This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from imburseag/AB1937-fix-batch-flushing
AB1937 Re-structure code in line with confluent kafka and periodic batch sink documentation
- Loading branch information
Showing
3 changed files
with
170 additions
and
122 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
114 changes: 114 additions & 0 deletions
114
src/Serilog.Sinks.Kafka/LoggerConfigurationExtensions.cs
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,114 @@ | ||
using System; | ||
using Confluent.Kafka; | ||
using Serilog.Configuration; | ||
using Serilog.Events; | ||
using Serilog.Formatting; | ||
using Serilog.Sinks.PeriodicBatching; | ||
|
||
namespace Serilog.Sinks.Kafka | ||
{ | ||
public static class LoggerConfigurationExtensions | ||
{ | ||
/// <summary> | ||
/// Adds a sink that writes log events to a Kafka topic in the broker endpoints. | ||
/// </summary> | ||
/// <param name="loggerConfiguration">The logger configuration.</param> | ||
/// <param name="batchSizeLimit">The maximum number of events to include in a single batch.</param> | ||
/// <param name="period">The time in seconds to wait between checking for event batches.</param> | ||
/// <param name="bootstrapServers">The list of bootstrapServers separated by comma.</param> | ||
/// <param name="topic">The topic name.</param> | ||
/// <returns></returns> | ||
public static LoggerConfiguration Kafka( | ||
this LoggerSinkConfiguration loggerConfiguration, | ||
string bootstrapServers = "localhost:9092", | ||
int batchSizeLimit = 50, | ||
int period = 5, | ||
SecurityProtocol securityProtocol = SecurityProtocol.Plaintext, | ||
SaslMechanism saslMechanism = SaslMechanism.Plain, | ||
string topic = "logs", | ||
string saslUsername = null, | ||
string saslPassword = null, | ||
string sslCaLocation = null, | ||
ITextFormatter formatter = null) | ||
{ | ||
return loggerConfiguration.Kafka( | ||
bootstrapServers, | ||
batchSizeLimit, | ||
period, | ||
securityProtocol, | ||
saslMechanism, | ||
saslUsername, | ||
saslPassword, | ||
sslCaLocation, | ||
topic, | ||
topicDecider: null, | ||
formatter); | ||
} | ||
|
||
public static LoggerConfiguration Kafka( | ||
this LoggerSinkConfiguration loggerConfiguration, | ||
Func<LogEvent, string> topicDecider, | ||
string bootstrapServers = "localhost:9092", | ||
int batchSizeLimit = 50, | ||
int period = 5, | ||
SecurityProtocol securityProtocol = SecurityProtocol.Plaintext, | ||
SaslMechanism saslMechanism = SaslMechanism.Plain, | ||
string saslUsername = null, | ||
string saslPassword = null, | ||
string sslCaLocation = null, | ||
ITextFormatter formatter = null) | ||
{ | ||
return loggerConfiguration.Kafka( | ||
bootstrapServers, | ||
batchSizeLimit, | ||
period, | ||
securityProtocol, | ||
saslMechanism, | ||
saslUsername, | ||
saslPassword, | ||
sslCaLocation, | ||
topic: null, | ||
topicDecider, | ||
formatter); | ||
} | ||
|
||
private static LoggerConfiguration Kafka( | ||
this LoggerSinkConfiguration loggerConfiguration, | ||
string bootstrapServers, | ||
int batchSizeLimit, | ||
int period, | ||
SecurityProtocol securityProtocol, | ||
SaslMechanism saslMechanism, | ||
string saslUsername, | ||
string saslPassword, | ||
string sslCaLocation, | ||
string topic, | ||
Func<LogEvent, string> topicDecider, | ||
ITextFormatter formatter) | ||
{ | ||
var kafkaSink = new KafkaSink( | ||
bootstrapServers, | ||
securityProtocol, | ||
saslMechanism, | ||
saslUsername, | ||
saslPassword, | ||
sslCaLocation, | ||
topic, | ||
topicDecider, | ||
formatter); | ||
|
||
var batchingOptions = new PeriodicBatchingSinkOptions | ||
{ | ||
BatchSizeLimit = batchSizeLimit, | ||
Period = TimeSpan.FromSeconds(period) | ||
}; | ||
|
||
var batchingSink = new PeriodicBatchingSink( | ||
kafkaSink, | ||
batchingOptions); | ||
|
||
return loggerConfiguration | ||
.Sink(batchingSink); | ||
} | ||
} | ||
} |
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