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

Using consoleLogger in Telemetry client within MultithreadedAnlyzeCom… #1002

Merged

Conversation

AllDwarf
Copy link
Collaborator

This pull request includes changes to the MultithreadedAnalyzeCommand class in src/BinSkim.Driver to enhance logging capabilities by adding a new console logger and ensuring proper namespaces are included.

Enhancements to logging:

Namespace inclusion:

@AllDwarf AllDwarf self-assigned this Jul 29, 2024
@AllDwarf AllDwarf force-pushed the users/marekaldorf/Telemetry_setup_for_consolelog_and_AppInsights branch 3 times, most recently from b0dd6cc to c1af956 Compare August 5, 2024 11:54
@AllDwarf AllDwarf marked this pull request as ready for review August 5, 2024 11:55
@AllDwarf
Copy link
Collaborator Author

AllDwarf commented Aug 5, 2024

Testing

Issue was tested with multiple setups

Setup

Running analyze command against empty folder wihout targets inside like this:

    analyze --config default --ignorePdbLoadError --hashes --statistics --output C:\Users\marekaldorf\source\repos\software-development-tools-samples\src\MyRun.sarif C:\Users\marekaldorf\source\repos\AllDwarf\BlobStorageTrigger\bin\*.so --log ForceOverwrite

You can change the path to point to any empty folder and the sarif you want and reproduce it the same way I did.

    analyze --config default --ignorePdbLoadError --hashes --statistics --output {PATH_TO_SARIF}\{NAME}.sarif {PATH_TO_EMPTY_FOLDER}\*.so --log ForceOverwrite
  1. --disable-telemetry set to false when env variable BinskimAppInsightsConnectionString set to point to AppInsights with connection string set.
    1. BEFORE
      image
    2. AFTER
      image

@michaelcfanning
Copy link
Member

        base.InitializeGlobalContextFromOptions(options, ref context);

try moving this line to the beginning of the method, perhaps the default console logging init will work then.


Refers to: src/BinSkim.Driver/MultithreadedAnalyzeCommand.cs:68 in c1af956. [](commit_id = c1af956, deletion_comment = True)

@AllDwarf AllDwarf force-pushed the users/marekaldorf/Telemetry_setup_for_consolelog_and_AppInsights branch from c1af956 to 5648804 Compare August 5, 2024 16:51
@AllDwarf AllDwarf force-pushed the users/marekaldorf/Telemetry_setup_for_consolelog_and_AppInsights branch from 5648804 to 3f42e99 Compare August 5, 2024 16:58
@@ -48,13 +48,16 @@ private bool IsValidScanTarget(string file)

public override BinaryAnalyzerContext InitializeGlobalContextFromOptions(AnalyzeOptions options, ref BinaryAnalyzerContext context)
{
base.InitializeGlobalContextFromOptions(options, ref context);
Copy link
Member

Choose a reason for hiding this comment

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

InitializeGlobalContextFromOptions

btw! now seems a good time to talk a bit about testing. one thing I observed in the previous change is that you might have broken support for the --quiet flag. So you might run a test: revert to your previous change and run all tests: hopefully we see that some test protecting the functionality of the quiet switch fired.

I was only peripherally involved with the telemetry client work, unfortunately, so I don't know the state of testing.

This method is very testable, however, you can create this object and call this public method on it. So I would suggest creating some tests that hit this code path.

Verification looks straightforward, populate the object with a non-null Telemetry object, then call this init method and ensure the context.Loggers property contains what you think that it should.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep good point. I tried it with --quiet true and I think seems to be working as expected no ConsoleLogger set, just telemetry is sent.

C:\Users\marekaldorf\source\repos\binskim\bld\bin\x64_Debug\netcoreapp3.1\BinSkim.exe (process 40088) exited with code 1.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

context.Logger in this case it is without ConsoleLogger. It has empty IAnalysisLogger. So in quiet mode this specific code will add one empty logger and one RuleTelemetryLogger together.

I tried to redo it a bit with something like this:

            if (this.Telemetry?.TelemetryClient != null)
            {
                // Create an aggregating logger that will combine all loggers into a single logger.
                var aggregatingLogger = new AggregatingLogger();
                if (context.Logger is AggregatingLogger)
                {
                    aggregatingLogger = context.Logger as AggregatingLogger;
                }
                else
                {
                    aggregatingLogger.Loggers.Add(context.Logger);
                }

                var ruleTelemetryLogger = new RuleTelemetryLogger(this.Telemetry.TelemetryClient);
                ruleTelemetryLogger.AnalysisStarted();

                // Combine rule telemetry with any other loggers that may be present.
                aggregatingLogger.Loggers.Add(ruleTelemetryLogger);
                context.Logger = aggregatingLogger;
            }

In this case we'll achieve non empty IAnalyseLogger in the first place but it's a bit hacky I would say with the type checks and such WDYT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

just FYI, I'm working now on the test hopefully tomorrow I'll make them working :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@michaelcfanning just added the tests

1. Verify ConsoleLogger when telemetry is disabled.
2. Verify ConsoleLogger and RuleTelemetryLogger when telemetry is enabled.
3. Verify RuleTelemetryLogger when quiet option and telemetry are enabled.
4. Verify no loggers when both quiet option and telemetry are disabled.
@@ -8,6 +8,8 @@
using System.Linq;
using System.Reflection;

using CommandLine;
Copy link
Member

Choose a reason for hiding this comment

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

CommandLine

necessary? btw - in other of our OSS projects we've figured out how to enforce certain style/other guidelines (like strictly removing unused namespace directives) that we could port here. the relationships between build, dotnet format, and .editorconfig are not obvious.

@@ -150,5 +155,89 @@ private static string[] Shuffle(string[] data)

return data;
}

[Fact]
public void MultithreadedAnalyzeCommand_InitializeGlobalContextFromOptions_TelemetryNotSpecifiedHasOneConsoleLogger()
Copy link
Member

Choose a reason for hiding this comment

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

nice tests!

Copy link
Member

@michaelcfanning michaelcfanning left a comment

Choose a reason for hiding this comment

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

:shipit:

@AllDwarf AllDwarf removed the request for review from marmegh August 12, 2024 11:39
@AllDwarf AllDwarf merged commit ab73bee into main Aug 12, 2024
5 of 6 checks passed
@AllDwarf AllDwarf deleted the users/marekaldorf/Telemetry_setup_for_consolelog_and_AppInsights branch August 12, 2024 11:39
@AllDwarf AllDwarf mentioned this pull request Aug 12, 2024
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.

2 participants