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

Commit a537c60

Browse files
nenadvicenticNenad Vicentic
and
Nenad Vicentic
authored
Read Elasticsearch server version from a root page response (#502)
Co-authored-by: Nenad Vicentic <[email protected]>
1 parent 578d5cb commit a537c60

File tree

4 files changed

+36
-21
lines changed

4 files changed

+36
-21
lines changed

src/Serilog.Sinks.Elasticsearch/Sinks/ElasticSearch/ElasticsearchVersionManager.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,18 @@ public Version EffectiveVersion
5656
{
5757
try
5858
{
59-
var response = _client.Cat.Nodes<StringResponse>(new CatNodesRequestParameters()
60-
{
61-
Headers = new[] { "v" }
62-
});
59+
var response = _client.DoRequest<DynamicResponse>(HttpMethod.GET, "/");
6360
if (!response.Success) return null;
6461

65-
var discoveredVersion = response.Body.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
66-
.FirstOrDefault();
62+
var discoveredVersion = response.Dictionary["version"]["number"];
6763

68-
if (discoveredVersion == null)
64+
if (!discoveredVersion.HasValue)
6965
return null;
7066

71-
return new Version(discoveredVersion);
67+
if (discoveredVersion.Value is not string strVersion)
68+
return null;
69+
70+
return new Version(strVersion);
7271

7372
}
7473
catch (Exception ex)

test/Serilog.Sinks.Elasticsearch.Tests/ElasticsearchSinkTests.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Elasticsearch.Net;
2+
using Serilog.Sinks.Elasticsearch.Tests.Stubs;
23
using System.Text;
34
using Xunit;
45

@@ -18,7 +19,7 @@ public void Ctor_DetectElasticsearchVersionSetToTrue_SetsTypeName(string elastic
1819
/* ARRANGE */
1920
var options = new ElasticsearchSinkOptions
2021
{
21-
Connection = FakeResponse(elasticVersion),
22+
Connection = FakeProductCheckResponse(elasticVersion),
2223
TypeName = configuredTypeName
2324
};
2425

@@ -41,7 +42,7 @@ public void Ctor_DetectElasticsearchVersionSetToFalseAssumesVersion7_SetsTypeNam
4142
/* ARRANGE */
4243
var options = new ElasticsearchSinkOptions
4344
{
44-
Connection = FakeResponse(elasticVersion),
45+
Connection = FakeProductCheckResponse(elasticVersion),
4546
DetectElasticsearchVersion = false,
4647
TypeName = configuredTypeName
4748
};
@@ -65,7 +66,7 @@ public void CreateLogger_DetectElasticsearchVersionSetToTrue_SetsTypeName(string
6566
/* ARRANGE */
6667
var options = new ElasticsearchSinkOptions
6768
{
68-
Connection = FakeResponse(elasticVersion),
69+
Connection = FakeProductCheckResponse(elasticVersion),
6970
DetectElasticsearchVersion = true,
7071
TypeName = configuredTypeName
7172
};
@@ -83,10 +84,10 @@ public void CreateLogger_DetectElasticsearchVersionSetToTrue_SetsTypeName(string
8384
Assert.Equal(expectedTypeName, options.TypeName);
8485
}
8586

86-
private static IConnection FakeResponse(string responseText)
87+
private static IConnection FakeProductCheckResponse(string responseText)
8788
{
88-
byte[] responseBody = Encoding.UTF8.GetBytes(responseText);
89-
return new InMemoryConnection(responseBody, contentType: "text/plain; charset=UTF-8");
89+
var productCheckResponse = ConnectionStub.ModifiedProductCheckResponse(responseText);
90+
return new InMemoryConnection(productCheckResponse);
9091
}
9192
}
9293
}

test/Serilog.Sinks.Elasticsearch.Tests/Stubs/ConnectionStub.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public override TReturn Request<TReturn>(RequestData requestData)
5353
$" to be productCheck pre-flight request");
5454

5555
_productCheckDone = true;
56-
return ReturnConnectionStatus<TReturn>(requestData); // root page returned
56+
return ReturnConnectionStatus<TReturn>(requestData); // hard-coded root page returned
5757
}
5858

5959
byte[] responseBytes = Array.Empty<byte>();
@@ -66,6 +66,7 @@ public override TReturn Request<TReturn>(RequestData requestData)
6666

6767
int responseStatusCode = 200;
6868
string contentType = null;
69+
InMemoryHttpResponse productCheckResponse = null;
6970

7071
switch (requestData.Method)
7172
{
@@ -79,13 +80,10 @@ public override TReturn Request<TReturn>(RequestData requestData)
7980
switch (requestData.Uri.PathAndQuery.ToLower())
8081
{
8182
case "/":
82-
// ReturnConnectionStatus(...) call at the bottom will return dummy product page
83-
// when root "/" is requested.
83+
productCheckResponse = ModifiedProductCheckResponse(_productVersion);
8484
break;
85-
case "/_cat/nodes":
8685
case "/_cat/nodes?h=v":
8786
responseBytes = Encoding.UTF8.GetBytes(_productVersion);
88-
responseStatusCode = 200;
8987
contentType = "text/plain; charset=UTF-8";
9088
break;
9189
}
@@ -100,12 +98,29 @@ public override TReturn Request<TReturn>(RequestData requestData)
10098
break;
10199
}
102100

103-
return ReturnConnectionStatus<TReturn>(requestData, responseBytes, responseStatusCode, contentType);
101+
return ReturnConnectionStatus<TReturn>(requestData, productCheckResponse, responseBytes, responseStatusCode, contentType);
104102
}
105103

106104
public override Task<TResponse> RequestAsync<TResponse>(RequestData requestData, CancellationToken cancellationToken)
107105
{
108106
return Task.FromResult(Request<TResponse>(requestData));
109107
}
108+
109+
public static InMemoryHttpResponse ModifiedProductCheckResponse(string productVersion)
110+
{
111+
var productCheckResponse = ValidProductCheckResponse();
112+
if (productVersion is not null)
113+
{
114+
using var originalMemoryStream = new MemoryStream(productCheckResponse.ResponseBytes, false);
115+
{
116+
var json = LowLevelRequestResponseSerializer.Instance.Deserialize<dynamic>(originalMemoryStream);
117+
json["version"]["number"] = productVersion;
118+
using var modifiedMemoryStream = new MemoryStream();
119+
LowLevelRequestResponseSerializer.Instance.Serialize(json, modifiedMemoryStream);
120+
productCheckResponse.ResponseBytes = modifiedMemoryStream.ToArray();
121+
}
122+
}
123+
return productCheckResponse;
124+
}
110125
}
111126
}

test/Serilog.Sinks.Elasticsearch.Tests/Templating/DiscoverVersionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public DiscoverVersionTests()
3434
public void TemplatePutToCorrectUrl()
3535
{
3636
var uri = _templateGet.Item1;
37-
uri.AbsolutePath.Should().Be("/_cat/nodes");
37+
uri.AbsolutePath.Should().Be("/");
3838
}
3939
}
4040
}

0 commit comments

Comments
 (0)