Skip to content

Commit

Permalink
Allow configuration of TransportConfiguration when using Sink helpers (
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz authored Mar 15, 2023
1 parent 2a746e6 commit af53e3c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 26 deletions.
12 changes: 12 additions & 0 deletions examples/Elastic.CommonSchema.Serilog.Sink.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,22 @@


// -- Setup Serilog --
var nodes = new[] { new Uri("http://localhost:9200") };
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Elasticsearch(nodes, opts =>
{
opts.BootstrapMethod = BootstrapMethod.Failure;
opts.DataStream = new DataStreamName("logs", "console-example");
opts.ConfigureChannel = channelOpts => {
channelOpts.BufferOptions = new BufferOptions { ExportMaxConcurrency = 10 };
};
}, transport =>
{
//transport.Authentication();
})
// This is the bit that Elastic.CommonSchema.Serilog.Sink introduces
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(client.Transport)
{
Expand Down
8 changes: 6 additions & 2 deletions src/Elastic.CommonSchema.Serilog.Sink/ElasticsearchSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,25 @@ public ElasticsearchSinkOptions(HttpTransport transport) : base(transport) { }
public class ElasticsearchSinkOptions<TEcsDocument> where TEcsDocument : EcsDocument, new()
{
/// <inheritdoc cref="ElasticsearchSinkOptions"/>
public ElasticsearchSinkOptions() : this(TransportHelper.Default()) { }
public ElasticsearchSinkOptions() : this(new DefaultHttpTransport(TransportHelper.Default())) { }

/// <inheritdoc cref="ElasticsearchSinkOptions"/>
public ElasticsearchSinkOptions(HttpTransport transport) => Transport = transport;

/// <inheritdoc cref="HttpTransport{TConfiguration}"/>
public HttpTransport Transport { get; }
internal HttpTransport Transport { get; }

/// <inheritdoc cref="EcsTextFormatterConfiguration{TEcsDocument}"/>
public EcsTextFormatterConfiguration<TEcsDocument> TextFormatting { get; set; } = new();

/// <inheritdoc cref="DataStreamName"/>
public DataStreamName DataStream { get; set; } = new("logs", "dotnet");

/// <summary>
/// Allows you to configure the <see cref="EcsDataStreamChannel{TEcsDocument}"/> used by the sink to send data to Elasticsearch
/// </summary>
public Action<DataStreamChannelOptions<TEcsDocument>>? ConfigureChannel { get; set; }

/// <inheritdoc cref="BootstrapMethod"/>
public BootstrapMethod BootstrapMethod { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Elastic.Transport;
using Serilog;
using Serilog.Configuration;

Expand Down Expand Up @@ -35,10 +36,13 @@ public static LoggerConfiguration Elasticsearch(
this LoggerSinkConfiguration loggerConfiguration,
ICollection<Uri> nodes,
Action<ElasticsearchSinkOptions>? configureOptions = null,
Action<TransportConfiguration>? configureTransport = null,
bool useSniffing = true
)
{
var sinkOptions = new ElasticsearchSinkOptions(useSniffing ? TransportHelper.Static(nodes) : TransportHelper.Sniffing(nodes));
var transportConfig = useSniffing ? TransportHelper.Static(nodes) : TransportHelper.Sniffing(nodes);
configureTransport?.Invoke(transportConfig);
var sinkOptions = new ElasticsearchSinkOptions(new DefaultHttpTransport(transportConfig));
configureOptions?.Invoke(sinkOptions);

return loggerConfiguration.Sink(new ElasticsearchSink(sinkOptions));
Expand All @@ -54,10 +58,13 @@ public static LoggerConfiguration Elasticsearch<TEcsDocument>(
this LoggerSinkConfiguration loggerConfiguration,
ICollection<Uri> nodes,
Action<ElasticsearchSinkOptions<TEcsDocument>>? configureOptions = null,
Action<TransportConfiguration>? configureTransport = null,
bool useSniffing = true
) where TEcsDocument : EcsDocument, new()
{
var sinkOptions = new ElasticsearchSinkOptions<TEcsDocument>(useSniffing ? TransportHelper.Static(nodes) : TransportHelper.Sniffing(nodes));
var transportConfig = useSniffing ? TransportHelper.Static(nodes) : TransportHelper.Sniffing(nodes);
configureTransport?.Invoke(transportConfig);
var sinkOptions = new ElasticsearchSinkOptions<TEcsDocument>(new DefaultHttpTransport(transportConfig));
configureOptions?.Invoke(sinkOptions);

return loggerConfiguration.Sink(new ElasticsearchSink<TEcsDocument>(sinkOptions));
Expand All @@ -73,10 +80,13 @@ public static LoggerConfiguration ElasticCloud(
this LoggerSinkConfiguration loggerConfiguration,
string cloudId,
string apiKey,
Action<ElasticsearchSinkOptions>? configureOptions = null
Action<ElasticsearchSinkOptions>? configureOptions = null,
Action<TransportConfiguration>? configureTransport = null
)
{
var sinkOptions = new ElasticsearchSinkOptions(TransportHelper.Cloud(cloudId, apiKey));
var transportConfig = TransportHelper.Cloud(cloudId, apiKey);
configureTransport?.Invoke(transportConfig);
var sinkOptions = new ElasticsearchSinkOptions(new DefaultHttpTransport(transportConfig));
configureOptions?.Invoke(sinkOptions);

return loggerConfiguration.Sink(new ElasticsearchSink(sinkOptions));
Expand All @@ -93,10 +103,13 @@ public static LoggerConfiguration ElasticCloud<TEcsDocument>(
this LoggerSinkConfiguration loggerConfiguration,
string cloudId,
string apiKey,
Action<ElasticsearchSinkOptions<TEcsDocument>>? configureOptions = null
Action<ElasticsearchSinkOptions<TEcsDocument>>? configureOptions = null,
Action<TransportConfiguration>? configureTransport = null
) where TEcsDocument : EcsDocument, new()
{
var sinkOptions = new ElasticsearchSinkOptions<TEcsDocument>(TransportHelper.Cloud(cloudId, apiKey));
var transportConfig = TransportHelper.Cloud(cloudId, apiKey);
configureTransport?.Invoke(transportConfig);
var sinkOptions = new ElasticsearchSinkOptions<TEcsDocument>(new DefaultHttpTransport(transportConfig));
configureOptions?.Invoke(sinkOptions);

return loggerConfiguration.Sink(new ElasticsearchSink<TEcsDocument>(sinkOptions));
Expand All @@ -113,10 +126,13 @@ public static LoggerConfiguration ElasticCloud(
string cloudId,
string username,
string password,
Action<ElasticsearchSinkOptions>? configureOptions = null
Action<ElasticsearchSinkOptions>? configureOptions = null,
Action<TransportConfiguration>? configureTransport = null
)
{
var sinkOptions = new ElasticsearchSinkOptions(TransportHelper.Cloud(cloudId, username, password));
var transportConfig = TransportHelper.Cloud(cloudId, username, password);
configureTransport?.Invoke(transportConfig);
var sinkOptions = new ElasticsearchSinkOptions(new DefaultHttpTransport(transportConfig));
configureOptions?.Invoke(sinkOptions);

return loggerConfiguration.Sink(new ElasticsearchSink(sinkOptions));
Expand All @@ -134,10 +150,13 @@ public static LoggerConfiguration ElasticCloud<TEcsDocument>(
string cloudId,
string username,
string password,
Action<ElasticsearchSinkOptions<TEcsDocument>>? configureOptions = null
Action<ElasticsearchSinkOptions<TEcsDocument>>? configureOptions = null,
Action<TransportConfiguration>? configureTransport = null
) where TEcsDocument : EcsDocument, new()
{
var sinkOptions = new ElasticsearchSinkOptions<TEcsDocument>(TransportHelper.Cloud(cloudId, username, password));
var transportConfig = TransportHelper.Cloud(cloudId, username, password);
configureTransport?.Invoke(transportConfig);
var sinkOptions = new ElasticsearchSinkOptions<TEcsDocument>(new DefaultHttpTransport(transportConfig));
configureOptions?.Invoke(sinkOptions);

return loggerConfiguration.Sink(new ElasticsearchSink<TEcsDocument>(sinkOptions));
Expand Down
28 changes: 14 additions & 14 deletions src/Elastic.CommonSchema.Serilog.Sink/TransportHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,39 @@ namespace Elastic.CommonSchema.Serilog.Sink
{
internal static class TransportHelper
{
private static ProductRegistration DefaultProduct = new ElasticsearchProductRegistration();
public static HttpTransport Default() =>
new DefaultHttpTransport(new TransportConfiguration(new Uri("http://localhost:9200"), DefaultProduct));
private static readonly ProductRegistration DefaultProduct = new ElasticsearchProductRegistration();

public static HttpTransport Static(IEnumerable<string> nodes) => Static(nodes.Select(n => new Uri(n)));
public static TransportConfiguration Default() =>
new TransportConfiguration(new Uri("http://localhost:9200"), DefaultProduct);

public static HttpTransport Static(IEnumerable<Uri> nodes)
public static TransportConfiguration Static(IEnumerable<string> nodes) => Static(nodes.Select(n => new Uri(n)));

public static TransportConfiguration Static(IEnumerable<Uri> nodes)
{
var pool = new StaticNodePool(nodes.Select(e => new Node(e)));
return new DefaultHttpTransport(new TransportConfiguration(pool, productRegistration: DefaultProduct));
return new TransportConfiguration(pool, productRegistration: DefaultProduct);
}

public static HttpTransport Sniffing(IEnumerable<string> nodes) => Sniffing(nodes.Select(n => new Uri(n)));
public static TransportConfiguration Sniffing(IEnumerable<string> nodes) => Sniffing(nodes.Select(n => new Uri(n)));

public static HttpTransport Sniffing(IEnumerable<Uri> nodes)
public static TransportConfiguration Sniffing(IEnumerable<Uri> nodes)
{
var pool = new SniffingNodePool(nodes.Select(e => new Node(e)));
return new DefaultHttpTransport(new TransportConfiguration(pool, productRegistration: DefaultProduct));
return new TransportConfiguration(pool, productRegistration: DefaultProduct);
}

public static HttpTransport Cloud(string cloudId, string apiKey)
public static TransportConfiguration Cloud(string cloudId, string apiKey)
{
var header = new ApiKey(apiKey);
var pool = new CloudNodePool(cloudId, header);
return new DefaultHttpTransport(new TransportConfiguration(pool, productRegistration: DefaultProduct));
return new TransportConfiguration(pool, productRegistration: DefaultProduct);
}

public static HttpTransport Cloud(string cloudId, string username, string password)
public static TransportConfiguration Cloud(string cloudId, string username, string password)
{
var header = new BasicAuthentication(username, password);
var pool = new CloudNodePool(cloudId, header);
return new DefaultHttpTransport(new TransportConfiguration(pool, productRegistration: DefaultProduct));
return new TransportConfiguration(pool, productRegistration: DefaultProduct);
}

}
}

0 comments on commit af53e3c

Please sign in to comment.