-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import json | ||
|
||
import rasa_sdk.endpoint as ep | ||
|
||
from typing import Sequence | ||
# from opentelemetry import trace | ||
from opentelemetry.sdk.trace import TracerProvider | ||
|
||
from opentelemetry.sdk.trace import ReadableSpan | ||
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter | ||
# from opentelemetry.sdk.trace.export import SimpleSpanProcessor, ConsoleSpanExporter | ||
|
||
# trace.set_tracer_provider(TracerProvider()) | ||
# tracer_provider = trace.get_tracer_provider().add_span_processor( | ||
# SimpleSpanProcessor(ConsoleSpanExporter()) | ||
# ) | ||
# noinspection PyTypeChecker | ||
|
||
def test_server_webhook_custom_action_is_instrumented( | ||
tracer_provider: TracerProvider, | ||
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"}, | ||
}, | ||
} | ||
app = ep.create_app(None, tracer_provider=tracer_provider) | ||
_, 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"), | ||
} |