Skip to content

Commit 467a6f8

Browse files
authored
Merge pull request #42 from python-microservices/feature/replace_tracer_jaeger
Replace tracer Jaeger with Lightstep
2 parents 85940b7 + 24728e3 commit 467a6f8

File tree

10 files changed

+137
-87
lines changed

10 files changed

+137
-87
lines changed

Pipfile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ name = "pypi"
55

66
[packages]
77
flask = "*"
8-
jaeger-client = "==3.13.0"
98
python-json-logger = ">=0.1.10"
109
pyyaml = ">=4.2b4"
1110
anyconfig = ">=0.9.8"
1211
swagger-ui-bundle = ">=0.0.2"
1312
connexion = {extras = ["swagger-ui"],version = ">=2.2.0"}
14-
flask-opentracing = "==0.2.0"
15-
tornado = "==4.5.3"
13+
lightstep = "*"
14+
flask-opentracing = "*"
1615

1716
[dev-packages]
1817
requests-mock = "*"

Pipfile.lock

Lines changed: 61 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
PyMS, Python MicroService, is a collections of libraries, best practices and recommended ways to build
1313
microservices with Python.
1414

15-
To know how use, install or build a porject see the docs (TODO: add url to readthedocs)
15+
To know how use, install or build a project see the docs: https://py-ms.readthedocs.io/en/latest/
1616

