Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 1 | """ |
| 2 | Copyright 2020 The Magma Authors. |
| 3 | |
| 4 | This source code is licensed under the BSD-style license found in the |
| 5 | LICENSE file in the root directory of this source tree. |
| 6 | |
| 7 | Unless required by applicable law or agreed to in writing, software |
| 8 | distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | See the License for the specific language governing permissions and |
| 11 | limitations under the License. |
| 12 | """ |
| 13 | |
| 14 | import asyncio |
| 15 | from typing import Any |
| 16 | |
| 17 | from common.job import Job |
| 18 | from common.log_count_handler import MsgCounterHandler |
| 19 | from common.metrics import SERVICE_ERRORS |
| 20 | |
| 21 | # How frequently to poll systemd for error logs, in seconds |
| 22 | POLL_INTERVAL = 10 |
| 23 | |
| 24 | |
| 25 | class ServiceLogErrorReporter(Job): |
| 26 | """ Reports the number of logged errors for the service """ |
| 27 | |
| 28 | def __init__( |
| 29 | self, |
| 30 | loop: asyncio.BaseEventLoop, |
| 31 | service_config: Any, |
| 32 | handler: MsgCounterHandler, |
| 33 | ) -> None: |
| 34 | super().__init__(interval=POLL_INTERVAL, loop=loop) |
| 35 | self._service_config = service_config |
| 36 | self._handler = handler |
| 37 | |
| 38 | async def _run(self): |
| 39 | error_count = self._handler.pop_error_count() |
| 40 | SERVICE_ERRORS.inc(error_count) |