-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpydemoapp.py
More file actions
81 lines (72 loc) · 2.42 KB
/
pydemoapp.py
File metadata and controls
81 lines (72 loc) · 2.42 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Use the following environment variables to configure the OpenTelemetry settings:
# OTEL_EXPORTER_OTLP_TRACES_TIMEOUT
# OTEL_EXPORTER_OTLP_TRACES_PROTOCOL
# OTEL_EXPORTER_OTLP_TRACES_HEADERS
# OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
# OTEL_EXPORTER_OTLP_TRACES_COMPRESSION
# OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE
# OTEL_EXPORTER_OTLP_TIMEOUT
# OTEL_EXPORTER_OTLP_PROTOCOL
# OTEL_EXPORTER_OTLP_HEADERS
# OTEL_EXPORTER_OTLP_ENDPOINT
# OTEL_EXPORTER_OTLP_COMPRESSION
# OTEL_EXPORTER_OTLP_CERTIFICATE
#
# Importing flask module in the project is mandatory
# An object of Flask class is our WSGI application.
from flask import Flask, request
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.export import (
BatchSpanProcessor,
ConsoleSpanExporter,
)
from opentelemetry.trace import get_tracer_provider, set_tracer_provider
from opentelemetry.exporter.otlp.proto.http.trace_exporter import (
OTLPSpanExporter,
)
import operator
ops = {
'add' : operator.add,
'sub' : operator.sub,
'mul' : operator.mul,
'div' : operator.truediv, # use operator.div for Python 2
'mod' : operator.mod,
'xor' : operator.xor,
}
# OpenTelemetry initializations
resource = Resource(attributes={
"service.name": "PyDemoApp"
})
set_tracer_provider(TracerProvider(resource=resource))
get_tracer_provider().add_span_processor(
BatchSpanProcessor(ConsoleSpanExporter())
)
get_tracer_provider().add_span_processor(
BatchSpanProcessor(OTLPSpanExporter())
)
instrumentor = FlaskInstrumentor()
# Flask constructor takes the name of
# current module (__name__) as argument.
app = Flask(__name__)
instrumentor.instrument_app(app)
# The route() function of the Flask class is a decorator,
# which tells the application which URL should call
# the associated function.
@app.route('/')
# ‘/’ URL is bound with hello_world() function.
def hello_world():
return 'Hello from PyDemoApp!'
# Example: http://somepath/math?first=1&second=5?operator=+
@app.route('/math')
def login():
first = int(request.args.get('first'))
op = request.args.get('operator')
second = int(request.args.get('second'))
return str(ops[op](first, second))
# main driver function
if __name__ == '__main__':
# run() method of Flask class runs the application
# on the local development server.
app.run(host='0.0.0.0')