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/23 06:52:56 UTC

[skywalking-python] 01/01: changed kubernetes to urllib3

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

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

commit 1bfa47c94c9316615d7fbcb06db02cb95d7274aa
Author: huawei <hu...@bit-s.cn>
AuthorDate: Sun Aug 23 14:52:29 2020 +0800

    changed kubernetes to urllib3
---
 .github/workflows/build.yaml                       |  6 --
 docs/Plugins.md                                    |  2 +-
 requirements.txt                                   | 12 +++-
 setup.py                                           |  2 +-
 skywalking/__init__.py                             |  2 +-
 .../plugins/{sw_kubernetes.py => sw_urllib3.py}    | 30 ++++----
 .../services => sw_urllib3}/__init__.py            |  0
 .../docker-compose.yml                             | 22 +++++-
 .../expected.data.yml                              | 82 ++++++++--------------
 .../services}/__init__.py                          |  0
 .../services/consumer.py                           | 20 ++----
 .../services/provider.py}                          | 21 ++----
 .../test_urllib3.py}                               |  3 +-
 13 files changed, 93 insertions(+), 109 deletions(-)

diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml
index bbec008..69d871c 100644
--- a/.github/workflows/build.yaml
+++ b/.github/workflows/build.yaml
@@ -39,12 +39,6 @@ jobs:
         uses: actions/setup-python@v2
         with:
           python-version: ${{ matrix.python-version }}
-      - name: Setup k3s
-          run: curl -sfL https://get.k3s.io | sh -
-      - name: set kubeconfig
-          env:
-            KUBECONFIG: /etc/rancher/k3s/k3s.yaml
-        run: sudo mkdir ~/.kube && sudo cp ${KUBECONFIG} ~/.kube/config
       - name: Set up dependencies
         run: make setup install
       - name: Lint codes
diff --git a/docs/Plugins.md b/docs/Plugins.md
index 7a579c0..4ed549d 100644
--- a/docs/Plugins.md
+++ b/docs/Plugins.md
@@ -31,7 +31,7 @@ Library | Versions | Plugin Name
 | [pika](https://pika.readthedocs.io/en/stable/) | 1.1.0 | `sw_rabbitmq` |
 | [pymongo](https://pymongo.readthedocs.io/en/stable/) | 3.11.0 | `sw_pymongo` |
 | [elasticsearch](https://github.com/elastic/elasticsearch-py) | 7.9.0 | `sw_elasticsearch` |
-| [kubernetes](https://github.com/kubernetes-client/python) | 11.0.0 | `sw_kubernetes` |
+| [urllib3](https://urllib3.readthedocs.io/en/latest/) | 1.25.10 | `sw_urllib3` |
 
 The column `Versions` only indicates that the versions are tested, if you found the newer versions are also supported, welcome to add the newer version into the table.
 >>>>>>> master
diff --git a/requirements.txt b/requirements.txt
index 7fb382b..68ebe78 100755
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,6 +1,8 @@
+apache-skywalking==0.2.0
 asgiref==3.2.10
 attrs==19.3.0
 blindspin==2.0.1
+cachetools==4.1.1
 certifi==2020.6.20
 chardet==3.0.4
 click==7.1.2
@@ -12,9 +14,11 @@ docker==4.2.2
 elasticsearch==7.8.0
 Flask==1.1.2
 gevent==20.6.2
+google-auth==1.20.1
 greenlet==0.4.16
 grpcio==1.31.0
 grpcio-tools==1.31.0
+gunicorn==20.0.4
 idna==2.10
 importlib-metadata==1.7.0
 iniconfig==1.0.1
@@ -23,30 +27,36 @@ Jinja2==2.11.2
 kafka-python==2.0.1
 MarkupSafe==1.1.1
 more-itertools==8.4.0
+oauthlib==3.1.0
 packaging==20.4
 parameterized==0.7.4
 pika==1.1.0
 pluggy==0.13.1
 protobuf==3.12.4
 py==1.9.0
+pyasn1==0.4.8
+pyasn1-modules==0.2.8
 pymongo==3.11.0
 PyMySQL==0.10.0
 pyparsing==2.4.7
 pytest==6.0.1
+python-dateutil==2.8.1
 pytz==2020.1
 PyYAML==5.3.1
 redis==3.5.3
 requests==2.24.0
+requests-oauthlib==1.3.0
+rsa==4.6
 six==1.15.0
 sqlparse==0.3.1
 testcontainers==3.0.3
 toml==0.10.1
 tornado==6.0.4
 urllib3==1.25.10
+uWSGI==2.0.19.1
 websocket-client==0.57.0
 Werkzeug==1.0.1
 wrapt==1.12.1
-kubernetes==11.0.0
 zipp==3.1.0
 zope.event==4.4
 zope.interface==5.1.0
diff --git a/setup.py b/setup.py
index 98a00c2..4d3f669 100644
--- a/setup.py
+++ b/setup.py
@@ -54,7 +54,7 @@ setup(
             "pika",
             "pymongo",
             "elasticsearch",
-            "kubernetes",
+            "urllib3",
             "pytest",
         ],
     },
diff --git a/skywalking/__init__.py b/skywalking/__init__.py
index 005e93a..6df8ecb 100644
--- a/skywalking/__init__.py
+++ b/skywalking/__init__.py
@@ -36,7 +36,7 @@ class Component(Enum):
     RabbitmqProducer = 52
     RabbitmqConsumer = 53
     Elasticsearch = 47
-    Kubernetes = 96
+    Urllib3 = 7006
 
 
 class Layer(Enum):
diff --git a/skywalking/plugins/sw_kubernetes.py b/skywalking/plugins/sw_urllib3.py
similarity index 69%
rename from skywalking/plugins/sw_kubernetes.py
rename to skywalking/plugins/sw_urllib3.py
index f6af33a..a8fc984 100644
--- a/skywalking/plugins/sw_kubernetes.py
+++ b/skywalking/plugins/sw_urllib3.py
@@ -19,6 +19,7 @@ import logging
 
 from skywalking import Layer, Component
 from skywalking.trace import tags
+from skywalking.trace.carrier import Carrier
 from skywalking.trace.context import get_context
 from skywalking.trace.tags import Tag
 
@@ -28,25 +29,30 @@ logger = logging.getLogger(__name__)
 def install():
     # noinspection PyBroadException
     try:
-        from kubernetes.client.rest import RESTClientObject
+        from urllib3.request import RequestMethods
 
-        _request = RESTClientObject.request
+        _request = RequestMethods.request
 
-        def _sw_request(this: RESTClientObject, method, url, query_params=None, headers=None,
-                        body=None, post_params=None, _preload_content=True,
-                        _request_timeout=None):
+        def _sw_request(this: RequestMethods, method, url, fields=None, headers=None, **urlopen_kw):
 
             from urllib.parse import urlparse
             url_param = urlparse(url)
-
+            carrier = Carrier()
             context = get_context()
-            with context.new_exit_span(op=url_param.path or "/", peer=url_param.netloc) as span:
+            with context.new_exit_span(op=url_param.path or "/", peer=url_param.netloc, carrier=carrier) as span:
                 span.layer = Layer.Http
-                span.component = Component.Kubernetes
+                span.component = Component.Urllib3
+
+                if headers is None:
+                    headers = {}
+                    for item in carrier:
+                        headers[item.key] = item.val
+                else:
+                    for item in carrier:
+                        headers[item.key] = item.val
+
                 try:
-                    res = _request(this, method, url, query_params=query_params, headers=headers,
-                                   post_params=post_params, body=body, _preload_content=_preload_content,
-                                   _request_timeout=_request_timeout)
+                    res = _request(this, method, url, fields=fields, headers=headers, **urlopen_kw)
 
                     span.tag(Tag(key=tags.HttpMethod, val=method.upper()))
                     span.tag(Tag(key=tags.HttpUrl, val=url))
@@ -58,6 +64,6 @@ def install():
                     raise e
                 return res
 
-        RESTClientObject.request = _sw_request
+        RequestMethods.request = _sw_request
     except Exception:
         logger.warning('failed to install plugin %s', __name__)
diff --git a/tests/plugin/sw_kubernates/services/__init__.py b/tests/plugin/sw_urllib3/__init__.py
similarity index 100%
rename from tests/plugin/sw_kubernates/services/__init__.py
rename to tests/plugin/sw_urllib3/__init__.py
diff --git a/tests/plugin/sw_kubernates/docker-compose.yml b/tests/plugin/sw_urllib3/docker-compose.yml
similarity index 64%
rename from tests/plugin/sw_kubernates/docker-compose.yml
rename to tests/plugin/sw_urllib3/docker-compose.yml
index 4d2bb8d..d9683a9 100644
--- a/tests/plugin/sw_kubernates/docker-compose.yml
+++ b/tests/plugin/sw_urllib3/docker-compose.yml
@@ -23,6 +23,24 @@ services:
       service: collector
       file: ../docker/docker-compose.base.yml
 
+  provider:
+    extends:
+      service: agent
+      file: ../docker/docker-compose.base.yml
+    ports:
+      - 9091:9091
+    volumes:
+      - .:/app
+    command: ['bash', '-c', 'pip install flask && pip install -r /app/requirements.txt && python3 /app/services/provider.py']
+    depends_on:
+      collector:
+        condition: service_healthy
+    healthcheck:
+      test: ["CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/9091"]
+      interval: 5s
+      timeout: 60s
+      retries: 120
+
   consumer:
     extends:
       service: agent
@@ -31,10 +49,12 @@ services:
       - 9090:9090
     volumes:
       - ./services/consumer.py:/app/consumer.py
-    command: ['bash', '-c', 'pip install flask && pip install kubernetes && python3 /app/consumer.py']
+    command: ['bash', '-c', 'pip install flask && pip install urllib3 && python3 /app/consumer.py']
     depends_on:
       collector:
         condition: service_healthy
+      provider:
+        condition: service_healthy
 
 networks:
   beyond:
diff --git a/tests/plugin/sw_kubernates/expected.data.yml b/tests/plugin/sw_urllib3/expected.data.yml
similarity index 58%
rename from tests/plugin/sw_kubernates/expected.data.yml
rename to tests/plugin/sw_urllib3/expected.data.yml
index 8ce92f7..42f9c3c 100644
--- a/tests/plugin/sw_kubernates/expected.data.yml
+++ b/tests/plugin/sw_urllib3/expected.data.yml
@@ -15,86 +15,62 @@
 # limitations under the License.
 #
 
+
 segmentItems:
-  - serviceName: consumer
+  - serviceName: provider
     segmentSize: 1
     segments:
       - segmentId: not null
         spans:
-          - operationName: /api/v1/namespaces
+          - operationName: /users
             operationId: 0
-            parentSpanId: 0
-            spanId: 1
+            parentSpanId: -1
+            spanId: 0
             spanLayer: Http
-            startTime: gt 0
-            endTime: gt 0
-            componentId: 96
-            isError: false
-            spanType: Exit
-            peer: not null
-            skipAnalysis: false
             tags:
               - key: http.method
-                value: GET
+                value: POST
               - key: url
-                value: not null
+                value: http://provider:9091/users
               - key: status.code
                 value: '200'
-          - operationName: /api/v1/pods
-            operationId: 0
-            parentSpanId: 0
-            spanId: 2
-            spanLayer: Http
+            refs:
+              - parentEndpoint: /users
+                networkAddress: 'provider:9091'
+                refType: CrossProcess
+                parentSpanId: 1
+                parentTraceSegmentId: not null
+                parentServiceInstance: not null
+                parentService: consumer
+                traceId: not null
             startTime: gt 0
             endTime: gt 0
-            componentId: 96
-            isError: false
-            spanType: Exit
-            peer: not null
-            skipAnalysis: false
-            tags:
-              - key: http.method
-                value: GET
-              - key: url
-                value: not null
-              - key: status.code
-                value: '200'
-          - operationName: /apis/apps/v1/deployments
-            operationId: 0
-            parentSpanId: 0
-            spanId: 3
-            spanLayer: Http
-            startTime: gt 0
-            endTime: gt 0
-            componentId: 96
-            isError: false
-            spanType: Exit
+            componentId: 7001
+            spanType: Entry
             peer: not null
             skipAnalysis: false
-            tags:
-              - key: http.method
-                value: GET
-              - key: url
-                value: not null
-              - key: status.code
-                value: '200'
-          - operationName: /apis/networking.k8s.io/v1beta1/ingresses
+  - serviceName: consumer
+    segmentSize: 1
+    segments:
+      - segmentId: not null
+        spans:
+          - operationName: /users
             operationId: 0
             parentSpanId: 0
-            spanId: 4
+            spanId: 1
             spanLayer: Http
             startTime: gt 0
             endTime: gt 0
-            componentId: 96
+            componentId: 7006
             isError: false
             spanType: Exit
             peer: not null
             skipAnalysis: false
             tags:
               - key: http.method
-                value: GET
+                value: POST
               - key: url
-                value: not null
+                value: 'http://provider:9091/users'
               - key: status.code
                 value: '200'
           - operationName: /users
@@ -115,4 +91,4 @@ segmentItems:
               - key: url
                 value: 'http://0.0.0.0:9090/users'
               - key: status.code
-                value: '200'
+                value: '200'
\ No newline at end of file
diff --git a/tests/plugin/sw_kubernates/__init__.py b/tests/plugin/sw_urllib3/services/__init__.py
similarity index 100%
rename from tests/plugin/sw_kubernates/__init__.py
rename to tests/plugin/sw_urllib3/services/__init__.py
diff --git a/tests/plugin/sw_kubernates/services/consumer.py b/tests/plugin/sw_urllib3/services/consumer.py
similarity index 69%
copy from tests/plugin/sw_kubernates/services/consumer.py
copy to tests/plugin/sw_urllib3/services/consumer.py
index 4b160c0..a039baf 100644
--- a/tests/plugin/sw_kubernates/services/consumer.py
+++ b/tests/plugin/sw_urllib3/services/consumer.py
@@ -14,11 +14,10 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
-
+import json
 
 from skywalking import agent, config
 
-
 if __name__ == '__main__':
     config.service_name = 'consumer'
     config.logging_level = 'DEBUG'
@@ -26,25 +25,16 @@ if __name__ == '__main__':
 
     from flask import Flask, jsonify
 
-
     app = Flask(__name__)
+    import urllib3
 
 
     @app.route("/users", methods=["POST", "GET"])
     def application():
-        from kubernetes import client
-        import kubernetes.config as kube_config
-        kube_config.load_kube_config()
-        core_conn = client.CoreV1Api()
-        apps_conn = client.AppsV1Api()
-        net_conn = client.NetworkingV1beta1Api()
-
-        core_conn.list_namespace()
-        core_conn.list_pod_for_all_namespaces()
-        apps_conn.list_deployment_for_all_namespaces()
-        net_conn.list_ingress_for_all_namespaces()
-        return jsonify({"song": "Despacito", "artist": "Luis Fonsi"})
+        http = urllib3.PoolManager()
+        res = http.request("POST", "http://provider:9091/users")
 
+        return jsonify(json.loads(res.data.decode('utf-8')))
 
     PORT = 9090
     app.run(host='0.0.0.0', port=PORT, debug=True)
diff --git a/tests/plugin/sw_kubernates/services/consumer.py b/tests/plugin/sw_urllib3/services/provider.py
similarity index 70%
rename from tests/plugin/sw_kubernates/services/consumer.py
rename to tests/plugin/sw_urllib3/services/provider.py
index 4b160c0..11f2c0b 100644
--- a/tests/plugin/sw_kubernates/services/consumer.py
+++ b/tests/plugin/sw_urllib3/services/provider.py
@@ -15,36 +15,23 @@
 # limitations under the License.
 #
 
+import time
 
 from skywalking import agent, config
 
-
 if __name__ == '__main__':
-    config.service_name = 'consumer'
+    config.service_name = 'provider'
     config.logging_level = 'DEBUG'
     agent.start()
 
     from flask import Flask, jsonify
 
-
     app = Flask(__name__)
 
-
     @app.route("/users", methods=["POST", "GET"])
     def application():
-        from kubernetes import client
-        import kubernetes.config as kube_config
-        kube_config.load_kube_config()
-        core_conn = client.CoreV1Api()
-        apps_conn = client.AppsV1Api()
-        net_conn = client.NetworkingV1beta1Api()
-
-        core_conn.list_namespace()
-        core_conn.list_pod_for_all_namespaces()
-        apps_conn.list_deployment_for_all_namespaces()
-        net_conn.list_ingress_for_all_namespaces()
+        time.sleep(0.5)
         return jsonify({"song": "Despacito", "artist": "Luis Fonsi"})
 
-
-    PORT = 9090
+    PORT = 9091
     app.run(host='0.0.0.0', port=PORT, debug=True)
diff --git a/tests/plugin/sw_kubernates/test_kubernates.py b/tests/plugin/sw_urllib3/test_urllib3.py
similarity index 95%
rename from tests/plugin/sw_kubernates/test_kubernates.py
rename to tests/plugin/sw_urllib3/test_urllib3.py
index d1f2f17..e56a82b 100644
--- a/tests/plugin/sw_kubernates/test_kubernates.py
+++ b/tests/plugin/sw_urllib3/test_urllib3.py
@@ -30,7 +30,8 @@ def prepare():
 
 class TestPlugin(TestPluginBase):
     @pytest.mark.parametrize('version', [
-        'kubernetes==11.0.0',
+        'urllib3==1.25.10',
+        'urllib3==1.25.9'
     ])
     def test_plugin(self, docker_compose, version):
         self.validate()