Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Tawakalt committed Jul 28, 2023
1 parent 8019db4 commit 72421d7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
23 changes: 23 additions & 0 deletions tests/tracing/instrumentation/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest

from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter


@pytest.fixture(scope="session")
def tracer_provider() -> TracerProvider:
return TracerProvider()


@pytest.fixture(scope="session")
def span_exporter(tracer_provider: TracerProvider) -> InMemorySpanExporter:
exporter = InMemorySpanExporter() # type: ignore
tracer_provider.add_span_processor(SimpleSpanProcessor(exporter))
return exporter


@pytest.fixture(scope="function")
def previous_num_captured_spans(span_exporter: InMemorySpanExporter) -> int:
captured_spans = span_exporter.get_finished_spans() # type: ignore
return len(captured_spans)
50 changes: 50 additions & 0 deletions tests/tracing/instrumentation/test_tracing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import json

import rasa_sdk.endpoint as ep

from typing import Sequence
from opentelemetry.sdk.trace import ReadableSpan
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter


# noinspection PyTypeChecker
app = ep.create_app(None)


def test_server_webhook_custom_action_is_instrumented(
span_exporter: InMemorySpanExporter,
previous_num_captured_spans: int,
):
data = {
"next_action": "custom_action",
"version": "1.0.0",
"tracker": {
"sender_id": "1",
"conversation_id": "default",
"latest_message": {"message_id": "1"},
},
}
_, response = app.test_client.post("/webhook", data=json.dumps(data))
result = response.json

assert response.status == 200

captured_spans: Sequence[
ReadableSpan
] = span_exporter.get_finished_spans() # type: ignore

print("******** captured_spans ******", captured_spans)

num_captured_spans = len(captured_spans) - previous_num_captured_spans
assert num_captured_spans == 1

captured_span = captured_spans[-1]

assert captured_span.attributes == {
"http.method": "POST",
"http.route": "/webhook",
"next_action": result.get("next_action"),
"version": result.get("version"),
"sender_id": result.get("sender_id"),
"message_id": result.get("latest_message", {}).get("message_id"),
}

0 comments on commit 72421d7

Please sign in to comment.