blob: 3f8b63e088194c6e392f1a402aaca5401210d7b2 [file] [log] [blame]
Wei-Yu Chen49950b92021-11-08 19:19:18 +08001"""
2Copyright 2020 The Magma Authors.
3
4This source code is licensed under the BSD-style license found in the
5LICENSE file in the root directory of this source tree.
6
7Unless required by applicable law or agreed to in writing, software
8distributed under the License is distributed on an "AS IS" BASIS,
9WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10See the License for the specific language governing permissions and
11limitations under the License.
12"""
13import logging
14
15import grpc
16from google.protobuf.json_format import MessageToDict
17from common.service_registry import ServiceRegistry
18from orc8r.protos.eventd_pb2 import Event
19from orc8r.protos.eventd_pb2_grpc import EventServiceStub
20
21EVENTD_SERVICE_NAME = "eventd"
22DEFAULT_GRPC_TIMEOUT = 10
23
24
25def log_event(event: Event) -> None:
26 """
27 Make RPC call to 'LogEvent' method of local eventD service
28 """
29 try:
30 chan = ServiceRegistry.get_rpc_channel(
31 EVENTD_SERVICE_NAME, ServiceRegistry.LOCAL,
32 )
33 except ValueError:
34 logging.error("Cant get RPC channel to %s", EVENTD_SERVICE_NAME)
35 return
36 client = EventServiceStub(chan)
37 try:
38 # Location will be filled in by directory service
39 client.LogEvent(event, DEFAULT_GRPC_TIMEOUT)
40 except grpc.RpcError as err:
41 if err.code() == grpc.StatusCode.UNAVAILABLE:
42 logging.debug(
43 "LogEvent will not occur unless eventd configuration "
44 "is set up.",
45 )
46 else:
47 logging.error(
48 "LogEvent error for event: %s, [%s] %s",
49 MessageToDict(event),
50 err.code(),
51 err.details(),
52 )