wip
This commit is contained in:
parent
0a4b32dffe
commit
774ea44af1
9 changed files with 160 additions and 115 deletions
75
main.py
75
main.py
|
|
@ -1,36 +1,42 @@
|
|||
import time
|
||||
import random
|
||||
from flask import Flask
|
||||
from opentelemetry import trace
|
||||
from opentelemetry import trace, metrics
|
||||
from opentelemetry.sdk.trace import TracerProvider
|
||||
from opentelemetry.sdk.trace.export import BatchSpanProcessor
|
||||
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
|
||||
from opentelemetry.instrumentation.flask import FlaskInstrumentor
|
||||
from opentelemetry.sdk.metrics import MeterProvider
|
||||
from opentelemetry.sdk.resources import Resource
|
||||
from opentelemetry.sdk.trace.export import BatchSpanProcessor
|
||||
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
|
||||
from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter
|
||||
import logging
|
||||
from opentelemetry.trace import get_current_span
|
||||
|
||||
# === Resource ===
|
||||
resource = Resource(attributes={"service.name": "my-flask-app"})
|
||||
|
||||
# Configure tracing
|
||||
trace.set_tracer_provider(
|
||||
TracerProvider(resource=Resource.create({"service.name": "my-flask-app"}))
|
||||
)
|
||||
# === Tracing ===
|
||||
trace.set_tracer_provider(TracerProvider(resource=resource))
|
||||
tracer = trace.get_tracer(__name__)
|
||||
span_processor = BatchSpanProcessor(OTLPSpanExporter())
|
||||
trace.get_tracer_provider().add_span_processor(span_processor)
|
||||
# trace_exporter = OTLPSpanExporter(
|
||||
# endpoint="http://otel-collector.meta.svc:4318/v1/traces"
|
||||
# )
|
||||
trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(trace_exporter))
|
||||
|
||||
# === Metrics ===
|
||||
reader = PeriodicExportingMetricReader(
|
||||
OTLPMetricExporter(endpoint="http://otel-collector.meta.svc:4318/v1/metrics")
|
||||
)
|
||||
provider = MeterProvider(resource=resource, metric_readers=[reader])
|
||||
metrics.set_meter_provider(provider)
|
||||
|
||||
|
||||
# Setup logging with trace_id
|
||||
# === Logging with trace ID ===
|
||||
class TraceIdFilter(logging.Filter):
|
||||
def filter(self, record):
|
||||
span = trace.get_current_span()
|
||||
ctx = span.get_span_context()
|
||||
if ctx.trace_id != 0:
|
||||
record.trace_id = format(ctx.trace_id, "032x")
|
||||
record.span_id = format(ctx.span_id, "016x")
|
||||
else:
|
||||
record.trace_id = None
|
||||
record.span_id = None
|
||||
record.trace_id = format(ctx.trace_id, "032x") if ctx.trace_id != 0 else None
|
||||
record.span_id = format(ctx.span_id, "016x") if ctx.span_id != 0 else None
|
||||
return True
|
||||
|
||||
|
||||
|
|
@ -45,43 +51,22 @@ logger = logging.getLogger("myapp")
|
|||
logger.setLevel(logging.INFO)
|
||||
logger.addHandler(handler)
|
||||
|
||||
# Setup Tracer
|
||||
resource = Resource(
|
||||
attributes={
|
||||
"service.name": "python-otel-demo-app", # 👈 choose any name you like
|
||||
}
|
||||
)
|
||||
trace.set_tracer_provider(TracerProvider(resource=resource))
|
||||
tracer = trace.get_tracer(__name__)
|
||||
|
||||
# Configure OTLP HTTP exporter
|
||||
otlp_exporter = OTLPSpanExporter(
|
||||
endpoint="http://otel-collector.meta.svc:4318/v1/traces",
|
||||
# insecure=True,
|
||||
)
|
||||
span_processor = BatchSpanProcessor(otlp_exporter)
|
||||
trace.get_tracer_provider().add_span_processor(span_processor)
|
||||
|
||||
# Flask App
|
||||
# === Flask App ===
|
||||
app = Flask(__name__)
|
||||
FlaskInstrumentor().instrument_app(app)
|
||||
|
||||
|
||||
def get_trace_id():
|
||||
span = get_current_span()
|
||||
if span:
|
||||
return span.get_span_context().trace_id
|
||||
return None
|
||||
# FlaskInstrumentor().instrument_app(app) # auto-instruments metrics + traces
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def root():
|
||||
with tracer.start_as_current_span("handle_homepage"):
|
||||
logger.info("handling request", extra={"trace_id": get_trace_id()})
|
||||
logger.info(
|
||||
"handling request",
|
||||
extra={"trace_id": get_current_span().get_span_context().trace_id},
|
||||
)
|
||||
time.sleep(random.random() * 0.05) # simulate request parsing
|
||||
fetch_user()
|
||||
calculate_recommendations()
|
||||
return "Hello from / with traces!"
|
||||
return "Hello from / with traces and metrics!"
|
||||
|
||||
|
||||
def fetch_user():
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue