Skip to content

Commit 89f73c8

Browse files
[ElasticSearch] Add .NET TFMs
- Add TFMs for .NET 8-10. - Fix new analyzer warnings/suggestions. Resolves open-telemetry#3707.
1 parent 5c61ada commit 89f73c8

4 files changed

Lines changed: 45 additions & 33 deletions

File tree

‎src/OpenTelemetry.Instrumentation.ElasticsearchClient/CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
* Add `net8.0`, `net9.0` and `net10.0` target frameworks.
6+
([#TODO](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/TODO))
7+
58
## 1.14.0-beta.1
69

710
Released 2025-Nov-13

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,22 @@
1616

1717
namespace OpenTelemetry.Instrumentation.ElasticsearchClient.Implementation;
1818

19-
internal class ElasticsearchRequestPipelineDiagnosticListener : ListenerHandler
19+
internal partial class ElasticsearchRequestPipelineDiagnosticListener : ListenerHandler
2020
{
2121
internal const string DatabaseSystemName = "elasticsearch";
2222
internal const string ExceptionCustomPropertyName = "OTel.Elasticsearch.Exception";
2323
internal const string AttributeDbMethod = "db.method";
2424

2525
internal static readonly Assembly Assembly = typeof(ElasticsearchRequestPipelineDiagnosticListener).Assembly;
2626
internal static readonly AssemblyName AssemblyName = Assembly.GetName();
27-
internal static readonly string ActivitySourceName = AssemblyName.Name;
27+
internal static readonly string ActivitySourceName = AssemblyName.Name!;
2828
internal static readonly ActivitySource ActivitySource = new(ActivitySourceName, Assembly.GetPackageVersion());
2929

30-
private static readonly Regex ParseRequest = new(@"\n# Request:\r?\n(\{.*)\n# Response", RegexOptions.Compiled | RegexOptions.Singleline);
30+
private const string RequestRegexPattern = @"\n# Request:\r?\n(\{.*)\n# Response";
31+
32+
#if !NET8_0_OR_GREATER
33+
private static readonly Regex ParseRequest = new(RequestRegexPattern, RegexOptions.Compiled | RegexOptions.Singleline);
34+
#endif
3135
private static readonly ConcurrentDictionary<object, string> MethodNameCache = new();
3236

3337
private readonly ElasticsearchClientInstrumentationOptions options;
@@ -88,35 +92,54 @@ private static string GetDisplayName(Activity activity, object? method, string?
8892
}
8993

9094
// operations starting with _ are not indices (_cat, _search, etc.)
95+
#if NET8_0_OR_GREATER
96+
if (uri.Segments[1].StartsWith('_'))
97+
#else
9198
if (uri.Segments[1].StartsWith("_", StringComparison.Ordinal))
99+
#endif
92100
{
93101
return null;
94102
}
95103

96104
var elasticType = Uri.UnescapeDataString(uri.Segments[1]);
97105

98106
// multiple indices used, return null to avoid high cardinality
107+
#if NET8_0_OR_GREATER
108+
if (elasticType.Contains(',', StringComparison.Ordinal))
109+
#else
99110
if (elasticType.Contains(','))
111+
#endif
100112
{
101113
return null;
102114
}
103115

116+
#if NET8_0_OR_GREATER
117+
if (elasticType.EndsWith('/'))
118+
#else
104119
if (elasticType.EndsWith("/", StringComparison.Ordinal))
120+
#endif
105121
{
106122
elasticType = elasticType.Substring(0, elasticType.Length - 1);
107123
}
108124

109125
return elasticType;
110126
}
111127

128+
#if NET8_0_OR_GREATER
129+
[GeneratedRegex(RequestRegexPattern, RegexOptions.Compiled | RegexOptions.Singleline)]
130+
private static partial Regex RequestParser();
131+
#else
132+
private static Regex RequestParser() => ParseRequest;
133+
#endif
134+
112135
private string ParseAndFormatRequest(string debugInformation)
113136
{
114137
if (!this.options.ParseAndFormatRequest)
115138
{
116139
return debugInformation;
117140
}
118141

119-
var request = ParseRequest.Match(debugInformation);
142+
var request = RequestParser().Match(debugInformation);
120143
if (request.Success)
121144
{
122145
var body = request.Groups[1]?.Value?.Trim();

‎src/OpenTelemetry.Instrumentation.ElasticsearchClient/OpenTelemetry.Instrumentation.ElasticsearchClient.csproj‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<!-- OmniSharp/VS Code requires TargetFrameworks to be in descending order for IntelliSense and analysis. -->
5-
<TargetFrameworks>$(NetStandardMinimumSupportedVersion)</TargetFrameworks>
4+
<TargetFrameworks>$(TargetFrameworksForLibraries)</TargetFrameworks>
65
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);$(NetFrameworkMinimumSupportedVersion)</TargetFrameworks>
76
<Description>Elasticsearch instrumentation for OpenTelemetry .NET.</Description>
87
<PackageTags>$(PackageTags);distributed-tracing</PackageTags>

‎src/Shared/MultiTypePropertyFetcher.cs‎

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,16 @@ public MultiTypePropertyFetcher(string propertyName)
3939
var type = obj.GetType().GetTypeInfo();
4040
if (!this.innerFetcher.TryGetValue(type, out var fetcher))
4141
{
42-
var property = type.DeclaredProperties.FirstOrDefault(p => string.Equals(p.Name, this.propertyName, StringComparison.OrdinalIgnoreCase));
43-
if (property == null)
44-
{
45-
property = type.GetProperty(this.propertyName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
46-
}
42+
var property =
43+
type.DeclaredProperties.FirstOrDefault(p => string.Equals(p.Name, this.propertyName, StringComparison.OrdinalIgnoreCase)) ??
44+
type.GetProperty(this.propertyName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)!;
4745

4846
fetcher = PropertyFetch.FetcherForProperty(property);
4947

5048
this.innerFetcher.TryAdd(type, fetcher);
5149
}
5250

53-
if (fetcher == null)
54-
{
55-
return default;
56-
}
57-
58-
return fetcher.Fetch(obj);
51+
return fetcher == null ? default : fetcher.Fetch(obj);
5952
}
6053

6154
// see https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticSourceEventSource.cs
@@ -75,14 +68,11 @@ public static PropertyFetch FetcherForProperty(PropertyInfo propertyInfo)
7568

7669
var typedPropertyFetcher = typeof(TypedPropertyFetch<,>);
7770
var instantiatedTypedPropertyFetcher = typedPropertyFetcher.MakeGenericType(
78-
typeof(T), propertyInfo.DeclaringType, propertyInfo.PropertyType);
79-
return (PropertyFetch)Activator.CreateInstance(instantiatedTypedPropertyFetcher, propertyInfo);
71+
typeof(T), propertyInfo.DeclaringType!, propertyInfo.PropertyType);
72+
return (PropertyFetch)Activator.CreateInstance(instantiatedTypedPropertyFetcher, propertyInfo)!;
8073
}
8174

82-
public virtual T? Fetch(object obj)
83-
{
84-
return default;
85-
}
75+
public virtual T? Fetch(object obj) => default;
8676

8777
#pragma warning disable CA1812
8878
private sealed class TypedPropertyFetch<TDeclaredObject, TDeclaredProperty> : PropertyFetch
@@ -93,18 +83,15 @@ private sealed class TypedPropertyFetch<TDeclaredObject, TDeclaredProperty> : Pr
9383

9484
public TypedPropertyFetch(PropertyInfo property)
9585
{
96-
this.propertyFetch = (Func<TDeclaredObject, TDeclaredProperty>)property.GetMethod.CreateDelegate(typeof(Func<TDeclaredObject, TDeclaredProperty>));
86+
this.propertyFetch =
87+
#if NET8_0_OR_GREATER
88+
property.GetMethod!.CreateDelegate<Func<TDeclaredObject, TDeclaredProperty>>();
89+
#else
90+
(Func<TDeclaredObject, TDeclaredProperty>)property.GetMethod.CreateDelegate(typeof(Func<TDeclaredObject, TDeclaredProperty>));
91+
#endif
9792
}
9893

99-
public override T? Fetch(object obj)
100-
{
101-
if (obj is TDeclaredObject o)
102-
{
103-
return this.propertyFetch(o);
104-
}
105-
106-
return default;
107-
}
94+
public override T? Fetch(object obj) => obj is TDeclaredObject o ? this.propertyFetch(o) : (T?)default;
10895
}
10996
}
11097
}

0 commit comments

Comments
 (0)