From 71e6c9b0a76946288556829b97bafedfdc266826 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Fri, 22 Sep 2023 10:27:20 +0200 Subject: [PATCH 1/3] Nullable Instrumentation.StackExchangeRedis.Tests --- ...isProfilerEntryToActivityConverterTests.cs | 10 +++++- ...umentation.StackExchangeRedis.Tests.csproj | 1 - ...kExchangeRedisCallsInstrumentationTests.cs | 32 +++++++------------ 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/test/OpenTelemetry.Instrumentation.StackExchangeRedis.Tests/Implementation/RedisProfilerEntryToActivityConverterTests.cs b/test/OpenTelemetry.Instrumentation.StackExchangeRedis.Tests/Implementation/RedisProfilerEntryToActivityConverterTests.cs index dcec655941..84aa7076a4 100644 --- a/test/OpenTelemetry.Instrumentation.StackExchangeRedis.Tests/Implementation/RedisProfilerEntryToActivityConverterTests.cs +++ b/test/OpenTelemetry.Instrumentation.StackExchangeRedis.Tests/Implementation/RedisProfilerEntryToActivityConverterTests.cs @@ -47,7 +47,7 @@ public RedisProfilerEntryToActivityConverterTests() this.tracerProvider = Sdk.CreateTracerProviderBuilder() .AddRedisInstrumentation(this.connection) - .Build(); + .Build()!; } public void Dispose() @@ -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); } @@ -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); } @@ -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)); } @@ -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)); } @@ -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)); } @@ -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)); @@ -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)); @@ -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)); } diff --git a/test/OpenTelemetry.Instrumentation.StackExchangeRedis.Tests/OpenTelemetry.Instrumentation.StackExchangeRedis.Tests.csproj b/test/OpenTelemetry.Instrumentation.StackExchangeRedis.Tests/OpenTelemetry.Instrumentation.StackExchangeRedis.Tests.csproj index e6f92d872f..54185af2cd 100644 --- a/test/OpenTelemetry.Instrumentation.StackExchangeRedis.Tests/OpenTelemetry.Instrumentation.StackExchangeRedis.Tests.csproj +++ b/test/OpenTelemetry.Instrumentation.StackExchangeRedis.Tests/OpenTelemetry.Instrumentation.StackExchangeRedis.Tests.csproj @@ -5,7 +5,6 @@ net7.0;net6.0 $(TargetFrameworks);net462 $(TARGET_FRAMEWORK) - disable diff --git a/test/OpenTelemetry.Instrumentation.StackExchangeRedis.Tests/StackExchangeRedisCallsInstrumentationTests.cs b/test/OpenTelemetry.Instrumentation.StackExchangeRedis.Tests/StackExchangeRedisCallsInstrumentationTests.cs index c3b40c7050..4ac9568f8c 100644 --- a/test/OpenTelemetry.Instrumentation.StackExchangeRedis.Tests/StackExchangeRedisCallsInstrumentationTests.cs +++ b/test/OpenTelemetry.Instrumentation.StackExchangeRedis.Tests/StackExchangeRedisCallsInstrumentationTests.cs @@ -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)] @@ -108,7 +108,7 @@ public void SuccessfulCommandTest(string value) }; connectionOptions.EndPoints.Add(RedisEndPoint); - IConnectionMultiplexer connection = null; + IConnectionMultiplexer? connection = null; var activityProcessor = new Mock>(); var sampler = new TestSampler(); using (Sdk.CreateTracerProviderBuilder() @@ -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); @@ -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 { @@ -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); } @@ -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(); @@ -306,7 +305,7 @@ 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); } @@ -314,17 +313,10 @@ public async Task ProfilerSessionsHandleMultipleSpans() 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(() => builder.AddRedisInstrumentation(connection: null)); - } - [Fact] public void StackExchangeRedis_DependencyInjection_Success() { @@ -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 { @@ -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"); } } From d447064e5e77d6b098e6a2e0db0bfa4b51690992 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Fri, 22 Sep 2023 10:27:59 +0200 Subject: [PATCH 2/3] Nullable Extensions.Enrichment.Tests --- .../OpenTelemetry.Extensions.Enrichment.Tests.csproj | 1 - ...enTelemetryEnrichmentProviderBuilderExtensions.cs | 4 ++++ ...etryEnrichmentServiceCollectionExtensionsTests.cs | 12 ++++++++++-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/test/OpenTelemetry.Extensions.Enrichment.Tests/OpenTelemetry.Extensions.Enrichment.Tests.csproj b/test/OpenTelemetry.Extensions.Enrichment.Tests/OpenTelemetry.Extensions.Enrichment.Tests.csproj index e0814e4c98..3b73750d9c 100644 --- a/test/OpenTelemetry.Extensions.Enrichment.Tests/OpenTelemetry.Extensions.Enrichment.Tests.csproj +++ b/test/OpenTelemetry.Extensions.Enrichment.Tests/OpenTelemetry.Extensions.Enrichment.Tests.csproj @@ -4,7 +4,6 @@ Unit test project for OpenTelemetry .NET SDK telemetry enrichment. $(NetFrameworkMinimumSupportedVersion) - disable diff --git a/test/OpenTelemetry.Extensions.Enrichment.Tests/OpenTelemetryEnrichmentProviderBuilderExtensions.cs b/test/OpenTelemetry.Extensions.Enrichment.Tests/OpenTelemetryEnrichmentProviderBuilderExtensions.cs index 032105b732..3c96a9dc5e 100644 --- a/test/OpenTelemetry.Extensions.Enrichment.Tests/OpenTelemetryEnrichmentProviderBuilderExtensions.cs +++ b/test/OpenTelemetry.Extensions.Enrichment.Tests/OpenTelemetryEnrichmentProviderBuilderExtensions.cs @@ -41,6 +41,7 @@ public void AddTraceEnricherOfTRegistersEnricher() using var source = new ActivitySource(SourceName); using (var activity = source.StartActivity(SourceName)) { + Assert.NotNull(activity); activity.Stop(); Assert.Single(exportedItems); @@ -70,6 +71,7 @@ public void AddTraceEnricherRegistersEnricher() using (var activity = source1.StartActivity(SourceName)) { + Assert.NotNull(activity); activity.Stop(); Assert.Single(exportedItems); @@ -104,6 +106,7 @@ public void AddTraceEnricherActionRegistersEnricher() using (var activity = source1.StartActivity(SourceName)) { + Assert.NotNull(activity); activity.Stop(); Assert.Single(exportedItems); @@ -133,6 +136,7 @@ public void AddTraceEnricherFactoryRegistersEnricher() using (var activity = source1.StartActivity(SourceName)) { + Assert.NotNull(activity); activity.Stop(); Assert.Single(exportedItems); diff --git a/test/OpenTelemetry.Extensions.Enrichment.Tests/OpenTelemetryEnrichmentServiceCollectionExtensionsTests.cs b/test/OpenTelemetry.Extensions.Enrichment.Tests/OpenTelemetryEnrichmentServiceCollectionExtensionsTests.cs index 96b4ac80d5..1ed3a1256e 100644 --- a/test/OpenTelemetry.Extensions.Enrichment.Tests/OpenTelemetryEnrichmentServiceCollectionExtensionsTests.cs +++ b/test/OpenTelemetry.Extensions.Enrichment.Tests/OpenTelemetryEnrichmentServiceCollectionExtensionsTests.cs @@ -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); @@ -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); @@ -139,6 +145,7 @@ public async Task AddTraceEnricherActionRegistersEnricher() using (var activity = source1.StartActivity(SourceName)) { + Assert.NotNull(activity); activity.Stop(); Assert.Single(exportedItems); @@ -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); From 61c052184931e90e711712be8887371f26ea39a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Fri, 22 Sep 2023 10:32:32 +0200 Subject: [PATCH 3/3] Nullable Extensions.Tests --- .../ActivityEventAttachingLogProcessorTests.cs | 17 +++++++++++------ .../OpenTelemetry.Extensions.Tests.csproj | 1 - .../Trace/AutoFlushActivityProcessorTests.cs | 3 +++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/test/OpenTelemetry.Extensions.Tests/ActivityEventAttachingLogProcessorTests.cs b/test/OpenTelemetry.Extensions.Tests/ActivityEventAttachingLogProcessorTests.cs index c5cd3af1c1..79cf07fbf5 100644 --- a/test/OpenTelemetry.Extensions.Tests/ActivityEventAttachingLogProcessorTests.cs +++ b/test/OpenTelemetry.Extensions.Tests/ActivityEventAttachingLogProcessorTests.cs @@ -89,16 +89,18 @@ public void AttachLogsToActivityEventTest( }); ILogger logger = loggerFactory.CreateLogger(); - 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"); @@ -116,7 +118,7 @@ public void AttachLogsToActivityEventTest( Assert.NotNull(logEvent); Assert.Equal("log", logEvent.Value.Name); - Dictionary tags = logEvent.Value.Tags?.ToDictionary(i => i.Key, i => i.Value); + Dictionary? tags = logEvent.Value.Tags?.ToDictionary(i => i.Key, i => i.Value); Assert.NotNull(tags); Assert.Equal("OpenTelemetry.Extensions.Tests.ActivityEventAttachingLogProcessorTests", tags[nameof(LogRecord.CategoryName)]); @@ -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 exLogTags = exLogEvent.Value.Tags?.ToDictionary(i => i.Key, i => i.Value); + Dictionary? exLogTags = exLogEvent.Value.Tags?.ToDictionary(i => i.Key, i => i.Value); Assert.NotNull(exLogTags); Assert.Equal(99, exLogTags["scope[0].NodeId"]); @@ -169,7 +172,7 @@ public void AttachLogsToActivityEventTest( Assert.NotNull(exEvent); Assert.Equal("exception", exEvent.Value.Name); - Dictionary exTags = exEvent.Value.Tags?.ToDictionary(i => i.Key, i => i.Value); + Dictionary? exTags = exEvent.Value.Tags?.ToDictionary(i => i.Key, i => i.Value); Assert.NotNull(exTags); Assert.Equal("System.InvalidOperationException", exTags["exception.type"]); @@ -218,7 +221,8 @@ public void AttachLogsToActivityEventTest_Filter( }); ILogger logger = loggerFactory.CreateLogger(); - Activity activity = this.activitySource.StartActivity("Test"); + Activity? activity = this.activitySource.StartActivity("Test"); + Assert.NotNull(activity); using IDisposable scope = logger.BeginScope("{NodeId}", 99); @@ -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"); diff --git a/test/OpenTelemetry.Extensions.Tests/OpenTelemetry.Extensions.Tests.csproj b/test/OpenTelemetry.Extensions.Tests/OpenTelemetry.Extensions.Tests.csproj index 3b61643e38..aac58b1b7b 100644 --- a/test/OpenTelemetry.Extensions.Tests/OpenTelemetry.Extensions.Tests.csproj +++ b/test/OpenTelemetry.Extensions.Tests/OpenTelemetry.Extensions.Tests.csproj @@ -4,7 +4,6 @@ Unit test project for OpenTelemetry .NET SDK preview features and extensions net7.0;net6.0 - disable diff --git a/test/OpenTelemetry.Extensions.Tests/Trace/AutoFlushActivityProcessorTests.cs b/test/OpenTelemetry.Extensions.Tests/Trace/AutoFlushActivityProcessorTests.cs index 4f279c4410..18a58a6add 100644 --- a/test/OpenTelemetry.Extensions.Tests/Trace/AutoFlushActivityProcessorTests.cs +++ b/test/OpenTelemetry.Extensions.Tests/Trace/AutoFlushActivityProcessorTests.cs @@ -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); @@ -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()); @@ -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);