This repository was archived by the owner on Jun 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 193
Make Serilog.Sinks.Elasticsearch version 9.0.0 work with Elasticsearch version <7, 7 and 8 by default, without need to specify TypeName #460
Copy link
Copy link
Closed
Description
This is a suggestion for a new feature in version 9. Simplification of the way Serilog.Sinks.Elasticserach handles different version of Elasticsearch backend.
If TypeName parameter has not been explicitly specified in code or config, handle _type parameter sent to a different versions of Elasticsearch automatically! That means:
- Set
_type: "logevent"for <v7, - Set
_type: "_doc"for v7 - Omit
for v8 (or higher)._type
We could do this by:
- Not setting any default
ElasticsearchSinkOptions.TypeNameduring the Sink initialization in the constructors (so the value keep being null). - Modifying
DiscoverClusterVersionmethod like shown bellow. Method could be triggered by exposing"detectElasticsearchVersion": trueconfiguration option, since currentlyDetectElasticsearchVersionis not used at all (DiscoverClusterVersion()method never goes through). Alternatively, more intuitive option name could be used, likeautomaticallyResolveTypeName.
public void DiscoverClusterVersion()
{
if (!_options.DetectElasticsearchVersion) return;
if (_options.TypeName != null) return; // Do not do anything if user explicitly configured `TypeName`
try
{
var response = _client.Cat.Nodes<StringResponse>(new CatNodesRequestParameters()
{
Headers = new[] { "v" }
});
if (!response.Success) return;
_discoveredVersion = response.Body.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
.FirstOrDefault();
if (_discoveredVersion == null)
return;
if (int.TryParse(_discoveredVersion.Substring(0, _discoveredVersion.IndexOf('.')), out int majorVersion))
{
if (majorVersion < 7)
_options.TypeName = "logevent"; // maybe this should be `ElasticsearchSinkOptions.DefaultTypeName`
if (majorVersion == 7)
_options.TypeName = "_doc";
// Version 8 or higher leave `_options.TypeName` to be `null` (condition is already at the start of the method), so that `_type` is not sent to Elasticsearch at all.
}
}
catch (Exception ex)
{
SelfLog.WriteLine("Failed to discover the cluster version. {0}", ex);
}
}Perhaps ElasticsearchSinkOptions.DefaultTypeName should be reverted back to "logevent", from "_doc" and used, as described in the summary: "The default Elasticsearch type name used for Elasticsearch versions prior to 7."
Schoof-T, snebjorn and zorannik
Metadata
Metadata
Assignees
Labels
No labels