Skip to content

Commit

Permalink
implement healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
airkei committed Feb 3, 2025
1 parent 2db4303 commit 0a523e2
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 10 deletions.
14 changes: 12 additions & 2 deletions src/otaclient/_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.
"""Configure the logging for otaclient."""


from __future__ import annotations

import atexit
Expand Down Expand Up @@ -60,6 +59,10 @@ def __init__(self, logging_upload_endpoint, ecu_id: str):
def send(self, log_type: LogType, message: str, timeout: int) -> None:
pass

@abstractmethod
def check(self, timeout: int) -> None:
pass


class TransmitterGrpc(Transmitter):
def __init__(self, logging_upload_endpoint: AnyHttpUrl, ecu_id: str):
Expand Down Expand Up @@ -100,6 +103,9 @@ def send(self, log_type: LogType, message: str, timeout: int) -> None:
with contextlib.suppress(Exception):
self._session.post(self.log_upload_endpoint, data=message, timeout=timeout)

def check(self, timeout: int) -> None:
pass


class TransmitterFactory:
@staticmethod
Expand Down Expand Up @@ -145,7 +151,11 @@ def _thread_main():
if not entry:
continue # skip uploading empty log line

_transmitter.send(entry.log_type, entry.message, timeout=3)
try:
_transmitter.send(entry.log_type, entry.message, timeout=3)
except Exception:
# ignore the exception and continue
pass

log_upload_thread = Thread(target=_thread_main, daemon=True)
log_upload_thread.start()
Expand Down
20 changes: 13 additions & 7 deletions src/otaclient/grpc/log_v1/otaclient_iot_logging_server_v1_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions src/otaclient/grpc/log_v1/otaclient_iot_logging_server_v1_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,29 @@ class PutLogResponse(_message.Message):
code: _Optional[_Union[ErrorCode, str]] = ...,
message: _Optional[str] = ...,
) -> None: ...

class HealthCheckRequest(_message.Message):
__slots__ = ["service"]
SERVICE_FIELD_NUMBER: _ClassVar[int]
service: str
def __init__(self, service: _Optional[str] = ...) -> None: ...

class HealthCheckResponse(_message.Message):
__slots__ = ["status"]

class ServingStatus(int, metaclass=_enum_type_wrapper.EnumTypeWrapper):
__slots__ = []
UNKNOWN: _ClassVar[HealthCheckResponse.ServingStatus]
SERVING: _ClassVar[HealthCheckResponse.ServingStatus]
NOT_SERVING: _ClassVar[HealthCheckResponse.ServingStatus]
SERVICE_UNKNOWN: _ClassVar[HealthCheckResponse.ServingStatus]

UNKNOWN: HealthCheckResponse.ServingStatus
SERVING: HealthCheckResponse.ServingStatus
NOT_SERVING: HealthCheckResponse.ServingStatus
SERVICE_UNKNOWN: HealthCheckResponse.ServingStatus
STATUS_FIELD_NUMBER: _ClassVar[int]
status: HealthCheckResponse.ServingStatus
def __init__(
self, status: _Optional[_Union[HealthCheckResponse.ServingStatus, str]] = ...
) -> None: ...
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def __init__(self, channel):
request_serializer=otaclient__iot__logging__server__pb2_dot_v1_dot_otaclient__iot__logging__server__v1__pb2.PutLogRequest.SerializeToString,
response_deserializer=otaclient__iot__logging__server__pb2_dot_v1_dot_otaclient__iot__logging__server__v1__pb2.PutLogResponse.FromString,
)
self.Check = channel.unary_unary(
"/OtaClientIoTLoggingService/Check",
request_serializer=otaclient__iot__logging__server__pb2_dot_v1_dot_otaclient__iot__logging__server__v1__pb2.HealthCheckRequest.SerializeToString,
response_deserializer=otaclient__iot__logging__server__pb2_dot_v1_dot_otaclient__iot__logging__server__v1__pb2.HealthCheckResponse.FromString,
)


class OtaClientIoTLoggingServiceServicer(object):
Expand All @@ -34,6 +39,15 @@ def PutLog(self, request, context):
context.set_details("Method not implemented!")
raise NotImplementedError("Method not implemented!")

def Check(self, request, context):
"""
`Check` requests OTA Client logging service to check the health of the
service.
"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details("Method not implemented!")
raise NotImplementedError("Method not implemented!")


def add_OtaClientIoTLoggingServiceServicer_to_server(servicer, server):
rpc_method_handlers = {
Expand All @@ -42,6 +56,11 @@ def add_OtaClientIoTLoggingServiceServicer_to_server(servicer, server):
request_deserializer=otaclient__iot__logging__server__pb2_dot_v1_dot_otaclient__iot__logging__server__v1__pb2.PutLogRequest.FromString,
response_serializer=otaclient__iot__logging__server__pb2_dot_v1_dot_otaclient__iot__logging__server__v1__pb2.PutLogResponse.SerializeToString,
),
"Check": grpc.unary_unary_rpc_method_handler(
servicer.Check,
request_deserializer=otaclient__iot__logging__server__pb2_dot_v1_dot_otaclient__iot__logging__server__v1__pb2.HealthCheckRequest.FromString,
response_serializer=otaclient__iot__logging__server__pb2_dot_v1_dot_otaclient__iot__logging__server__v1__pb2.HealthCheckResponse.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
"OtaClientIoTLoggingService", rpc_method_handlers
Expand Down Expand Up @@ -81,3 +100,32 @@ def PutLog(
timeout,
metadata,
)

@staticmethod
def Check(
request,
target,
options=(),
channel_credentials=None,
call_credentials=None,
insecure=False,
compression=None,
wait_for_ready=None,
timeout=None,
metadata=None,
):
return grpc.experimental.unary_unary(
request,
target,
"/OtaClientIoTLoggingService/Check",
otaclient__iot__logging__server__pb2_dot_v1_dot_otaclient__iot__logging__server__v1__pb2.HealthCheckRequest.SerializeToString,
otaclient__iot__logging__server__pb2_dot_v1_dot_otaclient__iot__logging__server__v1__pb2.HealthCheckResponse.FromString,
options,
channel_credentials,
insecure,
call_credentials,
compression,
wait_for_ready,
timeout,
metadata,
)
10 changes: 9 additions & 1 deletion tests/test_otaclient/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ def __init__(self, test_queue, data_ready):
self._test_queue = test_queue
self._data_ready = data_ready

async def Check(self, request: log_pb2.HealthCheckRequest, context):
"""
Dummy gRPC method for health check.
"""
return log_pb2.HealthCheckResponse(
status=log_pb2.HealthCheckResponse.ServingStatus.SERVING
)

async def PutLog(self, request: log_pb2.PutLogRequest, context):
"""
Dummy gRPC method to put a log message to the queue.
Expand All @@ -72,7 +80,7 @@ async def PutLog(self, request: log_pb2.PutLogRequest, context):


class TestLogClient:
OTA_CLIENT_LOGGING_SERVER = "http://127.0.0.1:8083"
OTA_CLIENT_LOGGING_SERVER = "http://127.0.0.1:8084"
ECU_ID = "testclient"

@pytest.fixture(autouse=True)
Expand Down

0 comments on commit 0a523e2

Please sign in to comment.