blob: 3211a51b8e74adf749ee86d8bb1c51e0d139427c [file] [log] [blame]
Wei-Yu Chenad55cb82022-02-15 20:07:01 +08001# 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 Chen49950b92021-11-08 19:19:18 +08005
Wei-Yu Chen49950b92021-11-08 19:19:18 +08006import logging
7
8import grpc
9from google.protobuf.json_format import MessageToDict
10from common.service_registry import ServiceRegistry
11from orc8r.protos.eventd_pb2 import Event
12from orc8r.protos.eventd_pb2_grpc import EventServiceStub
13
14EVENTD_SERVICE_NAME = "eventd"
15DEFAULT_GRPC_TIMEOUT = 10
16
17
18def 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 )