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

Nullable for tests-projects with already handled projects #1370

Merged
merged 6 commits into from
Oct 9, 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<Description>Unit test project for OpenTelemetry .NET SDK telemetry enrichment.</Description>
<!-- OmniSharp/VS Code requires TargetFrameworks to be in descending order for IntelliSense and analysis. -->
<TargetFrameworks>$(NetFrameworkMinimumSupportedVersion)</TargetFrameworks>
<Nullable>disable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void AddTraceEnricherOfTRegistersEnricher()
using var source = new ActivitySource(SourceName);
using (var activity = source.StartActivity(SourceName))
{
Assert.NotNull(activity);
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't really need these Assert.NotNull(activity); statements. The unit tests would fail anyway if the activity ends up being null.

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 can use either use ! as null-forgiving operator or add this assertion. Assertion looks better IMO.

activity.Stop();

Assert.Single(exportedItems);
Expand Down Expand Up @@ -70,6 +71,7 @@ public void AddTraceEnricherRegistersEnricher()

using (var activity = source1.StartActivity(SourceName))
{
Assert.NotNull(activity);
activity.Stop();

Assert.Single(exportedItems);
Expand Down Expand Up @@ -104,6 +106,7 @@ public void AddTraceEnricherActionRegistersEnricher()

using (var activity = source1.StartActivity(SourceName))
{
Assert.NotNull(activity);
activity.Stop();

Assert.Single(exportedItems);
Expand Down Expand Up @@ -133,6 +136,7 @@ public void AddTraceEnricherFactoryRegistersEnricher()

using (var activity = source1.StartActivity(SourceName))
{
Assert.NotNull(activity);
activity.Stop();

Assert.Single(exportedItems);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,15 @@ public async Task AddTraceEnricherOfTRegistersEnricher()
using var source = new ActivitySource(SourceName);
using (var activity = source.StartActivity(SourceName))
{
Assert.NotNull(activity);
activity.Stop();

Assert.Equal(1, (enrichers[0] as MyTraceEnricher).TimesCalled);
Assert.Equal(1, (enrichers[1] as MyTraceEnricher2).TimesCalled);
var myTraceEnricher = enrichers[0] as MyTraceEnricher;
var myTraceEnricher2 = enrichers[1] as MyTraceEnricher2;
Assert.NotNull(myTraceEnricher);
Assert.NotNull(myTraceEnricher2);
Assert.Equal(1, myTraceEnricher.TimesCalled);
Assert.Equal(1, myTraceEnricher2.TimesCalled);

Assert.Single(exportedItems);

Expand Down Expand Up @@ -97,6 +102,7 @@ public async Task AddTraceEnricherRegistersEnricher()
using var source = new ActivitySource(SourceName);
using (var activity = source.StartActivity(SourceName))
{
Assert.NotNull(activity);
activity.Stop();

Assert.Single(exportedItems);
Expand Down Expand Up @@ -139,6 +145,7 @@ public async Task AddTraceEnricherActionRegistersEnricher()

using (var activity = source1.StartActivity(SourceName))
{
Assert.NotNull(activity);
activity.Stop();

Assert.Single(exportedItems);
Expand Down Expand Up @@ -177,6 +184,7 @@ public async Task AddTraceEnricherFactoryRegistersEnricher()
using var source = new ActivitySource(SourceName);
using (var activity = source.StartActivity(SourceName))
{
Assert.NotNull(activity);
activity.Stop();

Assert.Single(exportedItems);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,18 @@ public void AttachLogsToActivityEventTest(
});

ILogger logger = loggerFactory.CreateLogger<ActivityEventAttachingLogProcessorTests>();
Activity activity = this.activitySource.StartActivity("Test");
Activity? activity = this.activitySource.StartActivity("Test");
Assert.NotNull(activity);

using IDisposable scope = logger.BeginScope("{NodeId}", 99);

logger.LogInformation(eventId, "Hello OpenTelemetry {UserId}!", 8);

Activity innerActivity = null;
Activity? innerActivity = null;
if (recordException)
{
innerActivity = this.activitySource.StartActivity("InnerTest");
Assert.NotNull(innerActivity);

using IDisposable innerScope = logger.BeginScope("{RequestId}", "1234");

Expand All @@ -116,7 +118,7 @@ public void AttachLogsToActivityEventTest(
Assert.NotNull(logEvent);
Assert.Equal("log", logEvent.Value.Name);

Dictionary<string, object> tags = logEvent.Value.Tags?.ToDictionary(i => i.Key, i => i.Value);
Dictionary<string, object?>? tags = logEvent.Value.Tags?.ToDictionary(i => i.Key, i => i.Value);
Assert.NotNull(tags);

Assert.Equal("OpenTelemetry.Extensions.Tests.ActivityEventAttachingLogProcessorTests", tags[nameof(LogRecord.CategoryName)]);
Expand Down Expand Up @@ -153,12 +155,13 @@ public void AttachLogsToActivityEventTest(

if (recordException)
{
Assert.NotNull(innerActivity);
ActivityEvent? exLogEvent = innerActivity.Events.FirstOrDefault();

Assert.NotNull(exLogEvent);
Assert.Equal("log", exLogEvent.Value.Name);

Dictionary<string, object> exLogTags = exLogEvent.Value.Tags?.ToDictionary(i => i.Key, i => i.Value);
Dictionary<string, object?>? exLogTags = exLogEvent.Value.Tags?.ToDictionary(i => i.Key, i => i.Value);
Assert.NotNull(exLogTags);

Assert.Equal(99, exLogTags["scope[0].NodeId"]);
Expand All @@ -169,7 +172,7 @@ public void AttachLogsToActivityEventTest(
Assert.NotNull(exEvent);
Assert.Equal("exception", exEvent.Value.Name);

Dictionary<string, object> exTags = exEvent.Value.Tags?.ToDictionary(i => i.Key, i => i.Value);
Dictionary<string, object?>? exTags = exEvent.Value.Tags?.ToDictionary(i => i.Key, i => i.Value);
Assert.NotNull(exTags);

Assert.Equal("System.InvalidOperationException", exTags["exception.type"]);
Expand Down Expand Up @@ -218,7 +221,8 @@ public void AttachLogsToActivityEventTest_Filter(
});

ILogger logger = loggerFactory.CreateLogger<ActivityEventAttachingLogProcessorTests>();
Activity activity = this.activitySource.StartActivity("Test");
Activity? activity = this.activitySource.StartActivity("Test");
Assert.NotNull(activity);

using IDisposable scope = logger.BeginScope("{NodeId}", 99);

Expand All @@ -227,6 +231,7 @@ public void AttachLogsToActivityEventTest_Filter(
if (recordException)
{
var innerActivity = this.activitySource.StartActivity("InnerTest");
Assert.NotNull(innerActivity);

using IDisposable innerScope = logger.BeginScope("{RequestId}", "1234");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<Description>Unit test project for OpenTelemetry .NET SDK preview features and extensions</Description>
<!-- OmniSharp/VS Code requires TargetFrameworks to be in descending order for IntelliSense and analysis. -->
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<Nullable>disable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void AutoFlushActivityProcessor_FlushAfterLocalServerSideRootSpans_EndMat

using var source = new ActivitySource(sourceName);
using var activity = source.StartActivity("name", ActivityKind.Server);
Assert.NotNull(activity);
activity.Stop();

mockExporting.Protected().Verify("OnForceFlush", Times.Once(), 5_000);
Expand All @@ -59,6 +60,7 @@ public void AutoFlushActivityProcessor_FlushAfterLocalServerSideRootSpans_EndNon

using var source = new ActivitySource(sourceName);
using var activity = source.StartActivity("name", ActivityKind.Client);
Assert.NotNull(activity);
activity.Stop();

mockExporting.Protected().Verify("OnForceFlush", Times.Never(), It.IsAny<int>());
Expand All @@ -78,6 +80,7 @@ public void AutoFlushActivityProcessor_PredicateThrows_DoesNothing()

using var source = new ActivitySource(sourceName);
using var activity = source.StartActivity("name", ActivityKind.Server);
Assert.NotNull(activity);
activity.Stop();

mockExporting.Protected().Verify("OnForceFlush", Times.Never(), 5_000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public RedisProfilerEntryToActivityConverterTests()

this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddRedisInstrumentation(this.connection)
.Build();
.Build()!;
}

public void Dispose()
Expand All @@ -67,6 +67,7 @@ public void ProfilerCommandToActivity_UsesCommandAsName()

var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand.Object, new StackExchangeRedisInstrumentationOptions());

Assert.NotNull(result);
Assert.Equal("SET", result.DisplayName);
}

Expand All @@ -80,6 +81,7 @@ public void ProfilerCommandToActivity_UsesTimestampAsStartTime()

var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand.Object, new StackExchangeRedisInstrumentationOptions());

Assert.NotNull(result);
Assert.Equal(now, result.StartTimeUtc);
}

Expand All @@ -92,6 +94,7 @@ public void ProfilerCommandToActivity_SetsDbTypeAttributeAsRedis()

var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand.Object, new StackExchangeRedisInstrumentationOptions());

Assert.NotNull(result);
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributeDbSystem));
Assert.Equal("redis", result.GetTagValue(SemanticConventions.AttributeDbSystem));
}
Expand All @@ -106,6 +109,7 @@ public void ProfilerCommandToActivity_UsesCommandAsDbStatementAttribute()

var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand.Object, new StackExchangeRedisInstrumentationOptions());

Assert.NotNull(result);
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributeDbStatement));
Assert.Equal("SET", result.GetTagValue(SemanticConventions.AttributeDbStatement));
}
Expand All @@ -122,6 +126,7 @@ public void ProfilerCommandToActivity_UsesFlagsForFlagsAttribute()

var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand.Object, new StackExchangeRedisInstrumentationOptions());

Assert.NotNull(result);
Assert.NotNull(result.GetTagValue(StackExchangeRedisConnectionInstrumentation.RedisFlagsKeyName));
Assert.Equal("PreferMaster, FireAndForget, NoRedirect", result.GetTagValue(StackExchangeRedisConnectionInstrumentation.RedisFlagsKeyName));
}
Expand All @@ -140,6 +145,7 @@ public void ProfilerCommandToActivity_UsesIpEndPointAsEndPoint()

var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand.Object, new StackExchangeRedisInstrumentationOptions());

Assert.NotNull(result);
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributeNetPeerIp));
Assert.Equal($"{address}.0.0.0", result.GetTagValue(SemanticConventions.AttributeNetPeerIp));
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributeNetPeerPort));
Expand All @@ -158,6 +164,7 @@ public void ProfilerCommandToActivity_UsesDnsEndPointAsEndPoint()

var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand.Object, new StackExchangeRedisInstrumentationOptions());

Assert.NotNull(result);
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributeNetPeerName));
Assert.Equal(dnsEndPoint.Host, result.GetTagValue(SemanticConventions.AttributeNetPeerName));
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributeNetPeerPort));
Expand All @@ -176,6 +183,7 @@ public void ProfilerCommandToActivity_UsesOtherEndPointAsEndPoint()

