|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +import os |
| 19 | +import ast |
| 20 | +import logging |
| 21 | + |
| 22 | +from skywalking import config |
| 23 | +from skywalking.client import ServiceManagementClient, TraceSegmentReportService |
| 24 | +from skywalking.protocol.common.Common_pb2 import KeyStringValuePair |
| 25 | +from skywalking.protocol.management.Management_pb2 import InstancePingPkg, InstanceProperties |
| 26 | + |
| 27 | +from kafka import KafkaProducer |
| 28 | + |
| 29 | +logger = logging.getLogger(__name__) |
| 30 | + |
| 31 | +kafka_configs = {} |
| 32 | + |
| 33 | + |
| 34 | +def __init_kafka_configs(): |
| 35 | + kafka_configs["bootstrap_servers"] = config.kafka_bootstrap_servers.split(",") |
| 36 | + # process all kafka configs in env |
| 37 | + kafka_keys = [key for key in os.environ.keys() if key.startswith("SW_KAFKA_REPORTER_CONFIG_")] |
| 38 | + for kafka_key in kafka_keys: |
| 39 | + key = kafka_key[25:] |
| 40 | + val = os.environ.get(kafka_key) |
| 41 | + |
| 42 | + if val is not None: |
| 43 | + if val.isnumeric(): |
| 44 | + val = int(val) |
| 45 | + elif val in ["True", "False"]: |
| 46 | + val = ast.literal_eval(val) |
| 47 | + else: |
| 48 | + continue |
| 49 | + |
| 50 | + # check if the key was already set |
| 51 | + if kafka_configs.get(key) is None: |
| 52 | + kafka_configs[key] = val |
| 53 | + else: |
| 54 | + raise KafkaConfigDuplicated(key) |
| 55 | + |
| 56 | + |
| 57 | +__init_kafka_configs() |
| 58 | + |
| 59 | + |
| 60 | +class KafkaServiceManagementClient(ServiceManagementClient): |
| 61 | + def __init__(self): |
| 62 | + logger.debug("kafka reporter configs: %s", kafka_configs) |
| 63 | + self.producer = KafkaProducer(**kafka_configs) |
| 64 | + self.topic_key_register = "register-" |
| 65 | + self.topic = config.kafka_topic_management |
| 66 | + |
| 67 | + self.send_instance_props() |
| 68 | + |
| 69 | + def send_instance_props(self): |
| 70 | + instance = InstanceProperties( |
| 71 | + service=config.service_name, |
| 72 | + serviceInstance=config.service_instance, |
| 73 | + properties=[KeyStringValuePair(key='language', value='Python')], |
| 74 | + ) |
| 75 | + |
| 76 | + key = bytes(self.topic_key_register + instance.serviceInstance, encoding='utf-8') |
| 77 | + value = bytes(instance.SerializeToString()) |
| 78 | + self.producer.send(topic=self.topic, key=key, value=value) |
| 79 | + |
| 80 | + def send_heart_beat(self): |
| 81 | + logger.debug( |
| 82 | + 'service heart beats, [%s], [%s]', |
| 83 | + config.service_name, |
| 84 | + config.service_instance, |
| 85 | + ) |
| 86 | + |
| 87 | + instance_ping_pkg = InstancePingPkg( |
| 88 | + service=config.service_name, |
| 89 | + serviceInstance=config.service_instance, |
| 90 | + ) |
| 91 | + |
| 92 | + key = bytes(instance_ping_pkg.serviceInstance, encoding="utf-8") |
| 93 | + value = bytes(instance_ping_pkg.SerializeToString()) |
| 94 | + future = self.producer.send(topic=self.topic, key=key, value=value) |
| 95 | + res = future.get(timeout=10) |
| 96 | + logger.debug('heartbeat response: %s', res) |
| 97 | + |
| 98 | + |
| 99 | +class KafkaTraceSegmentReportService(TraceSegmentReportService): |
| 100 | + def __init__(self): |
| 101 | + self.producer = KafkaProducer(**kafka_configs) |
| 102 | + self.topic = config.kafka_topic_segment |
| 103 | + |
| 104 | + def report(self, generator): |
| 105 | + for segment in generator: |
| 106 | + key = bytes(segment.traceSegmentId, encoding="utf-8") |
| 107 | + value = bytes(segment.SerializeToString()) |
| 108 | + self.producer.send(topic=self.topic, key=key, value=value) |
| 109 | + |
| 110 | + |
| 111 | +class KafkaConfigDuplicated(Exception): |
| 112 | + def __init__(self, key): |
| 113 | + self.key = key |
0 commit comments