|
| 1 | +from basictracer import Sampler, SpanRecorder |
| 2 | +import instana.log as l |
| 3 | +import instana.agent_const as a |
| 4 | +import thread |
| 5 | +import time |
| 6 | + |
| 7 | +class InstanaSpan(object): |
| 8 | + t = 0 |
| 9 | + p = None |
| 10 | + s = 0 |
| 11 | + ts = 0 |
| 12 | + d = 0 |
| 13 | + n = None |
| 14 | + f = None |
| 15 | + data = None |
| 16 | + |
| 17 | + def __init__(self, **kwds): |
| 18 | + self.__dict__.update(kwds) |
| 19 | + |
| 20 | +class InstanaRecorder(SpanRecorder): |
| 21 | + sensor = None |
| 22 | + |
| 23 | + def __init__(self, sensor): |
| 24 | + super(InstanaRecorder, self).__init__() |
| 25 | + self.sensor = sensor |
| 26 | + |
| 27 | + def record_span(self, span): |
| 28 | + if self.sensor.agent.can_send(): |
| 29 | + data = self.get_span_log_field(span, "data") |
| 30 | + if not data.service: |
| 31 | + data.service = self.sensor.service_name |
| 32 | + |
| 33 | + thread.start_new_thread(self.sensor.agent.request, |
| 34 | + (self.sensor.agent.make_url(a.AGENT_TRACES_URL), "POST", |
| 35 | + [InstanaSpan(t=span.context.trace_id, |
| 36 | + p=span.parent_id, |
| 37 | + s=span.context.span_id, |
| 38 | + ts=int(round(span.start_time * 1000)), |
| 39 | + d=int(round(span.duration * 1000)), |
| 40 | + n=self.get_string_span_log_field(span, "type"), |
| 41 | + f=self.sensor.agent.from_, |
| 42 | + data=data)])) |
| 43 | + |
| 44 | + def get_string_span_log_field(self, span, field): |
| 45 | + ret = self.get_span_log_field(span, field) |
| 46 | + if not ret: |
| 47 | + return "" |
| 48 | + |
| 49 | + return ret |
| 50 | + |
| 51 | + def get_span_log_field(self, span, field): |
| 52 | + for e in span.logs: |
| 53 | + if field in e.key_values: |
| 54 | + return e.key_values[field] |
| 55 | + |
| 56 | + return None |
| 57 | + |
| 58 | +class InstanaSampler(Sampler): |
| 59 | + def sampled(self, trace_id): |
| 60 | + return False |
0 commit comments