var result = RedisProfilerEntryToActivityConverter.ProfilerCommandToActivity(activity, profiledCommand.Object, new StackExchangeRedisInstrumentationOptions());

Assert.NotNull(result);
Assert.NotNull(result.GetTagValue(SemanticConventions.AttributePeerService));
Assert.Equal(unixEndPoint.ToString(), result.GetTagValue(SemanticConventions.AttributePeerService));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<TargetFrameworks Condition="$(TARGET_FRAMEWORK) == ''">net7.0;net6.0</TargetFrameworks>
<TargetFrameworks Condition="$(TARGET_FRAMEWORK) == '' and $(OS) == 'Windows_NT'">$(TargetFrameworks);net462</TargetFrameworks>
<TargetFrameworks Condition="$(TARGET_FRAMEWORK) != ''">$(TARGET_FRAMEWORK)</TargetFrameworks>
<Nullable>disable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ To use Docker...
*/

private const string RedisEndPointEnvVarName = "OTEL_REDISENDPOINT";
private static readonly string RedisEndPoint = SkipUnlessEnvVarFoundTheoryAttribute.GetEnvironmentVariable(RedisEndPointEnvVarName);
private static readonly string? RedisEndPoint = SkipUnlessEnvVarFoundTheoryAttribute.GetEnvironmentVariable(RedisEndPointEnvVarName);

