SIENTIAPDE-1712

Refactor metrics and API handling for improved consistency and clarity

- Removed the SIENTIA_CORE_LABELS constant and replaced it with CORE_LABELS for uniformity across metrics.
- Updated the API class to ensure operation_type is always included in core labels for PI Web API metrics.
- Simplified metric tag handling in the Gates class by consolidating common tags into a single core_tags dictionary.
- Enhanced the PredictionProcess class to improve error handling and variable naming for clarity.
This commit is contained in:
vitor-aignosi
2026-03-23 10:17:57 -03:00
parent 44558b4415
commit e17824eb85
5 changed files with 51 additions and 104 deletions

View File

@@ -74,34 +74,26 @@ class API(SientiaMonitoring):
def get_pi_web_api_core_labels(
self,
metadata: dict[str, Any],
operation_type: str | None = None,
operation_type: str = 'write_pi_web_api_data',
) -> dict[str, Any]:
"""
Generate core labels for metrics, optionally including operation_type.
Generate core labels for PI Web API metrics.
This override keeps compatibility with the base implementation while adding
a convenience overload behavior:
- When operation_type is provided, it behaves exactly like the base class,
returning labels that include the operation_type key.
- When operation_type is omitted (None), it removes the operation_type key
from the resulting labels. This is useful for metrics, such as the PI Web
API metrics, that are defined without the operation_type label.
PI Web API metrics in laborious use the shared ``CORE_LABELS`` from
``sientia_do``, which includes ``operation_type``. For this reason,
operation_type must always be present in emitted labels.
Args:
- metadata (dict[str, Any]): Workflow execution metadata used to derive labels
- operation_type (str | None): Optional operation type label. If None, the
operation_type key will be removed from the returned labels.
- metadata (dict[str, Any]): Workflow execution metadata used to derive labels.
- operation_type (str): Operation type label for metric cardinality.
Return:
dict[str, Any]: Core labels dictionary, with operation_type only when provided
dict[str, Any]: Core labels dictionary including operation_type.
"""
base_labels = super().get_core_labels(
return super().get_core_labels(
metadata=metadata,
operation_type=operation_type or '-',
operation_type=operation_type,
)
if operation_type is None:
base_labels.pop('operation_type', None)
return base_labels
def close(self) -> None:
"""

View File

@@ -703,34 +703,30 @@ class Gates(MinioManager):
self.info(f'Writing metrics for model {metadata["model_name"]}', metadata)
core_tags = {
'pod_id': self.pod_id,
'runtime': self.runtime,
'operation_type': 'predict',
'model_name': metadata['model_name'],
'workflow_name': metadata['workflow_name'],
}
await self.emit_metric(
metric_object=metrics.PREDICTIONS_WRITTEN_COUNT,
tags={
'pod_id': self.pod_id,
'model_name': metadata['model_name'],
'workflow_name': metadata['workflow_name'],
},
tags=core_tags,
)
await self.emit_metric(
metric_object=metrics.PREDICTION_CONFIDENCE_MONITOR,
method='set',
tags={
'pod_id': self.pod_id,
'model_name': metadata['model_name'],
'workflow_name': metadata['workflow_name'],
},
tags=core_tags,
value=prediction_confidence,
)
await self.emit_metric(
metric_object=metrics.PREDICTION_RESPONSE_TIME_MONITOR,
method='observe',
tags={
'pod_id': self.pod_id,
'model_name': metadata['model_name'],
'workflow_name': metadata['workflow_name'],
},
tags=core_tags,
value=response_time,
)
@@ -741,9 +737,7 @@ class Gates(MinioManager):
metric_object=metrics.PREDICTION_OPC_WRITING_RESPONSE_TIME_MONITOR,
method='observe',
tags={
'pod_id': self.pod_id,
'model_name': metadata['model_name'],
'workflow_name': metadata['workflow_name'],
**core_tags,
'opc_server_id': server_id,
'tag': tag,
},
@@ -753,9 +747,7 @@ class Gates(MinioManager):
await self.emit_metric(
metric_object=metrics.PREDICTION_OPC_WRITING_COUNT,
tags={
'pod_id': self.pod_id,
'model_name': metadata['model_name'],
'workflow_name': metadata['workflow_name'],
**core_tags,
'opc_server_id': server_id,
'tag': tag,
},