SIENTIAPDE-1169

Implement OPC writing metrics and enhance OPC class initialization

- Added metrics for counting predictions written to the OPC server and monitoring their response times.
- Enhanced the OPC class initialization to include the pod ID for better tracking.
- Updated the write method to increment the prediction count and observe response times.
This commit is contained in:
vitor-aignosi
2025-08-18 13:58:43 -03:00
parent 6eb71739db
commit e35eb300cb
3 changed files with 38 additions and 3 deletions

View File

@@ -1,4 +1,5 @@
import traceback
import time
from datetime import datetime
from pathlib import Path
from typing import Any
@@ -6,6 +7,7 @@ from asyncua.sync import Client
from asyncua.crypto.security_policies import SecurityPolicyBasic256
from asyncua.ua import DataValue, Variant, VariantType, DateTime
from regex import F
import metrics
from sientia_do.notifications.handlers import CoreNotificationHandler as NotificationHandler
from sientia_do.notifications.models import NotificationLevel
from sientia_do.temporal.utils.logger import Logger
@@ -38,7 +40,7 @@ class OpcRepository():
def __init__(self, id: str, url: str, logger: Logger,
notification_handler: NotificationHandler,
reconnection_interval: int = 60, server_uri: str = None, cert_path: str = None,
private_key_path: str = None, server_cert_path: str = None):
private_key_path: str = None, server_cert_path: str = None, pod_id: str = None):
self.url = url
self.id = id
self.server_uri = server_uri
@@ -90,6 +92,7 @@ class OpcRepository():
)
self.client.secure_channel_timeout = 10000000
self.client.session_timeout = 10000000
self.pod_id = pod_id
def connect(self) -> tuple[bool, dict[str, Any]]:
"""
@@ -215,6 +218,8 @@ class OpcRepository():
if not is_connected:
return False, error
start_time = time.time()
try:
node = self.client.get_node(node)
except Exception as e:
@@ -256,6 +261,21 @@ class OpcRepository():
try:
node.write_value(ua_data)
metrics.PREDICTION_OPC_WRITING_COUNT.labels(
pod_id=self.pod_id,
model_name=metadata['model_name'],
pipeline_name=metadata['workflow_name']
).inc()
end_time = time.time()
response_time = end_time - start_time
metrics.PREDICTION_OPC_WRITING_RESPONSE_TIME_MONITOR.labels(
pod_id=self.pod_id,
model_name=metadata['model_name'],
pipeline_name=metadata['workflow_name']
).observe(response_time)
except Exception as e:
trace = traceback.format_exc()
logger.custom_error(trace, metadata.get('schedule_name', 'N/A'))