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

[Propagators] Nullable annotations #5767

Merged
merged 3 commits into from
Aug 28, 2024

Conversation

Kielek
Copy link
Contributor

@Kielek Kielek commented Jul 30, 2024

Towards #3958

Changes

[Propagators] Nullable

  • OpenTelemetry.Api
  • OpenTelemetry.Extensions.Propagators
  • corresponding tests

Merge requirement checklist

  • CONTRIBUTING guidelines followed (license requirements, nullable enabled, static analysis, etc.)
  • Unit tests added/updated
  • [ ] Appropriate CHANGELOG.md files updated for non-trivial changes
  • Changes in public API reviewed (if applicable)

@github-actions github-actions bot added pkg:OpenTelemetry.Api Issues related to OpenTelemetry.Api NuGet package pkg:OpenTelemetry.Extensions.Propagators Issues related to OpenTelemetry.Extensions.Propagators NuGet package labels Jul 30, 2024
@Kielek
Copy link
Contributor Author

Kielek commented Jul 30, 2024

Potential diff for contrib repo:

diff --git a/src/OpenTelemetry.Extensions.AWS/.publicApi/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Extensions.AWS/.publicApi/PublicAPI.Unshipped.txt
index a7a1588b..cc69db22 100644
--- a/src/OpenTelemetry.Extensions.AWS/.publicApi/PublicAPI.Unshipped.txt
+++ b/src/OpenTelemetry.Extensions.AWS/.publicApi/PublicAPI.Unshipped.txt
@@ -2,7 +2,7 @@ OpenTelemetry.Extensions.AWS.AWSXRayIdGenerator
 OpenTelemetry.Extensions.AWS.Trace.AWSXRayPropagator
 OpenTelemetry.Extensions.AWS.Trace.AWSXRayPropagator.AWSXRayPropagator() -> void
 OpenTelemetry.Trace.TracerProviderBuilderExtensions
-override OpenTelemetry.Extensions.AWS.Trace.AWSXRayPropagator.Extract<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Func<T, string!, System.Collections.Generic.IEnumerable<string!>!>! getter) -> OpenTelemetry.Context.Propagation.PropagationContext
+override OpenTelemetry.Extensions.AWS.Trace.AWSXRayPropagator.Extract<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Func<T, string!, System.Collections.Generic.IEnumerable<string!>?>! getter) -> OpenTelemetry.Context.Propagation.PropagationContext
 override OpenTelemetry.Extensions.AWS.Trace.AWSXRayPropagator.Fields.get -> System.Collections.Generic.ISet<string!>!
 override OpenTelemetry.Extensions.AWS.Trace.AWSXRayPropagator.Inject<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Action<T, string!, string!>! setter) -> void
 static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddXRayTraceId(this OpenTelemetry.Trace.TracerProviderBuilder! builder) -> OpenTelemetry.Trace.TracerProviderBuilder!
diff --git a/src/OpenTelemetry.Extensions.AWS/Trace/AWSXRayPropagator.cs b/src/OpenTelemetry.Extensions.AWS/Trace/AWSXRayPropagator.cs
index 569bae53..4e8d5ac8 100644
--- a/src/OpenTelemetry.Extensions.AWS/Trace/AWSXRayPropagator.cs
+++ b/src/OpenTelemetry.Extensions.AWS/Trace/AWSXRayPropagator.cs
@@ -38,7 +38,7 @@ public class AWSXRayPropagator : TextMapPropagator
     public override ISet<string> Fields => new HashSet<string>() { AWSXRayTraceHeaderKey };
 
     /// <inheritdoc/>
-    public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>> getter)
+    public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>?> getter)
     {
         if (context.ActivityContext.IsValid())
         {
diff --git a/test/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.Tests/ActivityHelperTest.cs b/test/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.Tests/ActivityHelperTest.cs
index 0e7bacce..d5dabf7c 100644
--- a/test/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.Tests/ActivityHelperTest.cs
+++ b/test/OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.Tests/ActivityHelperTest.cs
@@ -555,7 +555,7 @@ public class ActivityHelperTest : IDisposable
     {
         public override ISet<string>? Fields => null;
 
-        public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>> getter)
+        public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>?> getter)
         {
             return default;
         }
diff --git a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs
index 03cc1600..4eeed567 100644
--- a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs
+++ b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs
@@ -1202,7 +1202,7 @@ public sealed class BasicTests
 
         public override ISet<string> Fields => throw new NotImplementedException();
 
-        public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>> getter)
+        public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>?> getter)
         {
             return new PropagationContext(this.activityContext, this.baggage);
         }
