Skip to content

Commit

Permalink
First iteration of transport disgnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
jpalac committed Sep 12, 2023
1 parent be5dbfa commit b6d98d0
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,89 @@ void Bar()

return Assert(AzureFunctionsDiagnostics.RouteReplyToAnyInstanceNotAllowedId, source);
}

[Test]
public Task DiagnosticIsReportedForMaxAutoLockRenewalDuration()
{
var source =
$@"using NServiceBus;
using System;
using System.Threading.Tasks;
class Foo
{{
void Bar(ServiceBusTriggeredEndpointConfiguration endpointConfig)
{{
[|endpointConfig.Transport.MaxAutoLockRenewalDuration|] = new System.TimeSpan(0, 0, 5, 0);
var transportConfig = endpointConfig.Transport;
[|transportConfig.MaxAutoLockRenewalDuration|] = new System.TimeSpan(0, 0, 5, 0);
}}
}}";

return Assert(AzureFunctionsDiagnostics.MaxAutoLockRenewalDurationNotAllowedId, source);
}

[Test]
public Task DiagnosticIsReportedForPrefetchCount()
{
var source =
$@"using NServiceBus;
using System;
using System.Threading.Tasks;
class Foo
{{
void Bar(ServiceBusTriggeredEndpointConfiguration endpointConfig)
{{
[|endpointConfig.Transport.PrefetchCount|] = 5;
var transportConfig = endpointConfig.Transport;
[|transportConfig.PrefetchCount|] = 5;
}}
}}";

return Assert(AzureFunctionsDiagnostics.PrefetchCountNotAllowedId, source);
}

[Test]
public Task DiagnosticIsReportedForPrefetchMultiplier()
{
var source =
$@"using NServiceBus;
using System;
using System.Threading.Tasks;
class Foo
{{
void Bar(ServiceBusTriggeredEndpointConfiguration endpointConfig)
{{
[|endpointConfig.Transport.PrefetchMultiplier|] = 5;
var transportConfig = endpointConfig.Transport;
[|transportConfig.PrefetchMultiplier|] = 5;
}}
}}";

return Assert(AzureFunctionsDiagnostics.PrefetchMultiplierNotAllowedId, source);
}

[Test]
public Task DiagnosticIsReportedForTimeToWaitBeforeTriggeringCircuitBreaker()
{
var source =
$@"using NServiceBus;
using System;
using System.Threading.Tasks;
class Foo
{{
void Bar(ServiceBusTriggeredEndpointConfiguration endpointConfig)
{{
[|endpointConfig.Transport.TimeToWaitBeforeTriggeringCircuitBreaker|] = new System.TimeSpan(0, 0, 5, 0);
var transportConfig = endpointConfig.Transport;
[|transportConfig.TimeToWaitBeforeTriggeringCircuitBreaker|] = new System.TimeSpan(0, 0, 5, 0);
}}
}}";

return Assert(AzureFunctionsDiagnostics.TimeToWaitBeforeTriggeringCircuitBreakerNotAllowedId, source);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ public class AzureFunctionsConfigurationAnalyzer : DiagnosticAnalyzer
AzureFunctionsDiagnostics.OverrideLocalAddressNotAllowed,
AzureFunctionsDiagnostics.RouteReplyToThisInstanceNotAllowed,
AzureFunctionsDiagnostics.RouteToThisInstanceNotAllowed,
AzureFunctionsDiagnostics.RouteReplyToAnyInstanceNotAllowed
AzureFunctionsDiagnostics.RouteReplyToAnyInstanceNotAllowed,
AzureFunctionsDiagnostics.MaxAutoLockRenewalDurationNotAllowed,
AzureFunctionsDiagnostics.PrefetchCountNotAllowed,
AzureFunctionsDiagnostics.PrefetchMultiplierNotAllowed,
AzureFunctionsDiagnostics.TimeToWaitBeforeTriggeringCircuitBreakerNotAllowed
);

static readonly Dictionary<string, DiagnosticDescriptor> NotAllowedEndpointConfigurationMethods
Expand All @@ -44,11 +48,21 @@ static readonly Dictionary<string, DiagnosticDescriptor> NotAllowedSendAndReplyO
["RouteReplyToAnyInstance"] = AzureFunctionsDiagnostics.RouteReplyToAnyInstanceNotAllowed
};

