Commit 35b630a3 authored by Nane Kratzke's avatar Nane Kratzke
Browse files

Distributed tracing?

parent 95d7a4c3
from flask import Flask, redirect, request from flask import Flask, redirect, request
import requests, os, logging, json import requests, os, logging, json
from elasticapm.contrib.flask import ElasticAPM import elasticapm
from elasticapm.utils.disttracing import TraceParent
# A very boring and basic hip-hop-service to demonstrate # A very boring and basic hip-hop-service to demonstrate
# observability via tracing across multiple services. # observability via tracing across multiple services.
...@@ -10,7 +11,7 @@ logging.basicConfig(level=logging.INFO) ...@@ -10,7 +11,7 @@ logging.basicConfig(level=logging.INFO)
service = os.environ.get('HIPHOP', 'hiphop') service = os.environ.get('HIPHOP', 'hiphop')
tier = int(os.environ.get('TIER', '1')) tier = int(os.environ.get('TIER', '1'))
app = Flask(__name__) app = Flask(__name__)
apm = ElasticAPM(app, service_name=service) client = elasticapm.Client(service_name=service)
@app.route('/') @app.route('/')
def index(): def index():
...@@ -19,6 +20,8 @@ def index(): ...@@ -19,6 +20,8 @@ def index():
@app.route('/hip') @app.route('/hip')
def hip(): def hip():
logging.info(f"Request from { request.remote_addr } with headers { json.dumps(dict(request.headers)) }") logging.info(f"Request from { request.remote_addr } with headers { json.dumps(dict(request.headers)) }")
trace_parent = TraceParent.from_headers(request.headers)
client.begin_transaction("main", trace_parent=trace_parent)
r = "" r = ""
url = f"http://{service}-{tier + 1}/hip" url = f"http://{service}-{tier + 1}/hip"
try: try:
...@@ -28,6 +31,8 @@ def hip(): ...@@ -28,6 +31,8 @@ def hip():
r = "END" r = "END"
hip = "hip " if tier == 1 else "" hip = "hip " if tier == 1 else ""
client.end_transaction()
return f"{hip} hop-{ tier } -> { r }".strip() return f"{hip} hop-{ tier } -> { r }".strip()
app.run(host="0.0.0.0", port=80) app.run(host="0.0.0.0", port=80)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment