__init__.py 3.7 KB
Newer Older
K
kezhenxu94 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
17

K
kezhenxu94 已提交
18 19 20 21 22
import logging
from queue import Queue

import grpc

23 24
from common.Common_pb2 import KeyStringValuePair
from language_agent.Tracing_pb2 import SegmentObject, SpanObject, Log
K
kezhenxu94 已提交
25 26
from skywalking import config
from skywalking.agent import Protocol
27 28
from skywalking.agent.protocol.grpc import interceptors
from skywalking.agent.protocol.grpc.interceptors import header_adder_interceptor
K
kezhenxu94 已提交
29 30 31 32 33 34 35 36 37 38
from skywalking.client.grpc import GrpcServiceManagementClient, GrpcTraceSegmentReportService
from skywalking.trace.segment import Segment

logger = logging.getLogger(__name__)


class GrpcProtocol(Protocol):
    def __init__(self):
        self.state = None
        self.channel = grpc.insecure_channel(config.collector_address)
39 40 41 42
        if config.authentication:
            self.channel = grpc.intercept_channel(
                self.channel, header_adder_interceptor('authentication', config.authentication)
            )
K
kezhenxu94 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

        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)

    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:
K
kezhenxu94 已提交
61
                segment = queue.get()  # type: Segment
K
kezhenxu94 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

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

                s = SegmentObject(
                    traceId=str(segment.trace_id),
                    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,
80 81 82 83
                        isError=span.error_occurred,
                        logs=[Log(
                            time=int(log.timestamp * 1000),
                            data=[KeyStringValuePair(key=item.key, value=item.val) for item in log.items],
K
kezhenxu94 已提交
84 85 86 87 88
                        ) for log in span.logs],
                        tags=[KeyStringValuePair(
                            key=str(tag.key),
                            value=str(tag.val),
                        ) for tag in span.tags],
K
kezhenxu94 已提交
89 90 91 92 93 94 95 96
                    ) for span in segment.spans],
                )

                yield s

                queue.task_done()

        self.traces_reporter.report(generator())