Skip to content

Commit efe6b75

Browse files
authored
Refactor OpenTelemetryData to Action<Activity> (#143)
Closes #142
1 parent 86a1ab5 commit efe6b75

File tree

8 files changed

+54
-100
lines changed

8 files changed

+54
-100
lines changed

src/Elastic.Transport.VirtualizedCluster/Components/VirtualizedCluster.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal VirtualizedCluster(TransportConfigurationDescriptor settings)
3232
_syncCall = (t, r) => t.Request<VirtualResponse>(
3333
path: RootPath,
3434
postData: PostData.Serializable(new { }),
35-
openTelemetryData: default,
35+
null,
3636
localConfiguration: r?.Invoke(new RequestConfigurationDescriptor())
3737
);
3838
_asyncCall = async (t, r) =>
@@ -41,7 +41,7 @@ internal VirtualizedCluster(TransportConfigurationDescriptor settings)
4141
(
4242
path: RootPath,
4343
postData: PostData.Serializable(new { }),
44-
openTelemetryData: default,
44+
null,
4545
localConfiguration: r?.Invoke(new RequestConfigurationDescriptor()),
4646
CancellationToken.None
4747
).ConfigureAwait(false);

src/Elastic.Transport/Diagnostics/OpenTelemetry/OpenTelemetry.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
using System;
66
using System.Diagnostics;
77

8+
#pragma warning disable IDE0130 // Namespace does not match folder structure
89
namespace Elastic.Transport.Diagnostics;
10+
#pragma warning restore IDE0130 // Namespace does not match folder structure
911

1012
/// <summary>
1113
/// Activity information for OpenTelemetry instrumentation.
@@ -37,7 +39,7 @@ public static class OpenTelemetry
3739
internal static bool CurrentSpanIsElasticTransportOwnedHasListenersAndAllDataRequested => ElasticTransportActivitySource.HasListeners() &&
3840
((Activity.Current?.Source.Name.Equals(ElasticTransportActivitySourceName, StringComparison.Ordinal) ?? false) && (Activity.Current?.IsAllDataRequested ?? false));
3941

40-
internal static void SetCommonAttributes(Activity? activity, OpenTelemetryData openTelemetryData, ITransportConfiguration settings)
42+
internal static void SetCommonAttributes(Activity? activity, ITransportConfiguration settings)
4143
{
4244
if (activity is null)
4345
return;
@@ -51,20 +53,7 @@ internal static void SetCommonAttributes(Activity? activity, OpenTelemetryData o
5153
}
5254

5355
var productSchemaVersion = string.Empty;
54-
if (openTelemetryData.SpanAttributes is not null)
55-
{
56-
foreach (var attribute in openTelemetryData.SpanAttributes)
57-
{
58-
activity?.SetTag(attribute.Key, attribute.Value);
59-
60-
if (attribute.Key.Equals(OpenTelemetryAttributes.DbElasticsearchSchemaUrl, StringComparison.Ordinal))
61-
{
62-
if (attribute.Value is string schemaVersion)
63-
productSchemaVersion = schemaVersion;
64-
}
65-
}
66-
}
67-
56+
6857
// We add the client schema version only when it differs from the product schema version
6958
if (!productSchemaVersion.Equals(OpenTelemetrySchemaVersion, StringComparison.Ordinal))
7059
activity?.SetTag(OpenTelemetryAttributes.ElasticTransportSchemaVersion, OpenTelemetrySchemaVersion);

src/Elastic.Transport/Diagnostics/OpenTelemetry/OpenTelemetryData.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/Elastic.Transport/DistributedTransport.cs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@
2020
namespace Elastic.Transport;
2121

2222
/// <inheritdoc cref="ITransport{TConfiguration}" />
23-
public sealed class DistributedTransport : DistributedTransport<ITransportConfiguration>
23+
/// <summary>
24+
/// Transport coordinates the client requests over the node pool nodes and is in charge of falling over on
25+
/// different nodes
26+
/// </summary>
27+
/// <param name="configuration">The configuration to use for this transport</param>
28+
public sealed class DistributedTransport(ITransportConfiguration configuration) : DistributedTransport<ITransportConfiguration>(configuration)
2429
{
25-
/// <summary>
26-
/// Transport coordinates the client requests over the node pool nodes and is in charge of falling over on
27-
/// different nodes
28-
/// </summary>
29-
/// <param name="configuration">The configuration to use for this transport</param>
30-
public DistributedTransport(ITransportConfiguration configuration)
31-
: base(configuration) { }
3230
}
3331

3432
/// <inheritdoc cref="ITransport{TConfiguration}" />
@@ -65,36 +63,36 @@ public DistributedTransport(TConfiguration configuration)
6563
public TResponse Request<TResponse>(
6664
in EndpointPath path,
6765
PostData? data,
68-
in OpenTelemetryData openTelemetryData,
66+
Action<Activity>? configureActivity,
6967
IRequestConfiguration? localConfiguration
7068
) where TResponse : TransportResponse, new() =>
71-
RequestCoreAsync<TResponse>(isAsync: false, path, data, openTelemetryData, localConfiguration)
69+
RequestCoreAsync<TResponse>(isAsync: false, path, data, configureActivity, localConfiguration)
7270
.EnsureCompleted();
7371

7472
/// <inheritdoc cref="ITransport.RequestAsync{TResponse}"/>
7573
public Task<TResponse> RequestAsync<TResponse>(
7674
in EndpointPath path,
7775
PostData? data,
78-
in OpenTelemetryData openTelemetryData,
76+
Action<Activity>? configureActivity,
7977
IRequestConfiguration? localConfiguration,
8078
CancellationToken cancellationToken = default
8179
) where TResponse : TransportResponse, new() =>
82-
RequestCoreAsync<TResponse>(isAsync: true, path, data, openTelemetryData, localConfiguration, cancellationToken)
80+
RequestCoreAsync<TResponse>(isAsync: true, path, data, configureActivity, localConfiguration, cancellationToken)
8381
.AsTask();
8482

8583
private async ValueTask<TResponse> RequestCoreAsync<TResponse>(
8684
bool isAsync,
8785
EndpointPath path,
8886
PostData? data,
89-
OpenTelemetryData openTelemetryData,
87+
Action<Activity>? configureActivity,
9088
IRequestConfiguration? localConfiguration,
9189
CancellationToken cancellationToken = default
9290
) where TResponse : TransportResponse, new()
9391
{
9492
Activity activity = null;
9593

9694
if (OpenTelemetry.ElasticTransportActivitySource.HasListeners())
97-
activity = OpenTelemetry.ElasticTransportActivitySource.StartActivity(openTelemetryData.SpanName ?? path.Method.GetStringValue(),
95+
activity = OpenTelemetry.ElasticTransportActivitySource.StartActivity(path.Method.GetStringValue(),
9896
ActivityKind.Client);
9997

10098
try
@@ -127,7 +125,7 @@ private async ValueTask<TResponse> RequestCoreAsync<TResponse>(
127125
if (activity is { IsAllDataRequested: true })
128126
{
129127
if (activity.IsAllDataRequested)
130-
OpenTelemetry.SetCommonAttributes(activity, openTelemetryData, Configuration);
128+
OpenTelemetry.SetCommonAttributes(activity, Configuration);
131129

132130
if (Configuration.Authentication is BasicAuthentication basicAuthentication)
133131
activity.SetTag(SemanticConventions.DbUser, basicAuthentication.Username);
@@ -136,11 +134,6 @@ private async ValueTask<TResponse> RequestCoreAsync<TResponse>(
136134
activity.SetTag(OpenTelemetryAttributes.ElasticTransportProductVersion, Configuration.ProductRegistration.ProductAssemblyVersion);
137135
activity.SetTag(OpenTelemetryAttributes.ElasticTransportVersion, ReflectionVersionInfo.TransportVersion);
138136
activity.SetTag(SemanticConventions.UserAgentOriginal, Configuration.UserAgent.ToString());
139-
140-
if (openTelemetryData.SpanAttributes is not null)
141-
foreach (var attribute in openTelemetryData.SpanAttributes)
142-
activity.SetTag(attribute.Key, attribute.Value);
143-
144137
activity.SetTag(SemanticConventions.HttpRequestMethod, endpoint.Method.GetStringValue());
145138
}
146139

@@ -268,6 +261,9 @@ private async ValueTask<TResponse> RequestCoreAsync<TResponse>(
268261
activity?.SetTag(SemanticConventions.HttpResponseStatusCode, response.ApiCallDetails.HttpStatusCode);
269262
activity?.SetTag(OpenTelemetryAttributes.ElasticTransportAttemptedNodes, attemptedNodes);
270263

264+
if (configureActivity is not null && activity is not null)
265+
configureActivity.Invoke(activity);
266+
271267
return FinalizeResponse(endpoint, boundConfiguration, data, pipeline, startedOn, auditor, seenExceptions, response);
272268
}
273269
finally

src/Elastic.Transport/ITransport.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5+
using System.Diagnostics;
6+
using System;
57
using System.Threading;
68
using System.Threading.Tasks;
7-
using Elastic.Transport.Diagnostics;
89

910
namespace Elastic.Transport;
1011

@@ -20,15 +21,15 @@ public interface ITransport
2021
/// <typeparam name="TResponse">The type to deserialize the response body into.</typeparam>
2122
/// <param name="path">The path of the request.</param>
2223
/// <param name="postData">The data to be included as the body of the HTTP request.</param>
23-
/// <param name="openTelemetryData">Data to be used to control the OpenTelemetry instrumentation.</param>
24+
/// <param name="configureActivity">An optional <see cref="Action"/> used to configure the <see cref="Activity"/>.</param>
2425
/// <param name="localConfiguration">Per request configuration</param>
2526
/// Allows callers to override completely how `TResponse` should be deserialized to a `TResponse` that implements <see cref="TransportResponse"/> instance.
2627
/// <para>Expert setting only</para>
2728
/// <returns>The deserialized <typeparamref name="TResponse"/>.</returns>
2829
public TResponse Request<TResponse>(
2930
in EndpointPath path,
3031
PostData? postData,
31-
in OpenTelemetryData openTelemetryData,
32+
Action<Activity>? configureActivity,
3233
IRequestConfiguration? localConfiguration
3334
)
3435
where TResponse : TransportResponse, new();
@@ -40,15 +41,15 @@ public TResponse Request<TResponse>(
4041
/// <param name="path">The path of the request.</param>
4142
/// <param name="postData">The data to be included as the body of the HTTP request.</param>
4243
/// <param name="cancellationToken">The cancellation token to use.</param>
43-
/// <param name="openTelemetryData">Data to be used to control the OpenTelemetry instrumentation.</param>
44+
/// <param name="configureActivity">An optional <see cref="Action"/> used to configure the <see cref="Activity"/>.</param>
4445
/// <param name="localConfiguration">Per request configuration</param>
4546
/// Allows callers to override completely how `TResponse` should be deserialized to a `TResponse` that implements <see cref="TransportResponse"/> instance.
4647
/// <para>Expert setting only</para>
4748
/// <returns>The deserialized <typeparamref name="TResponse"/>.</returns>
4849
public Task<TResponse> RequestAsync<TResponse>(
4950
in EndpointPath path,
5051
PostData? postData,
51-
in OpenTelemetryData openTelemetryData,
52+
Action<Activity>? configureActivity,
5253
IRequestConfiguration? localConfiguration,
5354
CancellationToken cancellationToken = default
5455
)

0 commit comments

Comments
 (0)