-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[v3] Add Diagnostics Metrics Tracking to MAUI Applications #31058
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
Parent:
[main] Net10.0
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
19d95a0
Add diagnostics metrics tracking to Maui application
PureWeen 493df19
- change this to just init once
PureWeen 258c83e
fix for netstandard
PureWeen 5f10f83
Made a pluggable tagger
mattleibow c6a24cf
A bit overboard with services
mattleibow 1834618
no linq
mattleibow b5671f1
oops
mattleibow b8019a1
Docs
mattleibow 8b027be
Rename things a bit
mattleibow e35ff37
ws
mattleibow afef529
this is useful
mattleibow d0e92b2
Rename again
mattleibow 6b63f4f
Added docs and null checks
mattleibow 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
24 changes: 24 additions & 0 deletions
24
src/Controls/src/Core/Diagnostics/ControlsViewDiagnosticTagger.cs
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,24 @@ | ||
| using System.Diagnostics; | ||
| using Microsoft.Maui.Diagnostics; | ||
|
|
||
| namespace Microsoft.Maui.Controls.Diagnostics; | ||
|
|
||
| internal class ControlsViewDiagnosticTagger : IDiagnosticTagger | ||
| { | ||
| public void AddTags(object? source, ref TagList tagList) | ||
| { | ||
| if (source is Element element) | ||
| { | ||
| tagList.Add("element.id", element.Id); | ||
| tagList.Add("element.automation_id", element.AutomationId); | ||
| tagList.Add("element.class_id", element.ClassId); | ||
| tagList.Add("element.style_id", element.StyleId); | ||
| } | ||
|
|
||
| if (source is VisualElement ve) | ||
| { | ||
| tagList.Add("element.class", ve.@class); | ||
| tagList.Add("element.frame", ve.Frame); | ||
| } | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
src/Controls/src/Core/Diagnostics/MauiControlsDiagnosticsExtensions.cs
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,21 @@ | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.DependencyInjection.Extensions; | ||
| using Microsoft.Maui.Diagnostics; | ||
| using Microsoft.Maui.Hosting; | ||
|
|
||
| namespace Microsoft.Maui.Controls.Diagnostics; | ||
|
|
||
| internal static class MauiControlsDiagnosticsExtensions | ||
| { | ||
| public static MauiAppBuilder ConfigureMauiControlsDiagnostics(this MauiAppBuilder builder) | ||
| { | ||
| if (!RuntimeFeature.IsMeterSupported) | ||
| { | ||
| return builder; | ||
| } | ||
|
|
||
| builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IDiagnosticTagger, ControlsViewDiagnosticTagger>(_ => new ControlsViewDiagnosticTagger())); | ||
|
|
||
| return builder; | ||
| } | ||
| } |
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
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,28 @@ | ||
| using System; | ||
| using System.Diagnostics; | ||
|
|
||
| namespace Microsoft.Maui.Diagnostics; | ||
|
|
||
| /// <summary> | ||
| /// Defines a contract for recording diagnostic events for instrumentation purposes. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Implementations of this interface enable the collection and tracking of diagnostic information, | ||
| /// typically for logging, monitoring, or telemetry scenarios. | ||
| /// | ||
| /// Any activities should be started automatically in the constructor of the implementing class. All | ||
| /// started activities should be stopped and disposed of when the implementing class is disposed. | ||
| /// </remarks> | ||
| internal interface IDiagnosticInstrumentation : IDisposable | ||
| { | ||
| /// <summary> | ||
| /// Called when the instrumentation is stopped. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This method is called when the instrumentation is stopped, allowing for any final | ||
| /// metrics to be recorded or cleanup to be performed. | ||
| /// </remarks> | ||
| /// <param name="diag"></param> | ||
| /// <param name="tagList"></param> | ||
| void Stopped(MauiDiagnostics diag, in TagList tagList); | ||
| } |
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,15 @@ | ||
| using System.Diagnostics.Metrics; | ||
|
|
||
| namespace Microsoft.Maui.Diagnostics; | ||
|
|
||
| /// <summary> | ||
| /// Defines a contract for creating diagnostic metrics using a specified <see cref="System.Diagnostics.Metrics.Meter"/>. | ||
| /// </summary> | ||
| internal interface IDiagnosticMetrics | ||
| { | ||
| /// <summary> | ||
| /// Called when the <see cref="System.Diagnostics.Metrics.Meter"/> is created to initialize the metrics. | ||
| /// </summary> | ||
| /// <param name="meter">The <see cref="System.Diagnostics.Metrics.Meter"/> instance to use for creating metrics.</param> | ||
| void Create(Meter meter); | ||
| } |
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,16 @@ | ||
| using System.Diagnostics; | ||
|
|
||
| namespace Microsoft.Maui.Diagnostics; | ||
|
|
||
| /// <summary> | ||
| /// Defines a contract for adding diagnostic tags to a <see cref="TagList"/> based on a specific object. | ||
| /// </summary> | ||
| internal interface IDiagnosticTagger | ||
| { | ||
| /// <summary> | ||
| /// Called to add diagnostic tags to a <see cref="TagList"/> based on the provided source object. | ||
| /// </summary> | ||
| /// <param name="source">The object to use to generate tags.</param> | ||
| /// <param name="tagList">The <see cref="TagList"/> to add tags to.</param> | ||
| void AddTags(object? source, ref TagList tagList); | ||
| } | ||
14 changes: 14 additions & 0 deletions
14
src/Core/src/Diagnostics/Instrumentation/DiagnosticInstrumentation.cs
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,14 @@ | ||
| namespace Microsoft.Maui.Diagnostics; | ||
|
|
||
| internal static class DiagnosticInstrumentation | ||
| { | ||
| public static LayoutMeasureInstrumentation? StartLayoutMeasure(IView view) => | ||
| RuntimeFeature.IsMeterSupported | ||
| ? new LayoutMeasureInstrumentation(view) | ||
| : null; | ||
|
|
||
| public static LayoutArrangeInstrumentation? StartLayoutArrange(IView view) => | ||
| RuntimeFeature.IsMeterSupported | ||
| ? new LayoutArrangeInstrumentation(view) | ||
| : null; | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/Core/src/Diagnostics/Instrumentation/LayoutArrangeInstrumentation.cs
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,14 @@ | ||
| using System.Diagnostics; | ||
|
|
||
| namespace Microsoft.Maui.Diagnostics; | ||
|
|
||
| readonly struct LayoutArrangeInstrumentation(IView view) : IDiagnosticInstrumentation | ||
| { | ||
| readonly Activity? _activity = view.StartDiagnosticActivity("Arrange"); | ||
|
|
||
| public void Dispose() => | ||
| view.StopDiagnostics(_activity, this); | ||
|
|
||
| public void Stopped(MauiDiagnostics diag, in TagList tagList) => | ||
| diag.GetMetrics<LayoutDiagnosticMetrics>()?.RecordArrange(_activity?.Duration, in tagList); | ||
| } |
53 changes: 53 additions & 0 deletions
53
src/Core/src/Diagnostics/Instrumentation/LayoutDiagnosticMetrics.cs
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,53 @@ | ||
| using System; | ||
| using System.Diagnostics; | ||
| using System.Diagnostics.Metrics; | ||
|
|
||
| namespace Microsoft.Maui.Diagnostics; | ||
|
|
||
| internal class LayoutDiagnosticMetrics : IDiagnosticMetrics | ||
| { | ||
| public Counter<int>? MeasureCounter { get; private set; } | ||
|
|
||
| public Histogram<int>? MeasureHistogram { get; private set; } | ||
|
|
||
| internal Counter<int>? ArrangeCounter { get; private set; } | ||
|
|
||
| internal Histogram<int>? ArrangeHistogram { get; private set; } | ||
|
|
||
| public void Create(Meter meter) | ||
| { | ||
| MeasureCounter = meter.CreateCounter<int>("maui.layout.measure_count", "{times}", "The number of times a measure happened."); | ||
| MeasureHistogram = meter.CreateHistogram<int>("maui.layout.measure_duration", "ns"); | ||
|
|
||
| ArrangeCounter = meter.CreateCounter<int>("maui.layout.arrange_count", "{times}", "The number of times an arrange happened."); | ||
| ArrangeHistogram = meter.CreateHistogram<int>("maui.layout.arrange_duration", "ns"); | ||
| } | ||
|
|
||
| public void RecordMeasure(TimeSpan? duration, in TagList tagList) | ||
| { | ||
| MeasureCounter?.Add(1, tagList); | ||
|
|
||
| if (duration is not null) | ||
| { | ||
| #if NET9_0_OR_GREATER | ||
| MeasureHistogram?.Record((int)duration.Value.TotalNanoseconds, tagList); | ||
| #else | ||
| MeasureHistogram?.Record((int)(duration.Value.TotalMilliseconds * 1_000_000), tagList); | ||
| #endif | ||
| } | ||
| } | ||
|
|
||
| public void RecordArrange(TimeSpan? duration, in TagList tagList) | ||
| { | ||
| ArrangeCounter?.Add(1, tagList); | ||
|
|
||
| if (duration is not null) | ||
| { | ||
| #if NET9_0_OR_GREATER | ||
| ArrangeHistogram?.Record((int)duration.Value.TotalNanoseconds, tagList); | ||
| #else | ||
| ArrangeHistogram?.Record((int)(duration.Value.TotalMilliseconds * 1_000_000), tagList); | ||
| #endif | ||
| } | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/Core/src/Diagnostics/Instrumentation/LayoutMeasureInstrumentation.cs
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,14 @@ | ||
| using System.Diagnostics; | ||
|
|
||
| namespace Microsoft.Maui.Diagnostics; | ||
|
|
||
| readonly struct LayoutMeasureInstrumentation(IView view) : IDiagnosticInstrumentation | ||
| { | ||
| readonly Activity? _activity = view.StartDiagnosticActivity("Measure"); | ||
|
|
||
| public void Dispose() => | ||
| view.StopDiagnostics(_activity, this); | ||
|
|
||
| public void Stopped(MauiDiagnostics diag, in TagList tagList) => | ||
| diag.GetMetrics<LayoutDiagnosticMetrics>()?.RecordMeasure(_activity?.Duration, in tagList); | ||
| } |
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,60 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Diagnostics.Metrics; | ||
|
|
||
| namespace Microsoft.Maui.Diagnostics; | ||
|
|
||
| internal class MauiDiagnostics | ||
| { | ||
| const string DiagnosticsNamespace = "Microsoft.Maui"; | ||
| const string DiagnosticsVersion = "1.0.0"; | ||
|
|
||
| readonly IDiagnosticTagger[] _taggers; | ||
| readonly IDiagnosticMetrics[] _metrics; | ||
| readonly Dictionary<Type, IDiagnosticMetrics> _initializedMetrics = new(); | ||
|
|
||
| public MauiDiagnostics(IEnumerable<IDiagnosticMetrics> metrics, IEnumerable<IDiagnosticTagger> taggers, IMeterFactory? meterFactory = null) | ||
| { | ||
| _taggers = [.. taggers]; | ||
| _metrics = [.. metrics]; | ||
|
|
||
| ActivitySource = new ActivitySource(DiagnosticsNamespace, DiagnosticsVersion); | ||
|
|
||
| Meter = meterFactory?.Create(DiagnosticsNamespace, DiagnosticsVersion); | ||
|
|
||
| if (Meter is not null) | ||
| { | ||
| foreach (var metric in _metrics) | ||
| { | ||
| metric.Create(Meter); | ||
| _initializedMetrics[metric.GetType()] = metric; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| internal ActivitySource ActivitySource { get; } | ||
|
|
||
| internal Meter? Meter { get; } | ||
|
|
||
| internal void GetTags(object source, out TagList tagList) | ||
| { | ||
| tagList = new TagList(); | ||
|
|
||
| foreach (var tagger in _taggers) | ||
| { | ||
| tagger.AddTags(source, ref tagList); | ||
| } | ||
| } | ||
|
|
||
| internal T? GetMetrics<T>() | ||
| where T : IDiagnosticMetrics | ||
| { | ||
| if (!_initializedMetrics.TryGetValue(typeof(T), out var metrics)) | ||
| { | ||
| return default; | ||
| } | ||
|
|
||
| return (T)metrics; | ||
| } | ||
| } |
Oops, something went wrong.
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.