The platform for continuously improving AI agents.
"""The collector's span projection fills the product Span row."""
import uuid
import pytest
from django.test import override_settings
from factories import make_project
from rest_framework.test import APIClient
from overbae.models import Span
pytestmark = [pytest.mark.django_db, pytest.mark.covers("otlp-ingest")]
TOKEN = "projection-test"
def _record(**overrides):
trace_id = overrides.pop("trace_id", uuid.uuid4().hex)
span_id = overrides.pop("span_id", uuid.uuid4().hex[:16])
record = {
"span_id": span_id,
"trace_id": trace_id,
"name": "assistant",
"start_time_ns": 1_000,
"end_time_ns": 9_000,
"attributes": {},
}
record.update(overrides)
return record
def _post(payload, token=TOKEN):
client = APIClient()
if token is not None:
client.credentials(HTTP_AUTHORIZATION=f"Bearer {token}")
return client.post("/api/internal/otel/spans", payload, format="json")
@override_settings(OTEL_PROJECTION_TOKEN=TOKEN)
def test_a_projected_span_lands_with_its_product_type():
project = make_project()
record = _record(
name="assistant",
attributes={"gen_ai.operation.name": "execute_tool"},
service_name="sample-trace",
)
response = _post({"project_id": str(project.id), "spans": [record]})
assert response.status_code == 200, response.content
assert response.json()["spans_ingested"] == 1
span = Span.objects.get(span_id=record["span_id"])
assert span.project_id == project.id
assert span.span_type == Span.SpanType.TOOL_CALL
assert span.operation == "execute_tool"
assert span.service_name == "sample-trace"
assert span.duration_ns == 8_000
@override_settings(OTEL_PROJECTION_TOKEN=TOKEN)
def test_a_reused_span_id_from_another_project_is_rejected():
owner, other = make_project(), make_project()
record = _record(name="owner.run", attributes={"k": "owner"})
assert _post({"project_id": str(owner.id), "spans": [record]}).status_code == 200
stolen = {**record, "name": "other.run", "attributes": {"k": "other"}}
response = _post({"project_id": str(other.id), "spans": [stolen]})
assert response.status_code == 200, response.content
assert response.json()["spans_rejected"] == 1
span = Span.objects.get(span_id=record["span_id"])
assert (span.project_id, span.name, span.attributes["k"]) == (owner.id, "owner.run", "owner")
@override_settings(OTEL_PROJECTION_TOKEN=TOKEN)
def test_projection_rejects_a_user_token_and_a_missing_project():
project = make_project()
missing = _post(
{"project_id": str(uuid.uuid4()), "spans": [_record()]},
)
assert missing.status_code == 404
denied = _post({"project_id": str(project.id), "spans": [_record()]}, token="nope")
assert denied.status_code == 403
def test_projection_is_closed_without_a_token():
project = make_project()
response = _post({"project_id": str(project.id), "spans": [_record()]}, token=None)
assert response.status_code == 403