[Core] DiagnosticSource.Write
needs type to work with trimming
#37733
Labels
DiagnosticSource.Write
needs type to work with trimming
#37733
Context
In order to support The Open Telemetry Support for Native AOT as described here, we need to make the
DiagnosticScope
class compatible with trimming. This is a blocker for doing so.The internal constructor for DiagnosticScope takes in the parameter
object? diagnosticSourceArgs
, see L30:And passes it to the private ActivityAdapter class's constructor which sets it to a field
object? _diagnosticSourceArgs
, see L222:Issue
The issue is in L426 and L529:
Write() is not trim-friendly because it allows anything to be passed in as the payload. The way to get around this is to use a generic version of write() and preserve the type used for the payload (which is actually introduced in .NET 8+ but we can easily implement the same thing as a method). In addition, the type used in the type parameter needs to be annotated correctly so that the trimmer can preserve the right values.
Examples
There are examples of this here:
Additional Context
Since _diagnosticSourceArgs is of type object, we need to figure out some way to preserve the type being passed in.
The constructor for
DiagnosticScope
is only used in 2 places outside of tests, one in theDiagnosticScopeFactory
, and one inRequestActivityPolicy
.DiagnosticScopeFactory
passes null in for diagnosticSourceArgs:RequestActivityPolicy
passes inHttpMessage
Options
object?
toHttpMessage?
HttpMessage
and dismiss the warning. Document/Log heavily that if something besidesHttpMessage
is being passed in then things can get trimmed away (this is not ideal)Non-options
The text was updated successfully, but these errors were encountered: