Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ share/python-wheels/
*.egg
MANIFEST
version.txt
version.py
_version.py

# PyInstaller
# Usually these files are written by a python script from a template
Expand Down
8 changes: 6 additions & 2 deletions newrelic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4126,8 +4126,12 @@ def _process_module_builtin_defaults():
"newrelic.hooks.framework_azurefunctions",
"instrument_azure_functions_worker_dispatcher",
)
_process_module_definition("pyzeebe.client.client", "newrelic.hooks.external_pyzeebe", "instrument_pyzeebe_client_client")
_process_module_definition("pyzeebe.worker.job_executor", "newrelic.hooks.external_pyzeebe", "instrument_pyzeebe_worker_job_executor")
_process_module_definition(
"pyzeebe.client.client", "newrelic.hooks.external_pyzeebe", "instrument_pyzeebe_client_client"
)
_process_module_definition(
"pyzeebe.worker.job_executor", "newrelic.hooks.external_pyzeebe", "instrument_pyzeebe_worker_job_executor"
)


def _process_module_entry_points():
Expand Down
22 changes: 16 additions & 6 deletions newrelic/hooks/external_pyzeebe.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import logging

from newrelic.api.application import application_instance
from newrelic.api.web_transaction import WebTransaction
from newrelic.api.function_trace import FunctionTrace
from newrelic.api.transaction import current_transaction
from newrelic.api.web_transaction import WebTransaction
from newrelic.common.object_wrapper import wrap_function_wrapper

_logger = logging.getLogger(__name__)
Expand All @@ -28,22 +28,26 @@

# Adds client method params as txn or span attributes
def _add_client_input_attributes(method_name, trace, args, kwargs):
bpmn_id = extract_agent_attribute_from_methods(args, kwargs, method_name, ("run_process", "run_process_with_result"), "bpmn_process_id", 0)
bpmn_id = extract_agent_attribute_from_methods(
args, kwargs, method_name, ("run_process", "run_process_with_result"), "bpmn_process_id", 0
)
if bpmn_id:
trace._add_agent_attribute("zeebe.client.bpmnProcessId", bpmn_id)

msg_name = extract_agent_attribute_from_methods(args, kwargs, method_name, ("publish_message"), "name", 0)
if msg_name:
trace._add_agent_attribute("zeebe.client.messageName", msg_name)

correlation_key = extract_agent_attribute_from_methods(args, kwargs, method_name, ("publish_message"), "correlation_key", 1)
correlation_key = extract_agent_attribute_from_methods(
args, kwargs, method_name, ("publish_message"), "correlation_key", 1
)
if correlation_key:
trace._add_agent_attribute("zeebe.client.correlationKey", correlation_key)

message_id = extract_agent_attribute_from_methods(args, kwargs, method_name, ("publish_message"), "message_id", 4)
if message_id:
trace._add_agent_attribute("zeebe.client.messageId", message_id)

resource = extract_agent_attribute_from_methods(args, {}, method_name, ("deploy_resource"), None, 0)
if resource:
try:
Expand All @@ -61,7 +65,13 @@ def extract_agent_attribute_from_methods(args, kwargs, method_name, methods, par
value = args[index]
return value
except Exception:
_logger.warning("Exception occurred in PyZeebe instrumentation: failed to extract %s from %s. Report this issue to New Relic support.", param, method_name, exc_info=True)
_logger.warning(
"Exception occurred in PyZeebe instrumentation: failed to extract %s from %s. Report this issue to New Relic support.",
param,
method_name,
exc_info=True,
)


# Async wrapper that instruments router/worker annotations`
async def _nr_wrapper_execute_one_job(wrapped, instance, args, kwargs):
Expand Down
18 changes: 11 additions & 7 deletions tests/external_pyzeebe/_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@

# Dummy RPC stub coroutines
async def dummy_create_process_instance(
self, bpmn_process_id: str, variables: dict = None, version: int = -1, tenant_id: str = None # noqa: RUF013
self,
bpmn_process_id: str,
variables: dict = None, # noqa: RUF013
version: int = -1,
tenant_id: str = None, # noqa: RUF013
):
"""Simulate ZeebeAdapter.create_process_instance"""
return DummyCreateProcessInstanceResponse
Expand All @@ -40,17 +44,17 @@ async def dummy_create_process_instance(
async def dummy_create_process_instance_with_result(
self,
bpmn_process_id: str,
variables: dict = None, # noqa: RUF013
variables: dict = None, # noqa: RUF013
version: int = -1,
timeout: int = 0,
variables_to_fetch=None,
tenant_id: str = None, # noqa: RUF013
tenant_id: str = None, # noqa: RUF013
):
"""Simulate ZeebeAdapter.create_process_instance_with_result"""
return DummyCreateProcessInstanceWithResultResponse


async def dummy_deploy_resource(*resource_file_path: str, tenant_id: str = None): # noqa: RUF013
async def dummy_deploy_resource(*resource_file_path: str, tenant_id: str = None): # noqa: RUF013
"""Simulate ZeebeAdapter.deploy_resource"""
# Create dummy deployment metadata for each provided resource path
deployments = [
Expand All @@ -73,10 +77,10 @@ async def dummy_publish_message(
self,
name: str,
correlation_key: str,
variables: dict = None, # noqa: RUF013
variables: dict = None, # noqa: RUF013
time_to_live_in_milliseconds: int = 60000,
message_id: str = None, # noqa: RUF013
tenant_id: str = None, # noqa: RUF013
message_id: str = None, # noqa: RUF013
tenant_id: str = None, # noqa: RUF013
):
"""Simulate ZeebeAdapter.publish_message"""
# Return the dummy response (contains message key)
Expand Down
14 changes: 11 additions & 3 deletions tests/external_pyzeebe/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
)
from pyzeebe import ZeebeClient, create_insecure_channel
from pyzeebe.grpc_internals.zeebe_adapter import ZeebeAdapter
from testing_support.validators.validate_span_events import validate_span_events
from testing_support.validators.validate_custom_event import validate_custom_event_count
from testing_support.validators.validate_span_events import validate_span_events
from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics

from newrelic.api.background_task import background_task
Expand Down Expand Up @@ -114,7 +114,14 @@ async def _test():
@validate_transaction_metrics(
"test_zeebe_client:publish_message", rollup_metrics=[("ZeebeClient/publish_message", 1)], background_task=True
)
@validate_span_events(exact_agents={"zeebe.client.messageName": "test_message", "zeebe.client.correlationKey": "999999", "zeebe.client.messageId": "abc123"}, count=1)
@validate_span_events(
exact_agents={
"zeebe.client.messageName": "test_message",
"zeebe.client.correlationKey": "999999",
"zeebe.client.messageId": "abc123",
},
count=1,
)
def test_publish_message(monkeypatch, loop):
monkeypatch.setattr(ZeebeAdapter, "publish_message", dummy_publish_message)

Expand All @@ -125,6 +132,7 @@ async def _test():

loop.run_until_complete(_test())


@validate_custom_event_count(count=0)
def test_publish_message_outside_txn(monkeypatch, loop):
monkeypatch.setattr(ZeebeAdapter, "publish_message", dummy_publish_message)
Expand All @@ -133,4 +141,4 @@ async def _test():
result = await client.publish_message(name="test_message", correlation_key="999999", message_id="abc123")
assert result.key == 999999

loop.run_until_complete(_test())
loop.run_until_complete(_test())
Loading