SIENTIAPDE-1083: add prometheus metrics to opc_manager.
This commit is contained in:
@@ -29,6 +29,7 @@ class IngestorManager():
|
||||
self.opc_servers = {}
|
||||
|
||||
self.notification_handler = notification_handler
|
||||
self.pod_id = pod_id
|
||||
|
||||
def initialize_opc_from_config(self, server_config: dict,
|
||||
data_manager: DataManager, logger: Logger) -> OpcManager | None:
|
||||
@@ -56,7 +57,7 @@ class IngestorManager():
|
||||
manager = OpcManager(
|
||||
server_config['name'], server_config['url'],
|
||||
data_manager, logger, server_config['server_uri'],
|
||||
self.notification_handler, server_config.get('cert_path'),
|
||||
self.notification_handler, self.pod_id, server_config.get('cert_path'),
|
||||
server_config.get('private_key_path'),
|
||||
server_config.get('server_cert_path')
|
||||
)
|
||||
|
||||
@@ -7,11 +7,12 @@ from asyncua.sync import Client
|
||||
from sientia_do.notifications.models import NotificationLevel
|
||||
from sientia_do.notifications.handlers import NotificationHandler
|
||||
from ingestor.managers.data_manager import DataManager
|
||||
import ingestor.metrics as metrics
|
||||
|
||||
|
||||
class OpcManager():
|
||||
def __init__(self, name: str, url: str, data_manager: DataManager,
|
||||
logger: Logger, server_uri: str, notification_handler: NotificationHandler,
|
||||
logger: Logger, server_uri: str, notification_handler: NotificationHandler, pod_id: str,
|
||||
cert_path: str = None, private_key_path: str = None, server_cert_path: str = None):
|
||||
self.url = url
|
||||
self.name = name
|
||||
@@ -26,8 +27,10 @@ class OpcManager():
|
||||
self.nodes = {}
|
||||
self.subscriptions = {}
|
||||
self.data_manager = data_manager
|
||||
|
||||
self.notification_handler = notification_handler
|
||||
self.pod_id = pod_id
|
||||
metrics.OPC_CONNECTION_STATUS.labels(pod_id=self.pod_id, server_name=self.name, server_url=self.url).set(0)
|
||||
metrics.OPC_TAGS_SUBSCRIBED.labels(pod_id=self.pod_id, server_name=self.name).set(0)
|
||||
|
||||
def __str__(self):
|
||||
return f"OpcManager(name={self.name}, url={self.url}, server_uri={self.server_uri})\n" \
|
||||
@@ -82,11 +85,20 @@ class OpcManager():
|
||||
Exception: If the connection to the OPC server fails.
|
||||
"""
|
||||
|
||||
self.client = Client(self.url)
|
||||
if self.cert_path:
|
||||
self.set_security()
|
||||
self.logger.info('Starting connection...')
|
||||
self.client.connect()
|
||||
metrics.OPC_CONNECTIONS_TOTAL.labels(pod_id=self.pod_id, server_name=self.name).inc()
|
||||
try:
|
||||
self.client = Client(self.url)
|
||||
if self.cert_path:
|
||||
self.set_security()
|
||||
self.logger.info(f'Starting connection to {self.name}...')
|
||||
self.client.connect()
|
||||
metrics.OPC_CONNECTION_STATUS.labels(pod_id=self.pod_id, server_name=self.name, server_url=self.url).set(1)
|
||||
self.logger.info(f'Connection to {self.name} successful.')
|
||||
except Exception as e:
|
||||
metrics.OPC_CONNECTION_STATUS.labels(pod_id=self.pod_id, server_name=self.name, server_url=self.url).set(0)
|
||||
metrics.OPC_CONNECTIONS_FAILED.labels(pod_id=self.pod_id, server_name=self.name).inc()
|
||||
self.logger.error(f"Failed to connect to {self.name}: {e}")
|
||||
raise
|
||||
|
||||
def create_subscription(self, name: str, period: int = 500):
|
||||
"""
|
||||
@@ -105,11 +117,14 @@ class OpcManager():
|
||||
|
||||
if not self.client:
|
||||
raise ValueError("Client not connected. Call connect first.")
|
||||
|
||||
p = period if period != None else 500
|
||||
self.subscriptions[name] = self.client.create_subscription(
|
||||
p, self)
|
||||
self.logger.info('Subscription created.')
|
||||
try:
|
||||
p = period if period is not None else 500
|
||||
self.subscriptions[name] = self.client.create_subscription(p, self)
|
||||
self.logger.info(f'Subscription {name} created on {self.name}.')
|
||||
metrics.OPC_SUBSCRIPTIONS_CREATED.labels(pod_id=self.pod_id, server_name=self.name, slot_name=name).inc()
|
||||
except Exception as e:
|
||||
self.logger.error(f"Failed to create subscription {name} on {self.name}: {e}")
|
||||
raise
|
||||
|
||||
def subscribe(self, subscription: str, nodes: dict, collect_period: int):
|
||||
"""
|
||||
@@ -129,14 +144,13 @@ class OpcManager():
|
||||
"""
|
||||
|
||||
if not self.subscriptions.get(subscription):
|
||||
raise ValueError(
|
||||
"Subscription not created. Call create_subscription first.")
|
||||
raise ValueError("Subscription not created. Call create_subscription first.")
|
||||
|
||||
self.logger.info(f"Subscribing to {subscription}...")
|
||||
self.logger.info(f"Subscribing to {subscription} on {self.name}...")
|
||||
self.logger.info(f"Subscribing to nodes: {nodes}")
|
||||
self.addr_nodes = [self.client.get_node(
|
||||
n) for n in nodes if n not in self.nodes]
|
||||
self.addr_nodes = [self.client.get_node(n) for n in nodes if n not in self.nodes]
|
||||
self.nodes.update(nodes)
|
||||
metrics.OPC_TAGS_SUBSCRIBED.labels(pod_id=self.pod_id, server_name=self.name).set(len(self.nodes))
|
||||
self.collect_period = collect_period
|
||||
|
||||
for node, config in self.nodes.items():
|
||||
@@ -202,6 +216,8 @@ class OpcManager():
|
||||
finally:
|
||||
del self.client
|
||||
self.client = None
|
||||
metrics.OPC_CONNECTION_STATUS.labels(pod_id=self.pod_id, server_name=self.name, server_url=self.url).set(0)
|
||||
metrics.OPC_TAGS_SUBSCRIBED.labels(pod_id=self.pod_id, server_name=self.name).set(0)
|
||||
self.logger.warning("Disconnected from OPC UA server.")
|
||||
|
||||
def datachange_notification(self, node, _val, data):
|
||||
@@ -236,6 +252,7 @@ class OpcManager():
|
||||
|
||||
self.nodes[tag]['cycle_rule']['cycle_count'] = 0
|
||||
self.non_receive_count = 0
|
||||
metrics.OPC_CYCLES_WITHOUT_DATA.labels(pod_id=self.pod_id, server_name=self.name).set(0)
|
||||
|
||||
data = {
|
||||
'tag': tag,
|
||||
@@ -278,6 +295,7 @@ class OpcManager():
|
||||
"""
|
||||
|
||||
self.non_receive_count += 1
|
||||
metrics.OPC_CYCLES_WITHOUT_DATA.labels(pod_id=self.pod_id, server_name=self.name).set(self.non_receive_count)
|
||||
if self.non_receive_count >= 5:
|
||||
self.notification_handler.build_and_send_notification(
|
||||
notification_id=f'OPC_LISTENNING_STOPPED__{self.name}',
|
||||
@@ -287,6 +305,7 @@ class OpcManager():
|
||||
level=NotificationLevel.ERROR
|
||||
)
|
||||
if self.non_receive_count >= 15:
|
||||
metrics.OPC_RECONNECTIONS_TOTAL.labels(pod_id=self.pod_id, server_name=self.name).inc()
|
||||
self.notification_handler.build_and_send_notification(
|
||||
notification_id=f'OPC_CONNECTION_RETRY__{self.name}',
|
||||
message=f'Retrying to connect to server {self.name}',
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
from prometheus_client import Counter, Gauge, Histogram
|
||||
|
||||
# It's useful to have a common set of labels, like the pod_id.
|
||||
# We'll add 'pod_id' to many metrics to distinguish instances.
|
||||
POD_ID_LABEL = ["pod_id"]
|
||||
SERVER_LABELS = ["pod_id", "server_name", "server_url"]
|
||||
SLOT_LABELS = ["pod_id", "slot_id"]
|
||||
TAG_LABELS = ["pod_id", "server_name", "tag_id", "tag_name"]
|
||||
KAFKA_LABELS = ["pod_id", "topic"]
|
||||
REDIS_LABELS = ["pod_id", "operation"]
|
||||
NOTIFICATION_LABELS = ["pod_id", "level", "block"]
|
||||
@@ -37,17 +33,14 @@ APP_UP = Gauge(
|
||||
ACTIVE_INGESTORS = Gauge(
|
||||
"ingestor_active_total",
|
||||
"Number of active ingestors reported by Redis",
|
||||
# No pod_id here, as it's a global view from Redis
|
||||
)
|
||||
SLOTS_TOTAL = Gauge(
|
||||
"ingestor_slots_total",
|
||||
"Total number of slots configured in Redis",
|
||||
# No pod_id here, as it's a global view from Redis
|
||||
)
|
||||
LEASES_TOTAL = Gauge(
|
||||
"ingestor_leases_total",
|
||||
"Total number of leases (allocated slots) in Redis",
|
||||
# No pod_id here, as it's a global view from Redis
|
||||
)
|
||||
SLOTS_MANAGED = Gauge(
|
||||
"ingestor_slots_managed_current",
|
||||
@@ -101,19 +94,6 @@ OPC_TAGS_SUBSCRIBED = Gauge(
|
||||
"Current number of OPC tags subscribed on a server",
|
||||
["pod_id", "server_name"],
|
||||
)
|
||||
OPC_DATACHANGE_NOTIFICATIONS = Counter(
|
||||
"opc_datachange_notifications_total",
|
||||
"Total data change notifications received (tag readings)",
|
||||
TAG_LABELS,
|
||||
)
|
||||
OPC_TAG_LAST_VALUE = Gauge(
|
||||
"opc_tag_last_value", "The last value read from an OPC tag", TAG_LABELS
|
||||
)
|
||||
OPC_TAG_LAST_READ_TIMESTAMP = Gauge(
|
||||
"opc_tag_last_read_timestamp_seconds",
|
||||
"Timestamp of the last value read from an OPC tag",
|
||||
TAG_LABELS,
|
||||
)
|
||||
OPC_CYCLES_WITHOUT_DATA = Gauge(
|
||||
"opc_cycles_without_data",
|
||||
"Current number of cycles without receiving data from a server",
|
||||
|
||||
Reference in New Issue
Block a user