Humbertzhang commented on a change in pull request #147: URL: https://github.com/apache/skywalking-python/pull/147#discussion_r684662955
########## File path: skywalking/log/sw_logging.py ########## @@ -0,0 +1,94 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import logging + +from skywalking import config, agent +from skywalking.protocol.common.Common_pb2 import KeyStringValuePair +from skywalking.protocol.logging.Logging_pb2 import LogData, LogDataBody, TraceContext, LogTags, TextLog +from skywalking.trace.context import get_context + + +def install(): + from logging import Logger + + _handle = Logger.handle + log_reporter_level = logging.getLevelName(config.log_grpc_reporter_level) # type: int + + def _sw_handle(self, record): + if self.name == "skywalking": # Ignore SkyWalking internal logger + return _handle(self, record) + + if record.levelno < log_reporter_level: + return _handle(self, record) + + def build_log_tags() -> LogTags: + core_tags = [ + KeyStringValuePair(key='level', value=str(record.levelname)), + KeyStringValuePair(key='logger', value=str(record.name)), + KeyStringValuePair(key='thread', value=str(record.threadName)) + ] + l_tags = LogTags() + l_tags.data.extend(core_tags) + + if config.log_grpc_reporter_formatted: + return l_tags + + for i, arg in enumerate(record.args): + l_tags.data.append(KeyStringValuePair(key='argument.' + str(i), value=str(arg))) + + if record.exc_info: + l_tags.data.append(KeyStringValuePair(key='exception', value=str(record.exc_info))) + + return l_tags + + context = get_context() + + log_data = LogData( + timestamp=round(record.created * 1000), + service=config.service_name, + serviceInstance=config.service_instance, + body=LogDataBody( + type='text', + text=TextLog( + text=transform(record) + ) + ), + traceContext=TraceContext( + traceId=str(context.segment.related_traces[0]), + traceSegmentId=str(context.segment.segment_id), + spanId=context.active_span().sid if context.active_span() else -1 + ), + tags=build_log_tags(), + ) + + _handle(self=self, record=record) + + agent.archive_log(log_data) + + Logger.handle = _sw_handle + + def transform(record) -> str: + if config.log_grpc_reporter_formatted: + layout = config.log_grpc_reporter_layout # type: str + if layout: + from logging import Formatter + formatter = Formatter(fmt=layout) Review comment: Is there a way not to init a new formatter object at every time? ########## File path: skywalking/agent/protocol/grpc_log.py ########## @@ -0,0 +1,103 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import logging +import traceback +from queue import Queue, Empty +from time import time + +import grpc +from skywalking.protocol.logging.Logging_pb2 import LogData + +from skywalking import config +from skywalking.agent import Protocol +from skywalking.agent.protocol.interceptors import header_adder_interceptor +from skywalking.client.grpc import GrpcServiceManagementClient, GrpcLogDataReportService +from skywalking.loggings import logger + + +class GrpcLogProtocol(Protocol): + def __init__(self): + self.properties_sent = False + self.state = None + + if config.force_tls: + self.channel = grpc.secure_channel(config.log_grpc_collector_address, grpc.ssl_channel_credentials(), + options=(('grpc.max_send_message_length', + config.log_grpc_reporter_max_message_size),)) + else: + self.channel = grpc.insecure_channel(config.log_grpc_collector_address, + options=(('grpc.max_send_message_length', + config.log_grpc_reporter_max_message_size),)) + if config.authentication: + self.channel = grpc.intercept_channel( + self.channel, header_adder_interceptor('authentication', config.authentication) + ) + + self.channel.subscribe(self._cb, try_to_connect=True) + self.service_management = GrpcServiceManagementClient(self.channel) + self.log_reporter = GrpcLogDataReportService(self.channel) + + def _cb(self, state): + logger.debug('grpc log reporter channel connectivity changed, [%s -> %s]', self.state, state) + self.state = state + + def heartbeat(self): + try: + if not self.properties_sent: + self.service_management.send_instance_props() + self.properties_sent = True + + logger.debug( + 'log reporter service heart beats, [%s], [%s]', + config.service_name, + config.service_instance, + ) + self.service_management.send_heart_beat() + + except grpc.RpcError: + self.on_error() + + def on_error(self): + traceback.print_exc() if logger.isEnabledFor(logging.DEBUG) else None + self.channel.unsubscribe(self._cb) + self.channel.subscribe(self._cb, try_to_connect=True) + + def report(self, queue: Queue, block: bool = True): + start = time() + + def generator(): + while True: + try: + timeout = config.QUEUE_TIMEOUT - int(time() - start) # type: int + if timeout <= 0: # this is to make sure we exit eventually instead of being fed continuously + return + log_data = queue.get(block=block, timeout=timeout) # type: LogData + except Empty: + return + + queue.task_done() + + logger.debug('Reporting Log %s', log_data) Review comment: No need to print the `log_data` object out. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
