Skip to content

Commit d7a8fc2

Browse files
authored
[repo/ElasticsearchClient] Prepare to .NET9 (open-telemetry#2292)
1 parent a1edda6 commit d7a8fc2

File tree

5 files changed

+29
-36
lines changed

5 files changed

+29
-36
lines changed

src/OpenTelemetry.Instrumentation.ElasticsearchClient/Implementation/ElasticsearchRequestPipelineDiagnosticListener.cs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public override void OnEventWritten(string name, object? payload)
5757
case "CallElasticsearch.Stop":
5858
this.OnStopActivity(activity, payload);
5959
break;
60+
default:
61+
break;
6062
}
6163
}
6264

@@ -69,12 +71,7 @@ private static string GetDisplayName(Activity activity, object? method, string?
6971
case "CallElasticsearch" when method != null:
7072
{
7173
var methodName = MethodNameCache.GetOrAdd(method, $"Elasticsearch {method}");
72-
if (elasticType == null)
73-
{
74-
return methodName;
75-
}
76-
77-
return $"{methodName} {elasticType}";
74+
return elasticType == null ? methodName : $"{methodName} {elasticType}";
7875
}
7976

8077
default:
@@ -122,7 +119,7 @@ private string ParseAndFormatRequest(string debugInformation)
122119
var request = ParseRequest.Match(debugInformation);
123120
if (request.Success)
124121
{
125-
string? body = request.Groups[1]?.Value?.Trim();
122+
var body = request.Groups[1]?.Value?.Trim();
126123
if (body == null)
127124
{
128125
return debugInformation;
@@ -193,7 +190,7 @@ private void OnStartActivity(Activity activity, object? payload)
193190
}
194191

195192
var uriHostNameType = Uri.CheckHostName(uri.Host);
196-
if (uriHostNameType == UriHostNameType.IPv4 || uriHostNameType == UriHostNameType.IPv6)
193+
if (uriHostNameType is UriHostNameType.IPv4 or UriHostNameType.IPv6)
197194
{
198195
activity.SetTag(SemanticConventions.AttributeNetPeerIp, uri.Host);
199196
}
@@ -269,14 +266,10 @@ private void OnStopActivity(Activity activity, object? payload)
269266

270267
if (originalException is HttpRequestException)
271268
{
272-
if (originalException.InnerException is SocketException exception)
269+
if (originalException.InnerException is SocketException { SocketErrorCode: SocketError.HostNotFound })
273270
{
274-
switch (exception.SocketErrorCode)
275-
{
276-
case SocketError.HostNotFound:
277-
activity.SetStatus(Status.Error.WithDescription(originalException.Message));
278-
return;
279-
}
271+
activity.SetStatus(Status.Error.WithDescription(originalException.Message));
272+
return;
280273
}
281274

282275
if (originalException.InnerException != null)

test/OpenTelemetry.Instrumentation.ElasticsearchClient.Tests/Customer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace OpenTelemetry.Instrumentation.ElasticsearchClient.Tests;
55

6-
public class Customer
6+
internal class Customer
77
{
88
public string? Id { get; set; }
99

test/OpenTelemetry.Instrumentation.ElasticsearchClient.Tests/DependencyInjectionConfigTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class DependencyInjectionConfigTests
1515
[InlineData("CustomName")]
1616
public async Task TestTracingOptionsDiConfig(string? name)
1717
{
18-
bool optionsPickedFromDi = false;
18+
var optionsPickedFromDi = false;
1919

2020
var services = new ServiceCollection();
2121

test/OpenTelemetry.Instrumentation.ElasticsearchClient.Tests/ElasticsearchClientTests.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public async Task CanCaptureSearchCall()
181181
[Fact]
182182
public async Task CanRecordAndSampleSearchCall()
183183
{
184-
bool samplerCalled = false;
184+
var samplerCalled = false;
185185

186186
var sampler = new TestSampler
187187
{
@@ -193,10 +193,10 @@ public async Task CanRecordAndSampleSearchCall()
193193
},
194194
};
195195

196-
using TestActivityProcessor testActivityProcessor = new TestActivityProcessor();
196+
using var testActivityProcessor = new TestActivityProcessor();
197197

198-
int startCalled = 0;
199-
int endCalled = 0;
198+
var startCalled = 0;
199+
var endCalled = 0;
200200

201201
testActivityProcessor.StartAction =
202202
(a) =>
@@ -239,9 +239,9 @@ public async Task CanRecordAndSampleSearchCall()
239239
}
240240

241241
[Fact]
242-
public async Task CanSupressDownstreamActivities()
242+
public async Task CanSuppressDownstreamActivities()
243243
{
244-
bool samplerCalled = false;
244+
var samplerCalled = false;
245245

246246
var sampler = new TestSampler
247247
{
@@ -253,10 +253,10 @@ public async Task CanSupressDownstreamActivities()
253253
},
254254
};
255255

256-
using TestActivityProcessor testActivityProcessor = new TestActivityProcessor();
256+
using var testActivityProcessor = new TestActivityProcessor();
257257

258-
int startCalled = 0;
259-
int endCalled = 0;
258+
var startCalled = 0;
259+
var endCalled = 0;
260260

261261
testActivityProcessor.StartAction =
262262
(a) =>
@@ -301,7 +301,7 @@ public async Task CanSupressDownstreamActivities()
301301
[Fact]
302302
public async Task CanDropSearchCall()
303303
{
304-
bool samplerCalled = false;
304+
var samplerCalled = false;
305305

306306
var sampler = new TestSampler
307307
{
@@ -313,10 +313,10 @@ public async Task CanDropSearchCall()
313313
},
314314
};
315315

316-
using TestActivityProcessor testActivityProcessor = new TestActivityProcessor();
316+
using var testActivityProcessor = new TestActivityProcessor();
317317

318-
int startCalled = 0;
319-
int endCalled = 0;
318+
var startCalled = 0;
319+
var endCalled = 0;
320320

321321
testActivityProcessor.StartAction =
322322
(a) =>
@@ -725,8 +725,8 @@ public async Task DoesNotCaptureWhenInstrumentationIsSuppressed()
725725
public async Task CapturesBasedOnSamplingDecision(SamplingDecision samplingDecision, bool isActivityExpected)
726726
{
727727
var expectedResource = ResourceBuilder.CreateDefault().AddService("test-service");
728-
bool startActivityCalled = false;
729-
bool endActivityCalled = false;
728+
var startActivityCalled = false;
729+
var endActivityCalled = false;
730730
var processor = new TestActivityProcessor(
731731
activity => startActivityCalled = true,
732732
activity => endActivityCalled = true);
@@ -851,7 +851,7 @@ public async Task ShouldRemoveSensitiveInformation()
851851
Assert.Single(exportedItems);
852852
var searchActivity = exportedItems[0];
853853

854-
string? dbUrl = (string?)searchActivity.GetTagValue(SemanticConventions.AttributeUrlFull);
854+
var dbUrl = (string?)searchActivity.GetTagValue(SemanticConventions.AttributeUrlFull);
855855

856856
Assert.DoesNotContain("sensitive", dbUrl);
857857
Assert.Contains("REDACTED:REDACTED", dbUrl);

test/OpenTelemetry.Instrumentation.ElasticsearchClient.Tests/InMemoryConnectionWithDownstreamActivity.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
namespace OpenTelemetry.Instrumentation.ElasticsearchClient.Tests;
88

9-
public class InMemoryConnectionWithDownstreamActivity : InMemoryConnection
9+
internal class InMemoryConnectionWithDownstreamActivity : InMemoryConnection
1010
{
11-
internal static readonly ActivitySource ActivitySource = new ActivitySource("Downstream");
12-
internal static readonly ActivitySource NestedActivitySource = new ActivitySource("NestedDownstream");
11+
internal static readonly ActivitySource ActivitySource = new("Downstream");
12+
internal static readonly ActivitySource NestedActivitySource = new("NestedDownstream");
1313

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

0 commit comments

Comments
 (0)