internal static void InitializeLibrdKafka()

in src/Microsoft.Azure.WebJobs.Extensions.Kafka/Config/AzureFunctionsFileHelper.cs [88:164]


        internal static void InitializeLibrdKafka(ILogger logger)
        {
            lock (librdkafkaInitializationLock)
            {
                if (librdkafkaInitialized)
                {
                    if (logger.IsEnabled(LogLevel.Debug))
                    {
                        logger.LogDebug("Librdkafka initialization: skipping as the initialization already happened");
                    }
                    return;
                }

                librdkafkaInitialized = true;

                var userSpecifiedLibrdKafkaLocation = Environment.GetEnvironmentVariable(LibrdKafkaLocationEnvVarName);
                if (!string.IsNullOrWhiteSpace(userSpecifiedLibrdKafkaLocation))
                {
                    logger.LogDebug("Librdkafka initialization: loading librdkafka from user specified location: {librdkafkaPath}", userSpecifiedLibrdKafkaLocation);
                    Confluent.Kafka.Library.Load(userSpecifiedLibrdKafkaLocation);
                    return;
                }

                if (!IsRunningAsFunctionInAzureOrContainer())
                {
                    if (logger.IsEnabled(LogLevel.Debug))
                    {
                        logger.LogDebug("Librdkafka initialization: skipping as we are not running in Azure or a container");
                    }

                    return;
                }

                var possibleLibrdKafkaLibraryPaths = new List<string>();

                var os = Environment.GetEnvironmentVariable(OSEnvVarName, EnvironmentVariableTarget.Process) ?? string.Empty;
                var isWindows = os.IndexOf("windows", 0, StringComparison.InvariantCultureIgnoreCase) != -1;

                if (isWindows)
                {
                    var websiteBitness = Environment.GetEnvironmentVariable(SiteBitnessEnvVarName) ?? string.Empty;
                    var is32 = websiteBitness.Equals(ProcessArchitecturex86Value, StringComparison.InvariantCultureIgnoreCase);
                    var architectureFolderName = is32 ? Windows32ArchFolderName : Windows64ArchFolderName;

                    var functionBaseFolder = GetFunctionBaseFolder();

                    // Functions v2 have the runtime under 'D:\home\site\wwwroot\runtimes'
                    possibleLibrdKafkaLibraryPaths.Add(Path.Combine(functionBaseFolder, RuntimesFolderName, architectureFolderName, NativeFolderName, LibrdKafkaWindowsFileName));

                    // Functions v3 have the runtime under 'D:\home\site\wwwroot\bin\runtimes'
                    possibleLibrdKafkaLibraryPaths.Add(Path.Combine(functionBaseFolder, BinFolderName, RuntimesFolderName, architectureFolderName, NativeFolderName, LibrdKafkaWindowsFileName));
                }
                else
                {
                    logger.LogInformation("Librdkafka initialization: running in non-Windows OS, expecting librdkafka to be there");
                }

                if (possibleLibrdKafkaLibraryPaths.Count > 0)
                {
                    foreach (var librdKafkaLibraryPath in possibleLibrdKafkaLibraryPaths)
                    {
                        if (File.Exists(librdKafkaLibraryPath))
                        {
                            logger.LogDebug("Librdkafka initialization: loading librdkafka from {librdkafkaPath}", librdKafkaLibraryPath);
                            Confluent.Kafka.Library.Load(librdKafkaLibraryPath);
                            return;
                        }
                    }

                    logger.LogWarning("Librdkafka initialization: did not attempt to load librdkafka because the desired file(s) does not exist: '{searchedPaths}' You can ignore this warning when you use extension bundle.", string.Join(",", possibleLibrdKafkaLibraryPaths));
                }
                else
                {
                    logger.LogInformation("Librdkafka initialization: could not find dll location");
                }
            }            
        }