-
Notifications
You must be signed in to change notification settings - Fork 10
feat(guardrails): implement new auth and endpoint proxy routing to kaapi-guardrails #1135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ffed94c
707cfa5
27c9216
a332c24
319c0ea
0cc41d8
9dcea43
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,19 +63,21 @@ def _extract_parent_context(task_instance) -> otel_context.Context: | |
|
|
||
|
|
||
| def _run_with_otel_parent(task_instance, fn): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Add narrow type hints to
🤖 Prompt for AI Agents |
||
| """Attach extracted parent context and execute function. | ||
|
|
||
| When Celery auto-instrumentation is active, there is already a current | ||
| `run/...` span. Re-attaching extracted parent context here would make | ||
| service spans become siblings of `run/...` instead of children. | ||
|
|
||
| We only attach extracted context as a fallback when no active span exists. | ||
| """Attach the extracted parent context and execute `fn` under it. | ||
|
|
||
| opentelemetry-instrumentation-celery's own extraction (CeleryGetter) | ||
| reads headers via getattr(task.request, key), but propagation headers | ||
| live in task.request.headers — so it never finds them and its `run/...` | ||
| span is always an unparented root. We extract from `.headers` ourselves | ||
| (see _extract_parent_context) and attach that as current before running | ||
| the task body, so spans created inside `fn` correctly nest under the | ||
| enqueueing request's trace instead of starting a disconnected one. | ||
| """ | ||
| current_ctx = trace.get_current_span().get_span_context() | ||
| if current_ctx and current_ctx.is_valid: | ||
| parent_ctx = _extract_parent_context(task_instance) | ||
| parent_span_ctx = trace.get_current_span(parent_ctx).get_span_context() | ||
| if not (parent_span_ctx and parent_span_ctx.is_valid): | ||
|
Comment on lines
+76
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift 🔎 Supported by static analysis🤖 get_repo_knowledge executed:
Length of output: 6658 🏁 Script executed: #!/bin/bash
set -e
printf '%s\n' '--- changed file ---'
sed -n '1,180p' backend/app/celery/tasks/job_execution.py
printf '%s\n' '--- related symbols and imports ---'
rg -n -C 3 '_extract_parent_context|parent_ctx|otel_context|trace.get_current_span|Celery|task_instance|def .*job|def .*task' backend/app backend/tests tests 2>/dev/null | head -n 300Repository: ProjectTech4DevAI/kaapi-backend Length of output: 26788 🏁 Script executed: #!/bin/bash
set -e
sed -n '1,180p' backend/app/celery/tasks/job_execution.pyRepository: ProjectTech4DevAI/kaapi-backend Length of output: 6269 🏁 Script executed: #!/bin/bash
set -e
printf '%s\n' '--- Celery/OpenTelemetry configuration ---'
rg -n -C 5 'instrumentation.celery|CeleryInstrumentor|instrument.*celery|opentelemetry.*celery|CeleryIntegration|celery_app|gevent_timeout' backend pyproject.toml poetry.lock requirements*.txt 2>/dev/null | head -n 400
printf '%s\n' '--- task wrapper implementation ---'
rg -n -C 8 'def gevent_timeout|gevent_timeout\(' backend/app/celery backend/app | head -n 300
printf '%s\n' '--- dependency declarations ---'
rg -n -C 2 'opentelemetry|celery' pyproject.toml poetry.lock requirements*.txt 2>/dev/null | head -n 250Repository: ProjectTech4DevAI/kaapi-backend Length of output: 48573 🌐 Web query:
💡 Result: The OpenTelemetry Python CeleryInstrumentor manages the context of a task execution primarily through the Celery Citations:
🏁 Script executed: #!/bin/bash
set -e
printf '%s\n' '--- celery_app observability setup ---'
sed -n '1,190p' backend/app/celery/celery_app.py
printf '%s\n' '--- application telemetry setup ---'
sed -n '1,140p' backend/app/main.py
printf '%s\n' '--- worker and task utility context ---'
sed -n '1,120p' backend/app/celery/utils.py
sed -n '350,390p' backend/app/celery/utils.py
printf '%s\n' '--- all CeleryInstrumentor references ---'
rg -n 'CeleryInstrumentor|instrument\(\)|uninstrument|opentelemetry-instrument|instrumentation.celery' backend --glob '*.py' --glob '*.toml' --glob '*.yaml' --glob '*.yml'Repository: ProjectTech4DevAI/kaapi-backend Length of output: 15757 🏁 Script executed: #!/bin/bash
set -e
sed -n '160,235p' backend/app/core/telemetry.pyRepository: ProjectTech4DevAI/kaapi-backend Length of output: 2988 🌐 Web query:
💡 Result: In opentelemetry-instrumentation-celery version 0.62b0, the CeleryGetter class is responsible for retrieving OpenTelemetry context from the Celery request object during the _trace_prerun signal handler [1][2]. The _trace_prerun method is connected to the task_prerun signal and executes when a Celery worker starts a task [2][3]. Inside this handler, the instrumentation uses the extract function from the OpenTelemetry propagation API, passing the Celery task request object as the carrier and the celery_getter instance to retrieve distributed tracing context (such as W3C TraceContext) injected into the task headers by the producer [1][2][4]. The CeleryGetter.get method implementation retrieves values from the carrier (the task request) using getattr(carrier, key, None) [2][3]. A known technical issue (Issue Citations:
Preserve the active Celery task span.
🤖 Prompt for AI Agents |
||
| return fn() | ||
|
|
||
| parent_ctx = _extract_parent_context(task_instance) | ||
| token = otel_context.attach(parent_ctx) | ||
| try: | ||
| return fn() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| from opentelemetry.instrumentation.logging import LoggingInstrumentor | ||
| from opentelemetry.instrumentation.requests import RequestsInstrumentor | ||
| from opentelemetry.instrumentation.utils import _SUPPRESS_HTTP_INSTRUMENTATION_KEY | ||
| from opentelemetry.propagate import set_global_textmap | ||
| from opentelemetry.sdk.resources import SERVICE_NAME, Resource | ||
| from opentelemetry.sdk.trace import TracerProvider | ||
|
|
||
|
|
@@ -192,9 +193,14 @@ def setup_telemetry(service_name: str | None = None) -> None: | |
|
|
||
| # Bridge OTel spans into Sentry as Sentry transactions and spans, with full attribute and error capture. | ||
| if settings.SENTRY_DSN: | ||
| from sentry_sdk.integrations.opentelemetry import SentrySpanProcessor | ||
| from sentry_sdk.integrations.opentelemetry import ( | ||
| SentryPropagator, | ||
| SentrySpanProcessor, | ||
| ) | ||
|
|
||
| tracer_provider.add_span_processor(SentrySpanProcessor()) | ||
| # Downstream services extract sentry-trace, not W3C traceparent. | ||
| set_global_textmap(SentryPropagator()) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🤖 get_repo_knowledge executed:
Length of output: 6658 🏁 Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- telemetry.py ---'
sed -n '170,215p' backend/app/core/telemetry.py
printf '%s\n' '--- job_execution.py extraction ---'
rg -n -C 8 'extract|_extract_parent_context|propagat|traceparent|sentry-trace|baggage' backend/app/celery/tasks/job_execution.py
printf '%s\n' '--- instrumentation and propagation references ---'
rg -n -C 4 'set_global_textmap|SentryPropagator|HTTPX|Requests|Celery|traceparent|sentry-trace|baggage|propagat' backend
printf '%s\n' '--- dependency pin ---'
rg -n 'sentry-sdk|opentelemetry|instrument' pyproject.toml requirements*.txt backend 2>/dev/null || trueRepository: ProjectTech4DevAI/kaapi-backend Length of output: 50388 🌐 Web query:
💡 Result: In the Sentry Python SDK, the SentryPropagator is a component designed to bridge Sentry's native distributed tracing mechanisms with OpenTelemetry (OTel) [1][2]. It enables interoperability by handling the propagation of Sentry's specific tracing headers—sentry-trace and baggage—within an OpenTelemetry environment [1][2]. Key details regarding its functionality and use as of version 2.20.0: 1. Functionality: The SentryPropagator implements the OpenTelemetry TextMapPropagator interface [1][2]. - Extraction: It reads incoming sentry-trace and baggage headers from the carrier (e.g., HTTP headers) and populates the OpenTelemetry context [1]. This ensures that traces originating from other Sentry-instrumented services are correctly continued within the OTel-instrumented service [2]. - Injection: It takes the current span from the OpenTelemetry context and injects the corresponding sentry-trace and baggage headers into the outgoing request, allowing the trace to continue in downstream services [1]. 2. Usage context: - Sentry now recommends using the OTLPIntegration for most use cases, as it simplifies the ingestion of OpenTelemetry traces into Sentry by automatically configuring the exporter and required event linking [3][4]. - When using the OTLPIntegration, a SentryPropagator is often set up automatically to maintain compatibility with services using Sentry's native tracing [3][5]. - Note: The automatic setup of the SentryPropagator by the OTLP integration is deprecated and slated for removal in future major versions [3][5]. Users will be expected to configure propagation manually using the standard OpenTelemetry propagation API to avoid conflicts with other configured propagators [3][5]. 3. Relation to Traceparent: - While SentryPropagator handles sentry-trace and baggage, cross-service propagation is also supported via the standard W3C traceparent header [3][6]. Enabling propagateTraceparent in the Sentry SDK configuration allows the SDK to send the W3C traceparent header on outgoing requests, which ensures compatibility with OTel-only services [7][6]. For manual setup in an OpenTelemetry environment, you would typically add the SentryPropagator to your OpenTelemetry global text map propagator configuration [8][9]. top_results: [1][3][2][4][5] Citations:
Preserve W3C propagation at instrumented boundaries. If any HTTP or Celery boundary uses W3C-only propagation, 🤖 Prompt for AI Agents |
||
|
|
||
| trace.set_tracer_provider(tracer_provider) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: ProjectTech4DevAI/kaapi-backend
Length of output: 169
🏁 Script executed:
Repository: ProjectTech4DevAI/kaapi-backend
Length of output: 50387
🏁 Script executed:
Repository: ProjectTech4DevAI/kaapi-backend
Length of output: 38033
🏁 Script executed:
Repository: ProjectTech4DevAI/kaapi-backend
Length of output: 11046
Sensitive Data Exposure (CWE-525): Use of Web Browser Cache Containing Sensitive Information
Reachability: External · Exploitability: Moderate
Prevent cache reuse of tenant-specific proxy responses.
_upstream_responsereturns a newResponseorJSONResponse, and the application middleware does not add cache directives. AddCache-Control: no-storeto both branches. Do not rely onprivatefor tenant isolation.🤖 Prompt for AI Agents