|
20 | 20 | import DIRAC
|
21 | 21 |
|
22 | 22 | from DIRAC import gConfig, gLogger, S_OK, S_ERROR
|
| 23 | +from DIRAC.ConfigurationSystem.Client.Helpers.Operations import Operations |
23 | 24 | from DIRAC.Core.Utilities import DErrno
|
24 | 25 | from DIRAC.Core.DISET.AuthManager import AuthManager
|
25 | 26 | from DIRAC.Core.Utilities.JEncode import decode, encode
|
| 27 | +from DIRAC.Core.Utilities import Network, TimeUtilities |
26 | 28 | from DIRAC.Core.Utilities.ReturnValues import isReturnStructure
|
27 | 29 | from DIRAC.Core.Security.X509Chain import X509Chain # pylint: disable=import-error
|
28 | 30 | from DIRAC.Resources.IdProvider.Utilities import getProvidersForInstance
|
@@ -475,6 +477,12 @@ def __initialize(cls, request):
|
475 | 477 |
|
476 | 478 | cls.initializeHandler(cls._componentInfoDict)
|
477 | 479 |
|
| 480 | + cls.activityMonitoringReporter = None |
| 481 | + if "Monitoring" in Operations().getMonitoringBackends(monitoringType="ServiceMonitoring"): |
| 482 | + from DIRAC.MonitoringSystem.Client.MonitoringReporter import MonitoringReporter |
| 483 | + |
| 484 | + cls.activityMonitoringReporter = MonitoringReporter(monitoringType="ServiceMonitoring") |
| 485 | + |
478 | 486 | cls.__init_done = True
|
479 | 487 |
|
480 | 488 | return S_OK()
|
@@ -662,26 +670,46 @@ def on_finish(self):
|
662 | 670 | Called after the end of HTTP request.
|
663 | 671 | Log the request duration
|
664 | 672 | """
|
665 |
| - elapsedTime = 1000.0 * self.request.request_time() |
| 673 | + elapsedTime = self.request.request_time() |
| 674 | + |
666 | 675 | credentials = self.srv_getFormattedRemoteCredentials()
|
667 | 676 |
|
668 | 677 | argsString = f"OK {self._status_code}"
|
| 678 | + monitoringRetStatus = "Unknown" |
669 | 679 | # Finish with DIRAC result
|
670 | 680 | if isReturnStructure(self.__result):
|
671 | 681 | if self.__result["OK"]:
|
672 | 682 | argsString = "OK"
|
| 683 | + monitoringRetStatus = "OK" |
673 | 684 | else:
|
674 | 685 | argsString = f"ERROR: {self.__result['Message']}"
|
| 686 | + monitoringRetStatus = "ERROR" |
675 | 687 | if callStack := self.__result.pop("CallStack", None):
|
676 | 688 | argsString += "\n" + "".join(callStack)
|
677 | 689 | # If bad HTTP status code
|
678 | 690 | if self._status_code >= 400:
|
679 | 691 | argsString = f"ERROR {self._status_code}: {self._reason}"
|
680 | 692 |
|
681 | 693 | self.log.notice(
|
682 |
| - "Returning response", f"{credentials} {self._fullComponentName} ({elapsedTime:.2f} ms) {argsString}" |
| 694 | + "Returning response", |
| 695 | + f"{credentials} {self._fullComponentName} ({1000.0 * elapsedTime:.2f} ms) {argsString}", |
683 | 696 | )
|
684 | 697 |
|
| 698 | + if self.activityMonitoringReporter: |
| 699 | + record = { |
| 700 | + "timestamp": int(TimeUtilities.toEpochMilliSeconds()), |
| 701 | + "Host": Network.getFQDN(), |
| 702 | + "ServiceName": "_".join(self._fullComponentName.split("/")), |
| 703 | + "Location": self.request.uri, |
| 704 | + "ResponseTime": elapsedTime, |
| 705 | + # Take the method name from the POST call |
| 706 | + "MethodName": self.request.arguments.get("method", ["Unknown"])[0], |
| 707 | + "Protocol": "https", |
| 708 | + "Status": monitoringRetStatus, |
| 709 | + } |
| 710 | + |
| 711 | + self.activityMonitoringReporter.addRecord(record) |
| 712 | + |
685 | 713 | def _gatherPeerCredentials(self, grants: list = None) -> dict:
|
686 | 714 | """Return a dictionary designed to work with the :py:class:`AuthManager <DIRAC.Core.DISET.AuthManager.AuthManager>`,
|
687 | 715 | already written for DISET and re-used for HTTPS.
|
|
0 commit comments