static readonly Dictionary<string, DiagnosticDescriptor> NotAllowedTransportSettings
= new Dictionary<string, DiagnosticDescriptor>
{
["MaxAutoLockRenewalDuration"] = AzureFunctionsDiagnostics.MaxAutoLockRenewalDurationNotAllowed,
["PrefetchCount"] = AzureFunctionsDiagnostics.PrefetchCountNotAllowed,
["PrefetchMultiplier"] = AzureFunctionsDiagnostics.PrefetchMultiplierNotAllowed,
["TimeToWaitBeforeTriggeringCircuitBreaker"] = AzureFunctionsDiagnostics.TimeToWaitBeforeTriggeringCircuitBreakerNotAllowed
};

public override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.RegisterSyntaxNodeAction(Analyze, SyntaxKind.InvocationExpression);
context.RegisterSyntaxNodeAction(AnalyzeTransport, SyntaxKind.SimpleMemberAccessExpression);
}

static void Analyze(SyntaxNodeAnalysisContext context)
Expand All @@ -68,6 +82,33 @@ static void Analyze(SyntaxNodeAnalysisContext context)
AnalyzeSendAndReplyOptions(context, invocationExpression, memberAccessExpression);
}

static void AnalyzeTransport(SyntaxNodeAnalysisContext context)
{
if (!(context.Node is MemberAccessExpressionSyntax memberAccess))
{
return;
}

if (!NotAllowedTransportSettings.TryGetValue(memberAccess.Name.ToString(), out var diagnosticDescriptor))
{
return;

}

var memberAccessSymbol = context.SemanticModel.GetSymbolInfo(memberAccess, context.CancellationToken);

if (!(memberAccessSymbol.Symbol is IPropertySymbol propertySymbol))
{
return;
}

if (propertySymbol.ContainingType.ToString() == "NServiceBus.AzureServiceBusTransport")
{
context.ReportDiagnostic(diagnosticDescriptor, memberAccess);

}
}

static void AnalyzeEndpointConfiguration(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocationExpression, MemberAccessExpressionSyntax memberAccessExpression)
{
if (!NotAllowedEndpointConfigurationMethods.TryGetValue(memberAccessExpression.Name.Identifier.Text, out var diagnosticDescriptor))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public static class AzureFunctionsDiagnostics
public const string RouteToThisInstanceNotAllowedId = "NSBFUNC011";
public const string RouteReplyToAnyInstanceNotAllowedId = "NSBFUNC012";

public const string MaxAutoLockRenewalDurationNotAllowedId = "NSBFUNC013";
public const string PrefetchCountNotAllowedId = "NSBFUNC014";
public const string PrefetchMultiplierNotAllowedId = "NSBFUNC015";
public const string TimeToWaitBeforeTriggeringCircuitBreakerNotAllowedId = "NSBFUNC016";

const string DiagnosticCategory = "NServiceBus.AzureFunctions";

internal static readonly DiagnosticDescriptor PurgeOnStartupNotAllowed = new DiagnosticDescriptor(
Expand Down Expand Up @@ -106,5 +111,41 @@ public static class AzureFunctionsDiagnostics
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true
);

internal static readonly DiagnosticDescriptor MaxAutoLockRenewalDurationNotAllowed = new DiagnosticDescriptor(
id: MaxAutoLockRenewalDurationNotAllowedId,
title: "MaxAutoLockRenewalDuration is not supported in Azure Functions",
messageFormat: "Azure Functions endpoints do not control the message receiver and cannot decide the lock renewal duration.",
category: DiagnosticCategory,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true
);

internal static readonly DiagnosticDescriptor PrefetchCountNotAllowed = new DiagnosticDescriptor(
id: PrefetchCountNotAllowedId,
title: "PrefetchCount is not supported in Azure Functions",
messageFormat: "Azure Functions endpoints do not control the message receiver and cannot decide the prefetch count.",
category: DiagnosticCategory,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true
);

internal static readonly DiagnosticDescriptor PrefetchMultiplierNotAllowed = new DiagnosticDescriptor(
id: PrefetchMultiplierNotAllowedId,
title: "PrefetchMultiplier is not supported in Azure Functions",
messageFormat: "Azure Functions endpoints do not control the message receiver and cannot decide the prefetch multiplier.",
category: DiagnosticCategory,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true
);

internal static readonly DiagnosticDescriptor TimeToWaitBeforeTriggeringCircuitBreakerNotAllowed = new DiagnosticDescriptor(
id: TimeToWaitBeforeTriggeringCircuitBreakerNotAllowedId,
title: "TimeToWaitBeforeTriggeringCircuitBreaker is not supported in Azure Functions",
messageFormat: "Azure Functions endpoints do not control the message receiver and cannot access circuit breaker settings.",
category: DiagnosticCategory,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true
);
}
}

0 comments on commit b6d98d0

Please sign in to comment.