Skip to content

Commit

Permalink
Merge pull request #3 from mjrulesamrat/opentelemetry-integration
Browse files Browse the repository at this point in the history
Integrate opentelemetry with flask
  • Loading branch information
mjrulesamrat authored Apr 3, 2021
2 parents 9c6156f + a5fc3be commit 9311955
Show file tree
Hide file tree
Showing 3 changed files with 32 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
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9311955

Please sign in to comment.