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

FunctionMetadataManager.AddMetadataFromCustomProviders logging incorrect function count #10220

Closed
kshyju opened this issue Jun 11, 2024 · 0 comments · Fixed by #10224
Closed

Comments

@kshyju
Copy link
Member

kshyju commented Jun 11, 2024

The FunctionMetadataManager.AddMetadataFromCustomProviders method is logging incorrect message.

If you look at the code at line 230, we are logging the length of functionMetadataListArray , not the actual total function count.

var functionMetadataListArray = Task.WhenAll(functionProviderTasks).GetAwaiter().GetResult();
// This is used to make sure no duplicates are registered
var distinctFunctionNames = new HashSet<string>(functionMetadataList.Select(m => m.Name));
_logger.FunctionsReturnedByProvider(functionMetadataListArray.Length, _metadataProviderName);

and the logging action

private static readonly Action<ILogger, int, string, Exception> _functionsReturnedByProvider =
LoggerMessage.Define<int, string>(LogLevel.Information,
new EventId(327, nameof(FunctionsReturnedByProvider)),
"{count} functions found ({provider})");

To reproduce this issue, add the below test to FunctionMetadataMangerTests.cs and run. The test will fail in the last assert statement and that confirms that we are logging incorrect message.

In this test, there are 2 providers. The GetFunctionMetadataAsync of first provider returns 1 function and the second provider returns 3 functions. So Ideally we should be logging "4 functions found (Custom)". But currently we are logging the provider count (2 functions found (Custom))

[Fact]
public void FunctionMetadataManager_GetsMetadata_FromMultipleFunctionProviders_Success()
{
    var functionMetadataCollection = new Collection<FunctionMetadata>();
    var mockFunctionErrors = new Dictionary<string, ImmutableArray<string>>();
    var mockFunctionMetadataProvider = new Mock<IFunctionMetadataProvider>();
    var mockFunctionProvider = new Mock<IFunctionProvider>();
    var mockFunctionProvider2 = new Mock<IFunctionProvider>();
    var workerConfigs = TestHelpers.GetTestWorkerConfigs();
    var testLoggerProvider = new TestLoggerProvider();
    var loggerFactory = new LoggerFactory();
    loggerFactory.AddProvider(testLoggerProvider);

    mockFunctionMetadataProvider.Setup(m => m.GetFunctionMetadataAsync(workerConfigs, SystemEnvironment.Instance, false)).Returns(Task.FromResult(new Collection<FunctionMetadata>().ToImmutableArray()));
    mockFunctionMetadataProvider.Setup(m => m.FunctionErrors).Returns(new Dictionary<string, ICollection<string>>().ToImmutableDictionary(kvp => kvp.Key, kvp => kvp.Value.ToImmutableArray()));

    functionMetadataCollection.Add(GetTestFunctionMetadata("somefile.dll", name: "anotherFunction"));

    mockFunctionProvider.Setup(m => m.GetFunctionMetadataAsync()).ReturnsAsync(functionMetadataCollection.ToImmutableArray());
    mockFunctionProvider.Setup(m => m.FunctionErrors).Returns(mockFunctionErrors.ToImmutableDictionary());

    // A second provider
    var goodFunctionMetadataCollection = new Collection<FunctionMetadata>
    {
        GetTestFunctionMetadata("somefile2.dll", name: "Function1"),
        GetTestFunctionMetadata("somefile2.dll", name: "Function2"),
        GetTestFunctionMetadata("somefile2.dll", name: "Function3")
    };
    mockFunctionProvider2.Setup(m => m.GetFunctionMetadataAsync()).ReturnsAsync(goodFunctionMetadataCollection.ToImmutableArray());

    FunctionMetadataManager testFunctionMetadataManager = TestFunctionMetadataManager.GetFunctionMetadataManager(new OptionsWrapper<ScriptJobHostOptions>(_scriptJobHostOptions),
        mockFunctionMetadataProvider.Object, new List<IFunctionProvider>() { mockFunctionProvider.Object, mockFunctionProvider2.Object }, new OptionsWrapper<HttpWorkerOptions>(_defaultHttpWorkerOptions), loggerFactory, new TestOptionsMonitor<LanguageWorkerOptions>(TestHelpers.GetTestLanguageWorkerOptions()));

    var actualFunctionMetadata = testFunctionMetadataManager.LoadFunctionMetadata();

    var expectedTotalFunctionsCount = 4;
    var traces = testLoggerProvider.GetAllLogMessages();
    Assert.Equal(expectedTotalFunctionsCount, actualFunctionMetadata.Length);
    Assert.Single(traces.Where(t => t.FormattedMessage.Contains("Reading functions metadata (Custom)")));
    Assert.Single(traces.Where(t => t.FormattedMessage.Contains($"{expectedTotalFunctionsCount} functions found (Custom)")));
}
kshyju added a commit that referenced this issue Jun 14, 2024
…on metadata from providers (#10224)

* Fixed incorrect function count in the log message.(#10220)

* Minor variable rename

* Check DefaultOrEmpty before accessing Length of immutable array.

* Fix integration test to reflect the change.
kshyju added a commit that referenced this issue Jun 14, 2024
…when getting function metadata from providers (#10223)

* Fixed incorrect function count in the log message.(#10220)

* Minor variable rename

* Check DefaultOrEmpty before accessing Length of immutable array.

* Fix integration test to reflect changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants