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

Custom Event Hooks Event Emission #164

Merged
merged 9 commits into from
Jan 23, 2025
Merged

Conversation

ajaycj
Copy link
Contributor

@ajaycj ajaycj commented Jan 14, 2025

  • Update CHANGELOG.md
  • Update README.md (as needed)
  • New dependencies were added to pyproject.toml
  • pip install succeeds with a clean virtualenv
  • There are new or modified tests that cover changes
  • Test coverage is maintained or expanded

This update introduces the ability to use custom hooks for emitting events in Django projects, providing a flexible mechanism for developers to handle application events outside of New Relic. By configuring a custom hook, you can process events in a way that fits your application's needs, whether it's logging, sending notifications, or integrating with other third-party services.

Signed-off-by: Ajay Chinthalapalli Jayakumar <[email protected]>
Copy link

Thanks for the contribution! It looks like @achinthalapalli is an internal user so signing the CLA is not required. However, we need to confirm this.

Signed-off-by: Ajay Chinthalapalli Jayakumar <[email protected]>
@ajaycj ajaycj changed the title custom hook impl Custom Event Hooks Implementation for Django Jan 15, 2025
@ajaycj ajaycj changed the title Custom Event Hooks Implementation for Django Custom Event Hooks for Non-NewRelic Event Emission Jan 15, 2025
Signed-off-by: Ajay Chinthalapalli Jayakumar <[email protected]>
@ajaycj ajaycj marked this pull request as ready for review January 15, 2025 21:28
Signed-off-by: Ajay Chinthalapalli Jayakumar <[email protected]>
Signed-off-by: Ajay Chinthalapalli Jayakumar <[email protected]>
Signed-off-by: Ajay Chinthalapalli Jayakumar <[email protected]>
Signed-off-by: Ajay Chinthalapalli Jayakumar <[email protected]>
)

try:
module = __import__(module_name, fromlist=[func_name])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

importlib is recommended rather than using __import__ directly. there are a couple other example usages in the library if you search for importlib.

try:
module = __import__(module_name, fromlist=[func_name])
return getattr(module, func_name)
except ImportError as e:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason to not just let these bubble up as is?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Addressed in b5866c6

TASK_RETRY = "task_runner:retry"


def _import_hook(hook_path):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this also check that the path is a callable? an example of that can be found here.


def emit_events(event_type, payload):
"""
Emit a metric event using the configured hook and New Relic if configured.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This docstring should be updated and have newrelic removed.

metric_type (str): Type of the metric (from MetricType enum).
payload (dict): The data associated with the metric.
"""
hook = getattr(settings, "DDA_EVENT_HOOK", None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't this and the next few lines be evaluated at import-time, or cached internally once executed the first time? are you intentionally importing on every execution?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in b5866c6

except Exception as e:
logger.error(f"Error in custom hook for events: {e}", exc_info=True)

if newrelic_agent:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newrelic should probably just be pulled from dda entirely. it was only used here and it can be added to our project repo instead of being here.

},
)
emit_events(
EventType.QUEUE_LENGTH.value,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a little odd. the current value of the enum is the value expected by newrelic schema. if we're removing that from the library, i'd expect emit_events to take an enum member rather than the concrete value. that way, callers can use the enum members directly for internal mapping rather than value. not merge blocking though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, the event name should probably be something like QUEUE_SNAPSHOT or something along those lines. length is one of the attributes and doesn't really represent the payload.

@ajaycj ajaycj changed the title Custom Event Hooks for Non-NewRelic Event Emission Custom Event Hooks Event Emission Jan 21, 2025
Signed-off-by: Ajay Chinthalapalli Jayakumar <[email protected]>
@ajaycj ajaycj requested a review from demianbrecht January 21, 2025 19:39
Signed-off-by: Ajay Chinthalapalli Jayakumar <[email protected]>
@ajaycj ajaycj closed this Jan 22, 2025
@ajaycj ajaycj reopened this Jan 22, 2025
@demianbrecht demianbrecht merged commit 737d508 into salesforce:main Jan 23, 2025
12 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants