def test_truncate_unicode_snowman(): # '\u2603' is 'SNOWMAN' u = u'snow\u2603' assert u.encode('utf-8') == b'snow\xe2\x98\x83' result = truncate(u, maxsize=5) assert isinstance(result, six.text_type) assert result == u'snow'
def emit(self, record): try: headers = { "Api-Key": self.license_key or "", "Content-Type": "application/json" } payload = self.format(record).encode("utf-8") with self.client: status_code, response = self.client.send_request( path=self.PATH, headers=headers, payload=payload) if status_code < 200 or status_code >= 300: raise RuntimeError( "An unexpected HTTP response of %r was received for request made to https://%s:%d%s." "The response payload for the request was %r. If this issue persists then please " "report this problem to New Relic support for further investigation." % ( status_code, self.client._host, self.client._port, self.PATH, truncate(response.decode("utf-8"), 1024), )) except Exception: self.handleError(record)
def test_truncate_combining_characters(): # '\u0308' is 'COMBINING DIAERESIS' (AKA 'umlaut') u = u'Zoe\u0308' assert u.encode('utf-8') == b'Zoe\xcc\x88' # truncate will chop off 'COMBINING DIAERESIS', which leaves # 'LATIN SMALL LETTER E' by itself. result = truncate(u, maxsize=3) assert isinstance(result, six.text_type) assert result == u'Zoe'
def extract_event_source_arn(event): try: arn = event.get('streamArn') or \ event.get('deliveryStreamArn') if not arn: record = event['Records'][0] arn = record.get('eventSourceARN') or \ record.get('EventSubscriptionArn') or \ record['s3']['bucket']['arn'] return truncate(str(arn)) except Exception: pass
def span_event(self, *args, **kwargs): attrs = super(DatabaseNode, self).span_event(*args, **kwargs) i_attrs = attrs[0] sql = self.formatted # Truncate to 2000 bytes and append ... new_sql = truncate(sql, maxsize=2000) if len(new_sql) != len(sql): sql_chars = list(new_sql) sql_chars[-3:] = '...' sql = ''.join(sql_chars) i_attrs['db.statement'] = sql return attrs
def send(self, method, payload=()): params, headers, payload = self._to_http(method, payload) try: response = self.client.send_request(params=params, headers=headers, payload=payload) except NetworkInterfaceException: # All HTTP errors are currently retried raise RetryDataForRequest status, data = response if not 200 <= status < 300: if status == 413: internal_count_metric( "Supportability/Python/Collector/MaxPayloadSizeLimit/%s" % method, 1, ) level, message = self.LOG_MESSAGES.get( status, self.LOG_MESSAGES["default"]) _logger.log( level, message, { "proxy_host": self._proxy_host, "proxy_port": self._proxy_port, "proxy_user": self._proxy_user, "method": method, "status_code": status, "headers": headers, "params": { k: v for k, v in params.items() if k in self.PARAMS_ALLOWLIST }, "content": truncate(data, 1024), "agent_run_id": self._run_token, }, ) exception = self.STATUS_CODE_RESPONSE.get(status, DiscardDataForRequest) raise exception if status == 200: return json_decode(data.decode("utf-8"))["return_value"]
def test_truncate_empty_unicode(): u = u'' result = truncate(u, maxsize=5) assert isinstance(result, six.text_type) assert result == u''
def test_truncate_empty_bytes(): b = b'' result = truncate(b, maxsize=3) assert isinstance(result, six.binary_type) assert result == b''
def test_truncate_empty_string(): s = '' result = truncate(s, maxsize=4) assert isinstance(result, six.string_types) assert result == ''