Skip to content

Commit f06ab34

Browse files
committed
[REVERT] back up and do the try thingys
1 parent 8943c56 commit f06ab34

1 file changed

Lines changed: 60 additions & 19 deletions

File tree

src/daqpytools/apps/logging_demonstrator.py

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,34 @@ def test_handlertypes(main_logger: logging.Logger) -> None:
181181
extra={"handlers": [HandlerType.Rich, HandlerType.Protobufstream]}
182182
)
183183

184+
ers_envs = {
185+
"DUNEDAQ_ERS_WARNING" : "erstrace,throttle,lstdout",
186+
"DUNEDAQ_ERdS_INFO" : "erstrace,throttle,lstdout",
187+
"DUNEDAQ_ERS_FATAL" : "erstrace,lstdout",
188+
"DUNEDAQ_ERS_ERROR" : (
189+
"erstrace,"
190+
"throttle,"
191+
"lstdout,"
192+
"protobufstream(monkafka.cern.ch:30092)"
193+
)
194+
}
195+
196+
def obtain_original_envs(envs: dict) -> dict:
197+
return {
198+
key: os.environ.get(key)
199+
for key in envs
200+
}
201+
202+
203+
204+
def restore_original_envs(original:dict) -> None:
205+
for var_name, original_value in original.items():
206+
if original_value is None:
207+
os.environ.pop(var_name, None)
208+
else:
209+
os.environ[var_name] = original_value
210+
211+
184212

185213
def test_handlerconf(main_logger: logging.Logger) -> None:
186214
"""Demonstrates the main functionality of the handlerconf. With ERS support.
@@ -194,15 +222,12 @@ def test_handlerconf(main_logger: logging.Logger) -> None:
194222
main_logger.warning("Handlerconf Opmon", extra=handlerconf.Opmon)
195223

196224
#* Interlude: Inject sample environment variables
197-
os.environ["DUNEDAQ_ERS_WARNING"] = "erstrace,throttle,lstdout"
198-
os.environ["DUNEDAQ_ERS_INFO"] = "erstrace,throttle,lstdout"
199-
os.environ["DUNEDAQ_ERS_FATAL"] = "erstrace,lstdout"
200-
os.environ["DUNEDAQ_ERS_ERROR"] = (
201-
"erstrace,"
202-
"throttle,"
203-
"lstdout,"
204-
"protobufstream(monkafka.cern.ch:30092)"
205-
)
225+
# Save envs from current shell before we rewrite
226+
saved_envs = obtain_original_envs(ers_envs)
227+
228+
for key, value in ers_envs.items():
229+
# Inject ERS envs into the python shell
230+
os.environ[key] = value
206231

207232
info_out = f"{os.getenv('DUNEDAQ_ERS_ERROR')=}"
208233
main_logger.info(info_out)
@@ -214,17 +239,33 @@ def test_handlerconf(main_logger: logging.Logger) -> None:
214239
# HandlerConf will require that these variables are defined!
215240
# They come from the OKS, so whatever tools you have should have this up
216241
# You can also initialise via handlerconf = LogHandlerConf(init_ers=True)
217-
handlerconf.init_ers_stream()
242+
try:
243+
handlerconf.init_ers_stream()
244+
245+
#* Test ERS Streams
246+
main_logger.warning("ERS Warning erstrace,throttle,lstdout", extra=handlerconf.ERS)
247+
main_logger.info("ERS Info erstrace,throttle,lstdout", extra=handlerconf.ERS)
248+
main_logger.critical("ERS Fatal erstrace,lstdout", extra=handlerconf.ERS)
249+
main_logger.debug("ERS Debug none", extra=handlerconf.ERS)
250+
main_logger.error("ERS Error erstrace,throttle,lstdout,"
251+
"protobufstream(monkafka.cern.ch:30092)",
252+
extra=handlerconf.ERS
253+
)
254+
except Exception as e:
255+
main_logger.error(f"UHhhh not sure what happened here {e}")
256+
finally:
257+
258+
# Restore original environment variables
259+
## Note that in https://dagster.io/blog/
260+
## python-environment-variables#modifying-and-adding-environment-variables
261+
## It looks like os.environ only affects the local python shell, and doesn't
262+
## "leak" out into the bash envs.
263+
## However, for best practices this is still done
264+
265+
restore_original_envs(saved_envs)
266+
267+
218268

219-
#* Test ERS Streams
220-
main_logger.warning("ERS Warning erstrace,throttle,lstdout", extra=handlerconf.ERS)
221-
main_logger.info("ERS Info erstrace,throttle,lstdout", extra=handlerconf.ERS)
222-
main_logger.critical("ERS Fatal erstrace,lstdout", extra=handlerconf.ERS)
223-
main_logger.debug("ERS Debug none", extra=handlerconf.ERS)
224-
main_logger.error("ERS Error erstrace,throttle,lstdout,"
225-
"protobufstream(monkafka.cern.ch:30092)",
226-
extra=handlerconf.ERS
227-
)
228269

229270

230271
def test_fallback_handlers(log_level: str) -> None:

0 commit comments

Comments
 (0)