-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
[System.Diagnostics.DiagnosticSource] Implement metrics advice API #102524
base: main
Are you sure you want to change the base?
Conversation
Note regarding the
|
...aries/System.Diagnostics.DiagnosticSource/ref/System.Diagnostics.DiagnosticSourceActivity.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Diagnostics.DiagnosticSource/src/Resources/Strings.resx
Outdated
Show resolved
Hide resolved
...raries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/HistogramAdvice.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Diagnostics.DiagnosticSource/tests/MetricsAdviceTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Diagnostics.DiagnosticSource/tests/MetricsTests.cs
Outdated
Show resolved
Hide resolved
...raries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/HistogramAdvice.cs
Outdated
Show resolved
Hide resolved
CC @noahfalk |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks @CodeBlanch for providing the implementation.
...raries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/HistogramAdvice.cs
Outdated
Show resolved
Hide resolved
...aries/System.Diagnostics.DiagnosticSource/ref/System.Diagnostics.DiagnosticSourceActivity.cs
Outdated
Show resolved
Hide resolved
...raries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/HistogramAdvice.cs
Outdated
Show resolved
Hide resolved
...raries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/HistogramAdvice.cs
Outdated
Show resolved
Hide resolved
...libraries/System.Diagnostics.DiagnosticSource/src/System.Diagnostics.DiagnosticSource.csproj
Outdated
Show resolved
Hide resolved
/// Contains configuration settings advised to be used by metrics consumers when recording measurements for a given <see cref="Instrument{T}"/>. | ||
/// </summary> | ||
/// <typeparam name="T">Instrument value type.</typeparam> | ||
public sealed class InstrumentAdvice<T> where T : struct |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need a parameter-less constructor here and inside it we need to call Instrument.ValidateTypeParameter<T>()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to add the constructor to the ref file too src\libraries\System.Diagnostics.DiagnosticSource\ref\System.Diagnostics.DiagnosticSourceActivity.cs
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't complain without it so maybe it is an implied thing? I went ahead and added it though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. the compiler automatically generates the parameter-less constructor. So, it implicitly existed in the ref too. It is good to explicitly list it as we add some code there.
CC @noahfalk if need to take one final look before we merge. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've still got concerns about the 'init' keyword :)
public IReadOnlyList<T>? HistogramBucketBoundaries | ||
{ | ||
get => _HistogramBucketBoundaries; | ||
init |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've still got concerns about 'init' due to the difficulty invoking it from .NET Framework while using the officially supported 7.3 C# language version. Maybe in the future we'll decide to drop support for .NET Framework but we've not agreed to that so far.
I maintain the suggestion to define this property with as 'set', not 'init' and if we want to protect against mutations then we can make a read-only copy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I humbly disagree. As I mentioned before, doing the allocation and copying is not good IMO especially we need to maintain that in the future too. We discussed that in the design review and didn't get any objection. Also, init
is already used in the same library too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code as-is already takes a copy of the data 😄 The issue I think set
creates is more that it can't be changed after the first publish of a metric. What OTel is going to do is on instrument published take the advice and construct the buckets after which point they are fixed. So having the set
could just be misleading for users thinking they can set it whenever. init
really conveys the meaning nicely for what this class is doing.
I guess we could have set
throw based on some internal state tracking if the thing was published? But then what if you hand the same advice class to multiple instruments? 🤔
/// Contains configuration settings advised to be used by metrics consumers when recording measurements for a given <see cref="Instrument{T}"/>. | ||
/// </summary> | ||
/// <typeparam name="T">Instrument value type.</typeparam> | ||
public sealed class InstrumentAdvice<T> where T : struct |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why must T be a value type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see, it's not that it needs to be a value type, it's that it must be just one of byte, short, int, long, double, float, or decimal. Do we use such a constraint in other places for the same purpose?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We support only specific set of types
Lines 141 to 149 in ced2117
internal static void ValidateTypeParameter<T>() | |
{ | |
Type type = typeof(T); | |
if (type != typeof(byte) && type != typeof(short) && type != typeof(int) && type != typeof(long) && | |
type != typeof(double) && type != typeof(float) && type != typeof(decimal)) | |
{ | |
throw new InvalidOperationException(SR.Format(SR.UnsupportedType, type)); | |
} | |
} |
The supported T
types in the advice class should exactly match the same supported types in Instrument<T>
. Metrics work only with numerical types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we use such a constraint in other places for the same purpose?
I am not aware of other places other than metrics. @tannergooding may know if there are other places that apply similar restrictions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we already use the same constraint for the same limited set of types elsewhere in metrics?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we already use the same constraint for the same limited set of types elsewhere in metrics?
Correct. For Instrument<T> we do the same type validation here:
https://github.com/dotnet/runtime/blob/main/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/Instrument.common.cs#L40
Fixes #63650
/cc @tarekgh @vishweshbankwar