[Trait("CategoryName", "RedisIntegrationTests")]
[SkipUnlessEnvVarFoundTheory(RedisEndPointEnvVarName)]
Expand Down Expand Up @@ -108,7 +108,7 @@ public void SuccessfulCommandTest(string value)
};
connectionOptions.EndPoints.Add(RedisEndPoint);

IConnectionMultiplexer connection = null;
IConnectionMultiplexer? connection = null;
var activityProcessor = new Mock<BaseProcessor<Activity>>();
var sampler = new TestSampler();
using (Sdk.CreateTracerProviderBuilder()
Expand Down Expand Up @@ -162,7 +162,7 @@ public async void ProfilerSessionUsesTheSameDefault()
var profilerFactory = instrumentation.GetProfilerSessionsFactory();
var first = profilerFactory();
var second = profilerFactory();
ProfilingSession third = null;
ProfilingSession? third = null;
await Task.Delay(1).ContinueWith((t) => { third = profilerFactory(); });
Assert.Equal(first, second);
Assert.Equal(second, third);
Expand All @@ -173,7 +173,7 @@ public async void ProfilerSessionUsesTheSameDefault()
[InlineData("value1")]
public void CanEnrichActivityFromCommand(string value)
{
StackExchangeRedisInstrumentation instrumentation = null;
StackExchangeRedisInstrumentation? instrumentation = null;

var connectionOptions = new ConfigurationOptions
{
Expand Down Expand Up @@ -248,13 +248,12 @@ public void CheckCacheIsFlushedProperly()

// get an initial profiler from root activity
Activity.Current = rootActivity;
ProfilingSession profiler0 = profilerFactory();
ProfilingSession? profiler0 = profilerFactory();

// expect different result from synchronous child activity
ProfilingSession profiler1;
using (Activity.Current = new Activity("Child-Span-1").SetParentId(rootActivity.Id).Start())
{
profiler1 = profilerFactory();
var profiler1 = profilerFactory();
Assert.NotSame(profiler0, profiler1);
}

Expand Down Expand Up @@ -288,10 +287,10 @@ public async Task ProfilerSessionsHandleMultipleSpans()

// get an initial profiler from root activity
Activity.Current = rootActivity;
ProfilingSession profiler0 = profilerFactory();
ProfilingSession? profiler0 = profilerFactory();

// expect different result from synchronous child activity
ProfilingSession profiler1;
ProfilingSession? profiler1;
using (Activity.Current = new Activity("Child-Span-1").SetParentId(rootActivity.Id).Start())
{
profiler1 = profilerFactory();
Expand All @@ -306,25 +305,18 @@ public async Task ProfilerSessionsHandleMultipleSpans()
// lose async context on purpose
await Task.Delay(100).ConfigureAwait(false);

ProfilingSession profiler2 = profilerFactory();
ProfilingSession? profiler2 = profilerFactory();
Assert.NotSame(profiler0, profiler2);
Assert.NotSame(profiler1, profiler2);
}

Activity.Current = rootActivity;

// ensure same result back in root activity
ProfilingSession profiles3 = profilerFactory();
ProfilingSession? profiles3 = profilerFactory();
Assert.Same(profiler0, profiles3);
}

[Fact]
public void StackExchangeRedis_BadArgs()
{
TracerProviderBuilder builder = null;
Assert.Throws<ArgumentNullException>(() => builder.AddRedisInstrumentation(connection: null));
}

[Fact]
public void StackExchangeRedis_DependencyInjection_Success()
{
Expand Down Expand Up @@ -360,7 +352,7 @@ public void StackExchangeRedis_DependencyInjection_Success()
[Fact]
public void StackExchangeRedis_StackExchangeRedisInstrumentation_Test()
{
StackExchangeRedisInstrumentation instrumentation = null;
StackExchangeRedisInstrumentation? instrumentation = null;

var connectionOptions = new ConfigurationOptions
{
Expand Down Expand Up @@ -456,6 +448,6 @@ private static void VerifySamplingParameters(SamplingParameters samplingParamete
Assert.Contains(
samplingParameters.Tags,
kvp => kvp.Key == SemanticConventions.AttributeDbSystem
&& (string)kvp.Value == "redis");
&& (string?)kvp.Value == "redis");
}
}
Loading