DUNE-DAQ
DUNE Trigger and Data Acquisition software
Toggle main menu visibility
Loading...
Searching...
No Matches
dunedaq
sourcecode
kafkaopmon
python
kafkaopmon
OpMonPublisher.py
Go to the documentation of this file.
1
#!/usr/bin/env python3
2
3
import
logging
4
import
sys
5
6
from
google.protobuf.message
import
Message
as
Msg
7
from
kafka
import
KafkaProducer
8
from
opmonlib.conf
import
OpMonConf
9
from
opmonlib.opmon_entry_pb2
import
OpMonEntry
10
from
opmonlib.publisher_base
import
OpMonPublisherBase
11
from
opmonlib.utils
import
logging_log_level_from_str, setup_rich_handler
12
13
14
class
OpMonPublisher
(
OpMonPublisherBase
):
15
"""Tool for publishing operational monitoring metrics to kafka."""
16
17
def
__init__
(self, conf: OpMonConf) ->
None
:
18
"""Construct the object to publish OpMon metrics to kafka."""
19
super().
__init__
()
20
self.
log
= logging.getLogger(
"OpMonPublisher"
)
21
self.
conf
= conf
22
if
isinstance(self.
conf
.level, str):
23
self.
conf
.level = logging_log_level_from_str(self.
conf
.level)
24
self.
log
.setLevel(self.
conf
.level)
25
self.
log
.addHandler(setup_rich_handler())
26
27
if
self.
conf
.opmon_type !=
"stream"
:
28
self.
log
.
error
(
"Type must be stream to publish to kafka."
)
29
sys.exit(1)
30
31
if
self.
conf
.bootstrap ==
""
:
32
self.
log
.warning(
33
"There is no boostrap provided, not initializing publisher to topic %s"
,
34
self.
conf
.default_topic,
35
)
36
self.
opmon_producer
=
None
37
return
38
39
self.
default_topic
=
"monitoring."
+ self.
conf
.topic
40
self.
publisher
= KafkaProducer(
41
bootstrap_servers=conf.bootstrap,
42
value_serializer=
lambda
v: v.SerializeToString(),
43
key_serializer=
lambda
k: str(k).encode(
"utf-8"
),
44
)
45
46
super().
__post_init__
()
47
return
48
49
def
extract_key
(self, opmon_entry: OpMonEntry) -> str:
50
"""Extract the key from the OpMonEntry."""
51
self.
check_publisher
()
52
key = str(opmon_entry.origin.session)
53
if
opmon_entry.origin.application !=
""
:
54
key +=
"."
+ opmon_entry.origin.application
55
if
opmon_entry.origin.substructure:
56
for
substructure_id
in
opmon_entry.origin.substructure:
57
key +=
"."
+ substructure_id
58
key +=
"/"
+ str(opmon_entry.measurement)
59
return
key
60
61
def
publish
(
62
self,
63
message: Msg,
64
custom_origin: dict[str, str] |
None
=
None
,
65
level: int | str |
None
=
None
,
66
) ->
None
:
67
"""Send an OpMonEntry to Kafka."""
68
if
not
isinstance(message, Msg):
69
self.
log
.
error
(
"Passed message needs to be of type google.protobuf.message"
)
70
return
71
if
not
level:
72
level = self.
conf
.level
73
if
isinstance(level, str):
74
level = logging_log_level_from_str(level)
75
if
level < self.
conf
.level:
76
return
77
78
metric = self.
to_entry
(message=message, custom_origin=custom_origin)
79
80
if
len(metric.data) == 0:
81
self.
log
.warning(
"OpMonEntry of type %s has no data"
, message.__name__)
82
return
83
84
target_topic = self.
extract_topic
(message)
85
target_key = self.
extract_key
(metric)
86
87
self.
publisher
.send(target_topic, value=metric, key=target_key)
88
return
kafkaopmon.OpMonPublisher.OpMonPublisher
Definition
OpMonPublisher.py:14
kafkaopmon.OpMonPublisher.OpMonPublisher.extract_key
str extract_key(self, OpMonEntry opmon_entry)
Definition
OpMonPublisher.py:49
kafkaopmon.OpMonPublisher.OpMonPublisher.publish
None publish(self, Msg message, dict[str, str]|None custom_origin=None, int|str|None level=None)
Definition
OpMonPublisher.py:66
kafkaopmon.OpMonPublisher.OpMonPublisher.opmon_producer
opmon_producer
Definition
OpMonPublisher.py:36
kafkaopmon.OpMonPublisher.OpMonPublisher.__init__
None __init__(self, OpMonConf conf)
Definition
OpMonPublisher.py:17
opmonlib.publisher_base.OpMonPublisherBase
Definition
publisher_base.py:18
opmonlib.publisher_base.OpMonPublisherBase.log
log
Definition
publisher_base.py:30
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.conf
conf
Definition
publisher_base.py:33
opmonlib.publisher_base.OpMonPublisherBase.__post_init__
None __post_init__(self)
Definition
publisher_base.py:28
opmonlib.publisher_base.OpMonPublisherBase.publisher
publisher
Definition
publisher_base.py:39
opmonlib.publisher_base.OpMonPublisherBase.extract_topic
str extract_topic(self, Msg message)
Definition
publisher_base.py:67
opmonlib.publisher_base.OpMonPublisherBase.default_topic
default_topic
Definition
publisher_base.py:42
opmonlib.publisher_base.OpMonPublisherBase.check_publisher
None check_publisher(self)
Definition
publisher_base.py:57
opmonlib.conf
Definition
conf.py:1
opmonlib::opmon_entry_pb2
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