Search code examples
azureappinsights

Azure ApplicationInsights sampling overrides with multiple telemetry types, with different default sampling rates


I'm using sampling overrides (java sdk) to apply sampling on different telemetry types. I'd like to know if I can use different default percentages for different telemetry types, along with their own overrides. A use case for me is:

  • Overall default sampling percentage should be 100 (i.e. log everything). The exceptions to this are health check request spans (0% logging for health checks) and below overrides
  • Default sampling for trace logs should be 10%, except for trace logs that contain .audit. Audit logs should be logged 100% of time
  • Default sampling for dependency logs should be 25%

Here is a pseudo-config I'm thinking:

{
    "preview": {
        "sampling": {
            "percentage": 100                                                 => overall sampling percentage (100)
            "overrides": [
                {
                    "telemetryType": "trace",
                    "percentage": 10,                                          => overall sampling percentage for traces. This should override the default set at parent (100)
                    "overrides": [
                        {
                            "percentage": 100,                                => override the default sampling for traces (10%)
                            "attributes": [
                                <match with entries that contain `.audit` in the trace message>               => is this even possible? the trace message body is not part of attributes. Even if it was in the attributes, can we achieve this?
                            ]
                        }
                    ]
                },
                {
                    "telemetryType": "request",
                    "attributes": [
                        {
                            "key": "http.target",
                            "value": "/actuator/health",
                            "matchType": "strict"
                        }
                    ],
                    "percentage": 0
                },
                {
                    "telemetryType": "dependency",
                    "percentage": 25                                           => overall sampling percentage for dependencies
                }
            ]
        }
    }
}

I am also using telemetry processors to sample custom metrics, but for the above use case (since percentages are involved), I am not sure if I can use telemetry processors


Solution

  • Set the overall default sampling rate to 100% to log everything.

    Trace Logs: Default to 10%, but 100% for audit logs. Request Logs: 0% for health check requests. Dependency Logs: 25%.

    Direct matching on the content of trace messages for .audit will not be directly possible through the sampling configuration alone.

    • Create a custom telemetry processor to handle the .audit keyword in trace messages

    CustomTelemetryProcessor.java

    import com.microsoft.applicationinsights.telemetry.TraceTelemetry;
    import com.microsoft.applicationinsights.channel.TelemetryProcessor;
    import com.microsoft.applicationinsights.channel.TelemetryProcessorChain;
    
    public class CustomTelemetryProcessor implements TelemetryProcessor {
        private TelemetryProcessor next;
    
        @Override
        public void initialize(TelemetryProcessorChain chain) {
            this.next = chain;
        }
    
        @Override
        public boolean process(TraceTelemetry telemetry) {
            String message = telemetry.getMessage();
            if (message != null && message.contains(".audit")) {
                return true; // Always send audit logs
            }
    
            return next.process(telemetry); // Continue with other processors
        }
    
        // Implement other telemetry types if needed
    }
    

    applicationinsights.json:

    {
        "sampling": {
            "percentage": 100,
            "requests": {
                "percentage": 100,
                "overrides": [
                    {
                        "attribute": "http.target",
                        "value": "/actuator/health",
                        "percentage": 0
                    }
                ]
            },
            "dependencies": {
                "percentage": 25
            },
            "traces": {
                "percentage": 10
            }
        }
    }
    

    Result:

    Resquest logs: enter image description here

    Trace logs: enter image description here

    Exception logs: enter image description here