diff --git a/test/OpenTelemetry.Instrumentation.GrpcCore.Tests/GrpcCoreClientInterceptorTests.cs b/test/OpenTelemetry.Instrumentation.GrpcCore.Tests/GrpcCoreClientInterceptorTests.cs
index 150f20a3..57db6c4d 100644
--- a/test/OpenTelemetry.Instrumentation.GrpcCore.Tests/GrpcCoreClientInterceptorTests.cs
+++ b/test/OpenTelemetry.Instrumentation.GrpcCore.Tests/GrpcCoreClientInterceptorTests.cs
@@ -389,9 +389,11 @@ public class GrpcCoreClientInterceptorTests
         {
             propagatorCalled++;
             capturedPropagationContext = propagation;
-            capturedCarrier = (Metadata)carrier;
+            capturedCarrier = (Metadata?)carrier;
 
             // Make sure the original metadata make it through
+
+            Assert.NotNull(capturedCarrier);
             if (additionalMetadata != null)
             {
                 Assert.Equal(capturedCarrier, additionalMetadata);
diff --git a/test/Shared/CustomTextMapPropagator.cs b/test/Shared/CustomTextMapPropagator.cs
index 8d3404e8..fc869441 100644
--- a/test/Shared/CustomTextMapPropagator.cs
+++ b/test/Shared/CustomTextMapPropagator.cs
@@ -24,7 +24,7 @@ internal sealed class CustomTextMapPropagator : TextMapPropagator
 #pragma warning restore SA1010 // Opening square brackets should be spaced correctly
 #pragma warning restore SA1201 // Elements should appear in the correct order
 
-    public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>> getter)
+    public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>?> getter)
     {
         if (this.TraceId != default && this.SpanId != default)
         {
diff --git a/test/Shared/TestTextMapPropagator.cs b/test/Shared/TestTextMapPropagator.cs
index ba58606a..5c2c36da 100644
--- a/test/Shared/TestTextMapPropagator.cs
+++ b/test/Shared/TestTextMapPropagator.cs
@@ -9,13 +9,13 @@ namespace OpenTelemetry.Tests;
 
 internal class TestTextMapPropagator : TextMapPropagator
 {
-    public Action<PropagationContext, object, Action<object, string, string>>? OnInject { get; set; }
+    public Action<PropagationContext, object?, Action<object, string, string>>? OnInject { get; set; }
 
     public Action? Extracted { get; set; }
 
     public override ISet<string> Fields => throw new NotImplementedException();
 
-    public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>> getter)
+    public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>?> getter)
     {
         this.Extracted?.Invoke();
         return context;
@@ -23,10 +23,9 @@ internal class TestTextMapPropagator : TextMapPropagator
 
     public override void Inject<T>(PropagationContext context, T carrier, Action<T, string, string> setter)
     {
-        var newAction = new Action<T, string, string>((c, k, v) => setter(c, k, v));
         this.OnInject?.Invoke(
             context,
             carrier,
-            new Action<object, string, string>((c, k, v) => setter((T)c, k, v)));
+            (c, k, v) => setter((T)c, k, v));
     }
 }

Copy link

codecov bot commented Jul 30, 2024

Codecov Report

Attention: Patch coverage is 81.81818% with 4 lines in your changes missing coverage. Please review.

Project coverage is 86.21%. Comparing base (6250307) to head (8bb5e43).
Report is 299 commits behind head on main.

Files with missing lines Patch % Lines
src/OpenTelemetry.Api/Trace/SpanContext.cs 0.00% 3 Missing ⚠️
...etry.Api/Context/Propagation/PropagationContext.cs 0.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #5767      +/-   ##
==========================================
+ Coverage   83.38%   86.21%   +2.82%     
==========================================
  Files         297      256      -41     
  Lines       12531    11147    -1384     
