diff --git a/.gitignore b/.gitignore index b439376..b00d5ff 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,6 @@ venv/ # production environment config .env + +# vscode +.vscode/ diff --git a/app.py b/app.py index bf90673..92b347b 100644 --- a/app.py +++ b/app.py @@ -7,13 +7,36 @@ from flask_restful import Resource, Api from dotenv import load_dotenv +from opentelemetry import trace +from opentelemetry.instrumentation.flask import FlaskInstrumentor +from opentelemetry.instrumentation.requests import RequestsInstrumentor +from opentelemetry.sdk.trace import TracerProvider +from opentelemetry.sdk.trace.export import ( + ConsoleSpanExporter, + SimpleSpanProcessor, +) + load_dotenv() # take environment variables from .env +# set trace provider as default one +trace.set_tracer_provider(TracerProvider()) +trace.get_tracer_provider().add_span_processor( + SimpleSpanProcessor(ConsoleSpanExporter()) +) + app = Flask(__name__) api = Api(app) +FlaskInstrumentor().instrument_app(app) +RequestsInstrumentor().instrument() + + class HelloWorld(Resource): def get(self): + tracer = trace.get_tracer(__name__) + with tracer.start_as_current_span("example-request"): + requests.get("http://www.example.com") + print("Hello World") return {'hello': 'world'} api.add_resource(HelloWorld, '/') diff --git a/requirements.txt b/requirements.txt index f6a5782..ffd1079 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,9 @@ python-dotenv==0.17.0 # flask Flask==1.1.2 Flask-RESTful==0.3.8 + +# opentelemetry +opentelemetry-api==1.0.0 +opentelemetry-sdk==1.0.0 +opentelemetry-instrumentation-flask==0.19b0 +opentelemetry-instrumentation-requests==0.19b0