1111import com .microsoft .applicationinsights .alerting .config .AlertConfiguration ;
1212import com .microsoft .applicationinsights .alerting .config .AlertMetricType ;
1313import com .microsoft .applicationinsights .alerting .config .AlertingConfiguration ;
14+ import com .microsoft .applicationinsights .alerting .config .AlertingProfileFileTriggerConfiguration ;
1415import com .microsoft .applicationinsights .alerting .config .CollectionPlanConfiguration ;
1516import com .microsoft .applicationinsights .alerting .config .CollectionPlanConfiguration .EngineMode ;
1617import com .microsoft .applicationinsights .alerting .config .DefaultConfiguration ;
18+ import java .io .File ;
1719import java .time .Instant ;
1820import java .util .ArrayList ;
1921import java .util .HashSet ;
3335public class AlertingSubsystem {
3436
3537 private static final Logger logger = LoggerFactory .getLogger (AlertingSubsystem .class );
36-
3738 // Downstream observer of alerts produced by the alerting system
3839 private final Consumer <AlertBreach > alertHandler ;
3940
@@ -46,25 +47,40 @@ public class AlertingSubsystem {
4647 // Current configuration of the alerting subsystem
4748 private AlertingConfiguration alertConfig ;
4849
49- private boolean enableRequestTriggerUpdates ;
50+ /** Configuration controlling the file-based manual profile trigger. */
51+ private final AlertingProfileFileTriggerConfiguration alertingProfileFileTriggerConfiguration ;
5052
51- protected AlertingSubsystem (Consumer <AlertBreach > alertHandler ) {
52- this (alertHandler , TimeSource .DEFAULT , false );
53- }
53+ private boolean enableRequestTriggerUpdates ;
5454
5555 protected AlertingSubsystem (
5656 Consumer <AlertBreach > alertHandler ,
5757 TimeSource timeSource ,
58- boolean enableRequestTriggerUpdates ) {
58+ boolean enableRequestTriggerUpdates ,
59+ AlertingProfileFileTriggerConfiguration alertingProfileFileTriggerConfiguration ) {
5960 this .alertHandler = alertHandler ;
6061 this .alertPipelines = new AlertPipelines (alertHandler );
6162 this .timeSource = timeSource ;
6263 this .enableRequestTriggerUpdates = enableRequestTriggerUpdates ;
64+ this .alertingProfileFileTriggerConfiguration = alertingProfileFileTriggerConfiguration ;
6365 }
6466
67+ /**
68+ * Creates and initializes an {@link AlertingSubsystem} with an initially-disabled configuration.
69+ *
70+ * @param alertHandler downstream consumer that handles generated alert breaches
71+ * @param timeSource time source used for alert evaluation windows
72+ * @param alertingProfileFileTriggerConfiguration configuration for the file-based manual trigger
73+ * @return a fully initialized alerting subsystem ready to receive configuration updates
74+ */
6575 public static AlertingSubsystem create (
66- Consumer <AlertBreach > alertHandler , TimeSource timeSource ) {
67- AlertingSubsystem alertingSubsystem = new AlertingSubsystem (alertHandler , timeSource , true );
76+ Consumer <AlertBreach > alertHandler ,
77+ TimeSource timeSource ,
78+ AlertingProfileFileTriggerConfiguration alertingProfileFileTriggerConfiguration ) {
79+
80+ AlertingSubsystem alertingSubsystem =
81+ new AlertingSubsystem (
82+ alertHandler , timeSource , true , alertingProfileFileTriggerConfiguration );
83+
6884 // init with disabled config
6985 alertingSubsystem .initialize (
7086 AlertingConfiguration .create (
@@ -145,14 +161,23 @@ private void updateRequestPipelineConfig(
145161 }
146162 }
147163
148- /** Determine if a manual alert has been requested. */
164+ /**
165+ * Determine if a manual alert has been requested via any supported mechanism. Currently evaluates
166+ * both the server-side collection plan and the local file-based trigger.
167+ */
149168 private void evaluateManualTrigger (AlertingConfiguration alertConfig ) {
169+ evaluateCollectionPlanTrigger (alertConfig );
170+ evaluateFileTrigger (alertConfig );
171+ }
172+
173+ /** Check if the collection plan configuration requests a manual profile. */
174+ private void evaluateCollectionPlanTrigger (AlertingConfiguration alertConfig ) {
150175 CollectionPlanConfiguration config = alertConfig .getCollectionPlanConfiguration ();
151176
152177 boolean shouldTrigger =
153178 config .isSingle ()
154179 && config .getMode () == EngineMode .immediate
155- && Instant . now ().isBefore (config .getExpiration ())
180+ && timeSource . getNow ().isBefore (config .getExpiration ())
156181 && !manualTriggersExecuted .contains (config .getSettingsMoniker ());
157182
158183 if (shouldTrigger ) {
@@ -176,6 +201,78 @@ private void evaluateManualTrigger(AlertingConfiguration alertConfig) {
176201 }
177202 }
178203
204+ /**
205+ * Check if a trigger file is present on the local file system and was recently modified. If so,
206+ * delete the file and trigger a manual profile. The global cooldown in Profiler prevents
207+ * overlapping profiles.
208+ */
209+ public void evaluateFileTrigger () {
210+ if (!alertingProfileFileTriggerConfiguration .isEnabled ()) {
211+ return ;
212+ }
213+
214+ if (alertConfig == null ) {
215+ return ;
216+ }
217+
218+ evaluateFileTrigger (alertConfig );
219+ }
220+
221+ private void evaluateFileTrigger (AlertingConfiguration currentConfig ) {
222+ if (!alertingProfileFileTriggerConfiguration .isEnabled ()) {
223+ return ;
224+ }
225+
226+ if (currentConfig == null ) {
227+ return ;
228+ }
229+
230+ File manualTriggerFile = alertingProfileFileTriggerConfiguration .getFilePath ();
231+ if (manualTriggerFile == null || !manualTriggerFile .exists ()) {
232+ return ;
233+ }
234+
235+ long lastModified = manualTriggerFile .lastModified ();
236+ long age = timeSource .getNow ().toEpochMilli () - lastModified ;
237+
238+ if (age < 0 || age > AlertingProfileFileTriggerConfiguration .MANUAL_TRIGGER_FILE_MAX_AGE_MS ) {
239+ return ;
240+ }
241+
242+ // Delete the trigger file to prevent re-triggering
243+ if (!manualTriggerFile .delete ()) {
244+ logger .warn (
245+ "Failed to delete manual profile trigger file: {}" , manualTriggerFile .getAbsolutePath ());
246+ return ;
247+ }
248+
249+ logger .info ("Manual profile trigger file detected, initiating profile recording" );
250+
251+ // Use the collection plan's duration if configured, otherwise fall back to the
252+ // file trigger's default duration setting.
253+ CollectionPlanConfiguration collectionPlan = currentConfig .getCollectionPlanConfiguration ();
254+ int durationSeconds = collectionPlan .getImmediateProfilingDurationSeconds ();
255+ if (durationSeconds <= 0 ) {
256+ durationSeconds = alertingProfileFileTriggerConfiguration .getDefaultProfileDurationSeconds ();
257+ }
258+
259+ AlertBreach alertBreach =
260+ AlertBreach .builder ()
261+ .setType (AlertMetricType .MANUAL )
262+ .setAlertValue (0.0 )
263+ .setAlertConfiguration (
264+ AlertConfiguration .builder ()
265+ .setType (AlertMetricType .MANUAL )
266+ .setEnabled (true )
267+ .setProfileDurationSeconds (durationSeconds )
268+ .build ())
269+ .setProfileId (UUID .randomUUID ().toString ())
270+ .setCpuMetric (0 )
271+ .setMemoryUsage (0 )
272+ .build ();
273+ alertHandler .accept (alertBreach );
274+ }
275+
179276 public void setPipeline (AlertMetricType type , AlertPipeline alertPipeline ) {
180277 alertPipelines .setAlertPipeline (type , alertPipeline );
181278 }
0 commit comments