Wei-Yu Chen | ad55cb8 | 2022-02-15 20:07:01 +0800 | [diff] [blame^] | 1 | # SPDX-FileCopyrightText: 2020 The Magma Authors. |
| 2 | # SPDX-FileCopyrightText: 2022 Open Networking Foundation <support@opennetworking.org> |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 5 | |
Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 6 | import logging |
| 7 | |
| 8 | import grpc |
| 9 | from google.protobuf.json_format import MessageToDict |
| 10 | from common.service_registry import ServiceRegistry |
| 11 | from orc8r.protos.eventd_pb2 import Event |
| 12 | from orc8r.protos.eventd_pb2_grpc import EventServiceStub |
| 13 | |
| 14 | EVENTD_SERVICE_NAME = "eventd" |
| 15 | DEFAULT_GRPC_TIMEOUT = 10 |
| 16 | |
| 17 | |
| 18 | def log_event(event: Event) -> None: |
| 19 | """ |
| 20 | Make RPC call to 'LogEvent' method of local eventD service |
| 21 | """ |
| 22 | try: |
| 23 | chan = ServiceRegistry.get_rpc_channel( |
| 24 | EVENTD_SERVICE_NAME, ServiceRegistry.LOCAL, |
| 25 | ) |
| 26 | except ValueError: |
| 27 | logging.error("Cant get RPC channel to %s", EVENTD_SERVICE_NAME) |
| 28 | return |
| 29 | client = EventServiceStub(chan) |
| 30 | try: |
| 31 | # Location will be filled in by directory service |
| 32 | client.LogEvent(event, DEFAULT_GRPC_TIMEOUT) |
| 33 | except grpc.RpcError as err: |
| 34 | if err.code() == grpc.StatusCode.UNAVAILABLE: |
| 35 | logging.debug( |
| 36 | "LogEvent will not occur unless eventd configuration " |
| 37 | "is set up.", |
| 38 | ) |
| 39 | else: |
| 40 | logging.error( |
| 41 | "LogEvent error for event: %s, [%s] %s", |
| 42 | MessageToDict(event), |
| 43 | err.code(), |
| 44 | err.details(), |
| 45 | ) |