Skip to content
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

Trace notifications #2604

Merged
merged 10 commits into from
Jan 13, 2023
Merged

Trace notifications #2604

merged 10 commits into from
Jan 13, 2023

Conversation

michaelcfanning
Copy link
Member

No description provided.

/// <param name="associatedRule">
/// The scan rule, if any, associated with the notification.
/// </param>
void LogToolNotification(Notification notification, ReportingDescriptor associatedRule = null);
Copy link
Member Author

@michaelcfanning michaelcfanning Jan 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

associatedRule = null

An optional argument in an interface contract is an interesting thing. Any caller to the interface does not need to provide this argument. Anyone implementing this interface can override the optional value.

That can get confusing. I made this change to minimize code churn, so that callers who don't have an associated rule aren't strictly required to provide one.

@@ -305,6 +305,11 @@ private ISet<string> ValidateTargetsExist(TContext context, ISet<string> targets
Policy = policy ?? new PropertiesDictionary()
};

context.Traces =
Copy link
Member Author

@michaelcfanning michaelcfanning Jan 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

context

This handling can now be removed from upstream driver tools that are currently required to do the same thing.

In point of fact, I didn't need to add this change as we're not keeping analyze command up to date with all features, it will get removed shortly.

@michaelcfanning michaelcfanning marked this pull request as ready for review January 12, 2023 17:53
@@ -479,6 +479,16 @@ public void LogToolNotification(Notification notification)
_run.Invocations[0].ExecutionSuccessful &= notification.Level != FailureLevel.Error;

CaptureFilesInNotification(notification);

if (associatedRule != null)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

associatedRule

With this change, the rule metadata for any notification associated with a rule will be persisted to the rules table in the log.

@@ -1041,7 +1056,31 @@ public static void AnalyzeTargetHelper(TContext context, IEnumerable<Skimmer<TCo

try
{
Stopwatch stopwatch = context.Traces.HasFlag(DefaultTraces.RuleScanTime)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stopwatch

Shaopeng, here's a clean pattern for producing timing data on a per-rule + target file basis. We can also consider adding memory consumption information in a similar change, if helpful.

@@ -663,11 +663,100 @@ public void AnalyzeCommandBase_ReportsWarningOnUnsupportedPlatformForRuleAndNoRu
return run;
}

[Fact]
public void AnalyzeCommand_Traces()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AnalyzeCommand_Traces

Good things about this test: there are minimal literal constant 'magic values', it runs a comprehensive set of tests and will report on all break (instead of failing on the first one), it nicely buckets all trace testing in one place and can easily be extended. This change also makes some nice incremental improvements to the general test methodology, for example, returning the Run object from the test helper to assist in validation.

int executionNotificationsCount = 0;
int configurationNotificationCount = 0;

SarifHelpers.ValidateRun(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ValidateRun

Note that this validation will halt the test early if it fails. That's ok. If this fails, then the follow-on validation is unlikely to succeed, because we already know that we don't have the expected results + notifications data generated.


if (executionNotifications?.Where(t => t.Message.Text.Contains("elapsed")).Count() != expectedNotificationsCount)
{
sb.AppendLine($"\t{trace} : did not observe term 'elapsed' in rule timing notifications.");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\t

Notice how this code indents individual reporting lines to maximize readability in the VS test window.

}
}
}
sb.Length.Should().Be(0, $"test cases failed : {Environment.NewLine}{sb}");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$"test cases failed : {Environment.NewLine}{sb}"

notice this construct which maximizes readability in the VS test window. this reporting subtlety should be captured in some test helpers sometime soon.


foreach (Result result in results)
{
sb.AppendLine(result.ToString());

Check warning

Code scanning / CodeQL

Use of default ToString()

Default 'ToString()': [Result](1) inherits 'ToString()' from 'Object', and so is not suitable for printing. Default 'ToString()': [Result](2) inherits 'ToString()' from 'Object', and so is not suitable for printing.
@@ -2035,6 +2145,8 @@

public int ExpectedReturnCode;

public int RulesCount;

Check notice

Code scanning / CodeQL

Missed 'readonly' opportunity

Field 'RulesCount' can be 'readonly'.

foreach (Result result in results)
{
sb.AppendLine(result.ToString());

Check notice

Code scanning / CodeQL

Redundant ToString() call

Redundant call to 'ToString' on a String object.
@michaelcfanning
Copy link
Member Author

michaelcfanning commented Jan 13, 2023

@shaopeng, can you look at this for me? :)

@michaelcfanning michaelcfanning merged commit 7805847 into main Jan 13, 2023
@michaelcfanning michaelcfanning deleted the trace-notifications branch January 13, 2023 17:40
@shaopeng-gh
Copy link
Collaborator

@michaelcfanning
ah I see wrong shaopeng was added here should be shaopeng-gh so I missed this pr.
I will read the pattern and work on the memory consumption information in a similar change.


In reply to: 1381184325

@michaelcfanning
Copy link
Member Author

Very sorry! Please review this change and let me know if you have feedback. :) we've got other worked blocked by this, so I merged already. I will remember to use the proper account reference : @shaopeng-gh . :)

This was referenced Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants