From 7cda14342222d8617a9236340e1ba7a553ec7d48 Mon Sep 17 00:00:00 2001 From: ChromaticPanic <78883122+ChromaticPanic@users.noreply.github.com> Date: Thu, 19 Sep 2024 21:29:43 -0500 Subject: [PATCH] tracer --- .env.template | 1 + app.py | 12 ++++++++++++ requirements.txt | 4 ++++ 3 files changed, 17 insertions(+) diff --git a/.env.template b/.env.template index 5a8a7c0..4e8506b 100644 --- a/.env.template +++ b/.env.template @@ -12,3 +12,4 @@ NACHET_MAX_CONTENT_LENGTH= NACHET_VALID_EXTENSION= NACHET_VALID_DIMENSION= DEV_USER_EMAIL= +OTEL_COLLECTOR_ENDPOINT= diff --git a/app.py b/app.py index b97d9b1..8190782 100644 --- a/app.py +++ b/app.py @@ -16,6 +16,10 @@ from collections import namedtuple from cryptography.fernet import Fernet from opentelemetry.instrumentation.asgi import OpenTelemetryMiddleware +from opentelemetry import trace +from opentelemetry.sdk.trace import TracerProvider +from opentelemetry.sdk.trace.export import BatchSpanProcessor +from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter load_dotenv() # noqa: E402 @@ -86,6 +90,8 @@ class MaxContentLengthWarning(APIWarnings): NACHET_DATA = os.getenv("NACHET_DATA") +OTEL_COLLECTOR_URL = os.getenv("OTEL_COLLECTOR_ENDPOINT") + try: VALID_EXTENSION = json.loads(os.getenv("NACHET_VALID_EXTENSION")) VALID_DIMENSION = json.loads(os.getenv("NACHET_VALID_DIMENSION")) @@ -131,6 +137,12 @@ class MaxContentLengthWarning(APIWarnings): "validators": [] } + +trace.set_tracer_provider(TracerProvider()) +tracer = trace.get_tracer(__name__) +span_processor = BatchSpanProcessor(OTLPSpanExporter(endpoint=OTEL_COLLECTOR_URL)) +trace.get_tracer_provider().add_span_processor(span_processor) + app = Quart(__name__) app = cors(app, allow_origin="*", allow_methods=["GET", "POST", "OPTIONS"]) app.config["MAX_CONTENT_LENGTH"] = MAX_CONTENT_LENGTH_MEGABYTES * 1024 * 1024 diff --git a/requirements.txt b/requirements.txt index a57a61e..fc43b60 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,4 +11,8 @@ pyyaml pydantic python-magic opentelemetry-instrumentation-asgi +opentelemetry-distro +opentelemetry-exporter-otlp +opentelemetry-sdk +opentelemetry-api nachet-datastore @git+https://github.com/ai-cfia/ailab-datastore.git@main