def record_rpc_request(self, service, call, request, response, rpc): """Record the request of an RPC call. Args: service: The service name, e.g. 'memcache'. call: The call name, e.g. 'Get'. request: The request object. response: The response object (ignored). rpc: The RPC object; may be None. """ pre_now = time.time() sreq = format_value(request) now = time.time() delta = int(1000 * (now - self.start_timestamp)) trace = datamodel_pb.IndividualRpcStatsProto() self.get_call_stack(trace) trace.set_service_call_name('%s.%s' % (service, call)) trace.set_request_data_summary(sreq) trace.set_start_offset_milliseconds(delta) with self._lock: if rpc is not None: self.pending[rpc] = len(self.traces) self.traces.append(trace) self.overhead += (now - pre_now)
def record_custom_event(self, label, data=None, start=None, end=None): """Record a custom event. Args: label: A string to use as event label; a 'custom.' prefix will be added. data: Optional value to record. This can be anything; the value will be formatted using format_value() before it is recorded. start: time.time() of the start of the time range, default now. Supply start when you want a range intead of a 0ms duration event. end: time.time() of the end of the time range, default now. Supply end when the range was in the past or when you want the range to match other monitoring/analytics. Supplying end without start is an error. """ pre_now = time.time() sreq = format_value(data) now = time.time() if end and not start: raise ValueError('Please specify a start time along with the end time.') start = start or pre_now end = end or pre_now delta = int(1000 * (start - self.start_timestamp)) duration = int(1000 * (end - start)) trace = datamodel_pb.IndividualRpcStatsProto() self.get_call_stack(trace) trace.set_service_call_name('custom.' + label) trace.set_request_data_summary(sreq) trace.set_start_offset_milliseconds(delta) trace.set_duration_milliseconds(duration) with self._lock: self.traces.append(trace) self.overhead += (now - pre_now)
def record_rpc_response(self, service, call, request, response, rpc): """Record the response of an RPC call. Args: service: The service name, e.g. 'memcache'. call: The call name, e.g. 'Get'. request: The request object. response: The response object (ignored). rpc: The RPC object; may be None. This first tries to match the request with an unmatched request trace. If no matching request trace is found, this is logged as a new trace. """ now = time.time() key = '%s.%s' % (service, call) delta = int(1000 * (now - self.start_timestamp)) sresp = format_value(response) api_mcycles = 0 if rpc is not None: api_mcycles = rpc.cpu_usage_mcycles with self._lock: index = self.pending.get(rpc) if index is not None: del self.pending[rpc] if 0 <= index < len(self.traces): trace = self.traces[index] trace.set_response_data_summary(sresp) trace.set_api_mcycles(api_mcycles) duration = delta - trace.start_offset_milliseconds() trace.set_duration_milliseconds(duration) self.overhead += (time.time() - now) return else: with self._lock: for trace in reversed(self.traces): if (trace.service_call_name() == key and not trace.response_data_summary()): if config.DEBUG: logging.debug('Matched RPC response without rpc object') trace.set_response_data_summary(sresp) duration = delta - trace.start_offset_milliseconds() trace.set_duration_milliseconds(duration) self.overhead += (time.time() - now) return logging.warn('RPC response without matching request') trace = datamodel_pb.IndividualRpcStatsProto() self.get_call_stack(trace) trace.set_service_call_name(key) trace.set_request_data_summary(sresp) trace.set_start_offset_milliseconds(delta) with self._lock: self.traces.append(trace) self.overhead += (time.time() - now)
def start_appstats_recording(self): """ Starts appstats recorder """ recorder = recording.recorder_proxy if recorder.has_recorder_for_current_request(): start = time() self.trace = datamodel_pb.IndividualRpcStatsProto() delta = int(1000 * (start - recorder.start_timestamp)) self.trace.set_start_offset_milliseconds(delta)
def record_custom_event(self, label, data=None): """Record a custom event. Args: label: A string to use as event label; a 'custom.' prefix will be added. data: Optional value to record. This can be anything; the value will be formatted using format_value() before it is recorded. """ pre_now = time.time() sreq = format_value(data) now = time.time() delta = int(1000 * (now - self.start_timestamp)) trace = datamodel_pb.IndividualRpcStatsProto() self.get_call_stack(trace) trace.set_service_call_name('custom.' + label) trace.set_request_data_summary(sreq) trace.set_start_offset_milliseconds(delta) self.traces.append(trace) self.overhead += (now - pre_now)