Skip to content
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

[Instrumentation.MySqlData] Nullable #1094

Merged
merged 1 commit into from
Mar 20, 2023
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
10 changes: 6 additions & 4 deletions src/OpenTelemetry.Contrib.Shared/Api/StatusHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
// </copyright>

#nullable enable

using System;
using System.Runtime.CompilerServices;
using OpenTelemetry.Trace;
Expand All @@ -27,7 +29,7 @@ internal static class StatusHelper
public const string ErrorStatusCodeTagValue = "ERROR";

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string GetTagValueForStatusCode(StatusCode statusCode)
public static string? GetTagValueForStatusCode(StatusCode statusCode)
{
return statusCode switch
{
Expand All @@ -53,9 +55,9 @@ public static string GetTagValueForStatusCode(StatusCode statusCode)
* first because assumption is most spans will be
* Unset, then Error. Ok is not set by the SDK.
*/
string _ when UnsetStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => StatusCode.Unset,
string _ when ErrorStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => StatusCode.Error,
string _ when OkStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => StatusCode.Ok,
not null when UnsetStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => StatusCode.Unset,
not null when ErrorStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => StatusCode.Error,
not null when OkStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => StatusCode.Ok,
_ => (StatusCode?)null,
};
}
Expand Down
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
Expand Up @@ -7,5 +7,5 @@ OpenTelemetry.Instrumentation.MySqlData.MySqlDataInstrumentationOptions.RecordEx
OpenTelemetry.Instrumentation.MySqlData.MySqlDataInstrumentationOptions.SetDbStatement.get -> bool
OpenTelemetry.Instrumentation.MySqlData.MySqlDataInstrumentationOptions.SetDbStatement.set -> void
OpenTelemetry.Trace.TracerProviderBuilderExtensions
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddMySqlDataInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddMySqlDataInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.MySqlData.MySqlDataInstrumentationOptions> configure) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddMySqlDataInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder! builder) -> OpenTelemetry.Trace.TracerProviderBuilder!
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddMySqlDataInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder! builder, System.Action<OpenTelemetry.Instrumentation.MySqlData.MySqlDataInstrumentationOptions!>? configure) -> OpenTelemetry.Trace.TracerProviderBuilder!
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ internal class MySqlActivitySourceHelper
public static readonly string ActivitySourceName = AssemblyName.Name;
public static readonly string ActivityName = ActivitySourceName + ".Execute";

public static readonly IEnumerable<KeyValuePair<string, object>> CreationTags = new[]
public static readonly IEnumerable<KeyValuePair<string, object?>> CreationTags = new[]
{
new KeyValuePair<string, object>(SemanticConventions.AttributeDbSystem, MysqlDatabaseSystemName),
new KeyValuePair<string, object?>(SemanticConventions.AttributeDbSystem, MysqlDatabaseSystemName),
};

private static readonly Version Version = typeof(MySqlActivitySourceHelper).Assembly.GetName().Version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ internal class MySqlDataInstrumentation : DefaultTraceListener

private readonly MySqlDataInstrumentationOptions options;

private readonly Func<string, MySqlConnectionStringBuilder> builderFactory;
private readonly Func<string, MySqlConnectionStringBuilder>? builderFactory;

public MySqlDataInstrumentation(MySqlDataInstrumentationOptions options = null)
public MySqlDataInstrumentation(MySqlDataInstrumentationOptions? options = null)
{
this.options = options ?? new MySqlDataInstrumentationOptions();
MySqlTrace.Listeners.Clear();
Expand Down Expand Up @@ -186,7 +186,7 @@ private void BeforeExecuteCommand(MySqlDataTraceCommand command)
var activity = MySqlActivitySourceHelper.ActivitySource.StartActivity(
MySqlActivitySourceHelper.ActivityName,
ActivityKind.Client,
Activity.Current?.Context ?? default(ActivityContext),
Activity.Current?.Context ?? default,
MySqlActivitySourceHelper.CreationTags);
if (activity == null)
{
Expand Down Expand Up @@ -262,7 +262,7 @@ private void ErrorExecuteCommand(Exception exception)
}
}

