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

Remove ConfigureFromConfigurationOptions<T> from Sentry.Extensions.Logging #2823

Merged
merged 11 commits into from
Nov 16, 2023

Conversation

jamescrosswell
Copy link
Collaborator

@jamescrosswell jamescrosswell commented Nov 13, 2023

#skip-changelog

This change shouldn't impact SDK users at all.

Configure the new source generators

Adding the new source generators is simple enough - we can put something like the following in the respective *.csproj files (simplified somewhat):

  <PropertyGroup Condition="'$(FrameworkSupportsAot)' == 'true'">
    <IsAotCompatible>true</IsAotCompatible>
    <EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
  </PropertyGroup>

  ...

  <ItemGroup Condition="$(IfItsNotAlreadyIncludedAndFrameworkSupportsAot)">
    <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0-rc.2.23479.6" />
  </ItemGroup>

Remove ConfigureFromConfigurationOptions classes

We then need to remove the ConfigureFromConfigurationOptions<TOptions> classes we're using:

internal class SentryLoggingOptionsSetup : ConfigureFromConfigurationOptions<SentryLoggingOptions>
{
public SentryLoggingOptionsSetup(
ILoggerProviderConfiguration<SentryLoggerProvider> providerConfiguration)
: base(providerConfiguration.Configuration)
{ }
}

These aren't actually doing much. The SentryLoggingOptionsSetup here gets an ILoggerProviderConfiguration via dependency injection and uses that for the configuration binding. We can do the same with:

internal class SentryLoggingOptionsSetup : IConfigureOptions<SentryLoggingOptions>
{
private readonly IConfiguration _config;
public SentryLoggingOptionsSetup(ILoggerProviderConfiguration<SentryLoggerProvider> config)
{
ArgumentNullException.ThrowIfNull(config);
_config = config.Configuration;
}
public virtual void Configure(SentryLoggingOptions options)
{
ArgumentNullException.ThrowIfNull(options);
_config.Bind(options);
}
}

At that point however, we get a bunch of SYSLIB1100 and SYSLIB1101 errors, because the configuration binding source generators don't know how to handle lots of the members of the SentryOptions class. It struggles with custom types like SubstringOrRegex, it fails completely with members of type Func or Action and even then, it creates code with a bunch of nullability warnings. In the technical spikes we did, we couldn't overcome any of these when binding to the SentryOptions type.

Solution

The workaround that is used in this PR is to create a much simpler BindableSentryOptions class that won't trip up the configuration binding source generators . This class uses nullable primitives like string? instead of SubstringOrRegexPattern and then takes care of any conversions from those string values to the more complex types we actually use, when applying that BindableSentryOptions type to the actual SentryOptions.

internal class SentryLoggingOptionsSetup : IConfigureOptions<SentryLoggingOptions>
{
private readonly IConfiguration _config;
public SentryLoggingOptionsSetup(ILoggerProviderConfiguration<SentryLoggerProvider> config)
{
ArgumentNullException.ThrowIfNull(config);
_config = config.Configuration;
}
public void Configure(SentryLoggingOptions options)
{
ArgumentNullException.ThrowIfNull(options);
var bindable = new BindableSentryLoggingOptions();
_config.Bind(bindable);
bindable.ApplyTo(options);
}
}

For posterity

Some discussions kicked off in the dotnet/runtime repo:

Issues that are relevant to alternative potential solutions in the future:

@jamescrosswell jamescrosswell requested a review from vaind November 13, 2023 04:58
@jamescrosswell jamescrosswell linked an issue Nov 13, 2023 that may be closed by this pull request
@jamescrosswell jamescrosswell changed the base branch from main to feat/4.0.0 November 13, 2023 05:01
@jamescrosswell jamescrosswell marked this pull request as ready for review November 14, 2023 08:49
@jamescrosswell jamescrosswell marked this pull request as draft November 14, 2023 08:50
@jamescrosswell jamescrosswell changed the title WIP: Config from config options Remove ConfigureFromConfigurationOptions<T> from Sentry.Extensions.Logging Nov 14, 2023
@jamescrosswell jamescrosswell marked this pull request as ready for review November 14, 2023 09:10
Directory.Build.props Outdated Show resolved Hide resolved
Copy link
Contributor

@bitsandfoxes bitsandfoxes left a comment

Choose a reason for hiding this comment

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

I'm fine with having this since it's not user-facing or breaking and unblocks us from moving forward. But we really need to figure out a way to source-generate the BindingOptions once we can confirm this as the working solution.

@jamescrosswell jamescrosswell merged commit b84faa8 into feat/4.0.0 Nov 16, 2023
17 checks passed
@jamescrosswell jamescrosswell deleted the config-from-config-options branch November 16, 2023 02:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove ConfigureFromConfigurationOptions<T> from Sentry.Extensions.Logging
2 participants