private Map < MetricName , MetricValue > get_metrics_ ( DateTime now , Context ctx ) { final Consumer < Alert > alert_manager = ctx . getAlertManager ( ) ; final long failed_collections = registry_ . getFailedCollections ( ) ; final boolean has_config = registry_ . hasConfig ( ) ; final Optional < Duration > scrape_duration = registry_ . getScrapeDuration ( ) ; final Optional < Duration > rule_eval_duration = registry_ . getRuleEvalDuration ( ) ; final Optional < Duration > processor_duration = registry_ . getProcessorDuration ( ) ; first_scrape_ts_ . compareAndSet ( null , now ) ; // First time, register the timestamp. final Duration uptime = new Duration ( first_scrape_ts_ . get ( ) , now ) ; final Optional < DateTime > last_scrape = last_scrape_ ; last_scrape_ = Optional . of ( now ) ; final long metric_count = ctx . getTSData ( ) . getCurrentCollection ( ) . getTSValues ( ) . stream ( ) . map ( TimeSeriesValue :: getMetrics ) . collect ( Collectors . summingLong ( Map :: size ) ) ; alert_manager . accept ( new Alert ( now , MONITOR_FAIL_ALERT , ( ) -> "builtin rule" , Optional . of ( failed_collections != 0 ) , MON_ALERT_DURATION , "builtin rule: some collectors failed" , EMPTY_MAP ) ) ; alert_manager . accept ( new Alert ( now , HAS_CONFIG_ALERT , ( ) -> "builtin rule" , Optional . of ( ! has_config ) , Duration . ZERO , "builtin rule: monitor has no configuration file" , EMPTY_MAP ) ) ; Map < MetricName , MetricValue > result = new HashMap <> ( ) ; result . put ( FAILED_COLLECTIONS_METRIC , MetricValue . fromIntValue ( failed_collections ) ) ; result . put ( GROUP_COUNT_METRIC , MetricValue . fromIntValue ( ctx . getTSData ( ) . getCurrentCollection ( ) . getGroups ( x -> true ) . size ( ) ) ) ; result . put ( METRIC_COUNT_METRIC , MetricValue . fromIntValue ( metric_count ) ) ; result . put ( CONFIG_PRESENT_METRIC , MetricValue . fromBoolean ( has_config ) ) ; result . put ( SCRAPE_DURATION , opt_duration_to_metricvalue_ ( scrape_duration ) ) ; result . put ( RULE_EVAL_DURATION , opt_duration_to_metricvalue_ ( rule_eval_duration ) ) ; result . put ( PROCESSOR_DURATION , opt_duration_to_metricvalue_ ( processor_duration ) ) ; result . put ( UPTIME_DURATION , duration_to_metricvalue_ ( uptime ) ) ; result . put ( SCRAPE_COUNT , MetricValue . fromIntValue ( ++ scrape_count_ ) ) ; result . put ( SCRAPE_INTERVAL , opt_duration_to_metricvalue_ ( last_scrape . map ( prev -> new Duration ( prev , now ) ) ) ) ; result . put ( SCRAPE_TS , MetricValue . fromIntValue ( now . getMillis ( ) ) ) ; return result ; }
Get metrics for the monitor .