You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by al...@apache.org on 2020/08/17 13:30:43 UTC

[skywalking-python] 02/04: test

This is an automated email from the ASF dual-hosted git repository.

alonelaval pushed a commit to branch process-propagation
in repository https://gitbox.apache.org/repos/asf/skywalking-python.git

commit 8a637a054f84ddcb58cde75e0b2a583d365f612d
Author: huawei <hu...@bit-s.cn>
AuthorDate: Mon Aug 10 21:30:53 2020 +0800

    test
---
 browser/__init__.py              |   0
 common/__init__.py               |   0
 language_agent/__init__.py       |   0
 management/__init__.py           |   0
 profile/__init__.py              |   0
 service_mesh_probe/__init__.py   |   0
 skywalking/agent/__init__.py     |  30 +++++++-
 skywalking/config.py             |  68 +++++++++++++++++-
 skywalking/trace/ipc/__init__.py |  18 ++++-
 skywalking/trace/ipc/process.py  |  11 ++-
 tests/plugin/__init__.py         |   4 +-
 tests/test_process.py            | 144 ++++++++++++++++++++++++++++++++++++++-
 tests/test_uwsgi.py              |  20 +++++-
 13 files changed, 281 insertions(+), 14 deletions(-)

diff --git a/browser/__init__.py b/browser/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/common/__init__.py b/common/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/language_agent/__init__.py b/language_agent/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/management/__init__.py b/management/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/profile/__init__.py b/profile/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/service_mesh_probe/__init__.py b/service_mesh_probe/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/skywalking/agent/__init__.py b/skywalking/agent/__init__.py
index fe181a5..8200f4c 100644
--- a/skywalking/agent/__init__.py
+++ b/skywalking/agent/__init__.py
@@ -32,8 +32,10 @@ logger = logging.getLogger(__name__)
 def __heartbeat():
     while not __finished.is_set():
         if connected():
-            __protocol.heartbeat()
-
+            try:
+                __protocol.heartbeat()
+            except:
+                pass
         __finished.wait(30 if connected() else 3)
 
 
@@ -78,10 +80,34 @@ def start():
     __report_thread.start()
 
 
+# def restart():
+#     global __heartbeat_thread
+#     __heartbeat_thread = Thread(name='HeartbeatThread', target=__heartbeat, daemon=True)
+#     global __report_thread
+#     __report_thread = Thread(name='ReportThread', target=__report, daemon=True)
+#     global __queue
+#     __queue = Queue(maxsize=10000)
+#     global __finished
+#     __finished = Event()
+#     global __protocol
+#     __protocol = Protocol()  # type: Protocol
+#     global __started
+#     from skywalking import loggings
+#     loggings.init()
+#     __started = True
+#     __init()
+#     __heartbeat_thread.start()
+#     __report_thread.start()
+
+
 def stop():
     __finished.set()
 
 
+def started():
+    return __started
+
+
 def connected():
     return __protocol.connected()
 
diff --git a/skywalking/config.py b/skywalking/config.py
index fb89ba8..4b21912 100644
--- a/skywalking/config.py
+++ b/skywalking/config.py
@@ -35,14 +35,14 @@ pymongo_parameters_max_length = int(os.getenv('SW_PYMONGO_PARAMETERS_MAX_LENGTH'
 ignore_suffix = os.getenv('SW_IGNORE_SUFFIX') or '.jpg,.jpeg,.js,.css,.png,.bmp,.gif,.ico,.mp3,' \
                                                  '.mp4,.html,.svg '  # type: str
 flask_collect_http_params = True if os.getenv('SW_FLASK_COLLECT_HTTP_PARAMS') and \
-                                    os.getenv('SW_FLASK_COLLECT_HTTP_PARAMS') == 'True' else False   # type: bool
+                                    os.getenv('SW_FLASK_COLLECT_HTTP_PARAMS') == 'True' else False  # type: bool
 http_params_length_threshold = int(os.getenv('SW_HTTP_PARAMS_LENGTH_THRESHOLD') or '1024')  # type: int
 django_collect_http_params = True if os.getenv('SW_DJANGO_COLLECT_HTTP_PARAMS') and \
-                                    os.getenv('SW_DJANGO_COLLECT_HTTP_PARAMS') == 'True' else False   # type: bool
+                                     os.getenv('SW_DJANGO_COLLECT_HTTP_PARAMS') == 'True' else False  # type: bool
 correlation_element_max_number = int(os.getenv('SW_CORRELATION_ELEMENT_MAX_NUMBER') or '3')  # type: int
 correlation_value_max_length = int(os.getenv('SW_CORRELATION_VALUE_MAX_LENGTH') or '128')  # type: int
 trace_ignore = True if os.getenv('SW_TRACE_IGNORE') and \
-                       os.getenv('SW_TRACE_IGNORE') == 'True' else False   # type: bool
+                       os.getenv('SW_TRACE_IGNORE') == 'True' else False  # type: bool
 trace_ignore_path = (os.getenv('SW_TRACE_IGNORE_PATH') or '').split(',')  # type: List[str]
 
 
@@ -67,3 +67,65 @@ def init(
 
     global authentication
     authentication = token or authentication
+
+
+def serialize():
+    return {
+        "service_name": service_name,
+        "collector_address": collector_address,
+        "protocol": protocol,
+        "authentication": authentication,
+        "logging_level": logging_level,
+        "disable_plugins": disable_plugins,
+        "mysql_trace_sql_parameters": mysql_trace_sql_parameters,
+        "mysql_sql_parameters_max_length": mysql_sql_parameters_max_length,
+        "pymongo_trace_parameters": pymongo_trace_parameters,
+        "pymongo_parameters_max_length": pymongo_parameters_max_length,
+        "ignore_suffix": ignore_suffix,
+        "flask_collect_http_params": flask_collect_http_params,
+        "http_params_length_threshold": http_params_length_threshold,
+        "django_collect_http_params": django_collect_http_params,
+        "correlation_element_max_number": correlation_element_max_number,
+        "correlation_value_max_length": correlation_value_max_length,
+        "trace_ignore": trace_ignore,
+        "trace_ignore_path": trace_ignore_path
+    }
+
+
+def deserialize(data):
+    global service_name
+    service_name = data["service_name"]
+    global collector_address
+    collector_address = data["collector_address"]
+    global protocol
+    protocol = data["protocol"]
+    global authentication
+    authentication = data["authentication"]
+    global logging_level
+    logging_level = data["logging_level"]
+    global disable_plugins
+    disable_plugins = data["disable_plugins"]
+    global mysql_trace_sql_parameters
+    mysql_trace_sql_parameters = data["mysql_trace_sql_parameters"]
+    global mysql_sql_parameters_max_length
+    mysql_sql_parameters_max_length = data["mysql_sql_parameters_max_length"]
+    global pymongo_trace_parameters
+    pymongo_trace_parameters = data["pymongo_trace_parameters"]
+    global pymongo_parameters_max_length
+    pymongo_parameters_max_length = data["pymongo_parameters_max_length"]
+    global ignore_suffix
+    ignore_suffix = data["ignore_suffix"]
+    global flask_collect_http_params
+    flask_collect_http_params = data["flask_collect_http_params"]
+    global http_params_length_threshold
+    http_params_length_threshold = data["http_params_length_threshold"]
+    global django_collect_http_params
+    django_collect_http_params = data["django_collect_http_params"]
+    global correlation_element_max_number
+    correlation_element_max_number = data["correlation_element_max_number"]
+    global correlation_value_max_length
+    correlation_value_max_length = data["correlation_value_max_length"]
+    global trace_ignore
+    trace_ignore = data["trace_ignore"]
+    global trace_ignore_path
+    trace_ignore_path = data["trace_ignore_path"]
diff --git a/skywalking/trace/ipc/__init__.py b/skywalking/trace/ipc/__init__.py
index 61749c8..b1312a0 100644
--- a/skywalking/trace/ipc/__init__.py
+++ b/skywalking/trace/ipc/__init__.py
@@ -1,2 +1,16 @@
-# -*- coding:utf-8 -*-
-# author:huawei
\ No newline at end of file
+#
+# 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.
+#
diff --git a/skywalking/trace/ipc/process.py b/skywalking/trace/ipc/process.py
index 46911a3..b682ba5 100644
--- a/skywalking/trace/ipc/process.py
+++ b/skywalking/trace/ipc/process.py
@@ -15,6 +15,11 @@
 # limitations under the License.
 #
 
+from multiprocessing import Process
+
+from skywalking import config, agent
+
+
 class SwProcess(Process):
 
     def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, *,
@@ -23,8 +28,12 @@ class SwProcess(Process):
         self._sw_config = config.serialize()
 
     def run(self):
+        print(config.trace_ignore)
+        print(config.service_name)
+        print(config.collector_address)
         if agent.started() is False:
+            print("startttt-----------------")
             config.deserialize(self._sw_config)
             agent.start()
-
+        print(self._sw_config)
         super(SwProcess, self).run()
diff --git a/tests/plugin/__init__.py b/tests/plugin/__init__.py
index f858a1f..c838308 100644
--- a/tests/plugin/__init__.py
+++ b/tests/plugin/__init__.py
@@ -49,8 +49,8 @@ class BasePluginTest(unittest.TestCase, ABC):
 
     @classmethod
     def tearDownClass(cls):
-        cls.compose.stop()
-
+        # cls.compose.stop()
+        pass
     @classmethod
     def host(cls, service_port):
         # type: (ServicePort) -> str
diff --git a/tests/test_process.py b/tests/test_process.py
index 61749c8..48044f1 100644
--- a/tests/test_process.py
+++ b/tests/test_process.py
@@ -1,2 +1,142 @@
-# -*- coding:utf-8 -*-
-# author:huawei
\ No newline at end of file
+# # -*- coding:utf-8 -*-
+# # author:huawei
+#
+# import os
+# import time
+# from functools import wraps
+# from multiprocessing import Process
+# from multiprocessing.process import BaseProcess
+#
+# import requests
+#
+# from skywalking.trace.context import get_context
+#
+# from skywalking import agent, config
+# from skywalking.trace.tags import Tag
+#
+# origin__init__ = BaseProcess.__init__
+# origin_start = BaseProcess.start
+# origin_bootstrap = BaseProcess._bootstrap
+# origin_run = BaseProcess.run
+#
+#
+# def sw__init__(this: BaseProcess, group=None, target=None, name=None, args=(), kwargs={}, *,
+#                daemon=None):
+#     print(config.trace_ignore)
+#     if target is not None:
+#         if kwargs:
+#             kwargs["sw_config"] = config.serialize()
+#         else:
+#             kwargs = {"sw_config": config.serialize()}
+#     print("_init_ %s is running" % os.getpid())
+#     origin__init__(this, group=group, target=target, name=name, args=args, kwargs=kwargs, daemon=daemon)
+#
+#
+# def sw_start(this: BaseProcess):
+#     origin_start(this)
+#     print("start %s is running" % os.getpid())
+#
+#
+# def sw_run(this: BaseProcess):
+#     print("run %s is running" % os.getpid())
+#     origin_run(this)
+#
+#
+# def sw_bootstrap(this: BaseProcess, parent_sentinel=None):
+#     print("bootstrap %s is running" % os.getpid())
+#     origin_bootstrap(this, parent_sentinel=parent_sentinel)
+#
+#
+# def test_decorator():
+#     def decorator(func):
+#         @wraps(func)
+#         def wrapper(*args, **kwargs):
+#             if kwargs is not None and "sw_config" in kwargs:
+#                 print(kwargs.pop('sw_config', None))
+#                 print(agent.started())
+#                 if agent.started() is False:
+#                     agent.start()
+#             func(*args, **kwargs)
+#
+#         return wrapper
+#
+#     return decorator
+#
+#
+# # @test_decorator()
+# def test(name):
+#     # agent.start()
+#     print(name)
+#     print("test %s is running" % os.getpid())
+#     print(get_context())
+#     print(get_context().active_span())
+#     requests.post("http://www.baidu.com")
+#
+#
+# def test2(name):
+#     # agent.start()
+#     print(name)
+#     print("test %s is running" % os.getpid())
+#     print(get_context())
+#     print(get_context().active_span())
+#     with get_context().new_local_span("/test") as span:
+#         span.tag(Tag(key="huawei", val="test"))
+#         requests.post("http://www.baidu.com")
+#
+#
+# # 定义一个要共享实例化对象的类
+# class TestClass():
+#     def __init__(self):
+#         self.test_list = ["start flag"]
+#
+#     def test_function(self, arg):
+#         self.test_list.append(arg)
+#
+#     def print_test_list(self):
+#         for item in self.test_list:
+#             print(f"{item}")
+#
+#
+# class SwProcess(Process):
+#
+#     def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, *,
+#                  daemon=None):
+#         super(SwProcess, self).__init__(group=group, target=target, name=name, args=args, kwargs=kwargs, daemon=daemon)
+#         self._sw_config = config.serialize()
+#
+#     def run(self):
+#         if agent.started() is False:
+#             config.deserialize(self._sw_config)
+#             agent.start()
+#
+#         super(SwProcess, self).run()
+#
+#
+# if __name__ == '__main__':
+#     # manager = BaseManager()
+#     # # 一定要在start前注册,不然就注册无效
+#     # manager.register('Test', TestClass)
+#     # manager.start()
+#     # obj = manager.Test()
+#
+#     # config.service_name = 'consumer'
+#     # config.logging_level = 'DEBUG'
+#     # config.trace_ignore = True
+#     # agent.start()
+#     #
+#     # # BaseProcess.__init__ = sw__init__
+#     # # BaseProcess.start = sw_start
+#     # # BaseProcess._bootstrap = sw_bootstrap
+#     # # BaseProcess.run = sw_run
+#     #
+#     # print("main %s is running" % os.getpid())
+#     #
+#     # # @runnable
+#     #
+#     # p1 = SwProcess(target=test, args=('anni',))
+#     # p1.start()
+#     # p2 = SwProcess(target=test2, args=('anni',))
+#     # p2.start()
+#     #
+#     # time.sleep(50)
+#     pass
diff --git a/tests/test_uwsgi.py b/tests/test_uwsgi.py
index 61749c8..1e7e567 100644
--- a/tests/test_uwsgi.py
+++ b/tests/test_uwsgi.py
@@ -1,2 +1,18 @@
-# -*- coding:utf-8 -*-
-# author:huawei
\ No newline at end of file
+# from flask import Flask
+#
+# from skywalking import config, agent
+#
+# config.service_name = 'consumer'
+# config.logging_level = 'DEBUG'
+# agent.start()
+#
+# app = Flask(__name__)
+#
+# @app.route('/')
+# def index():
+#     return "<span style='color:red'>I am app 1</span>"
+#
+#
+# # app.run()
+# if __name__ == "__main__":
+#     app.run(host='0.0.0.0')