-
-
Notifications
You must be signed in to change notification settings - Fork 230
feat(metrics): Trace-connected Metrics (Analyzers) #4840
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
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
6be1e01
feat(metrics): Trace-connected Metrics (Implementation)
Flash0ver e462457
Merge branch 'main' into feat/trace-connected-metrics-implementation
Flash0ver 6c5ca23
add CHANGELOG entry
Flash0ver 9485ca4
more public overloads for convenience
Flash0ver aa46acb
more public overloads for convenience (API Approval Tests)
Flash0ver e9c349e
release: 6.1.0-alpha.1
getsentry-bot 484f17f
Merge branch 'release/6.1.0-alpha.1' into feat/trace-connected-metrics
e65db5f
rename APIs, validate parameters, fix attributes
Flash0ver de065c7
feat(metrics): Trace-connected Metrics (Analyzers)
Flash0ver 5228fbe
feat(metrics): add SentryMetricUnits class for supported units
Flash0ver 32caf06
Update SentryUnits.cs
Flash0ver 98991a3
Merge branch 'feat/trace-connected-metrics' into feat/trace-connected…
Flash0ver d730432
update API Approval Tests
Flash0ver e7bb36f
Merge branch 'feat/trace-connected-metrics' into feat/trace-connected…
Flash0ver d2a02e1
ref: align public compiler extensions with internal analyzer project
Flash0ver b87bc4d
feat: also analyze SetBeforeSendMetric invocation
Flash0ver 4b4bc03
Merge branch 'main' into feat/trace-connected-metrics-analyzers
Flash0ver 68a2c7a
merge: fix
Flash0ver 9855d05
update Analyzer to released API shape
Flash0ver 70a58d0
update Polyfill
Flash0ver b90d71e
Merge branch 'main' into feat/trace-connected-metrics-analyzers
Flash0ver f168d5c
Merge branch 'main' into feat/trace-connected-metrics-analyzers
Flash0ver 8af37bb
docs: Add CHANGELOG entry
Flash0ver 5d4128d
feat: change Diagnostic Category to Sentry
Flash0ver eb69de6
fix: RCS1187: Use constant instead of field
Flash0ver e8b9376
style: use target-typed new expression
Flash0ver a613c90
Merge branch 'main' into feat/trace-connected-metrics-analyzers
Flash0ver 04745db
docs: Specify XML comment
Flash0ver 3912ee5
chore: downgrade Polyfill
Flash0ver 7087e72
ref!: remove Experimental from Metrics API
Flash0ver 67edfd2
Merge branch 'ref/metrics-stable-api' into feat/trace-connected-metri…
Flash0ver c19c784
test: remove Experimental for invocations of Metrics methods
Flash0ver bd6e462
docs: remove CHANGELOG entry
Flash0ver 9330572
Merge branch 'main' into feat/trace-connected-metrics-analyzers
Flash0ver 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ; Shipped analyzer releases | ||
| ; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md | ||
|
|
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,8 @@ | ||
| ; Unshipped analyzer release | ||
| ; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md | ||
|
|
||
| ### New Rules | ||
|
|
||
| Rule ID | Category | Severity | Notes | ||
| --------|----------|----------|------- | ||
| SENTRY1001 | Sentry | Warning | TraceConnectedMetricsAnalyzer |
113 changes: 113 additions & 0 deletions
113
src/Sentry.Compiler.Extensions/Analyzers/TraceConnectedMetricsAnalyzer.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,113 @@ | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
| using Microsoft.CodeAnalysis.Operations; | ||
|
|
||
| namespace Sentry.Compiler.Extensions.Analyzers; | ||
|
|
||
| /// <summary> | ||
| /// Guide callers to use the public API of <see href="https://develop.sentry.dev/sdk/telemetry/metrics/">Sentry Trace-connected Metrics</see> correctly. | ||
| /// </summary> | ||
| [DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
| public sealed class TraceConnectedMetricsAnalyzer : DiagnosticAnalyzer | ||
| { | ||
| private const string Title = "Unsupported numeric type of Metric"; | ||
| private const string MessageFormat = "{0} is unsupported type for Sentry Metrics. The only supported types are byte, short, int, long, float, and double."; | ||
| private const string Description = "Integers should be a 64-bit signed integer, while doubles should be a 64-bit floating point number."; | ||
|
|
||
| private static readonly DiagnosticDescriptor Rule = new( | ||
| id: DiagnosticIds.Sentry1001, | ||
jamescrosswell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| title: Title, | ||
| messageFormat: MessageFormat, | ||
| category: DiagnosticCategories.Sentry, | ||
| defaultSeverity: DiagnosticSeverity.Warning, | ||
| isEnabledByDefault: true, | ||
| description: Description, | ||
| helpLinkUri: null | ||
jamescrosswell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
|
|
||
| /// <inheritdoc /> | ||
| public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule); | ||
|
|
||
| /// <inheritdoc /> | ||
| public override void Initialize(AnalysisContext context) | ||
| { | ||
| context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
| context.EnableConcurrentExecution(); | ||
|
|
||
| context.RegisterOperationAction(Execute, OperationKind.Invocation); | ||
| } | ||
|
|
||
| private static void Execute(OperationAnalysisContext context) | ||
| { | ||
| Debug.Assert(context.Operation.Language == LanguageNames.CSharp); | ||
| Debug.Assert(context.Operation.Kind is OperationKind.Invocation); | ||
|
|
||
| context.CancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| if (context.Operation is not IInvocationOperation invocation) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var method = invocation.TargetMethod; | ||
| if (method.DeclaredAccessibility != Accessibility.Public || method.IsStatic || method.Parameters.Length == 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (!method.IsGenericMethod || method.Arity != 1 || method.TypeArguments.Length != 1) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (method.ContainingAssembly is null || method.ContainingAssembly.Name != "Sentry") | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (method.ContainingNamespace is null || method.ContainingNamespace.Name != "Sentry") | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| string fullyQualifiedMetadataName; | ||
| if (method.Name is "EmitCounter" or "EmitGauge" or "EmitDistribution") | ||
| { | ||
| fullyQualifiedMetadataName = "Sentry.SentryMetricEmitter"; | ||
| } | ||
| else if (method.Name is "TryGetValue") | ||
| { | ||
| fullyQualifiedMetadataName = "Sentry.SentryMetric"; | ||
| } | ||
| else | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var typeArgument = method.TypeArguments[0]; | ||
| if (typeArgument.SpecialType is SpecialType.System_Byte or SpecialType.System_Int16 or SpecialType.System_Int32 or SpecialType.System_Int64 or SpecialType.System_Single or SpecialType.System_Double) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (typeArgument is ITypeParameterSymbol) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var sentryType = context.Compilation.GetTypeByMetadataName(fullyQualifiedMetadataName); | ||
| if (sentryType is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (!SymbolEqualityComparer.Default.Equals(method.ContainingType, sentryType)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var location = invocation.Syntax.GetLocation(); | ||
| var diagnostic = Diagnostic.Create(Rule, location, typeArgument.ToDisplayString(SymbolDisplayFormats.FullNameFormat)); | ||
| context.ReportDiagnostic(diagnostic); | ||
| } | ||
| } | ||
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,6 @@ | ||
| namespace Sentry.Compiler.Extensions; | ||
|
|
||
| internal static class DiagnosticCategories | ||
| { | ||
| internal const string Sentry = nameof(Sentry); | ||
Flash0ver marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
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,6 @@ | ||
| namespace Sentry.Compiler.Extensions; | ||
|
|
||
| internal static class DiagnosticIds | ||
| { | ||
| internal const string Sentry1001 = "SENTRY1001"; | ||
| } |
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,12 @@ | ||
| using Microsoft.CodeAnalysis; | ||
|
|
||
| namespace Sentry.Compiler.Extensions; | ||
|
|
||
| internal static class SymbolDisplayFormats | ||
| { | ||
| internal static SymbolDisplayFormat FullNameFormat { get; } = new( | ||
| globalNamespaceStyle: SymbolDisplayGlobalNamespaceStyle.Omitted, | ||
| typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces, | ||
| genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters | ||
| ); | ||
| } |
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.