Skip to content

[Backport 8.6] Respond to Transport changes and support OpenTelemetry #6997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions benchmarks/Benchmarks/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
},
"Elastic.Transport": {
"type": "Transitive",
"resolved": "0.4.2",
"contentHash": "umDR9uAzO8KPgb96O6llqN7DIv7oqATB4MPr3UrDCJ6Su+l7KJhB7lwItf/cw727e1jKTG9QEqkW6tBGjYoe4w==",
"resolved": "0.4.3",
"contentHash": "U4yHxScZG7YMDJJ9KGGWfhNx543NQmWlNPILyrd5UF0VMFfjuTZ+RXtWRCX8xMrqcSl4pUBKE+G4GoIVA65ZUQ==",
"dependencies": {
"Microsoft.CSharp": "4.7.0",
"System.Buffers": "4.5.1",
Expand Down Expand Up @@ -1195,7 +1195,7 @@
"elastic.clients.elasticsearch": {
"type": "Project",
"dependencies": {
"Elastic.Transport": "[0.4.2, )"
"Elastic.Transport": "[0.4.3, )"
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions benchmarks/Profiling/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
},
"Elastic.Transport": {
"type": "Transitive",
"resolved": "0.4.2",
"contentHash": "umDR9uAzO8KPgb96O6llqN7DIv7oqATB4MPr3UrDCJ6Su+l7KJhB7lwItf/cw727e1jKTG9QEqkW6tBGjYoe4w==",
"resolved": "0.4.3",
"contentHash": "U4yHxScZG7YMDJJ9KGGWfhNx543NQmWlNPILyrd5UF0VMFfjuTZ+RXtWRCX8xMrqcSl4pUBKE+G4GoIVA65ZUQ==",
"dependencies": {
"Microsoft.CSharp": "4.7.0",
"System.Buffers": "4.5.1",
Expand Down Expand Up @@ -131,7 +131,7 @@
"elastic.clients.elasticsearch": {
"type": "Project",
"dependencies": {
"Elastic.Transport": "[0.4.2, )"
"Elastic.Transport": "[0.4.3, )"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
},
"Elastic.Transport": {
"type": "Transitive",
"resolved": "0.4.2",
"contentHash": "umDR9uAzO8KPgb96O6llqN7DIv7oqATB4MPr3UrDCJ6Su+l7KJhB7lwItf/cw727e1jKTG9QEqkW6tBGjYoe4w==",
"resolved": "0.4.3",
"contentHash": "U4yHxScZG7YMDJJ9KGGWfhNx543NQmWlNPILyrd5UF0VMFfjuTZ+RXtWRCX8xMrqcSl4pUBKE+G4GoIVA65ZUQ==",
"dependencies": {
"Microsoft.CSharp": "4.7.0",
"System.Buffers": "4.5.1",
Expand Down Expand Up @@ -143,7 +143,7 @@
"elastic.clients.elasticsearch": {
"type": "Project",
"dependencies": {
"Elastic.Transport": "[0.4.2, )"
"Elastic.Transport": "[0.4.3, )"
}
}
},
Expand Down Expand Up @@ -183,8 +183,8 @@
},
"Elastic.Transport": {
"type": "Transitive",
"resolved": "0.4.2",
"contentHash": "umDR9uAzO8KPgb96O6llqN7DIv7oqATB4MPr3UrDCJ6Su+l7KJhB7lwItf/cw727e1jKTG9QEqkW6tBGjYoe4w==",
"resolved": "0.4.3",
"contentHash": "U4yHxScZG7YMDJJ9KGGWfhNx543NQmWlNPILyrd5UF0VMFfjuTZ+RXtWRCX8xMrqcSl4pUBKE+G4GoIVA65ZUQ==",
"dependencies": {
"Microsoft.CSharp": "4.7.0",
"System.Buffers": "4.5.1",
Expand Down Expand Up @@ -332,7 +332,7 @@
"elastic.clients.elasticsearch": {
"type": "Project",
"dependencies": {
"Elastic.Transport": "[0.4.2, )"
"Elastic.Transport": "[0.4.3, )"
}
}
},
Expand Down Expand Up @@ -381,8 +381,8 @@
},
"Elastic.Transport": {
"type": "Transitive",
"resolved": "0.4.2",
"contentHash": "umDR9uAzO8KPgb96O6llqN7DIv7oqATB4MPr3UrDCJ6Su+l7KJhB7lwItf/cw727e1jKTG9QEqkW6tBGjYoe4w==",
"resolved": "0.4.3",
"contentHash": "U4yHxScZG7YMDJJ9KGGWfhNx543NQmWlNPILyrd5UF0VMFfjuTZ+RXtWRCX8xMrqcSl4pUBKE+G4GoIVA65ZUQ==",
"dependencies": {
"Microsoft.CSharp": "4.7.0",
"System.Buffers": "4.5.1",
Expand Down Expand Up @@ -529,7 +529,7 @@
"elastic.clients.elasticsearch": {
"type": "Project",
"dependencies": {
"Elastic.Transport": "[0.4.2, )"
"Elastic.Transport": "[0.4.3, )"
}
}
},
Expand Down Expand Up @@ -569,8 +569,8 @@
},
"Elastic.Transport": {
"type": "Transitive",
"resolved": "0.4.2",
"contentHash": "umDR9uAzO8KPgb96O6llqN7DIv7oqATB4MPr3UrDCJ6Su+l7KJhB7lwItf/cw727e1jKTG9QEqkW6tBGjYoe4w==",
"resolved": "0.4.3",
"contentHash": "U4yHxScZG7YMDJJ9KGGWfhNx543NQmWlNPILyrd5UF0VMFfjuTZ+RXtWRCX8xMrqcSl4pUBKE+G4GoIVA65ZUQ==",
"dependencies": {
"Microsoft.CSharp": "4.7.0",
"System.Buffers": "4.5.1",
Expand Down Expand Up @@ -709,7 +709,7 @@
"elastic.clients.elasticsearch": {
"type": "Project",
"dependencies": {
"Elastic.Transport": "[0.4.2, )"
"Elastic.Transport": "[0.4.3, )"
}
}
},
Expand Down Expand Up @@ -749,8 +749,8 @@
},
"Elastic.Transport": {
"type": "Transitive",
"resolved": "0.4.2",
"contentHash": "umDR9uAzO8KPgb96O6llqN7DIv7oqATB4MPr3UrDCJ6Su+l7KJhB7lwItf/cw727e1jKTG9QEqkW6tBGjYoe4w==",
"resolved": "0.4.3",
"contentHash": "U4yHxScZG7YMDJJ9KGGWfhNx543NQmWlNPILyrd5UF0VMFfjuTZ+RXtWRCX8xMrqcSl4pUBKE+G4GoIVA65ZUQ==",
"dependencies": {
"Microsoft.CSharp": "4.7.0",
"System.Buffers": "4.5.1",
Expand Down Expand Up @@ -849,7 +849,7 @@
"elastic.clients.elasticsearch": {
"type": "Project",
"dependencies": {
"Elastic.Transport": "[0.4.2, )"
"Elastic.Transport": "[0.4.3, )"
}
}
},
Expand Down Expand Up @@ -889,8 +889,8 @@
},
"Elastic.Transport": {
"type": "Transitive",
"resolved": "0.4.2",
"contentHash": "umDR9uAzO8KPgb96O6llqN7DIv7oqATB4MPr3UrDCJ6Su+l7KJhB7lwItf/cw727e1jKTG9QEqkW6tBGjYoe4w==",
"resolved": "0.4.3",
"contentHash": "U4yHxScZG7YMDJJ9KGGWfhNx543NQmWlNPILyrd5UF0VMFfjuTZ+RXtWRCX8xMrqcSl4pUBKE+G4GoIVA65ZUQ==",
"dependencies": {
"Microsoft.CSharp": "4.7.0",
"System.Buffers": "4.5.1",
Expand Down Expand Up @@ -989,7 +989,7 @@
"elastic.clients.elasticsearch": {
"type": "Project",
"dependencies": {
"Elastic.Transport": "[0.4.2, )"
"Elastic.Transport": "[0.4.3, )"
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Elastic.Clients.Elasticsearch/Api/SearchRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal override void BeforeRequest()
}
}

protected override string ResolveUrl(RouteValues routeValues, IElasticsearchClientSettings settings)
protected override (string ResolvedUrl, string UrlTemplate) ResolveUrl(RouteValues routeValues, IElasticsearchClientSettings settings)
{
if (Pit is not null && !string.IsNullOrEmpty(Pit.Id ?? string.Empty) && routeValues.ContainsKey("index"))
{
Expand Down Expand Up @@ -60,7 +60,7 @@ internal override void BeforeRequest()
}
}

protected override string ResolveUrl(RouteValues routeValues, IElasticsearchClientSettings settings)
protected override (string ResolvedUrl, string UrlTemplate) ResolveUrl(RouteValues routeValues, IElasticsearchClientSettings settings)
{
if ((Self.PitValue is not null || Self.PitDescriptor is not null || Self.PitDescriptorAction is not null) && routeValues.ContainsKey("index"))
{
Expand Down
104 changes: 82 additions & 22 deletions src/Elastic.Clients.Elasticsearch/Client/ElasticsearchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.Json;
Expand All @@ -21,6 +22,8 @@ public partial class ElasticsearchClient
{
private readonly HttpTransport<IElasticsearchClientSettings> _transport;

private readonly ActivitySource _activitySource = new("Elastic.Clients.Elasticsearch.ElasticsearchClient");

internal static ConditionalWeakTable<JsonSerializerOptions, IElasticsearchClientSettings> SettingsTable { get; } = new();

/// <summary>
Expand Down Expand Up @@ -127,8 +130,16 @@ internal TResponse DoRequest<TRequest, TResponse, TRequestParameters>(
}
}

var (url, postData) = PrepareRequest<TRequest, TRequestParameters>(request, forceConfiguration);
var response = _transport.Request<TResponse>(request.HttpMethod, url, postData, parameters);
var (resolvedUrl, urlTemplate, postData) = PrepareRequest<TRequest, TRequestParameters>(request, forceConfiguration);

TResponse response;

using (var activity = _activitySource.StartActivity($"Elasticsearch: {request.HttpMethod} {urlTemplate}", ActivityKind.Client))
{
activity?.AddTag("db.system", "elasticsearch");
response = _transport.Request<TResponse>(request.HttpMethod, resolvedUrl, postData, parameters);
}

PostRequestProductCheck<TRequest, TResponse>(request, response);

if (_productCheckStatus == ProductCheckStatus.Failed)
Expand Down Expand Up @@ -187,8 +198,21 @@ internal TResponse DoRequest<TRequest, TResponse, TRequestParameters>(
}
}

var (url, postData) = PrepareRequest<TRequest, TRequestParameters>(request, forceConfiguration);
var response = _transport.Request<TResponse>(request.HttpMethod, url, postData, request.RequestParameters);
var (resolvedUrl, urlTemplate, postData) = PrepareRequest<TRequest, TRequestParameters>(request, forceConfiguration);

TResponse response;

using (var activity = _activitySource.StartActivity($"Elasticsearch: {request.HttpMethod} {urlTemplate}", ActivityKind.Client))
{
activity?.AddTag("db.system", "elasticsearch");
activity?.SetCustomProperty("elastic.transport.client", true);

response = _transport.Request<TResponse>(request.HttpMethod, resolvedUrl, postData, request.RequestParameters);

if (response.ApiCallDetails.RequestBodyInBytes is not null)
activity?.AddTag("db.statement", System.Text.Encoding.UTF8.GetString(response.ApiCallDetails.RequestBodyInBytes));
}

PostRequestProductCheck<TRequest, TResponse>(request, response);

if (_productCheckStatus == ProductCheckStatus.Failed)
Expand Down Expand Up @@ -249,16 +273,28 @@ internal Task<TResponse> DoRequestAsync<TRequest, TResponse, TRequestParameters>
}
}

var (url, postData) = PrepareRequest<TRequest, TRequestParameters>(request, null);
var (resolvedUrl, urlTemplate, postData) = PrepareRequest<TRequest, TRequestParameters>(request, null);

if (_productCheckStatus == ProductCheckStatus.Succeeded && !requestModified)
return _transport.RequestAsync<TResponse>(request.HttpMethod, url, postData, parameters, cancellationToken);
if (_productCheckStatus == ProductCheckStatus.Succeeded && !requestModified && !_activitySource.HasListeners())
return _transport.RequestAsync<TResponse>(request.HttpMethod, resolvedUrl, postData, parameters, cancellationToken);

return SendRequest(request, parameters, url, postData, hadRequestConfig, originalHeaders);
return SendRequest(request, parameters, resolvedUrl, postData, hadRequestConfig, originalHeaders);

async Task<TResponse> SendRequest(TRequest request, RequestParameters? parameters, string url, PostData postData, bool hadRequestConfig, HeadersList? originalHeaders)
{
var response = await _transport.RequestAsync<TResponse>(request.HttpMethod, url, postData, parameters).ConfigureAwait(false);
TResponse response;

using (var activity = _activitySource.StartActivity($"Elasticsearch: {request.HttpMethod} {urlTemplate}", ActivityKind.Client))
{
activity?.AddTag("db.system", "elasticsearch");
activity?.SetCustomProperty("elastic.transport.client", true);

response = await _transport.RequestAsync<TResponse>(request.HttpMethod, url, postData, parameters).ConfigureAwait(false);

if (response.ApiCallDetails.RequestBodyInBytes is not null)
activity?.AddTag("db.statement", System.Text.Encoding.UTF8.GetString(response.ApiCallDetails.RequestBodyInBytes));
}

PostRequestProductCheck<TRequest, TResponse>(request, response);

if (_productCheckStatus == ProductCheckStatus.Failed)
Expand Down Expand Up @@ -319,16 +355,28 @@ internal Task<TResponse> DoRequestAsync<TRequest, TResponse, TRequestParameters>
}
}

var (url, postData) = PrepareRequest<TRequest, TRequestParameters>(request, null);
var (resolvedUrl, urlTemplate, postData) = PrepareRequest<TRequest, TRequestParameters>(request, null);

if (_productCheckStatus == ProductCheckStatus.Succeeded && !requestModified)
return _transport.RequestAsync<TResponse>(request.HttpMethod, url, postData, request.RequestParameters, cancellationToken);
if (_productCheckStatus == ProductCheckStatus.Succeeded && !requestModified && !_activitySource.HasListeners())
return _transport.RequestAsync<TResponse>(request.HttpMethod, resolvedUrl, postData, request.RequestParameters, cancellationToken);

return SendRequest(request, request.RequestParameters, url, postData, hadRequestConfig, originalHeaders);
return SendRequest(request, request.RequestParameters, resolvedUrl, postData, hadRequestConfig, originalHeaders);

async Task<TResponse> SendRequest(TRequest request, RequestParameters? parameters, string url, PostData postData, bool hadRequestConfig, HeadersList? originalHeaders)
{
var response = await _transport.RequestAsync<TResponse>(request.HttpMethod, url, postData, parameters).ConfigureAwait(false);
TResponse response;

using (var activity = _activitySource.StartActivity($"Elasticsearch: {request.HttpMethod} {urlTemplate}", ActivityKind.Client))
{
activity?.AddTag("db.system", "elasticsearch");
activity?.SetCustomProperty("elastic.transport.client", true);

response = await _transport.RequestAsync<TResponse>(request.HttpMethod, url, postData, parameters).ConfigureAwait(false);

if (response.ApiCallDetails.RequestBodyInBytes is not null)
activity?.AddTag("db.statement", System.Text.Encoding.UTF8.GetString(response.ApiCallDetails.RequestBodyInBytes));
}

PostRequestProductCheck<TRequest, TResponse>(request, response);

if (_productCheckStatus == ProductCheckStatus.Failed)
Expand Down Expand Up @@ -391,16 +439,28 @@ internal Task<TResponse> DoRequestAsync<TRequest, TResponse, TRequestParameters>
}
}

var (url, postData) = PrepareRequest<TRequest, TRequestParameters>(request, forceConfiguration);
var (resolvedUrl, urlTemplate, postData) = PrepareRequest<TRequest, TRequestParameters>(request, null);

if (_productCheckStatus == ProductCheckStatus.Succeeded && !requestModified)
return _transport.RequestAsync<TResponse>(request.HttpMethod, url, postData, parameters, cancellationToken);
if (_productCheckStatus == ProductCheckStatus.Succeeded && !requestModified && !_activitySource.HasListeners())
return _transport.RequestAsync<TResponse>(request.HttpMethod, resolvedUrl, postData, parameters, cancellationToken);

return SendRequest(request, parameters, url, postData, hadRequestConfig, originalHeaders);
return SendRequest(request, parameters, resolvedUrl, postData, hadRequestConfig, originalHeaders);

async Task<TResponse> SendRequest(TRequest request, RequestParameters? parameters, string url, PostData postData, bool hadRequestConfig, HeadersList? originalHeaders)
{
var response = await _transport.RequestAsync<TResponse>(request.HttpMethod, url, postData, parameters).ConfigureAwait(false);
TResponse response;

using (var activity = _activitySource.StartActivity($"Elasticsearch: {request.HttpMethod} {urlTemplate}", ActivityKind.Client))
{
activity?.AddTag("db.system", "elasticsearch");
activity?.SetCustomProperty("elastic.transport.client", true);

response = await _transport.RequestAsync<TResponse>(request.HttpMethod, url, postData, parameters).ConfigureAwait(false);

if (response.ApiCallDetails.RequestBodyInBytes is not null)
activity?.AddTag("db.statement", System.Text.Encoding.UTF8.GetString(response.ApiCallDetails.RequestBodyInBytes));
}

PostRequestProductCheck<TRequest, TResponse>(request, response);

if (_productCheckStatus == ProductCheckStatus.Failed)
Expand All @@ -422,7 +482,7 @@ async Task<TResponse> SendRequest(TRequest request, RequestParameters? parameter
}
}

private (string url, PostData data) PrepareRequest<TRequest, TRequestParameters>(TRequest request,
private (string resolvedUrl, string urlTemplate, PostData data) PrepareRequest<TRequest, TRequestParameters>(TRequest request,
Action<IRequestConfiguration>? forceConfiguration)
where TRequest : Request<TRequestParameters>
where TRequestParameters : RequestParameters, new()
Expand All @@ -438,15 +498,15 @@ async Task<TResponse> SendRequest(TRequest request, RequestParameters? parameter
if (request.Accept is not null)
ForceAccept<TRequest, TRequestParameters>(request, request.Accept);

var url = request.GetUrl(ElasticsearchClientSettings);
var (resolvedUrl, urlTemplate) = request.GetUrl(ElasticsearchClientSettings);

var postData =
request.HttpMethod == HttpMethod.GET ||
request.HttpMethod == HttpMethod.HEAD || !request.SupportsBody
? null
: PostData.Serializable(request);

return (url, postData);
return (resolvedUrl, urlTemplate, postData);
}

private void PostRequestProductCheck<TRequest, TResponse>(TRequest request, TResponse response)
Expand Down
8 changes: 4 additions & 4 deletions src/Elastic.Clients.Elasticsearch/Core/Request/ApiUrls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,24 @@ internal ApiUrls(string[] routes)
/// </summary>
public Dictionary<int, List<UrlLookup>> Routes { get; }

public string Resolve(RouteValues routeValues, IElasticsearchClientSettings settings)
public (string ResolvedUrl, string UrlTemplate) Resolve(RouteValues routeValues, IElasticsearchClientSettings settings)
{
if (_fixedUrl != null)
return _fixedUrl;
return (_fixedUrl, _fixedUrl);

var resolved = routeValues.Resolve(settings);

if (!Routes.TryGetValue(resolved.Count, out var routes))
throw new Exception($"No route taking {resolved.Count} parameters{_errorMessageSuffix}");

if (routes.Count == 1)
return routes[0].ToUrl(resolved);
return (routes[0].ToUrl(resolved), routes[0].Route);

//find the first url with N parts that has all provided named parts
foreach (var u in routes)
{
if (u.Matches(resolved))
return u.ToUrl(resolved);
return (u.ToUrl(resolved), u.Route);
}

throw new Exception($"No route taking {routeValues.Count} parameters{_errorMessageSuffix}");
Expand Down
Loading