Commit 22282375 authored by Carlos Natalino's avatar Carlos Natalino
Browse files

Improving Prometheus metrics declaration.

parent 598ccfa6
Loading
Loading
Loading
Loading
+8 −28
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ import random
import grpc
import numpy as np
import redis
from prometheus_client import Histogram

from common.Constants import ServiceNameEnum
from common.method_wrappers.Decorator import (MetricsPool, MetricTypeEnum,
                                              safe_and_metered_rpc_method)
@@ -36,7 +38,6 @@ from dbscanserving.client.DbscanServingClient import DbscanServingClient
from monitoring.client.MonitoringClient import MonitoringClient
from opticalattackmitigator.client.OpticalAttackMitigatorClient import \
    OpticalAttackMitigatorClient
from prometheus_client import Histogram

LOGGER = logging.getLogger(__name__)

@@ -51,29 +52,11 @@ METRICS_POOL_DETAILS = MetricsPool(
    },
)


metric_labels = dict(operation="detect")
histogram_duration: Histogram = METRICS_POOL_DETAILS.get_or_create(
METRIC_LABELS = dict(operation="detect")
HISTOGRAM_DURATION: Histogram = METRICS_POOL_DETAILS.get_or_create(
    "details", MetricTypeEnum.HISTOGRAM_DURATION
)


DETECTION_RESPONSE_TIME = Histogram(
    "tfs_opticalattackdetector_inference_response_time",
    "Time taken by the inference component to reply",
)
MONITORING_RESPONSE_TIME = Histogram(
    "tfs_opticalattackdetector_monitoring_response_time",
    "Time taken by the monitoring component to reply",
)
MITIGATION_RESPONSE_TIME = Histogram(
    "tfs_opticalattackdetector_mitigation_response_time",
    "Time taken by the attack mitigator to reply",
)
CACHE_RESPONSE_TIME = Histogram(
    "tfs_opticalattackdetector_cache_response_time", "Time taken by the cache to reply"
)

monitoring_client: MonitoringClient = MonitoringClient()
dbscanserving_client: DbscanServingClient = DbscanServingClient()
attack_mitigator_client: OpticalAttackMitigatorClient = OpticalAttackMitigatorClient()
@@ -197,8 +180,7 @@ class OpticalAttackDetectorServiceServicerImpl(OpticalAttackDetectorServiceServi
                    detection_sample.append(random.uniform(5000.0, 6000.0))

        # adding the sample to the cache and recovering the cache
        with histogram_duration.labels(step="cachefetch", **metric_labels).time():
            # with CACHE_RESPONSE_TIME.time():
        with HISTOGRAM_DURATION.labels(step="cachefetch", **METRIC_LABELS).time():
            r.rpush(
                "opm_{}".format(s_uuid.replace("-", "_")),
                pickle.dumps(tuple(detection_sample)),
@@ -215,8 +197,7 @@ class OpticalAttackDetectorServiceServicerImpl(OpticalAttackDetectorServiceServi
                detection_sample.features.append(feature)
            detection_request.samples.append(detection_sample)

        with histogram_duration.labels(step="uldetection", **metric_labels).time():
            # with DETECTION_RESPONSE_TIME.time():
        with HISTOGRAM_DURATION.labels(step="uldetection", **METRIC_LABELS).time():
            response: dbscan.DetectionResponse = dbscanserving_client.Detect(
                detection_request
            )
@@ -236,15 +217,14 @@ class OpticalAttackDetectorServiceServicerImpl(OpticalAttackDetectorServiceServi
        else:
            kpi.kpi_value.int32Val = 0

        with histogram_duration.labels(step="includekpi", **metric_labels).time():
            # with MONITORING_RESPONSE_TIME.time():
        with HISTOGRAM_DURATION.labels(step="includekpi", **METRIC_LABELS).time():
            monitoring_client.IncludeKpi(kpi)

        # if -1 in response.cluster_indices:  # attack detected
        if kpi.kpi_value.int32Val == -1:
            attack = AttackDescription()
            attack.cs_id.uuid = request.service_id.service_uuid.uuid
            with histogram_duration.labels(step="mitigation", **metric_labels).time():
            with HISTOGRAM_DURATION.labels(step="mitigation", **METRIC_LABELS).time():
                # with MITIGATION_RESPONSE_TIME.time():
                response: AttackResponse = attack_mitigator_client.NotifyAttack(attack)