private MySqlDataTraceCommand GetCommand(object driverIdObj, object cmd)
private MySqlDataTraceCommand GetCommand(object driverIdObj, object? cmd)
{
var command = new MySqlDataTraceCommand();
if (this.dbConn.TryGetValue((long)driverIdObj, out var database))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace OpenTelemetry.Instrumentation.MySqlData;
/// </summary>
internal class MySqlDataTraceCommand
{
public MySqlConnectionStringBuilder ConnectionStringBuilder { get; set; }
public MySqlConnectionStringBuilder? ConnectionStringBuilder { get; set; }

public string SqlText { get; set; }
public string SqlText { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Description>OpenTelemetry instrumentation for MySql.Data</Description>
<PackageTags>$(PackageTags);distributed-tracing;MySql.Data</PackageTags>
<MinVerTagPrefix>Instrumentation.MySqlData-</MinVerTagPrefix>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Description>OpenTelemetry instrumentation for MySql.Data</Description>
<PackageTags>$(PackageTags);distributed-tracing;MySql.Data</PackageTags>
<MinVerTagPrefix>Instrumentation.MySqlData-</MinVerTagPrefix>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MySql.Data" Version="6.10.7" />
<PackageReference Include="OpenTelemetry.Api" Version="$(OpenTelemetryCoreLatestVersion)" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MySql.Data" Version="6.10.7" />
<PackageReference Include="OpenTelemetry.Api" Version="$(OpenTelemetryCoreLatestVersion)" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Contrib.Shared\Api\SemanticConventions.cs" Link="Includes\SemanticConventions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Contrib.Shared\Api\SpanAttributeConstants.cs" Link="Includes\SpanAttributeConstants.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Contrib.Shared\Api\StatusHelper.cs" Link="Includes\StatusHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Internal\Guard.cs" Link="Includes\Guard.cs" />
</ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Contrib.Shared\Api\SemanticConventions.cs" Link="Includes\SemanticConventions.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Contrib.Shared\Api\SpanAttributeConstants.cs" Link="Includes\SpanAttributeConstants.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Contrib.Shared\Api\StatusHelper.cs" Link="Includes\StatusHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Internal\Guard.cs" Link="Includes\Guard.cs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static TracerProviderBuilder AddMySqlDataInstrumentation(this TracerProvi
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddMySqlDataInstrumentation(
this TracerProviderBuilder builder,
Action<MySqlDataInstrumentationOptions> configure)
Action<MySqlDataInstrumentationOptions>? configure)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably not put ? to stay consistent with other instrumentation libraries.

Look at this for reference: https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/src/OpenTelemetry.Instrumentation.AspNetCore/TracerProviderBuilderExtensions.cs#L59-L71

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have discussion with @alanwest some time ago. The consensus is to make it nullable non-optional.

See #913

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay. Thanks for sharing that.

{
Guard.ThrowIfNull(builder);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
// limitations under the License.
// </copyright>

#nullable enable

using System.Diagnostics;

namespace OpenTelemetry.Tests;

internal static class ActivityHelperExtensions
{
public static object GetTagValue(this Activity activity, string tagName)
public static object? GetTagValue(this Activity activity, string tagName)
{
Debug.Assert(activity != null, "Activity should not be null");

foreach (var tag in activity.TagObjects)
{
if (tag.Key == tagName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
// </copyright>

#nullable enable

using System.Collections.Generic;
using System.Diagnostics;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
// limitations under the License.
// </copyright>

#nullable enable

using System;
using System.Diagnostics;

namespace OpenTelemetry.Tests;

internal sealed class TestActivityProcessor : BaseProcessor<Activity>
{
public Action<Activity> StartAction;
public Action<Activity> EndAction;
public Action<Activity>? StartAction;
public Action<Activity>? EndAction;

public TestActivityProcessor()
{
Expand Down
4 changes: 3 additions & 1 deletion test/OpenTelemetry.Contrib.Tests.Shared/TestSampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
// limitations under the License.
// </copyright>

#nullable enable

using System;
using OpenTelemetry.Trace;

namespace OpenTelemetry.Tests;

internal class TestSampler : Sampler
{
public Func<SamplingParameters, SamplingResult> SamplingAction { get; set; }
public Func<SamplingParameters, SamplingResult>? SamplingAction { get; set; }

public SamplingParameters LatestSamplingParameters { get; private set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void SuccessTraceEventTest(
})
.Build();

var traceListener = (TraceListener)Assert.Single(MySqlTrace.Listeners);
var traceListener = (TraceListener)Assert.Single(MySqlTrace.Listeners)!;

ExecuteSuccessQuery(traceListener, commandText, isFailure);

Expand Down Expand Up @@ -93,7 +93,7 @@ public void UnknownMySqlTraceEventType(MySqlTraceEventType eventType)
.AddMySqlDataInstrumentation()
.Build();

var traceListener = (TraceListener)Assert.Single(MySqlTrace.Listeners);
var traceListener = (TraceListener?)Assert.Single(MySqlTrace.Listeners);

traceListener?.TraceEvent(
new TraceEventCache(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<IncludeSharedTestSource>true</IncludeSharedTestSource>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down