Skip to content

Commit

Permalink
[Instrumentation.Elasticsearch] Nullable (#1881)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kielek committed Jun 14, 2024
1 parent 47247c4 commit 119ff0c
Show file tree
Hide file tree
Showing 18 changed files with 59 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#nullable enable
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions
OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions.ElasticsearchClientInstrumentationOptions() -> void
OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions.Enrich.get -> System.Action<System.Diagnostics.Activity, string, object>
OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions.Enrich.get -> System.Action<System.Diagnostics.Activity!, string!, object?>?
OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions.Enrich.set -> void
OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions.MaxDbStatementLength.get -> int
OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions.MaxDbStatementLength.set -> void
Expand All @@ -11,6 +11,6 @@ OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumenta
OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions.SuppressDownstreamInstrumentation.get -> bool
OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions.SuppressDownstreamInstrumentation.set -> void
OpenTelemetry.Trace.TracerProviderBuilderExtensions
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddElasticsearchClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions> configure) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddElasticsearchClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddElasticsearchClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, string name, System.Action<OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions> configure) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddElasticsearchClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder! builder, System.Action<OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions!>? configure) -> OpenTelemetry.Trace.TracerProviderBuilder!
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddElasticsearchClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder! builder) -> OpenTelemetry.Trace.TracerProviderBuilder!
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddElasticsearchClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder! builder, string? name, System.Action<OpenTelemetry.Instrumentation.ElasticsearchClient.ElasticsearchClientInstrumentationOptions!>? configure) -> OpenTelemetry.Trace.TracerProviderBuilder!
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ public class ElasticsearchClientInstrumentationOptions
/// <para>object: the raw object from which additional information can be extracted to enrich the activity.
/// The type of this object depends on the event, which is given by the above parameter.</para>
/// </remarks>
public Action<Activity, string, object> Enrich { get; set; }
public Action<Activity, string, object?>? Enrich { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@ internal class ElasticsearchRequestPipelineDiagnosticListener : ListenerHandler
internal static readonly string ActivitySourceName = AssemblyName.Name;
internal static readonly ActivitySource ActivitySource = new(ActivitySourceName, Assembly.GetPackageVersion());

private static readonly Regex ParseRequest = new Regex(@"\n# Request:\r?\n(\{.*)\n# Response", RegexOptions.Compiled | RegexOptions.Singleline);
private static readonly ConcurrentDictionary<object, string> MethodNameCache = new ConcurrentDictionary<object, string>();
private static readonly Regex ParseRequest = new(@"\n# Request:\r?\n(\{.*)\n# Response", RegexOptions.Compiled | RegexOptions.Singleline);
private static readonly ConcurrentDictionary<object, string> MethodNameCache = new();

private readonly ElasticsearchClientInstrumentationOptions options;
private readonly MultiTypePropertyFetcher<Uri> uriFetcher = new MultiTypePropertyFetcher<Uri>("Uri");
private readonly MultiTypePropertyFetcher<object> methodFetcher = new MultiTypePropertyFetcher<object>("Method");
private readonly MultiTypePropertyFetcher<string> debugInformationFetcher = new MultiTypePropertyFetcher<string>("DebugInformation");
private readonly MultiTypePropertyFetcher<int?> httpStatusFetcher = new MultiTypePropertyFetcher<int?>("HttpStatusCode");
private readonly MultiTypePropertyFetcher<Exception> originalExceptionFetcher = new MultiTypePropertyFetcher<Exception>("OriginalException");
private readonly MultiTypePropertyFetcher<object> failureReasonFetcher = new MultiTypePropertyFetcher<object>("FailureReason");
private readonly MultiTypePropertyFetcher<byte[]> responseBodyFetcher = new MultiTypePropertyFetcher<byte[]>("ResponseBodyInBytes");
private readonly MultiTypePropertyFetcher<Uri> uriFetcher = new("Uri");
private readonly MultiTypePropertyFetcher<object> methodFetcher = new("Method");
private readonly MultiTypePropertyFetcher<string> debugInformationFetcher = new("DebugInformation");
private readonly MultiTypePropertyFetcher<int?> httpStatusFetcher = new("HttpStatusCode");
private readonly MultiTypePropertyFetcher<Exception> originalExceptionFetcher = new("OriginalException");
private readonly MultiTypePropertyFetcher<object> failureReasonFetcher = new("FailureReason");
private readonly MultiTypePropertyFetcher<byte[]> responseBodyFetcher = new("ResponseBodyInBytes");

public ElasticsearchRequestPipelineDiagnosticListener(ElasticsearchClientInstrumentationOptions options)
: base("Elasticsearch.Net.RequestPipeline")
{
this.options = options;
}

public override void OnEventWritten(string name, object payload)
public override void OnEventWritten(string name, object? payload)
{
var activity = Activity.Current;
Guard.ThrowIfNull(activity);
Expand All @@ -61,7 +61,7 @@ public override void OnEventWritten(string name, object payload)
}
}

private static string GetDisplayName(Activity activity, object method, string elasticType = null)
private static string GetDisplayName(Activity activity, object? method, string? elasticType = null)
{
switch (activity.OperationName)
{
Expand All @@ -83,15 +83,15 @@ private static string GetDisplayName(Activity activity, object method, string el
}
}

private static string GetElasticIndex(Uri uri)
private static string? GetElasticIndex(Uri uri)
{
// first segment is always /
if (uri.Segments.Length < 2)
{
return null;
}

// operations starting with _ are not indices (_cat, _search, etc)
// operations starting with _ are not indices (_cat, _search, etc.)
if (uri.Segments[1].StartsWith("_", StringComparison.Ordinal))
{
return null;
Expand Down Expand Up @@ -123,7 +123,7 @@ private string ParseAndFormatRequest(string debugInformation)
var request = ParseRequest.Match(debugInformation);
if (request.Success)
{
string body = request.Groups[1]?.Value?.Trim();
string? body = request.Groups[1]?.Value?.Trim();
if (body == null)
{
return debugInformation;
Expand All @@ -150,7 +150,7 @@ private string ParseAndFormatRequest(string debugInformation)
return debugInformation;
}

private void OnStartActivity(Activity activity, object payload)
private void OnStartActivity(Activity activity, object? payload)
{
// By this time, samplers have already run and
// activity.IsAllDataRequested populated accordingly.
Expand Down Expand Up @@ -226,7 +226,7 @@ private void OnStartActivity(Activity activity, object payload)
}
}

private void OnStopActivity(Activity activity, object payload)
private void OnStopActivity(Activity activity, object? payload)
{
if (activity.IsAllDataRequested)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<Description>Elasticsearch instrumentation for OpenTelemetry .NET</Description>
<PackageTags>$(PackageTags);distributed-tracing</PackageTags>
<MinVerTagPrefix>Instrumentation.ElasticsearchClient-</MinVerTagPrefix>
<Nullable>disable</Nullable>
</PropertyGroup>

<!--Do not run Package Baseline Validation as this package has never released a stable version.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static TracerProviderBuilder AddElasticsearchClientInstrumentation(
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddElasticsearchClientInstrumentation(
this TracerProviderBuilder builder,
Action<ElasticsearchClientInstrumentationOptions> configure) =>
Action<ElasticsearchClientInstrumentationOptions>? configure) =>
AddElasticsearchClientInstrumentation(builder, name: null, configure);

/// <summary>
Expand All @@ -44,8 +44,8 @@ public static TracerProviderBuilder AddElasticsearchClientInstrumentation(
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddElasticsearchClientInstrumentation(
this TracerProviderBuilder builder,
string name,
Action<ElasticsearchClientInstrumentationOptions> configure)
string? name,
Action<ElasticsearchClientInstrumentationOptions>? configure)
{
Guard.ThrowIfNull(builder);

Expand Down
10 changes: 5 additions & 5 deletions src/Shared/ActivityInstrumentationHelper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#nullable disable
#nullable enable

#pragma warning disable IDE0005 // Using directive is unnecessary.
using System;
Expand All @@ -17,13 +17,13 @@ internal static class ActivityInstrumentationHelper

private static Action<Activity, ActivitySource> CreateActivitySourceSetter()
{
return (Action<Activity, ActivitySource>)typeof(Activity).GetProperty("Source")
.SetMethod.CreateDelegate(typeof(Action<Activity, ActivitySource>));
return (Action<Activity, ActivitySource>)typeof(Activity).GetProperty("Source")!
.SetMethod!.CreateDelegate(typeof(Action<Activity, ActivitySource>));
}

private static Action<Activity, ActivityKind> CreateActivityKindSetter()
{
return (Action<Activity, ActivityKind>)typeof(Activity).GetProperty("Kind")
.SetMethod.CreateDelegate(typeof(Action<Activity, ActivityKind>));
return (Action<Activity, ActivityKind>)typeof(Activity).GetProperty("Kind")!
.SetMethod!.CreateDelegate(typeof(Action<Activity, ActivityKind>));
}
}
2 changes: 1 addition & 1 deletion src/Shared/ListenerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal abstract class ListenerHandler
/// Initializes a new instance of the <see cref="ListenerHandler"/> class.
/// </summary>
/// <param name="sourceName">The name of the <see cref="ListenerHandler"/>.</param>
public ListenerHandler(string sourceName)
protected ListenerHandler(string sourceName)
{
this.SourceName = sourceName;
}
Expand Down
17 changes: 6 additions & 11 deletions src/Shared/MultiTypePropertyFetcher.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#nullable disable
#nullable enable

#pragma warning disable IDE0005 // Using directive is unnecessary.
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Reflection;
#pragma warning restore IDE0005 // Using directive is unnecessary.

namespace OpenTelemetry.Instrumentation;

/// <summary>
/// PropertyFetcher fetches a property from an object.
/// </summary>
/// <typeparam name="T">The type of the property being fetched.</typeparam>
#pragma warning disable CA1812
internal sealed class MultiTypePropertyFetcher<T>
#pragma warning restore CA1812
{
private readonly string propertyName;
private readonly ConcurrentDictionary<Type, PropertyFetch> innerFetcher = new ConcurrentDictionary<Type, PropertyFetch>();
private readonly ConcurrentDictionary<Type, PropertyFetch> innerFetcher = new();

/// <summary>
/// Initializes a new instance of the <see cref="MultiTypePropertyFetcher{T}"/> class.
Expand All @@ -37,16 +33,15 @@ public MultiTypePropertyFetcher(string propertyName)
/// </summary>
/// <param name="obj">Object to be fetched.</param>
/// <returns>Property fetched.</returns>
public T Fetch(object obj)
public T? Fetch(object? obj)
{
if (obj == null)
{
return default;
}

var type = obj.GetType().GetTypeInfo();
PropertyFetch fetcher = null;
if (!this.innerFetcher.TryGetValue(type, out fetcher))
if (!this.innerFetcher.TryGetValue(type, out var fetcher))
{
var property = type.DeclaredProperties.FirstOrDefault(p => string.Equals(p.Name, this.propertyName, StringComparison.OrdinalIgnoreCase));
if (property == null)
Expand Down Expand Up @@ -88,7 +83,7 @@ public static PropertyFetch FetcherForProperty(PropertyInfo propertyInfo)
return (PropertyFetch)Activator.CreateInstance(instantiatedTypedPropertyFetcher, propertyInfo);
}

public virtual T Fetch(object obj)
public virtual T? Fetch(object obj)
{
return default;
}
Expand All @@ -105,7 +100,7 @@ public TypedPropertyFetch(PropertyInfo property)
this.propertyFetch = (Func<TDeclaredObject, TDeclaredProperty>)property.GetMethod.CreateDelegate(typeof(Func<TDeclaredObject, TDeclaredProperty>));
}

public override T Fetch(object obj)
public override T? Fetch(object obj)
{
if (obj is TDeclaredObject o)
{
Expand Down
2 changes: 0 additions & 2 deletions src/Shared/PropertyFetcher.AOT.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#nullable disable

// NOTE: This version of PropertyFetcher is AOT-compatible.
// Usages of the non-AOT-compatible version can be moved over to this one when they need to support AOT/trimming.
// Copied from https://github.com/open-telemetry/opentelemetry-dotnet/blob/86a6ba0b7f7ed1f5e84e5a6610e640989cd3ae9f/src/Shared/DiagnosticSourceInstrumentation/PropertyFetcher.cs#L30
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#nullable disable

using Microsoft.AspNetCore.Mvc;

namespace RouteTests.Controllers;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#nullable disable

using Microsoft.AspNetCore.Mvc;

namespace RouteTests.Controllers;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#nullable disable

using Microsoft.AspNetCore.Mvc;

namespace RouteTests.Controllers;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#nullable disable

using Microsoft.AspNetCore.Mvc;

namespace RouteTests.Controllers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace OpenTelemetry.Instrumentation.ElasticsearchClient.Tests;

public class Customer
{
public string Id { get; set; }
public string? Id { get; set; }

public string Name { get; set; }
public string? Name { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class DependencyInjectionConfigTests
[Theory]
[InlineData(null)]
[InlineData("CustomName")]
public async Task TestTracingOptionsDiConfig(string name)
public async Task TestTracingOptionsDiConfig(string? name)
{
bool optionsPickedFromDi = false;

Expand Down
Loading

0 comments on commit 119ff0c

Please sign in to comment.