You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ke...@apache.org on 2020/05/06 09:57:03 UTC

[skywalking-python] 01/01: BugFix: http.method is always PATCH

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

kezhenxu94 pushed a commit to branch bugfix/method-and-op
in repository https://gitbox.apache.org/repos/asf/skywalking-python.git

commit a4e8e80395a3a88ecef4823c0d5908409179f5b1
Author: kezhenxu94 <ke...@163.com>
AuthorDate: Wed May 6 17:14:41 2020 +0800

    BugFix: http.method is always PATCH
    
    - Set operation name `/` when the url is just a domain name
    - Rename component `Http` to `General`
---
 README.md                                 |  4 ++--
 skywalking/__init__.py                    |  2 +-
 skywalking/plugins/sw_http/__init__.py    | 29 ++++++++++++++++-------------
 skywalking/plugins/sw_request/__init__.py |  4 ++--
 tests/main.py                             | 10 +++++-----
 5 files changed, 26 insertions(+), 23 deletions(-)

diff --git a/README.md b/README.md
index 22b833e..2d036bc 100644
--- a/README.md
+++ b/README.md
@@ -65,7 +65,7 @@ with context.new_entry_span(op='https://github.com/apache') as span:
 # the span automatically stops when exiting the `with` context
 
 with context.new_exit_span(op='https://github.com/apache', peer='localhost:8080') as span:
-    span.component = Component.Http
+    span.component = Component.Flask
 
 with context.new_local_span(op='https://github.com/apache') as span:
     span.tag(Tag(key='Singer', val='Nakajima'))
@@ -92,7 +92,7 @@ def some_method():
 
 context: SpanContext = get_context()
 with context.new_entry_span(op=str('https://github.com/apache/skywalking')) as span:
-    span.component = Component.Http
+    span.component = Component.Flask
     some_method()
 ```
 
diff --git a/skywalking/__init__.py b/skywalking/__init__.py
index cb1db8b..8848423 100644
--- a/skywalking/__init__.py
+++ b/skywalking/__init__.py
@@ -27,7 +27,7 @@ loggings.init()
 
 class Component(Enum):
     Unknown = 0
-    Http = 49
+    General = 7000  # built-in modules that may not have a logo to display
     Flask = 7001
 
 
diff --git a/skywalking/plugins/sw_http/__init__.py b/skywalking/plugins/sw_http/__init__.py
index 5f72148..d5034a2 100644
--- a/skywalking/plugins/sw_http/__init__.py
+++ b/skywalking/plugins/sw_http/__init__.py
@@ -36,21 +36,24 @@ def install():
         def _sw_handle(this: BaseHTTPRequestHandler):
             http_methods = ('GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH')
             for method in http_methods:
-                if hasattr(this, 'do_' + method) and inspect.ismethod(getattr(this, 'do_' + method)):
-                    _do_method = getattr(this, 'do_' + method)
-
-                    def _sw_do_method():
-                        context = get_context()
-                        with context.new_entry_span(op=this.path) as span:
-                            span.layer = Layer.Http
-                            span.component = Component.Http
-                            span.peer = '%s:%s' % this.client_address
-                            span.tag(Tag(key=tags.HttpMethod, val=method))
-                            _do_method()
-
-                    setattr(this, 'do_' + method, _sw_do_method)
+                _wrap_do_method(this, method)
             _handle(this)
 
+        def _wrap_do_method(this, method):
+            if hasattr(this, 'do_' + method) and inspect.ismethod(getattr(this, 'do_' + method)):
+                _do_method = getattr(this, 'do_' + method)
+
+                def _sw_do_method():
+                    context = get_context()
+                    with context.new_entry_span(op=this.path) as span:
+                        span.layer = Layer.Http
+                        span.component = Component.General
+                        span.peer = '%s:%s' % this.client_address
+                        span.tag(Tag(key=tags.HttpMethod, val=method))
+                        _do_method()
+
+                setattr(this, 'do_' + method, _sw_do_method)
+
         BaseHTTPRequestHandler.handle = _sw_handle
     except Exception:
         logger.warning('failed to install plugin %s', __name__)
diff --git a/skywalking/plugins/sw_request/__init__.py b/skywalking/plugins/sw_request/__init__.py
index 4168f78..6eb2b05 100644
--- a/skywalking/plugins/sw_request/__init__.py
+++ b/skywalking/plugins/sw_request/__init__.py
@@ -35,9 +35,9 @@ def install():
 
         def _sw_open(this: OpenerDirector, fullurl, data, timeout):
             context = get_context()
-            with context.new_exit_span(op=fullurl.selector, peer=fullurl.host) as span:
+            with context.new_exit_span(op=fullurl.selector or '/', peer=fullurl.host) as span:
                 span.layer = Layer.Http
-                span.component = Component.Http
+                span.component = Component.General
                 try:
                     res = _open(this, fullurl, data, timeout)
                     span.tag(Tag(key=tags.HttpMethod, val=fullurl.get_method()))
diff --git a/tests/main.py b/tests/main.py
index a4735e3..428ebcd 100644
--- a/tests/main.py
+++ b/tests/main.py
@@ -36,26 +36,26 @@ if __name__ == '__main__':
     for _ in range(1, 20):
         context = get_context()  # type: SpanContext
         with context.new_entry_span(op='https://github.com/1') as s1:
-            s1.component = Component.Http
+            s1.component = Component.General
             some_method()
             print(s1)
             with context.new_entry_span(op='https://github.com/2') as s2:
-                s2.component = Component.Http
+                s2.component = Component.General
                 s2.layer = Layer.Http
                 print(s2)
                 sleep(0.5)
                 with context.new_exit_span(op='https://github.com/3', peer='127.0.0.1:80') as s3:
-                    s3.component = Component.Http
+                    s3.component = Component.General
                     s3.layer = Layer.Http
                     print(s3)
                     sleep(0.5)
                     with context.new_entry_span(op='https://github.com/4') as s4:
-                        s4.component = Component.Http
+                        s4.component = Component.General
                         s4.layer = Layer.Http
                         print(s4)
                         sleep(0.5)
                 with context.new_exit_span(op='https://github.com/5', peer='127.0.0.1:80') as s5:
-                    s5.component = Component.Http
+                    s5.component = Component.General
                     s5.layer = Layer.Http
                     print(s5)
                     sleep(0.5)