Skip to content

Commit

Permalink
Add exception tracing examples
Browse files Browse the repository at this point in the history
Update README with demo guideline.
  • Loading branch information
mjrulesamrat committed Apr 3, 2021
1 parent 1f18769 commit 12102cb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ Run Jaeger container locally to collect tracing data from Flask application
docker run -p 16686:16686 -p 6831:6831/udp jaegertracing/all-in-one
```

## Features
## Demo

*ToDo
Make sure you have Flask application running and jaeger running.

- Make GET requests to below APIs

```
localhost:5000
localhost:5000/zero
localhost:5000/zero-unhandled
localhost:5000/zero-trace
```

- Go to jaeger home page and select service `my-helloworld-service` to find trace

```
http://localhost:16686/search
```

- Notice the number of spans for each request. We can add more spans to each
REST API for better detailed traces.
36 changes: 33 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
from dotenv import load_dotenv

from opentelemetry import trace
from opentelemetry.exporter.jaeger.thrift import JaegerExporter
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.trace.status import StatusCode
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from opentelemetry.instrumentation.requests import RequestsInstrumentor

from opentelemetry.sdk.trace.export import (
ConsoleSpanExporter,
SimpleSpanProcessor,
BatchSpanProcessor
)
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from opentelemetry.instrumentation.requests import RequestsInstrumentor
from opentelemetry.exporter.jaeger.thrift import JaegerExporter

load_dotenv() # take environment variables from .env

Expand All @@ -37,6 +39,7 @@
# add exporter to trace
trace.get_tracer_provider().add_span_processor(
BatchSpanProcessor(jaeger_exporter)
# SimpleSpanProcessor(ConsoleSpanExporter)
)

app = Flask(__name__)
Expand All @@ -56,7 +59,34 @@ def get(self):
print("Hello World", data)
return {'hello': 'world'}



class ZeroDivision(REST_Resource):
def get(self):
try:
explode = 1/0
except ZeroDivisionError as error:
span = trace.get_current_span()
span.record_exception(error)
span.set_status(StatusCode.ERROR)


class ZeroDivisionUnHandled(REST_Resource):
def get(self):
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("ZeroDivisonUnHandled"):
explode = 1/0


class ZeroDivisionZeroTrace(REST_Resource):
def get(self):
explode = 1/0


api.add_resource(HelloWorld, '/')
api.add_resource(ZeroDivision, '/zero')
api.add_resource(ZeroDivisionUnHandled, '/zero-unhandled')
api.add_resource(ZeroDivisionZeroTrace, '/zero-trace')


if __name__ == "__main__":
Expand Down

0 comments on commit 12102cb

Please sign in to comment.