def _registerAgent(self): while self.task_running: try: TCLogger.debug("sending agentinfo %s", self.agentinfo) self.stub.RequestAgentInfo(self.agentinfo) except Exception as e: TCLogger.error( " pinpoint collector is not available. Try it again [%s] ", self.agentinfo) continue finally: with self.exit_cv: if not self.task_running or self.exit_cv.wait( self.timeout): break iter_response = self.stub.PingSession(self._pingPPing(), metadata=self.ping_meta) try: for response in iter_response: TCLogger.debug('get ping response:%s agentinfo:%s', response, self.meta) except Exception as e: TCLogger.error("ping response abort with exception: [%s] %s", self.agentinfo, e) TCLogger.info('agent thread exit: %s', self.task_running)
def _listenhandler(self): try: sock = self.listen # client_socket client_socket, address = sock.accept() client_socket.setblocking(False) # sock = gsocket(_socket = client_socket) except gsocket.error as error: TCLogger.warning("accept:%s", error) return TCLogger.info("new connect fd:%s ", client_socket.fileno()) client_in_event = self.loop.io(client_socket.fileno(), 1) client_out_event = self.loop.io(client_socket.fileno(), 2) client = self.Client(client_socket, client_in_event, client_out_event, address) client_in_event.start(self.handleClientRead, client) ## say hello if self.say_hello_cb is not None: self.say_hello_cb(client) ## (socket,read_cv,write_cb) self.clients[client_socket.fileno()] = client
def getAndSendSpan(self): self.task_running = True spans = [] def get_n_span(queue, N): i = 0 try: while N > i: spans.append(queue.get(timeout=0.005)) i += 1 except Empty as e: pass return True if i > 0 else False client = GrpcSpan.SpanClient(self.address, self.meta) while self.task_running: try: if not get_n_span(self.queue, 10240): with self.exit_cv: if not self.task_running or self.exit_cv.wait(5): TCLogger.info("span send thread is done") break continue self.send_span_count += len(spans) client.sendSpans(iter(spans)) TCLogger.debug("send %d", self.send_span_count) except Exception as e: TCLogger.error("span channel catches an exception:%s", e) client.reconnect() finally: spans.clear()
def _connect(self): TCLogger.info("span channel connect %s with meta:%s", self.address, self.meta) self.channel = grpc.intercept_channel( grpc.insecure_channel(self.address), GrpcClient.InterceptorAddHeader(self.meta)) self.span_stub = Service_pb2_grpc.SpanStub(self.channel)
def __next__(self): response = self._response_queue.get() if response: return response else: TCLogger.info("HandStreamIterator stopped") return
def loopTheQueue(self): while True: try: body = self.mpQueue.get() except KeyboardInterrupt: break if body == None: TCLogger.info("agent: %s stopping", self.agent_meta) break else: content = body.decode('utf-8') try: stack = json.loads(content) except Exception as e: TCLogger.error("json is crash:[%s]", content) return super().sendSpan(stack, body) try: pSpan = self.span_factory.makeSpan(stack) spanMesg = PSpanMessage(span=pSpan) except Exception as e: TCLogger.warn("interrupted by %s", e) continue self.span_helper.sendSpan(spanMesg)
def _actionTimeOut(self, time): if self.state == E_CONNETING: TCLogger.info("connecting state:%d timeout:%ds", self.state, time) self.close() elif self.state & E_WRITING == E_WRITING: TCLogger.info("waiting writing %d timeout:%ds", self.state, time) else: TCLogger.error("unhandled state:%d", self.state)
def update(self, *args): if self.must_snd_meta_now and not self.snd_task_is_running: TCLogger.info("try to register all meta") self.snd_task_is_running = True self.thread = Thread(target=self._registerAllMeta, args=(10, )) self.thread.start() self.must_snd_meta_now = False result = fun(self, *args) return result
def __init__(self, span_addr, appid, appname, starttime,max_pending_sz): self.agent_meta = [('starttime', str(starttime)), ('agentid', appid), ('applicationname', appname)] self.agent_id = appid self.agent_name = appname self.span_addr = span_addr self.max_pending_sz =max_pending_sz self.span_queue = Queue(self.max_pending_sz) self.span_client = GrpcSpan(self.span_addr, self.agent_meta) self.dropped_span_count=0 TCLogger.info("Successfully create a Span Sender")
def _registerAllMeta(self): TCLogger.info("register all meta data") # register sql for key, value in self.sql_table.items(): self._sendSqlMeta(value[1]) # api for key, value in self.api_table.items(): self._sendApiMeta(value[1]) # string for key, value in self.string_table.items(): self._sendStringMeta(value[1])
def findApp(self, app_id, app_name, service_type): if app_id in self.app_map: app = self.app_map[app_id] ## check app_name if app.app_name != app_name: TCLogger.warning(" app_name can't change when using ") app = self.default_app ## check service_type else: TCLogger.info("collector-agent try to create a new application agent.[%s@%s@%d]",app_id,app_name,service_type) app = self.collector_conf.collector_implement(self.collector_conf, app_id, app_name,service_type) app.start() self.app_map[app_id] = app return app
def _connected(self, addr): self.peer = addr self.__rawFd = self.socket.fileno() TCLogger.info("connect peer: [%s:%d] success" % (addr[0], addr[1])) self.state = E_CONNECTED ### start watcher recv self._enableRead() ### stop write self._stopWrite() ### stop timer self.timer.stop() ### say hello if self.hello_cb is not None: self.hello_cb(self)
def _registerAllMeta(self, sec): TCLogger.info("register all meta data") while self.snd_task_is_running: try: for key, value in self.sql_table.items(): self.meta_stub.RequestSqlMetaData(value[1]) # api for key, value in self.api_table.items(): self.meta_stub.RequestApiMetaData(value[1]) # string for key, value in self.string_table.items(): self.meta_stub.RequestStringMetaData(value[1]) self.snd_task_is_running = False TCLogger.info("send all meta data is done") break except Exception as e: TCLogger.warning("re-send all meta data, met exception:%s", e) with self.exit_cv: if not self.snd_task_is_running or self.exit_cv.wait(sec): break
def findApp(self, app_id, app_name, service_type): if app_id in self.app_map: app = self.app_map[app_id] ## check app_name if app.app_name != app_name: TCLogger.warning(" app_name can't change when using ") app = self.default_app ## check service_type else: if service_type == PHP: TCLogger.info( "collector-agent try to create a new application agent.[%s@%s]", app_id, app_name) app = self.collector_conf.collector_implement( self.collector_conf, app_id, app_name) app.start() self.app_map[app_id] = app else: raise NotImplementedError("service_type:%d not support", service_type) return app
def stopAll(self): for app_id, instance in self.app_map.items(): assert (isinstance(instance, PinpointAgent)) TCLogger.info("application is stopping [%s]", app_id) instance.stop() TCLogger.info("recieved %d span from php-fpm", self.recv_count)
def stop(self): self.task_running = False with self.exit_cv: self.exit_cv.notify_all() self.task_thead.join() TCLogger.info("send %d to pinpoint collector", self.send_span_count)
def _connectPeerWithHello(self, remote): TCLogger.info("try to connect:%s with timeout:%d", str(remote), self.timeout) self._connectPeerAsy(remote, self.timeout)
def stop(self): self.span_client.stop() TCLogger.info("grpc agent dropped %d", self.dropped_span_count)
def _outputConfig(self): TCLogger.info("CollectorSpanIp:%s", self.CollectorSpanIp) TCLogger.info("CollectorStatIp:%s", self.CollectorStatIp) TCLogger.info("CollectorAgentIp:%s", self.CollectorAgentIp) TCLogger.info("CollectorSpanPort:%d", self.CollectorSpanPort) TCLogger.info("CollectorStatPort:%d", self.CollectorStatPort) TCLogger.info("CollectorAgentPort:%d", self.CollectorAgentPort) TCLogger.info( "collector_type:%s", "gRpc" if self.collector_type == SUPPORT_GRPC else "thrift") TCLogger.info("log_dir:%s", self.log_dir) TCLogger.info("log_level:%s", self.log_level) TCLogger.info("webPort:%s", self.webPort) TCLogger.info("agentIsDocker:%s", self.agentIsDocker)