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

Automatically handle TypeName parameter for different versions of Elasticsearch (<7, 7 or higher) #462

Merged
merged 28 commits into from
Jan 23, 2023
Merged
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
71f5031
fix file references in the Visual Studio Solution file.
Aug 16, 2022
705980d
Merge branch 'serilog-contrib:dev' into dev
nenadvicentic Jan 13, 2023
8ae37b9
Do not set `TypeName` by default any more.
Aug 17, 2022
7eae031
Automatically handle `ElasticsearchSinkOptions.TypeName` for differen…
Aug 17, 2022
e52c880
Add unit test for automatic settings of `TypeName` when `DetectElasti…
Aug 19, 2022
7be19ed
Add Elasticsearch v8 template + parsing of Elasticsearch major versio…
Dec 18, 2022
234c1fd
Upgrade to .NET 6.0 and update test-frameworks related NuGet pacakges
Dec 23, 2022
0568290
Merge branch 'dev' of https://github.com/nenadvicentic/serilog-sinks-…
Jan 16, 2023
77450bc
Upgrade to Elasticsearch.NET 7.17.5 + handle new "preflight" request
Jan 17, 2023
fe02aaa
Make `ConnectionStub` a bit more robust .
Jan 18, 2023
f6fcc06
Use `System.Version` to parse Elasticsearch server version number (si…
Jan 18, 2023
dfc8b05
Update NuGet packages
Jan 18, 2023
44b404d
Replace obsolete NuGet package `Serilog.Sinks.ColoredConsole` with `S…
Jan 18, 2023
28c1a88
Update `Serilog.Sinks.PeriodicBatching` package and reimplent `Elasti…
Jan 18, 2023
3741af9
Better handling of Elasticsearch server version number in mocked `Con…
Jan 18, 2023
9dd2fbd
Cleanup: refactor to use single `JsonEquals` method.
Jan 19, 2023
db20a77
Turn on `DetectElasticSearchVersion` option by default. Assume versio…
Jan 19, 2023
1d31d96
Cleanup: remove unused namespaces
Jan 20, 2023
9da3ae6
Cleanup: move `ElasticsearchSinkTestsBase` into `Stubs` subfolder.
Jan 20, 2023
c0a5d30
Refactor: extract `ConnectionStub` into a separate file.
Jan 20, 2023
bad5136
Fix: json comparison in .NET Framework 4.6+
Jan 20, 2023
0937748
Run unit-tests on multiple .NET frameworks.
Jan 20, 2023
a148a8e
Cleanup: remove unused NUnit runner package.
Jan 20, 2023
ee5e25a
Use newer, built-in compilation constants.
Jan 23, 2023
65f62d6
Use standard MSBuild property `IsPackable` for clarity.
Jan 23, 2023
1ae15d1
Cleanup: remove unused package refrence.
Jan 23, 2023
1bd39da
Update GitHub actions
Jan 23, 2023
778eefd
docs: updated documentation to reflect changes in behavior of the sink.
Jan 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make ConnectionStub a bit more robust .
  • Loading branch information
Nenad Vicentic committed Jan 18, 2023
commit fe02aaa90e1700c85b8ca0f3fcb21356dcd39ea2
Original file line number Diff line number Diff line change
@@ -119,12 +119,21 @@ protected string[] AssertSeenHttpPosts(List<string> _seenHttpPosts, int lastN, i

public class ConnectionStub : InMemoryConnection
{
private Func<int> _templateExistReturnCode;
private readonly Func<int> _templateExistReturnCode;
private readonly List<int> _seenHttpHeads;
private readonly List<Tuple<Uri, int>> _seenHttpGets;
private readonly List<string> _seenHttpPosts;
private readonly List<Tuple<Uri, string>> _seenHttpPuts;

private readonly string _productVersion;
private List<int> _seenHttpHeads;
private List<Tuple<Uri, int>> _seenHttpGets;
private List<string> _seenHttpPosts;
private List<Tuple<Uri, string>> _seenHttpPuts;

/// <summary>
/// Elasticsearch.NET client version 7.16 or higher
/// uses pre-flight request, before any other request is served,
/// to check product (Elasticsearch) and version of the product.
/// It can be seen on <see cref="IConnectionPool.ProductCheckStatus"/> property.
/// </summary>
private bool _productCheckDone;

public ConnectionStub(
List<string> _seenHttpPosts,
@@ -145,41 +154,59 @@ public ConnectionStub(

public override TReturn Request<TReturn>(RequestData requestData)
{
var ms = new MemoryStream();
if (_productCheckDone == false)
{
if (requestData.Method != HttpMethod.GET || requestData.PathAndQuery != string.Empty)
throw new InvalidOperationException(
$"{nameof(ConnectionStub)} expects first request" +
$" to be productCheck pre-flight request");

_productCheckDone = true;
return ReturnConnectionStatus<TReturn>(requestData); // root page returned
}

byte[] responseBytes = Array.Empty<byte>();
if (requestData.PostData != null)
{
using var ms = new MemoryStream();
requestData.PostData.Write(ms, new ConnectionConfiguration());
responseBytes = ms.ToArray();
}

var responseStream = new MemoryStream();
int responseStatusCode = 200;

switch (requestData.Method)
{
case HttpMethod.PUT:
_seenHttpPuts.Add(Tuple.Create(requestData.Uri, Encoding.UTF8.GetString(ms.ToArray())));
_seenHttpPuts.Add(Tuple.Create(requestData.Uri, Encoding.UTF8.GetString(responseBytes)));
break;
case HttpMethod.POST:
_seenHttpPosts.Add(Encoding.UTF8.GetString(ms.ToArray()));
_seenHttpPosts.Add(Encoding.UTF8.GetString(responseBytes));
break;
case HttpMethod.GET:
switch (requestData.Uri.PathAndQuery.ToLower())
{
case "/":
// handle pre-flight call to Elasticsearch, added in Elasticsearch.NET 7.16 version
return ReturnConnectionStatus<TReturn>(requestData);
// ReturnConnectionStatus(...) call at the bottom will return dummy product page
// when root "/" is requested.
break;
case "/_cat/nodes":
responseStream.Write(Encoding.UTF8.GetBytes(_productVersion));
responseStream.Position = 0;
responseBytes = Encoding.UTF8.GetBytes(_productVersion);
responseStatusCode = 200;
break;
}
_seenHttpGets.Add(Tuple.Create(requestData.Uri, responseStatusCode));
break;
case HttpMethod.HEAD:
responseStatusCode = _templateExistReturnCode();
if (requestData.Uri.PathAndQuery.ToLower().StartsWith("/_template/"))
{
responseStatusCode = _templateExistReturnCode();
}
_seenHttpHeads.Add(responseStatusCode);
break;
}

return ResponseBuilder.ToResponse<TReturn>(requestData, null, responseStatusCode, Enumerable.Empty<string>(), responseStream, "text/plain");
return ReturnConnectionStatus<TReturn>(requestData, responseBytes, responseStatusCode);
}

public override Task<TResponse> RequestAsync<TResponse>(RequestData requestData, CancellationToken cancellationToken)