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

Make Serilog.Sinks.Elasticsearch version 9.0.0 work with Elasticsearch version <7, 7 and 8 by default, without need to specify TypeName #460

@nenadvicentic

Description

@nenadvicentic

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 _type for v8 (or higher).

We could do this by:

  1. Not setting any default ElasticsearchSinkOptions.TypeName during the Sink initialization in the constructors (so the value keep being null).
  2. Modifying DiscoverClusterVersion method like shown bellow. Method could be triggered by exposing "detectElasticsearchVersion": true configuration option, since currently DetectElasticsearchVersion is not used at all (DiscoverClusterVersion() method never goes through). Alternatively, more intuitive option name could be used, like automaticallyResolveTypeName.
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."

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions