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 1 commit
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
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,18 @@ 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 could add a programmatic error as follows:
For example, you can add a programmatic error as follows:

```python
from opentelemetry.trace import get_current_span
from lumigo_opentelemetry import create_programmatic_error

get_current_span().add_event('<error-message>', {'lumigo.type': '<error-type>'})
create_programmatic_error("Error message", "ErrorType")
eugene-lumigo marked this conversation as resolved.
Show resolved Hide resolved
```

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

## Python 3.7 Support

**Deprecation Notice:** As of version 1.0.156, support for Python 3.7 has been deprecated. The last version of the Lumigo OpenTelemetry Distro to support Python 3.7 is version 1.0.155.
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",
]
34 changes: 34 additions & 0 deletions src/test/unit/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from json import loads
from os import environ

from opentelemetry.sdk.trace import SpanProcessor

from lumigo_opentelemetry import init


Expand Down Expand Up @@ -147,3 +149,35 @@ def test_python_version_too_new(self):
"Unsupported Python version 3.13; only Python 3.8 to 3.12 are supported.",
cm.output[0],
)


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")
Loading