88from forwarder .common import Channel , CommandType , ConfigUpdate
99from forwarder .configuration_store import ConfigurationStore , NullConfigurationStore
1010from forwarder .kafka .kafka_producer import KafkaProducer
11+ from forwarder .metrics import Gauge
1112from forwarder .status_reporter import StatusReporter
1213from forwarder .update_handlers .create_update_handler import (
1314 UpdateHandler ,
@@ -24,6 +25,7 @@ def _subscribe_to_pv(
2425 logger : Logger ,
2526 fake_pv_period : int ,
2627 pv_update_period : Optional [int ],
28+ pvs_subscribed_metric : Optional [Gauge ] = None ,
2729):
2830 if new_channel in update_handlers .keys ():
2931 logger .warning (
@@ -46,12 +48,15 @@ def _subscribe_to_pv(
4648 logger .info (
4749 f"Subscribed to PV name='{ new_channel .name } ', schema='{ new_channel .schema } ', topic='{ new_channel .output_topic } '"
4850 )
51+ if pvs_subscribed_metric :
52+ pvs_subscribed_metric .inc ()
4953
5054
5155def _unsubscribe_from_pv (
5256 remove_channel : Channel ,
5357 update_handlers : Dict [Channel , UpdateHandler ],
5458 logger : Logger ,
59+ pvs_subscribed_metric : Optional [Gauge ] = None ,
5560):
5661 def _match_channel_field (
5762 field_in_remove_request : Optional [str ], field_in_existing_channel : Optional [str ]
@@ -88,17 +93,23 @@ def _wildcard_match_channel_field(
8893 for channel in channels_to_remove :
8994 update_handlers [channel ].stop ()
9095 del update_handlers [channel ]
96+ if pvs_subscribed_metric :
97+ pvs_subscribed_metric .dec ()
9198
9299 logger .info (
93100 f"Unsubscribed from PVs matching name='{ remove_channel .name } ', schema='{ remove_channel .schema } ', topic='{ remove_channel .output_topic } '"
94101 )
95102
96103
97104def _unsubscribe_from_all (
98- update_handlers : Dict [Channel , UpdateHandler ], logger : Logger
105+ update_handlers : Dict [Channel , UpdateHandler ],
106+ logger : Logger ,
107+ pvs_subscribed_metric : Optional [Gauge ] = None ,
99108):
100109 for update_handler in update_handlers .values ():
101110 update_handler .stop ()
111+ if pvs_subscribed_metric :
112+ pvs_subscribed_metric .dec ()
102113 update_handlers .clear ()
103114 logger .info ("Unsubscribed from all PVs" )
104115
@@ -114,12 +125,15 @@ def handle_configuration_change(
114125 logger : Logger ,
115126 status_reporter : StatusReporter ,
116127 configuration_store : ConfigurationStore = NullConfigurationStore ,
128+ pvs_subscribed_metric : Optional [Gauge ] = None ,
117129):
118130 """
119131 Add or remove update handlers according to the requested change in configuration
120132 """
121133 if configuration_change .command_type == CommandType .REMOVE_ALL :
122- _unsubscribe_from_all (update_handlers , logger )
134+ _unsubscribe_from_all (
135+ update_handlers , logger , pvs_subscribed_metric = pvs_subscribed_metric
136+ )
123137 elif configuration_change .command_type == CommandType .INVALID :
124138 return
125139 else :
@@ -135,8 +149,14 @@ def handle_configuration_change(
135149 logger ,
136150 fake_pv_period ,
137151 pv_update_period ,
152+ pvs_subscribed_metric = pvs_subscribed_metric ,
138153 )
139154 elif configuration_change .command_type == CommandType .REMOVE :
140- _unsubscribe_from_pv (channel , update_handlers , logger )
155+ _unsubscribe_from_pv (
156+ channel ,
157+ update_handlers ,
158+ logger ,
159+ pvs_subscribed_metric = pvs_subscribed_metric ,
160+ )
141161 status_reporter .report_status ()
142162 configuration_store .save_configuration (update_handlers )
0 commit comments