==========================================
- Hits        10449     9610     -839     
+ Misses       2082     1537     -545     
Flag Coverage Δ
unittests ?
unittests-Project-Experimental 86.20% <81.81%> (?)
unittests-Project-Stable 86.14% <81.81%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...nTelemetry.Api/Context/Propagation/B3Propagator.cs 86.02% <100.00%> (+0.15%) ⬆️
...metry.Api/Context/Propagation/BaggagePropagator.cs 85.00% <100.00%> (-0.49%) ⬇️
.../Context/Propagation/CompositeTextMapPropagator.cs 100.00% <100.00%> (+16.66%) ⬆️
...y.Api/Context/Propagation/NoopTextMapPropagator.cs 33.33% <100.00%> (+33.33%) ⬆️
...enTelemetry.Api/Context/Propagation/Propagators.cs 100.00% <ø> (ø)
....Api/Context/Propagation/TraceContextPropagator.cs 90.82% <100.00%> (+1.35%) ⬆️
...etry.Api/Context/Propagation/TraceStateUtilsNew.cs 83.00% <100.00%> (ø)
...enTelemetry.Extensions.Propagators/B3Propagator.cs 86.02% <100.00%> (+0.15%) ⬆️
...lemetry.Extensions.Propagators/JaegerPropagator.cs 95.38% <100.00%> (ø)
...etry.Api/Context/Propagation/PropagationContext.cs 64.28% <0.00%> (ø)
... and 1 more

... and 211 files with indirect coverage changes

@Kielek Kielek marked this pull request as ready for review July 30, 2024 10:26
@Kielek Kielek requested a review from a team July 30, 2024 10:26
@@ -102,11 +104,11 @@ public override void Inject<T>(PropagationContext context, T carrier, Action<T,
}
}

internal static bool TryExtractBaggage(string[] baggageCollection, out Dictionary<string, string> baggage)
internal static bool TryExtractBaggage(string[] baggageCollection, out Dictionary<string, string>? baggage)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
internal static bool TryExtractBaggage(string[] baggageCollection, out Dictionary<string, string>? baggage)
internal static bool TryExtractBaggage(string[] baggageCollection, [NotNullWhen(true)] out Dictionary<string, string>? baggage)

Might also need tweaks to usings.

Copy link
Contributor Author

@Kielek Kielek Aug 27, 2024

Choose a reason for hiding this comment

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

@CodeBlanch, It is not so simple. Nullability shims are not included in OpenTelemetry.Api. It is not possible due to InternalsVisibleTo("OpenTelemetry") project.

When adding this shims, 2 attributes with exactly same names occurs in OpenTelemetry netstandard2.1 project.
To solve this, we could add additional target for OpenTelemetry.Api (netstandard2.1).

Fixes without adding shims in 8bb5e43

Copy link
Member

@CodeBlanch CodeBlanch left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

github-actions bot commented Aug 9, 2024

This PR was marked stale due to lack of activity and will be closed in 7 days. Commenting or Pushing will instruct the bot to automatically remove the label. This bot runs once per day.

@github-actions github-actions bot added Stale Issues and pull requests which have been flagged for closing due to inactivity and removed Stale Issues and pull requests which have been flagged for closing due to inactivity labels Aug 9, 2024
Copy link
Contributor

This PR was marked stale due to lack of activity and will be closed in 7 days. Commenting or pushing will instruct the bot to automatically remove the label. This bot runs once per day.

@github-actions github-actions bot added Stale Issues and pull requests which have been flagged for closing due to inactivity and removed Stale Issues and pull requests which have been flagged for closing due to inactivity labels Aug 22, 2024
@CodeBlanch CodeBlanch changed the title [Propagators] Nullable [Propagators] Nullable annotations Aug 28, 2024
@CodeBlanch CodeBlanch merged commit ba8a0e4 into open-telemetry:main Aug 28, 2024
34 checks passed
@Kielek Kielek deleted the propagators-nullable branch August 28, 2024 18:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg:OpenTelemetry.Api Issues related to OpenTelemetry.Api NuGet package pkg:OpenTelemetry.Extensions.Propagators Issues related to OpenTelemetry.Extensions.Propagators NuGet package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants