def test_encode_traces_json(self): # test encoding for JSON format traces = [] traces.append( [ Span(name="client.testing", tracer=None), Span(name="client.testing", tracer=None), ] ) traces.append( [ Span(name="client.testing", tracer=None), Span(name="client.testing", tracer=None), ] ) encoder = JSONEncoder() spans = encoder.encode_traces(traces) items = json.loads(spans) # test the encoded output that should be a string # and the output must be flatten assert isinstance(spans, string_type) assert len(items) == 2 assert len(items[0]) == 2 assert len(items[1]) == 2 for i in range(2): for j in range(2): assert "client.testing" == items[i][j]["name"]
class DummyWriter(AgentWriter): """DummyWriter is a small fake writer used for tests. not thread-safe.""" def __init__(self, *args, **kwargs): # original call if len(args) == 0 and "agent_url" not in kwargs: kwargs["agent_url"] = "http://localhost:8126" super(DummyWriter, self).__init__(*args, **kwargs) self.spans = [] self.traces = [] self.json_encoder = JSONEncoder() self.msgpack_encoder = MsgpackEncoder() def write(self, spans=None): if spans: # the traces encoding expect a list of traces so we # put spans in a list like we do in the real execution path # with both encoders trace = [spans] self.json_encoder.encode_traces(trace) self.msgpack_encoder.encode_traces(trace) self.spans += spans self.traces += trace def pop(self): # type: () -> List[Span] s = self.spans self.spans = [] return s def pop_traces(self): # type: () -> List[List[Span]] traces = self.traces self.traces = [] return traces
def test_join_encoded_json(self): # test encoding for JSON format traces = [] traces.append([ Span(name="client.testing", tracer=None), Span(name="client.testing", tracer=None), ]) traces.append([ Span(name="client.testing", tracer=None), Span(name="client.testing", tracer=None), ]) encoder = JSONEncoder() # Encode each trace on it's own encoded_traces = [encoder.encode_trace(trace) for trace in traces] # Join the encoded traces together data = encoder.join_encoded(encoded_traces) # Parse the resulting data items = json.loads(data) # test the encoded output that should be a string # and the output must be flatten assert isinstance(data, string_type) assert len(items) == 2 assert len(items[0]) == 2 assert len(items[1]) == 2 for i in range(2): for j in range(2): assert "client.testing" == items[i][j]["name"]
def __init__(self, *args, **kwargs): # original call if len(args) == 0 and "agent_url" not in kwargs: kwargs["agent_url"] = "http://localhost:8126" super(DummyWriter, self).__init__(*args, **kwargs) self.spans = [] self.traces = [] self.json_encoder = JSONEncoder() self.msgpack_encoder = MsgpackEncoder(4 << 20, 4 << 20)
def test_init(self): """ When calling `Payload.init` With an encoder We use that encoder With no encoder We use the default encoder """ default_encoder_type = type(Encoder()) payload = Payload() self.assertIsInstance(payload.encoder, default_encoder_type) json_encoder = JSONEncoder() payload = Payload(encoder=json_encoder) self.assertEqual(payload.encoder, json_encoder)