DUNE-DAQ
DUNE Trigger and Data Acquisition software
Toggle main menu visibility
Loading...
Searching...
No Matches
dunedaq
sourcecode
opmonlib
python
opmonlib
publisher.py
Go to the documentation of this file.
1
import
logging
2
import
sys
3
4
from
google.protobuf.json_format
import
MessageToJson
5
from
google.protobuf.message
import
Message
as
Msg
6
7
from
opmonlib.conf
import
OpMonConf
8
from
opmonlib.publisher_base
import
OpMonPublisherBase
9
from
opmonlib.utils
import
(
10
LoggingFormatter,
11
extract_opmon_file_path,
12
full_log_format,
13
setup_rich_handler,
14
)
15
16
17
class
OpMonPublisher
(
OpMonPublisherBase
):
18
"""Publish operational monitoring metrics to file or stream."""
19
20
def
__init__
(
21
self,
22
conf: OpMonConf,
23
log_level: int | str = logging.INFO,
24
rich_handler: bool =
False
,
25
) ->
None
:
26
"""Construct the object to publish OpMon metrics to stdout."""
27
super().
__init__
()
28
self.
log
= logging.getLogger(
"OpMonPublisher"
)
29
if
not
isinstance(log_level, int):
30
log_level = self.
log_level_to_int
(log_level)
31
self.
log
.setLevel(log_level)
32
self.
log
.addHandler(setup_rich_handler())
33
34
self.
conf
= conf
35
self.
conf
.level = self.
log_level_to_int
(self.
conf
.level)
36
37
if
self.
conf
.opmon_type ==
"stdout"
:
38
if
rich_handler:
39
handler = setup_rich_handler()
40
else
:
41
handler = logging.StreamHandler(sys.stdout)
42
handler.setFormatter(
LoggingFormatter
(fmt=full_log_format))
43
elif
self.
conf
.opmon_type ==
"file"
:
44
self.
conf
.path = extract_opmon_file_path(self.
conf
.path)
45
handler = logging.FileHandler(self.
conf
.path)
46
elif
self.
conf
.opmon_type ==
"stream"
:
47
self.
log
.
error
(
"Type must not be stream to use file or stdout handling."
)
48
sys.exit(1)
49
else
:
50
self.
log
.
error
(
"Unsupported OpMon type."
)
51
sys.exit(1)
52
53
self.
default_topic
=
"monitoring."
+ self.
conf
.topic
54
self.
publisher
= logging.getLogger(self.
default_topic
)
55
self.
publisher
.addHandler(handler)
56
self.
publisher
.setLevel(self.
conf
.level)
57
58
super().
__post_init__
()
59
return
60
61
def
publish_message
(
62
self, logger: logging.Logger, level: int | str, message: str
63
) ->
None
:
64
"""Log the metric with the appropriate level."""
65
method = getattr(logger, self.
log_level_to_str
(level).lower(), logger.info)
66
method(message)
67
return
68
69
def
publish
(
70
self,
71
message: Msg,
72
custom_origin: dict[str, str] |
None
=
None
,
73
level: int | str |
None
=
None
,
74
) ->
None
:
75
"""Publish the message to either a file or the terminal."""
76
if
not
isinstance(message, Msg):
77
self.
log
.
error
(
"Passed message needs to be of type google.protobuf.message"
)
78
return
79
80
if
not
level:
81
level = self.
conf
.level
82
if
not
isinstance(level, int):
83
level = self.
log_level_to_int
(level)
84
if
level < self.
conf
.level:
85
return
86
87
metric = self.
to_entry
(message=message, custom_origin=custom_origin)
88
target_topic = self.
extract_topic
(message)
89
publishing_logger = logging.getLogger(f
"{self.publisher.name}.{target_topic}"
)
90
self.
publish_message
(publishing_logger, level, MessageToJson(metric))
91
return
opmonlib.publisher.OpMonPublisher
Definition
publisher.py:17
opmonlib.publisher.OpMonPublisher.publish
None publish(self, Msg message, dict[str, str]|None custom_origin=None, int|str|None level=None)
Definition
publisher.py:74
opmonlib.publisher.OpMonPublisher.__init__
None __init__(self, OpMonConf conf, int|str log_level=logging.INFO, bool rich_handler=False)
Definition
publisher.py:25
opmonlib.publisher.OpMonPublisher.publish_message
None publish_message(self, logging.Logger logger, int|str level, str message)
Definition
publisher.py:63
opmonlib.publisher_base.OpMonPublisherBase
Definition
publisher_base.py:18
opmonlib.publisher_base.OpMonPublisherBase.to_entry
OpMonEntry to_entry(self, Msg message, dict[str, str]|None custom_origin)
Definition
publisher_base.py:134
opmonlib.publisher_base.OpMonPublisherBase.__post_init__
None __post_init__(self)
Definition
publisher_base.py:28
opmonlib.publisher_base.OpMonPublisherBase.log_level_to_str
str log_level_to_str(self, str|int level)
Definition
publisher_base.py:162
opmonlib.publisher_base.OpMonPublisherBase.log_level_to_int
int log_level_to_int(self, str|int level)
Definition
publisher_base.py:145
opmonlib.publisher_base.OpMonPublisherBase.extract_topic
str extract_topic(self, Msg message)
Definition
publisher_base.py:67
opmonlib.utils.LoggingFormatter
Definition
utils.py:88
opmonlib.conf
Definition
conf.py:1
opmonlib.publisher_base
Definition
publisher_base.py:1
opmonlib.utils
Definition
utils.py:1
error
Factory couldn t std::string alg_name Invalid configuration error
Definition
Issues.hpp:34
Generated on
for DUNE-DAQ by
1.17.0