-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1077 from RasaHQ/ATO-2103-instrument-FormValidati…
…onAction._extract_validation_events [ATO-2103] Instrument FormValidationAction._extract_validation_events
- Loading branch information
Showing
8 changed files
with
328 additions
and
13 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 @@ | ||
Instrument `ValidationAction._extract_validation_events` and `FormValidationAction._extract_validation_events` and extract `validated_events` and `slots` attributes. |
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
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
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
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
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
128 changes: 128 additions & 0 deletions
128
tests/tracing/instrumentation/test_form_validation_action.py
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,128 @@ | ||
from typing import Sequence, Optional | ||
|
||
import pytest | ||
from opentelemetry.sdk.trace import ReadableSpan, TracerProvider | ||
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter | ||
|
||
from rasa_sdk.tracing.instrumentation import instrumentation | ||
from tests.tracing.instrumentation.conftest import MockFormValidationAction | ||
from rasa_sdk import Tracker | ||
from rasa_sdk.executor import CollectingDispatcher | ||
from rasa_sdk.events import ActionExecuted, SlotSet | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"events, expected_slots_to_validate", | ||
[ | ||
([], "[]"), | ||
([ActionExecuted("my_form")], "[]"), | ||
( | ||
[SlotSet("name", "Tom"), SlotSet("address", "Berlin")], | ||
'["name", "address"]', | ||
), | ||
], | ||
) | ||
@pytest.mark.asyncio | ||
async def test_form_validation_action_run( | ||
tracer_provider: TracerProvider, | ||
span_exporter: InMemorySpanExporter, | ||
previous_num_captured_spans: int, | ||
events: Optional[str], | ||
expected_slots_to_validate: Optional[str], | ||
) -> None: | ||
component_class = MockFormValidationAction | ||
|
||
instrumentation.instrument( | ||
tracer_provider, | ||
validation_action_class=component_class, | ||
) | ||
|
||
mock_validation_action = component_class() | ||
dispatcher = CollectingDispatcher() | ||
tracker = Tracker.from_dict({"sender_id": "test", "events": events}) | ||
|
||
await mock_validation_action.run(dispatcher, tracker, {}) | ||
|
||
captured_spans: Sequence[ | ||
ReadableSpan | ||
] = span_exporter.get_finished_spans() # type: ignore | ||
|
||
num_captured_spans = len(captured_spans) - previous_num_captured_spans | ||
# includes the child span for `_extract_validation_events` method call | ||
assert num_captured_spans == 2 | ||
|
||
captured_span = captured_spans[-1] | ||
|
||
assert captured_span.name == "FormValidationAction.MockFormValidationAction.run" | ||
|
||
expected_attributes = { | ||
"class_name": component_class.__name__, | ||
"sender_id": "test", | ||
"slots_to_validate": expected_slots_to_validate, | ||
"action_name": "mock_form_validation_action", | ||
} | ||
|
||
assert captured_span.attributes == expected_attributes | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"events, slots, validation_events", | ||
[ | ||
([], "[]", "[]"), | ||
([ActionExecuted("my_form")], "[]", '["action"]'), | ||
( | ||
[SlotSet("name", "Tom")], | ||
'["name"]', | ||
'["slot"]', | ||
), | ||
( | ||
[SlotSet("name", "Tom"), SlotSet("address", "Berlin")], | ||
'["name", "address"]', | ||
'["slot"]', | ||
), | ||
], | ||
) | ||
@pytest.mark.asyncio | ||
async def test_form_validation_action_extract_validation_events( | ||
tracer_provider: TracerProvider, | ||
span_exporter: InMemorySpanExporter, | ||
previous_num_captured_spans: int, | ||
events: Optional[str], | ||
slots: Optional[str], | ||
validation_events: Optional[str], | ||
) -> None: | ||
component_class = MockFormValidationAction | ||
|
||
instrumentation.instrument( | ||
tracer_provider, | ||
form_validation_action_class=component_class, | ||
) | ||
|
||
mock_form_validation_action = component_class() | ||
dispatcher = CollectingDispatcher() | ||
tracker = Tracker.from_dict({"sender_id": "test", "events": events}) | ||
|
||
await mock_form_validation_action._extract_validation_events( | ||
dispatcher, tracker, {} | ||
) | ||
|
||
captured_spans: Sequence[ | ||
ReadableSpan | ||
] = span_exporter.get_finished_spans() # type: ignore | ||
|
||
num_captured_spans = len(captured_spans) - previous_num_captured_spans | ||
assert num_captured_spans == 1 | ||
|
||
captured_span = captured_spans[-1] | ||
expected_span_name = ( | ||
"FormValidationAction.MockFormValidationAction._extract_validation_events" | ||
) | ||
|
||
assert captured_span.name == expected_span_name | ||
|
||
expected_attributes = { | ||
"validation_events": validation_events, | ||
"slots": slots, | ||
} | ||
|
||
assert captured_span.attributes == expected_attributes |
Oops, something went wrong.