Source code for opensearchpy.metrics.metrics_events
# SPDX-License-Identifier: Apache-2.0## The OpenSearch Contributors require contributions made to# this file be licensed under the Apache-2.0 license or a# compatible open source license.## Modifications Copyright OpenSearch Contributors. See# GitHub history for details.importtimefromtypingimportOptionalfromeventsimportEventsfromopensearchpy.metrics.metricsimportMetrics
[docs]classMetricsEvents(Metrics):""" The MetricsEvents class implements the Metrics abstract base class and tracks metrics such as start time, end time, and service time during request processing. """@propertydefstart_time(self)->Optional[float]:returnself._start_time@propertydefend_time(self)->Optional[float]:returnself._end_time@propertydefservice_time(self)->Optional[float]:returnself._service_timedef__init__(self)->None:self.events=Events()self._start_time:Optional[float]=Noneself._end_time:Optional[float]=Noneself._service_time:Optional[float]=None# Subscribe to the request_start and request_end eventsself.events.request_start+=self._on_request_startself.events.request_end+=self._on_request_enddefrequest_start(self)->None:self.events.request_start()def_on_request_start(self)->None:self._start_time=time.perf_counter()self._end_time=Noneself._service_time=Nonedefrequest_end(self)->None:self.events.request_end()def_on_request_end(self)->None:self._end_time=time.perf_counter()ifself._start_timeisnotNone:self._service_time=self._end_time-self._start_time