1717
## Installation
1818
```bash

examples/microservice_requests/config.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pyms:
2-
requests: true
2+
requests:
3+
data: ""
34
swagger:
45
path: ""
56
file: "swagger.yaml"
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from examples.microservice_requests import ms
2-
2+
from flask import current_app
33

44
def example():
5-
return ms.requests.get_for_object("https://ghibliapi.herokuapp.com/films/2baf70d1-42bb-4437-b551-e5fed5a87abe")
5+
current_app.logger.info("start request")
6+
result = ms.requests.get_for_object("https://ghibliapi.herokuapp.com/films/2baf70d1-42bb-4437-b551-e5fed5a87abe")
7+
current_app.logger.info("end request")
8+
return result

pyms/flask/app/__init__.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55
import connexion
66
from flask import Flask
7-
from flask_opentracing import FlaskTracer
7+
from flask_opentracing import FlaskTracing
88

99
from pyms.config.conf import get_conf
10+
from pyms.constants import LOGGER_NAME
1011
from pyms.flask.healthcheck import healthcheck_blueprint
1112
from pyms.flask.services.driver import ServicesManager
1213
from pyms.logger import CustomJsonFormatter
13-
from pyms.tracer.main import init_jaeger_tracer
14+
from pyms.tracer.main import init_lightstep_tracer
15+
16+
logger = logging.getLogger(LOGGER_NAME)
1417

1518

1619
class Microservice:
@@ -32,15 +35,18 @@ def init_libs(self):
3235
return self.application
3336

3437
def init_tracer(self):
35-
self.application.tracer = FlaskTracer(init_jaeger_tracer(), True, self.application)
38+
self.application.opentracing_tracer = init_lightstep_tracer(self.application.config["APP_NAME"])
39+
self.application.tracer = FlaskTracing(self.application.opentracing_tracer, True, self.application)
3640

3741
def init_logger(self):
42+
self.application.logger = logger
43+
os.environ['WERKZEUG_RUN_MAIN'] = "true"
44+
3845
formatter = CustomJsonFormatter('(timestamp) (level) (name) (module) (funcName) (lineno) (message)')
3946
formatter.add_service_name(self.application.config["APP_NAME"])
40-
if getattr(self.application, "tracer", False):
41-
formatter.add_trace_span(self.application.tracer)
4247
log_handler = logging.StreamHandler()
4348
log_handler.setFormatter(formatter)
49+
4450
self.application.logger.addHandler(log_handler)
4551
self.application.logger.propagate = False
4652
self.application.logger.setLevel(logging.INFO)
@@ -78,8 +84,7 @@ def create_app(self):
7884
self.init_libs()
7985
self.add_error_handlers()
8086

81-
if not self.application.config["TESTING"]:
82-
self.init_tracer()
87+
self.init_tracer()
8388

8489
self.init_logger()
8590

pyms/flask/services/requests.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
"""Encapsulate common rest operations between business services propagating trace headers if configured.
22
"""
3+
import logging
4+
35
import opentracing
46
import requests
5-
from flask import current_app
7+
from flask import current_app, request
68

9+
from pyms.constants import LOGGER_NAME
710
from pyms.flask.services.driver import DriverService
811

12+
logger = logging.getLogger(LOGGER_NAME)
13+
914

1015
class Service(DriverService):
1116
service = "requests"
@@ -27,10 +32,10 @@ def insert_trace_headers(self, headers):
2732

2833
try:
2934
# FLASK https://github.com/opentracing-contrib/python-flask
30-
span = self._tracer.get_span()
31-
self._tracer._tracer.inject(span, opentracing.Format.HTTP_HEADERS, headers)
35+
span = self._tracer.get_span(request)
36+
self._tracer.tracer.inject(span.context, opentracing.Format.HTTP_HEADERS, headers)
3237
except Exception as ex:
33-
current_app.logger.warning("Tracer error {}".format(ex))
38+
logger.debug("Tracer error {}".format(ex))
3439
return headers
3540

3641
def _get_headers(self, headers):
@@ -87,7 +92,7 @@ def get(self, url, path_params=None, params=None, headers=None, **kwargs):
8792
:param params: (optional) Dictionary, list of tuples or bytes to send in the body of the :class:`Request` (as query
8893
string parameters)
8994
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
90-
:param \*\*kwargs: Optional arguments that ``request`` takes.
95+
:param kwargs: Optional arguments that ``request`` takes.
9196
:return: :class:`Response <Response>` object
9297
:rtype: requests.Response
9398
"""
@@ -109,7 +114,7 @@ def get_for_object(self, url, path_params=None, params=None, headers=None, **kwa
109114
:param params: (optional) Dictionary, list of tuples or bytes to send in the body of the :class:`Request` (as query
110115
string parameters)
111116
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
112-
:param \*\*kwargs: Optional arguments that ``request`` takes.
117+
:param kwargs: Optional arguments that ``request`` takes.
113118
:return: :class:`Response <Response>` object
114119
:rtype: requests.Response
115120
"""
@@ -126,7 +131,7 @@ def post(self, url, path_params=None, data=None, json=None, headers=None, **kwar
126131
:class:`Request`.
127132
:param json: (optional) json data to send in the body of the :class:`Request`.
128133
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
129-
:param \*\*kwargs: Optional arguments that ``request`` takes.
134+
:param kwargs: Optional arguments that ``request`` takes.
130135
:return: :class:`Response <Response>` object
131136
:rtype: requests.Response
132137
"""
@@ -149,7 +154,7 @@ def post_for_object(self, url, path_params=None, data=None, json=None, headers=N
149154
:class:`Request`.
150155
:param json: (optional) json data to send in the body of the :class:`Request`.
151156
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
152-
:param \*\*kwargs: Optional arguments that ``request`` takes.
157+
:param kwargs: Optional arguments that ``request`` takes.
153158
:return: :class:`Response <Response>` object
154159
:rtype: requests.Response
155160
"""
@@ -166,7 +171,7 @@ def put(self, url, path_params=None, data=None, headers=None, **kwargs):
166171
object to send in the body of the :class:`Request`.
167172
:param json: (optional) json data to send in the body of the :class:`Request`.
168173
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
169-
:param \*\*kwargs: Optional arguments that ``request`` takes.
174+
:param kwargs: Optional arguments that ``request`` takes.
170175
:return: :class:`Response <Response>` object
171176
:rtype: requests.Response
172177
"""
@@ -189,7 +194,7 @@ def put_for_object(self, url, path_params=None, data=None, headers=None, **kwarg
189194
object to send in the body of the :class:`Request`.
190195
:param json: (optional) json data to send in the body of the :class:`Request`.
191196
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
192-
:param \*\*kwargs: Optional arguments that ``request`` takes.
197+
:param kwargs: Optional arguments that ``request`` takes.
193198
:return: :class:`Response <Response>` object
194199
:rtype: requests.Response
195200
"""
@@ -203,7 +208,7 @@ def delete(self, url, path_params=None, headers=None, **kwargs):
203208
:param url: URL for the new :class:`Request` object. Could contain path parameters
204209
:param path_params: (optional) Dictionary, list of tuples with path parameters values to compose url
205210
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
206-
:param \*\*kwargs: Optional arguments that ``request`` takes.
211+
:param kwargs: Optional arguments that ``request`` takes.
207212
:return: :class:`Response <Response>` object
208213
:rtype: requests.Response
209214
"""

0 commit comments

Comments
 (0)