| |
@@ -21,6 +21,7 @@
|
| |
#
|
| |
# Written by Jan Kaluza <jkaluza@redhat.com>
|
| |
|
| |
+ import os
|
| |
from logging import getLogger
|
| |
|
| |
from flask import Flask, jsonify
|
| |
@@ -28,6 +29,15 @@
|
| |
from flask_sqlalchemy import SQLAlchemy
|
| |
from werkzeug.exceptions import BadRequest, Unauthorized, NotFound as WerkzeugNotFound
|
| |
|
| |
+ from opentelemetry import trace
|
| |
+ from opentelemetry.sdk.resources import Resource, SERVICE_NAME
|
| |
+ from opentelemetry.sdk.trace import TracerProvider
|
| |
+ from opentelemetry.sdk.trace.export import ConsoleSpanExporter
|
| |
+ from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
|
| |
+ from opentelemetry.sdk.trace.export import BatchSpanProcessor
|
| |
+ from opentelemetry.instrumentation.flask import FlaskInstrumentor
|
| |
+ from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
|
| |
+
|
| |
from cts.logger import init_logging
|
| |
from cts.config import init_config
|
| |
from cts.proxy import ReverseProxy
|
| |
@@ -59,6 +69,22 @@
|
| |
|
| |
init_auth(login_manager, conf.auth_backend)
|
| |
|
| |
+ # Set up telemetry exporter if configured.
|
| |
+ provider = TracerProvider(resource=Resource.create({SERVICE_NAME: "cts"}))
|
| |
+ trace.set_tracer_provider(provider)
|
| |
+ exporter_url = os.environ.get("OTEL_EXPORTER_OTLP_ENDPOINT")
|
| |
+ if exporter_url == "console":
|
| |
+ provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))
|
| |
+ elif exporter_url:
|
| |
+ provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
|
| |
+
|
| |
+ tracer = trace.get_tracer(__name__)
|
| |
+
|
| |
+ # Initialize Flask drop-in instrumentation middleware
|
| |
+ FlaskInstrumentor().instrument_app(app, tracer_provider=provider)
|
| |
+ if "CTS_INSTRUMENT_DATABASE" in os.environ:
|
| |
+ SQLAlchemyInstrumentor().instrument()
|
| |
+
|
| |
|
| |
def json_error(status, error, message):
|
| |
response = jsonify({"status": status, "error": error, "message": message})
|
| |
There are two commits here.
The first one adds basic tracing to the API: each request would create a new span.
The second commit adds tracing exporter for sqlalchemy. That adds a span for each executed query.
If
OTEL_EXPORTER_OTLP_ENDPOING
is set toconsole
, any other non-empty value configures the collector to send the traces to (similar to ODCS).The messages sent to message bus get a new field
traceparent
with a trace ID.