Skip to content

Commit

Permalink
Distributed tracing example
Browse files Browse the repository at this point in the history
  • Loading branch information
mjrulesamrat committed Apr 4, 2021
1 parent 12102cb commit dee2bb9
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 7 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ Create Python Virtual environment and run projec locally.
flask run
```

- Fire up another console and run second flask service locally

```bash
python app1.py
```

## Jaeger setup guidelines

Run Jaeger container locally to collect tracing data from Flask application
Expand All @@ -48,12 +54,12 @@ docker run -p 16686:16686 -p 6831:6831/udp jaegertracing/all-in-one

## Demo

Make sure you have Flask application running and jaeger running.
Make sure you have Both Flask applications running and jaeger running.

- Make GET requests to below APIs

```
localhost:5000
localhost:5000/hello
localhost:5000/zero
localhost:5000/zero-unhandled
localhost:5000/zero-trace
Expand All @@ -67,3 +73,6 @@ Make sure you have Flask application running and jaeger running.

- Notice the number of spans for each request. We can add more spans to each
REST API for better detailed traces.

- With first request, we can notice that it calls internally second service. As
second service also have tracing enabled, we have more in-depth tracing information.
18 changes: 13 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@
)
from opentelemetry.exporter.jaeger.thrift import JaegerExporter

# propagator
from opentelemetry.propagate import set_global_textmap
from opentelemetry.propagators.b3 import B3Format

load_dotenv() # take environment variables from .env

# Set global propagator
set_global_textmap(B3Format())

# set trace provider as default one
trace.set_tracer_provider(
TracerProvider(
Expand Down Expand Up @@ -52,13 +59,14 @@
class HelloWorld(REST_Resource):
def get(self):
tracer = trace.get_tracer(__name__)
world = None
with tracer.start_as_current_span("example-request"):
requests.get("http://www.example.com")
with tracer.start_as_current_span("get-oranges"):
data = requests.get("http://localhost:3000/get-hello")
world = data.json().get('hello')
with tracer.start_as_current_span("time-consuming-operation"):
data = 100*100
print("Hello World", data)
return {'hello': 'world'}

return {'hello': world}


class ZeroDivision(REST_Resource):
Expand All @@ -83,7 +91,7 @@ def get(self):
explode = 1/0


api.add_resource(HelloWorld, '/')
api.add_resource(HelloWorld, '/hello')
api.add_resource(ZeroDivision, '/zero')
api.add_resource(ZeroDivisionUnHandled, '/zero-unhandled')
api.add_resource(ZeroDivisionZeroTrace, '/zero-trace')
Expand Down
79 changes: 79 additions & 0 deletions app1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
__author__ = "Jay Modi"

import os
import requests

from flask import Flask
from flask_restful import Resource as REST_Resource, Api
from dotenv import load_dotenv

# opentelemetry
from opentelemetry import trace
from opentelemetry.trace.status import StatusCode
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace import TracerProvider

# flask and requests instrumentation
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from opentelemetry.instrumentation.requests import RequestsInstrumentor

# exporters
from opentelemetry.sdk.trace.export import (
ConsoleSpanExporter,
SimpleSpanProcessor,
BatchSpanProcessor
)
from opentelemetry.exporter.jaeger.thrift import JaegerExporter

# propagator
from opentelemetry.propagate import set_global_textmap
from opentelemetry.propagators.b3 import B3Format

load_dotenv() # take environment variables from .env

# Set global propagator
set_global_textmap(B3Format())

# set trace provider as default one
trace.set_tracer_provider(
TracerProvider(
resource=Resource.create({SERVICE_NAME: "my-other-service"})
)
)

# jaeger exporter
jaeger_exporter = JaegerExporter(
agent_host_name="localhost",
agent_port=6831,
)

# add exporter to trace
trace.get_tracer_provider().add_span_processor(
BatchSpanProcessor(jaeger_exporter)
# SimpleSpanProcessor(ConsoleSpanExporter)
)

app = Flask(__name__)
api = Api(app)

FlaskInstrumentor().instrument_app(app)
RequestsInstrumentor().instrument()


class GetHelloWorld(REST_Resource):
def get(self):
tracer = trace.get_tracer(__name__)
world = None
with tracer.start_as_current_span("get-hello-request"):
world = "World!"
return {'hello': world}


api.add_resource(GetHelloWorld, '/get-hello')


if __name__ == "__main__":
app.run(
debug=os.getenv("DEBUG"),
port=3000
)
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ opentelemetry-instrumentation-requests==0.19b0

# opentelemetry exporter
opentelemetry-exporter-jaeger==1.0.0

# opentelemetry propagator
opentelemetry-propagator-b3==1.0.0
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit dee2bb9

Please sign in to comment.