Skip to content

Commit

Permalink
Merge pull request #4 from mjrulesamrat/jaeger-integration
Browse files Browse the repository at this point in the history
Integrate Jaeger Exporter
  • Loading branch information
mjrulesamrat authored Apr 3, 2021
2 parents 9311955 + 1ed2eb2 commit 1f18769
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ Create Python Virtual environment and run projec locally.

## Jaeger setup guidelines

*ToDo
Run Jaeger container locally to collect tracing data from Flask application

```bash
docker run -p 16686:16686 -p 6831:6831/udp jaegertracing/all-in-one
```

## Features

Expand Down
31 changes: 24 additions & 7 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,39 @@
import requests

from flask import Flask
from flask_restful import Resource, Api
from flask_restful import Resource as REST_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.exporter.jaeger.thrift import JaegerExporter
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
ConsoleSpanExporter,
SimpleSpanProcessor,
BatchSpanProcessor
)
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from opentelemetry.instrumentation.requests import RequestsInstrumentor

load_dotenv() # take environment variables from .env

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

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

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

app = Flask(__name__)
Expand All @@ -31,12 +46,14 @@
RequestsInstrumentor().instrument()


class HelloWorld(Resource):
class HelloWorld(REST_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")
with tracer.start_as_current_span("get-oranges"):
data = 100*100
print("Hello World", data)
return {'hello': 'world'}

api.add_resource(HelloWorld, '/')
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ opentelemetry-api==1.0.0
opentelemetry-sdk==1.0.0
opentelemetry-instrumentation-flask==0.19b0
opentelemetry-instrumentation-requests==0.19b0

# opentelemetry exporter
opentelemetry-exporter-jaeger==1.0.0

0 comments on commit 1f18769

Please sign in to comment.