Skip to content

Commit

Permalink
Integrate Opentelemetry with Flask
Browse files Browse the repository at this point in the history
Watch tracing into console with ConsoleSpanExporter
  • Loading branch information
mjrulesamrat committed Apr 3, 2021
1 parent 5b5c5dc commit a5fc3be
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ venv/

# production environment config
.env

# vscode
.vscode/
23 changes: 23 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/')
Expand Down

0 comments on commit a5fc3be

Please sign in to comment.