Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
47 lines (40 loc) · 1.9 KB
/
Copy pathserver.py
File metadata and controls
47 lines (40 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import sys
if sys.version_info[:2] >= (3, 10):
# compat for Python written pre-3.10
import collections
import collections.abc
collections.MutableMapping = collections.abc.MutableMapping
import logging
from jaeger_client import Config
from flask_opentracing import FlaskTracing
from flask import Flask, request
from os import getenv
JAEGER_HOST = getenv('JAEGER_HOST', 'localhost')
if __name__ == '__main__':
app = Flask(__name__)
log_level = logging.DEBUG
logging.getLogger('').handlers = []
logging.basicConfig(format='%(asctime)s %(message)s', level=log_level)
# Create configuration object with enabled logging and sampling of all requests.
config = Config(config={'sampler': {'type': 'const', 'param': 1},
'logging': True,
'local_agent':
# Also, provide a hostname of Jaeger instance to send traces to.
{'reporting_host': JAEGER_HOST}},
# Service name can be arbitrary string describing this particular web service.
service_name="jaeger_opentracing_example")
jaeger_tracer = config.initialize_tracer()
tracing = FlaskTracing(jaeger_tracer)
@app.route('/log')
@tracing.trace() # Indicate that /log endpoint should be traced
def log():
# Extract the span information for request object.
with jaeger_tracer.start_active_span(
'python webserver internal span of log method') as scope:
# Perform some computations to be traced.
a = 1
b = 2
c = a + b
scope.span.log_kv({'event': 'my computer knows math!', 'result': c})
return "log"
app.run(debug=True, host='0.0.0.0', port=5000)