def test_method_is_valid(self): state = Tracestate() # empty state not allowed self.assertFalse(state.is_valid()) state['foo'] = 'x' * 256 self.assertTrue(state.is_valid()) # exceeds 512 bytes state['bar'] = 'x' * 256 self.assertFalse(state.is_valid()) self.assertTrue(Tracestate(state.to_string()[:512]).is_valid()) self.assertFalse(Tracestate(state.to_string()[:513]).is_valid())
def do_POST(self): self.send_response(200) arguments = json.loads( str(self.rfile.read(int(self.headers['Content-Length'])), 'ascii')) traceparent = None tracestate = Tracestate() try: temp_traceparent = BaseTraceparent.from_string( self.get_header('traceparent')) if temp_traceparent.version == 0: if temp_traceparent._residue: raise ValueError('illegal traceparent format') traceparent = Traceparent(0, temp_traceparent.trace_id, temp_traceparent.span_id, temp_traceparent.trace_flags) except ValueError: pass if traceparent is None: traceparent = Traceparent() else: try: header = self.get_header('tracestate', commaSeparated=True) if header is not None: tracestate = Tracestate(header) except ValueError: # if tracestate is malformed, reuse the traceparent instead of restart the trace # traceparent = Traceparent() pass for item in arguments: headers = {} headers['traceparent'] = str( Traceparent(0, traceparent.trace_id, None, traceparent.trace_flags)) if tracestate.is_valid(): headers['tracestate'] = str(tracestate) request = Request(method='POST', url=item['url'], headers=headers, data=bytes(json.dumps(item['arguments']), 'ascii')) with self.opener_director.open( request, timeout=self.timeout) as response: pass self.end_headers()