Search code examples
javaazureazure-application-insightsmetricsopen-telemetry

Send customDimentions from Azure Application Insights with an Alert Rule


I'm new to Azure and I'm some problems with the custom metrics feature in Azure. My app is sending custom metrics signals to the Azure Application Insights. If I go to the logs in Azure I can clearly see the signal with the content represented in form of customDimentions. This signal is captured by an Azure Alert, that sends a payload to a specified webhook. The issue is that I couldn't find add the customDImentions obj to the Alert payload. I know that Alerts have custom properties, but those are static.

Does anyone know a solution or a workaround?

After sending the signal from my application I can get some information in my alert like the signal name and the value, but not its properties


Solution

    • Set custom properties using the TelemetryClient. Make sure to replace "CustomProperty1" and "CustomProperty2" with your actual custom dimension names
    import com.microsoft.applicationinsights.TelemetryClient;
    import com.microsoft.applicationinsights.telemetry.MetricTelemetry;
    
    public class MyApp {
        private static TelemetryClient telemetry = new TelemetryClient();
    
        public static void main(String[] args) {
            // Your custom telemetry object
            MyTelemetryObject customTelemetry = new MyTelemetryObject();
    
            // Track custom metric
            telemetry.trackMetric(new MetricTelemetry("MetricName", customTelemetry.getValue()));
    
            // Add custom dimensions
            telemetry.getContext().getProperties().put("CustomProperty1", customTelemetry.getCustomProperty1());
            telemetry.getContext().getProperties().put("CustomProperty2", customTelemetry.getCustomProperty2());
    
            // Flush telemetry data
            telemetry.flush();
        }
    }
    
    
    • Using the Kusto Query Language (KQL) to filter and extract the relevant data.
    customDimensions.CustomProperty1 == "Value1" and customDimensions.CustomProperty2 == "Value2"
    
    

    We need to check that the Log Analytics query includes the necessary custom dimensions in the result set.
    enter image description here

    • Update the Azure Monitor alert rule to trigger the Azure Logic App you created instead of directly sending a webhook. Set up a server or endpoint in the Java application to handle the webhook notifications sent by the Logic App.