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.warn( " pinpoint collector is not available. Try it again [%s] ", self.agentinfo) continue finally: with self.exit_cv: if 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.debug('agent thread exit: %s', self.task_running)
def stop(self): self.task_running = False with self.exit_cv: self.exit_cv.notify_all() self.task_thead.join() super().stop() TCLogger.info("send %d to pinpoint collector", self.send_span_count)
def __next__(self): response = self._response_queue.get() if response: return response else: TCLogger.info("HandStreamIterator stopped") return
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 while self.task_running: try: if not get_n_span(self.queue, 10240): with self.exit_cv: if self.exit_cv.wait(5): break continue self.send_span_count += len(spans) self.span_stub.SendSpan(iter(spans)) except Exception as e: TCLogger.error("span channel, can't work:exception:%s", e) traceback.print_exc() 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 handleFrontAgentData(self, client, type, body): content = body.decode('utf-8') try: TCLogger.debug(content) stack = json.loads(content) except Exception as e: TCLogger.error("json is crash") return if 'appid' not in stack: TCLogger.warning(" drop %s, as no appid", content) return else: appid = stack['appid'] if 'appname' not in stack: TCLogger.warning(" drop %s, as no appname", content) return else: appname = stack['appname'] if 'FT' not in stack: TCLogger.warning(" drop %s, as no FT", content) return else: ft = stack['FT'] app = self.findApp(appid, appname, ft) app.asynSendSpan(stack, body) self.recv_count += 1
def _listenhandler(self): try: sock = self.listen # client_socket client_socket, address = sock.accept() client_socket.setblocking(False) # sock = gsocket(_socket = client_socket) except asy_socket.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) 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 _send_thread_count(self, requestId): channel = grpc.insecure_channel(self.address) stub = Service_pb2_grpc.ProfilerCommandServiceStub(channel) def generator_cmd(): i = 0 while True: try: cmd_response = PCmdStreamResponse(responseId=requestId, sequenceId=i) cmd_response.message.value = 'hello' threadCountRes = PCmdActiveThreadCountRes( commonStreamResponse=cmd_response) threadCountRes.histogramSchemaType = 2 for stat in self.get_req_stat(): threadCountRes.activeThreadCount.append(stat) threadCountRes.timeStamp = int(time.time()) i += 1 yield threadCountRes with self.exit_cv: if self.exit_cv.wait(1): break except Exception as e: TCLogger.warning("catch exception %s", e) break try: stub.CommandStreamActiveThreadCount(generator_cmd(), metadata=self.profile_meta) TCLogger.debug("send req state requestId: %d done", requestId) except Exception as e: TCLogger.error( "CommandStreamActiveThreadCount, catch exception %s", e)
def sendData(self, data): try: self.__socket.send(data) except asy_socket.error as e: if e.errno in (errno.EINPROGRESS, errno.EWOULDBLOCK): pass else: TCLogger.warning("connect:%s error:%s" % (self.remote, str(e)))
def sendMeta(self, meta): ''' :param APIMeta meta: :return: ''' TCLogger.debug("meta: %s", meta.name) self.collector_cb(meta.toPacket().getSerializedData())
def stop(self): TCLogger.debug("agent thread try to stop") self.task_running = False with self.exit_cv: self.exit_cv.notify_all() self.agent_thread.join() super().stop() TCLogger.debug("agent thread exit")
def start(self): try: ## start self._connectPeerWithHello(self.remote) except Timeout as t: TCLogger.warning("remote:%s time out state:%d", str(self.remote), self.state) self.close()
def _pingPPing(self): while self.task_running: ping = PPing() TCLogger.debug("%s send ping", self) yield ping with self.exit_cv: if self.exit_cv.wait(self.timeout): TCLogger.debug("generate ping exit") break
def _channelStateChange(self, activity): if activity == grpc.ChannelConnectivity.TRANSIENT_FAILURE: self.channelSetError() elif activity == grpc.ChannelConnectivity.READY: self.channelSetReady() elif activity == grpc.ChannelConnectivity.IDLE: self.channelSetIdle() TCLogger.debug("channel state change %s dst:%s", activity, self.address)
def sendData(self, outData=None): if self.state in (E_CLOSED, E_CONNETING): # self.write_wait_queue.append(data) TCLogger.debug("current state is:%d, data drop", self.state) else: if outData is not None: self.send_buf += outData self._write_watcher.start(self._sendData)
def toPacket(self): stringMeta = TStringMetaData(agentId=self.agentId, agentStartTime=self.agentStartTime, stringId=self.apiId, stringValue=self.value) TCLogger.debug("generator stringMate:%s", stringMeta) body = CollectorPro.obj2bin(stringMeta, STRING_META_DATA) packet = Packet(PacketType.APPLICATION_REQUEST, CollectorPro.getCurReqCount(), len(body), body) return packet
def _generator_PstatMessage(self): try: while self.task_running: ps = PStatMessage(agentStat=self._generAgentStat()) TCLogger.debug(ps) yield ps time.sleep(STAT_INTERVAL) except Exception as e: TCLogger.warning("_generator_PstatMessage catch %s", e)
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 toPacket(self): apiMeta = TApiMetaData(agentId=self.agentId, agentStartTime=self.agentStartTime, apiId=self.apiId, type=self.type, apiInfo=self.name) TCLogger.debug("generator ThriftAPIMeta:%s", apiMeta) body = CollectorPro.obj2bin(apiMeta, API_META_DATA) packet = Packet(PacketType.APPLICATION_REQUEST, CollectorPro.getCurReqCount(), len(body), body) return packet
def close(self): TCLogger.debug("close client:%d(%s)", self.socket.fileno(), self.address) if self._read_watcher is not None: self._read_watcher.stop() if self._write_watcher is not None: self._write_watcher.stop() self.socket.close()
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 run(self): while True: try: gevent.sleep(10) except: break # break by a signal self.app_management.stopAll() self.php_agent.stop() TCLogger.warning("collector agent exit with SIGNAL")
def _stat_thread_main(self): self.task_running = True while self.task_running: try: self.meta_stub.SendAgentStat(self._generator_PstatMessage()) except Exception as e: TCLogger.warning("SendAgentStat met:%s", e) with self.exit_cv: if self.exit_cv.wait(self.timeout): break
def sendSpan(self, spanMesg): try: self.span_queue.put(spanMesg, False) except Full as e: self.dropped_span_count+=1 return False except Exception as e: TCLogger.error("send span failed: %s", e) return False return True
def updateSqlMeta(self, sql): if sql in self.sql_table: return self.sql_table[sql][0] else: id = self.id meta = PSqlMetaData(sqlId=id, sql=sql) self.sql_table[sql] = [id, meta] self._sendSqlMeta(meta) self.id += 1 TCLogger.debug("register sql meta id:%d -> sql:[%s]", id, sql) return id
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 start(self): self.__socket = asy_socket.socket(asy_socket.AF_INET,asy_socket.SOCK_DGRAM) self.__socket.setblocking(False) try: self.state = E_CONNECTED self.__socket.connect(self.remote) except asy_socket.error as e: if not (e.errno in (errno.EINPROGRESS, errno.EWOULDBLOCK)): TCLogger.warning("connect:%s error:%s" % (self.remote, str(e))) self.__socket.close() raise Exception("create DgramLayer failed")
def close(self): TCLogger.warning("close connection (%s)", str(self.remote)) self._read_watcher.stop() self._write_watcher.stop() self.timer.stop() self.socket.close() self.state = E_CLOSED ## reset send buffer self.send_buf = b'' self.timer = self.loop.timer(self.timeout) self.timer.start(self._connectPeerWithHello, self.remote)
def updateStringMeta(self, value): if value in self.string_table: return self.string_table[value][0] else: id = self.id meta = PStringMetaData(stringId=id, stringValue=value) self.string_table[value] = [id, meta] self._sendStringMeta(meta) self.id += 1 TCLogger.debug("register string meta id:%d -> value:[%s]", id, value) return id
def _phpClientSayHello(self, client): response = {} for cb in self.hello_callback: data = cb() for key in data: response[key] = data[key] data = json.dumps(response) buf = struct.pack('!ii', RESPONSE_AGENT_INFO, len(data)) + data.encode() client.sendData(buf) TCLogger.debug("send hello:%d len:%d", client.socket.fileno(), len(buf))