azure-functionsazure-application-insights

How to optimize Application Insight costs for Azure Functions?


I have Azure Function(Python) and Azure Application Insight. Application Insight costs are huge, because it get 200GB data by month. I'm trying to track where is the issue.

I wonder Azure Function logs everything and is it possible to optimize. We would not need "info".

How to optimize costs in Application Insight for Azure Functions?

Application Insight:

logger = logging.getLogger(__name__)
    logger.info('Python HTTP trigger function received a request.')

    try:
        instrumentationKey = os.environ['APPINSIGHTS_INSTRUMENTATIONKEY']
        logger.addHandler(AzureLogHandler(
            connection_string=f'InstrumentationKey={instrumentationKey}')
        )
    except Exception as e:
        exception_text = f"{e}"
        logging.error(f"Could not add Application Insights logging: {exception_text}")

Logging usage:

 logging.error(f"EXCEPTION: {exception_text}")
 logging.info(f" Calling Activity Function")

Solution

  • I wonder Azure Function logs everything and is it possible to optimize. We would not need "info".

    1. Disable unneeded modules: Edit the ApplicationInsights.config to turn off collection modules which are not required.

    2. Disable telemetry dynamically: To disable telemetry conditionally and dynamically anywhere in the code, set the DisableTelemetry flag on it using TelemetryConfiguration instance.

    public void ConfigureServices(IServiceCollection services)
        {
            services.AddApplicationInsightsTelemetry();
        }
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, TelemetryConfiguration configuration)
    {
        configuration.DisableTelemetry = true;
        ...
    }
    

    This code sample prevents the sending of telemetry to Application Insights but not the automatic collection modules from colleting telemetry and in order to remove auto collection modules also, please refer this Microsoft Documentation.

    3. Customize Logs Collection:

    {
      "Logging": {
        "LogLevel": {
          "Default": "Warning"
        },
        "ApplicationInsights": {
          "LogLevel": {
            "Default": "Information"
          }
        }
      }
    }
    

    The following above configuration allows Application Insights to capture all Information logs and severe warning logs. To change this behavior, explicitly override the logging configuration for the provider ApplicationInsights as shown below:

    {
      "Logging": {
        "LogLevel": {
          "Default": "Warning"
        }
      }
    }
    

    There are few more techniques to manage the data volume used for telemetry data optimizing also like:

    Please check these references for more information:

    1. Resolve if logs shows twice in Application Insights
    2. Optimizing logging costs of Azure Functions
    3. Configuring or removing the necessary Telemetry Initializers