Skip to content

Enable MetricsSupport by default for non-Release builds #9928

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

Merged
merged 3 commits into from
Mar 20, 2025

Conversation

jfversluis
Copy link
Member

@jfversluis jfversluis commented Mar 17, 2025

Primarily to light up the metrics when using .NET MAUI apps with .NET Aspire, but this will emit metrics for potential other purposes as well.

With this change the MetricsSupport property will only be set to false when the property has no value and the build is not a Release build.

That means, if the property is not explicitly set in the .NET MAUI project (or .NET for Android project) and the build is not a Release build, the property will be set to true (the default value) and emit metrics.

cc: @rolfbjarne I was informed you were going to disable this for iOS, so it might be good to keep this in mind as well for that!

@rolfbjarne
Copy link
Member

Thanks for the heads-up, I've changed our logic to be identical to this (dotnet/macios#22389).

@dellis1972
Copy link
Contributor

@jfversluis sorry silly question, but what does the MetricsSupport actually do? Do you have any docs I can look at?

@jonpryor
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jonpryor
Copy link
Contributor

SystemTests.AppContextTests.TestPrivateSwitches("System.Diagnostics.Metrics.Meter, System.Diagnostics.DiagnosticSource") fails:

static readonly object [] TestPrivateSwitchesSource = new object [] {
new object [] {
/* className */ "System.LocalAppContextSwitches, System.Private.CoreLib",
/* propertyName */ "ForceInterpretedInvoke",
/* expected */ true,
},
new object [] {
/* className */ "System.Diagnostics.Metrics.Meter, System.Diagnostics.DiagnosticSource",
/* propertyName */ "<IsSupported>k__BackingField",
/* expected */ false,
},
};
[Test]
[TestCaseSource (nameof (TestPrivateSwitchesSource))]
public void TestPrivateSwitches (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)]
string className,
string propertyName,
object expected)
{
var type = Type.GetType (className, throwOnError: true);
var members = type.GetMember (propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
Assert.AreEqual (1, members.Length);
if (members [0] is PropertyInfo property) {
Assert.AreEqual (expected, property.GetValue (null));
} else if (members [0] is FieldInfo field) {
Assert.AreEqual (expected, field.GetValue (null));
} else {
Assert.Fail($"Unknown member type: {members [0]}");
}
}

because in Debug config builds, the <IsSupported>k__BackingField field exists.

We'll need to update the test to not assert the non-existence of this field in Debug builds.

@jonpryor jonpryor self-requested a review as a code owner March 18, 2025 18:50
@jonpryor
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

rolfbjarne added a commit to dotnet/macios that referenced this pull request Mar 19, 2025
@jonpryor
Copy link
Contributor

jonpryor commented Mar 19, 2025

Draft commit message:

[Xamarin.Android.Build.Tasks] $(MetricsSupport)=false in "Release" (#9928)

Context: eb6397d4a9dcef69e1d012fd52fbb9688557b0b4
Context: 200f843c819a634b3c7921738de4d726c8e675c7
Context: https://github.com/dotnet/runtime/issues/89880
Context: https://github.com/dotnet/aspire/issues/4684
Context: https://github.com/dotnet/maui/pull/24365
Context: https://github.com/jfversluis/MauiAspire

.NET 8 introduced use of System.Diagnostics.Metrics into `HttpClient`,
which immediately slowed down .NET for Android app startup times
(dotnet/runtime#89880).  This was "fixed" by *disabling* use of Metrics
within .NET for Android apps *by default* (eb6397d4, 200f843c).
Support for Metrics could be re-enabled by setting the
[`$(MetricsSupport)` MSBuild property][0].

.NET Aspire also uses System.Diagnostics.Metrics, and we'd like to
allow Metrics to be available by default in that environment; see also
dotnet/aspire#4684 and dotnet/maui#24365.

Update the `$(MetricsSupport)` MSBuild property to be false by default
only in "Release" configuration builds.

[0]: https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options

@jfversluis: is there anything useful we should link to or mention wrt .NET MAUI, .NET Aspire, and System.Diagnostics.Metrics?

@@ -125,7 +125,7 @@
<JsonSerializerIsReflectionEnabledByDefault Condition="'$(JsonSerializerIsReflectionEnabledByDefault)' == '' and '$(TrimMode)' == 'partial'">true</JsonSerializerIsReflectionEnabledByDefault>
<!-- Set to disable throwing behavior, see: https://github.com/dotnet/runtime/issues/109724 -->
<_DefaultValueAttributeSupport Condition="'$(_DefaultValueAttributeSupport)' == '' and '$(TrimMode)' == 'partial'">true</_DefaultValueAttributeSupport>
<MetricsSupport Condition="'$(MetricsSupport)' == ''">false</MetricsSupport>
<MetricsSupport Condition="'$(MetricsSupport)' == '' and '$(Configuration)' == 'Release'">false</MetricsSupport>
Copy link
Contributor

Choose a reason for hiding this comment

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

@dellis1972: What are your thoughts on using $(Configuration) in this manner? I think historically we've tried to avoid this, because it's not uncommon for people to "invent" new configuration names. Is there some other property we should use instead, perhaps '$(DebugSymbols)' == 'True'?

@jonpryor
Copy link
Contributor

@jfversluis: I imagine dotnet/aspire#4684 is a relevant link?

@jonpryor
Copy link
Contributor

wrt merging this, I'm not sold on using '$(Configuration)'=='Release'. We should get this PR and #9933 on the same semantic page; see also:

@jonpryor
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jonpryor jonpryor merged commit 8a1acdd into main Mar 20, 2025
57 of 59 checks passed
@jonpryor jonpryor deleted the jfversluis-patch-1 branch March 20, 2025 18:08
rolfbjarne added a commit to dotnet/macios that referenced this pull request Mar 21, 2025
Disable Metrics support when `Optimize=true` (instead of when `Release=true`) to avoid depending on a configuration.

This makes our logic identical to Android's logic: dotnet/android#9928.
rolfbjarne added a commit to dotnet/macios that referenced this pull request Mar 25, 2025
Disable Metrics support when `Optimize=true` (instead of when `Release=true`) to avoid depending on a configuration.

This makes our logic identical to Android's logic: dotnet/android#9928.
@github-actions github-actions bot locked and limited conversation to collaborators Apr 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants