Beispiel #1
0
    def __init__(self):
        self.state = None

        if config.force_tls:
            self.channel = grpc.secure_channel(
                config.collector_address,
                grpc.ssl_channel_credentials(),
                options=(('grpc.max_connection_age_grace_ms',
                          1000 * config.GRPC_TIMEOUT), ))
        else:
            self.channel = grpc.insecure_channel(
                config.collector_address,
                options=(('grpc.max_connection_age_grace_ms',
                          1000 * config.GRPC_TIMEOUT), ))

        if config.authentication:
            self.channel = grpc.intercept_channel(
                self.channel,
                header_adder_interceptor('authentication',
                                         config.authentication))

        self.channel.subscribe(self._cb, try_to_connect=True)
        self.service_management = GrpcServiceManagementClient(self.channel)
        self.traces_reporter = GrpcTraceSegmentReportService(self.channel)
        self.profile_query = GrpcProfileTaskChannelService(self.channel)
Beispiel #2
0
    def __init__(self):
        self.state = None
        self.channel = grpc.insecure_channel(config.collector_address)

        def cb(state):
            logger.debug('grpc channel connectivity changed, [%s -> %s]',
                         self.state, state)
            self.state = state

        self.channel.subscribe(cb, try_to_connect=True)
        self.service_management = GrpcServiceManagementClient(self.channel)
        self.traces_reporter = GrpcTraceSegmentReportService(self.channel)
Beispiel #3
0
    def __init__(self):
        self.state = None
        self.channel = grpc.insecure_channel(config.collector_address)
        if config.authentication:
            self.channel = grpc.intercept_channel(
                self.channel,
                header_adder_interceptor('authentication',
                                         config.authentication))

        self.channel.subscribe(self._cb, try_to_connect=True)
        self.service_management = GrpcServiceManagementClient(self.channel)
        self.traces_reporter = GrpcTraceSegmentReportService(self.channel)
Beispiel #4
0
    def __init__(self):
        self.state = None
        self.channel = grpc.insecure_channel(config.collector_address)
        if config.authentication:
            self.channel = grpc.intercept_channel(
                self.channel, header_adder_interceptor('authentication', config.authentication)
            )

        def cb(state):
            logger.debug('grpc channel connectivity changed, [%s -> %s]', self.state, state)
            self.state = state
            if self.connected():
                self.service_management.send_instance_props()

        self.channel.subscribe(cb, try_to_connect=True)
        self.service_management = GrpcServiceManagementClient(self.channel)
        self.traces_reporter = GrpcTraceSegmentReportService(self.channel)
Beispiel #5
0
class GrpcProtocol(Protocol):
    def __init__(self):
        self.state = None
        self.channel = grpc.insecure_channel(config.collector_address)
        if config.authentication:
            self.channel = grpc.intercept_channel(
                self.channel, header_adder_interceptor('authentication', config.authentication)
            )

        def cb(state):
            logger.debug('grpc channel connectivity changed, [%s -> %s]', self.state, state)
            self.state = state
            if self.connected():
                self.service_management.send_instance_props()

        self.channel.subscribe(cb, try_to_connect=True)
        self.service_management = GrpcServiceManagementClient(self.channel)
        self.traces_reporter = GrpcTraceSegmentReportService(self.channel)

    def heartbeat(self):
        self.service_management.send_heart_beat()

    def connected(self):
        return self.state == grpc.ChannelConnectivity.READY

    def report(self, queue: Queue):
        def generator():
            while True:
                segment = queue.get()  # type: Segment

                logger.debug('reporting segment %s', segment)

                s = SegmentObject(
                    traceId=str(segment.related_traces[0]),
                    traceSegmentId=str(segment.segment_id),
                    service=config.service_name,
                    serviceInstance=config.service_instance,
                    spans=[SpanObject(
                        spanId=span.sid,
                        parentSpanId=span.pid,
                        startTime=span.start_time,
                        endTime=span.end_time,
                        operationName=span.op,
                        peer=span.peer,
                        spanType=span.kind.name,
                        spanLayer=span.layer.name,
                        componentId=span.component.value,
                        isError=span.error_occurred,
                        logs=[Log(
                            time=int(log.timestamp * 1000),
                            data=[KeyStringValuePair(key=item.key, value=item.val) for item in log.items],
                        ) for log in span.logs],
                        tags=[KeyStringValuePair(
                            key=str(tag.key),
                            value=str(tag.val),
                        ) for tag in span.tags],
                        refs=[SegmentReference(
                            refType=0,
                            traceId=ref.trace_id,
                            parentTraceSegmentId=ref.segment_id,
                            parentSpanId=ref.span_id,
                            parentService=ref.service,
                            parentServiceInstance=ref.service_instance,
                            parentEndpoint=ref.endpoint,
                            networkAddressUsedAtPeer=ref.client_address,
                        ) for ref in span.refs if ref.trace_id],
                    ) for span in segment.spans],
                )

                yield s

                queue.task_done()

        self.traces_reporter.report(generator())
Beispiel #6
0
class GrpcProtocol(Protocol):
    def __init__(self):
        self.state = None

        if config.force_tls:
            self.channel = grpc.secure_channel(
                config.collector_address,
                grpc.ssl_channel_credentials(),
                options=(('grpc.max_connection_age_grace_ms',
                          1000 * config.GRPC_TIMEOUT), ))
        else:
            self.channel = grpc.insecure_channel(
                config.collector_address,
                options=(('grpc.max_connection_age_grace_ms',
                          1000 * config.GRPC_TIMEOUT), ))

        if config.authentication:
            self.channel = grpc.intercept_channel(
                self.channel,
                header_adder_interceptor('authentication',
                                         config.authentication))

        self.channel.subscribe(self._cb, try_to_connect=True)
        self.service_management = GrpcServiceManagementClient(self.channel)
        self.traces_reporter = GrpcTraceSegmentReportService(self.channel)
        self.profile_query = GrpcProfileTaskChannelService(self.channel)

    def _cb(self, state):
        logger.debug('grpc channel connectivity changed, [%s -> %s]',
                     self.state, state)
        self.state = state
        if self.connected():
            try:
                self.service_management.send_instance_props()
            except grpc.RpcError:
                self.on_error()

    def query_profile_commands(self):
        logger.debug("query profile commands")
        self.profile_query.do_query()

    def heartbeat(self):
        try:
            self.service_management.send_heart_beat()
        except grpc.RpcError:
            self.on_error()

    def connected(self):
        return self.state == grpc.ChannelConnectivity.READY

    def on_error(self):
        traceback.print_exc() if logger.isEnabledFor(logging.DEBUG) else None
        self.channel.unsubscribe(self._cb)
        self.channel.subscribe(self._cb, try_to_connect=True)

    def report(self, queue: Queue, block: bool = True):
        start = time()
        segment = None

        def generator():
            nonlocal segment

            while True:
                try:
                    timeout = max(0, config.QUEUE_TIMEOUT -
                                  int(time() - start))  # type: int
                    segment = queue.get(block=block,
                                        timeout=timeout)  # type: Segment
                except Empty:
                    return

                logger.debug('reporting segment %s', segment)

                s = SegmentObject(
                    traceId=str(segment.related_traces[0]),
                    traceSegmentId=str(segment.segment_id),
                    service=config.service_name,
                    serviceInstance=config.service_instance,
                    spans=[
                        SpanObject(
                            spanId=span.sid,
                            parentSpanId=span.pid,
                            startTime=span.start_time,
                            endTime=span.end_time,
                            operationName=span.op,
                            peer=span.peer,
                            spanType=span.kind.name,
                            spanLayer=span.layer.name,
                            componentId=span.component.value,
                            isError=span.error_occurred,
                            logs=[
                                Log(
                                    time=int(log.timestamp * 1000),
                                    data=[
                                        KeyStringValuePair(key=item.key,
                                                           value=item.val)
                                        for item in log.items
                                    ],
                                ) for log in span.logs
                            ],
                            tags=[
                                KeyStringValuePair(
                                    key=str(tag.key),
                                    value=str(tag.val),
                                ) for tag in span.tags
                            ],
                            refs=[
                                SegmentReference(
                                    refType=0
                                    if ref.ref_type == "CrossProcess" else 1,
                                    traceId=ref.trace_id,
                                    parentTraceSegmentId=ref.segment_id,
                                    parentSpanId=ref.span_id,
                                    parentService=ref.service,
                                    parentServiceInstance=ref.service_instance,
                                    parentEndpoint=ref.endpoint,
                                    networkAddressUsedAtPeer=ref.
                                    client_address,
                                ) for ref in span.refs if ref.trace_id
                            ],
                        ) for span in segment.spans
                    ],
                )

                yield s

                queue.task_done()

        try:
            self.traces_reporter.report(generator())

        except grpc.RpcError:
            self.on_error()

            if segment:
                try:
                    queue.put(segment, block=False)
                except Full:
                    pass