-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[v2] Add Diagnostics Metrics Tracking to MAUI Applications #31049
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
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| using System; | ||
| using System.Diagnostics; | ||
| using Microsoft.Maui.Diagnostics; | ||
|
|
||
| namespace Microsoft.Maui.Controls.Diagnostics; | ||
|
|
||
| enum DiagnosticsMeasuring | ||
| { | ||
| None, | ||
| Measure, | ||
| Arrange | ||
| } | ||
|
|
||
| readonly struct MetricsTracker : IDisposable | ||
| { | ||
| readonly Activity? _activity; | ||
| readonly IView _view; | ||
| readonly DiagnosticsMeasuring _activityName; | ||
|
|
||
| public static MetricsTracker? Create(IView view, DiagnosticsMeasuring diagnosticsMeasuring) | ||
| { | ||
| if (RuntimeFeature.IsMeterSupported) | ||
| return new MetricsTracker(view, diagnosticsMeasuring); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| MetricsTracker(IView view, DiagnosticsMeasuring diagnosticsMeasuring) | ||
| { | ||
| _view = view; | ||
| _activity = view.StartActivity($"{diagnosticsMeasuring}"); | ||
| _activityName = diagnosticsMeasuring; | ||
|
|
||
| if (_activity is null) | ||
| return; | ||
|
|
||
| if (view is Element element) | ||
| { | ||
| _activity.SetTag("element.id", element.Id); | ||
| _activity.SetTag("element.automation_id", element.AutomationId); | ||
| _activity.SetTag("element.class_id", element.ClassId); | ||
| _activity.SetTag("element.style_id", element.StyleId); | ||
| } | ||
|
|
||
| if (view is VisualElement ve) | ||
| { | ||
| _activity.SetTag("element.class", ve.@class); | ||
| _activity.SetTag("element.frame", ve.Frame); | ||
| } | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| _activity?.Stop(); | ||
|
|
||
| switch (_activityName) | ||
| { | ||
| case DiagnosticsMeasuring.Arrange: | ||
| _view.RecordArrange(_activity?.Duration); | ||
| break; | ||
| case DiagnosticsMeasuring.Measure: | ||
| _view.RecordMeasure(_activity?.Duration); | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
|
|
||
| _activity?.Dispose(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| using System; | ||
| using System.Diagnostics; | ||
| using System.Diagnostics.Metrics; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Maui.Hosting; | ||
|
|
||
| namespace Microsoft.Maui.Diagnostics; | ||
|
|
||
| internal class MauiDiagnostics | ||
| { | ||
| public MauiDiagnostics(IMeterFactory? meterFactory = null) | ||
| { | ||
| ActivitySource = new ActivitySource("Microsoft.Maui.Diagnostics", "1.0.0"); | ||
|
|
||
| Meters = meterFactory?.Create("Microsoft.Maui.Diagnostics", "1.0.0"); | ||
|
|
||
| MeasureCounter = Meters?.CreateCounter<int>("maui.layout.measure_count", "{times}", "The number of times a measure happened."); | ||
| ArrangeCounter = Meters?.CreateCounter<int>("maui.layout.arrange_count", "{times}", "The number of times an arrange happened."); | ||
|
|
||
| MeasureHistogram = Meters?.CreateHistogram<int>("maui.layout.measure_duration", "ns"); | ||
| ArrangeHistogram = Meters?.CreateHistogram<int>("maui.layout.arrange_duration", "ns"); | ||
PureWeen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public ActivitySource ActivitySource { get; } | ||
| public Meter? Meters { get; } | ||
| public Counter<int>? MeasureCounter { get; } | ||
| public Counter<int>? ArrangeCounter { get; } | ||
| public Histogram<int>? MeasureHistogram { get; } | ||
| public Histogram<int>? ArrangeHistogram { get; } | ||
| } | ||
|
|
||
| internal static class MauiDiagnosticsExtensions | ||
| { | ||
| public static MauiAppBuilder ConfigureMauiDiagnostics(this MauiAppBuilder builder) | ||
| { | ||
| if (RuntimeFeature.IsMeterSupported) | ||
| { | ||
| builder.Services.AddSingleton(serviceProvider => new MauiDiagnostics(serviceProvider.GetService<IMeterFactory>())); | ||
| } | ||
|
|
||
| return builder; | ||
| } | ||
|
|
||
| static MauiDiagnostics? GetMauiDiagnostics(this IView view) | ||
| { | ||
| if (!RuntimeFeature.IsMeterSupported) | ||
| return null; | ||
|
|
||
| return view.Handler?.MauiContext?.Services.GetService<MauiDiagnostics>(); | ||
| } | ||
|
|
||
| public static Activity? StartActivity(this IView view, string name) | ||
| { | ||
| if (!RuntimeFeature.IsMeterSupported) | ||
| return null; | ||
|
|
||
| var elementName = view.GetType().Name; | ||
|
|
||
| var activity = view.GetMauiDiagnostics()?.ActivitySource.StartActivity($"{name} {elementName}"); | ||
|
|
||
| activity?.SetTag("element.type", view.GetType().FullName); | ||
|
|
||
| return activity; | ||
| } | ||
|
|
||
| public static void RecordMeasure(this IView view, TimeSpan? duration) | ||
| { | ||
| if (!RuntimeFeature.IsMeterSupported) | ||
| return; | ||
|
|
||
| var diag = view.GetMauiDiagnostics(); | ||
| diag?.MeasureCounter?.Add(1); | ||
|
|
||
| if (duration is not null) | ||
| diag?.MeasureHistogram?.Record((int)duration.Value.TotalNanoseconds); | ||
| } | ||
|
|
||
| public static void RecordArrange(this IView view, TimeSpan? duration) | ||
| { | ||
| if (!RuntimeFeature.IsMeterSupported) | ||
| return; | ||
|
|
||
| var diag = view.GetMauiDiagnostics(); | ||
| diag?.ArrangeCounter?.Add(1); | ||
|
|
||
| if (duration is not null) | ||
| diag?.ArrangeHistogram?.Record((int)duration.Value.TotalNanoseconds); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.