Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
added TemplateCustomSettings as Dictionary<string,string> (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
tresoldigiorgio authored and mivano committed Jan 22, 2020
1 parent 91748ad commit a9359d1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public class ElasticsearchSinkOptions
/// </summary>
public Func<object> GetTemplateContent { get; set; }

/// <summary>
/// When using the <see cref="AutoRegisterTemplate"/> feature, this allows you to override the default template content.
/// If not provided, a default template that is optimized to deal with Serilog events is used.
/// </summary>
public Dictionary<string,string> TemplateCustomSettings { get; set; }

/// <summary>
/// When using the <see cref="AutoRegisterTemplate"/> feature, this allows you to overwrite the template in Elasticsearch if it already exists.
/// Defaults to: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void RegisterTemplateIfNeeded()
{
var templateExistsResponse = _client.Indices.TemplateExistsForAll<VoidResponse>(_templateName, new IndexTemplateExistsRequestParameters()
{
RequestConfiguration = new RequestConfiguration() { AllowedStatusCodes = new [] {200, 404} }
RequestConfiguration = new RequestConfiguration() { AllowedStatusCodes = new[] { 200, 404 } }
});
if (templateExistsResponse.HttpStatusCode == 200)
{
Expand All @@ -173,7 +173,7 @@ public void RegisterTemplateIfNeeded()
var result = _client.Indices.PutTemplateForAll<StringResponse>(_templateName, GetTemplatePostData(),
new PutIndexTemplateRequestParameters
{
IncludeTypeName = IncludeTypeName ? true : (bool?) null
IncludeTypeName = IncludeTypeName ? true : (bool?)null
});

if (!result.Success)
Expand Down Expand Up @@ -222,15 +222,15 @@ private object GetTemplateData()
if (_options.GetTemplateContent != null)
return _options.GetTemplateContent();

var settings = new Dictionary<string, string>
{
{"index.refresh_interval", "5s"}
};
var settings = _options.TemplateCustomSettings ?? new Dictionary<string, string>();

if (!settings.ContainsKey("index.refresh_interval"))
settings.Add("index.refresh_interval", "5s");

if (_options.NumberOfShards.HasValue)
if (_options.NumberOfShards.HasValue && !settings.ContainsKey("number_of_shards"))
settings.Add("number_of_shards", _options.NumberOfShards.Value.ToString());

if (_options.NumberOfReplicas.HasValue)
if (_options.NumberOfReplicas.HasValue && !settings.ContainsKey("number_of_replicas"))
settings.Add("number_of_replicas", _options.NumberOfReplicas.Value.ToString());

return ElasticsearchTemplateProvider.GetTemplate(
Expand All @@ -245,14 +245,14 @@ private object GetTemplateData()
public void DiscoverClusterVersion()
{
if (!_options.DetectElasticsearchVersion) return;

var response = _client.Cat.Nodes<StringResponse>(new CatNodesRequestParameters()
{
Headers = new [] { "v"}
{
Headers = new[] { "v" }
});
if (!response.Success) return;

_discoveredVersion = response.Body.Split(new[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries)
_discoveredVersion = response.Body.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
.FirstOrDefault();

if (_discoveredVersion?.StartsWith("7.") ?? false)
Expand Down

0 comments on commit a9359d1

Please sign in to comment.