Skip to content
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

RD-14162-Programatic-errors-creation-with-new-create_programatic_error-function #967

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,19 @@ Programmatic Errors indicating that a non-fatal error occurred, such as an appli

#### Creating a Programmatic Error

Programmatic errors are created by adding [span events](https://opentelemetry.io/docs/instrumentation/python/manual/#adding-events) with a custom attribute being set with the key name `lumigo.type`.
Programmatic errors can now be created easily using the `create_programmatic_error` function provided by the `lumigo_opentelemetry` package. This allows you to capture and report errors with minimal setup, without requiring direct interaction with the OpenTelemetry trace SDK.

For example, you can add a programmatic error as follows:

```python
from lumigo_opentelemetry import create_programmatic_error

create_programmatic_error("The customer 123 was not found", "CustomerNotExist")
```

The first argument, "Error message", is a descriptive message for the error, while the second argument, "ErrorType", represents the type of the error.

Alternately, programmatic errors can also be created by adding [span events](https://opentelemetry.io/docs/instrumentation/python/manual/#adding-events) with a custom attribute being set with the key name `lumigo.type`.

For example, you could add a programmatic error as follows:

Expand Down
7 changes: 7 additions & 0 deletions src/lumigo_opentelemetry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@ def wrapper(*args: List[Any], **kwargs: Dict[Any, Any]) -> T:
return wrapper


def create_programmatic_error(error_message: str, error_type: str) -> None:
from opentelemetry.trace import get_current_span

get_current_span().add_event(error_message, {"lumigo.type": error_type})


# Load the package on import
init_data = init()

Expand All @@ -309,4 +315,5 @@ def wrapper(*args: List[Any], **kwargs: Dict[Any, Any]) -> T:
"logger",
"tracer_provider",
"logger_provider",
"create_programmatic_error",
]
32 changes: 32 additions & 0 deletions src/test/unit/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,38 @@ def test_python_version_too_new(self):
)


class TestCreateProgrammaticError(unittest.TestCase):
@httpretty.activate(allow_net_connect=False)
def test_create_event_with_correct_attributes(self):
from lumigo_opentelemetry import create_programmatic_error, tracer_provider

self.assertIsNotNone(create_programmatic_error)

span_processor = Mock(SpanProcessor)
tracer_provider.add_span_processor(span_processor)

tracer = tracer_provider.get_tracer(__name__)
with tracer.start_as_current_span("Root") as span:
create_programmatic_error("Error message", "ErrorType")

# Verify `on_start` was called
span_processor.on_start.assert_called()

# Capture the span passed to `on_start`
span = span_processor.on_start.call_args[0][
0
] # Extract the `span` argument from the first call

events = span.events
# Verify the event was added
self.assertEqual(len(events), 1)

event = events[0]
# Verify the event has the correct attributes
self.assertEqual(event.name, "Error message")
self.assertEqual(event.attributes["lumigo.type"], "ErrorType")


class TestLumigoWrapped(unittest.TestCase):
@httpretty.activate(allow_net_connect=False)
def test_access_lumigo_wrapped(self):
Expand Down
Loading