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

Make Container hooks more useful for dynamic registration #274

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

alassek
Copy link
Contributor

@alassek alassek commented Jun 15, 2024

This is a (long-overdue) followup PR to our conversation related to integrating APM into a Dry-System app.

Also relates to hanami/hanami#1360

Use Case: I want to be able to dynamically decorate keys as they are added into the container system in order to perform APM metrics without having to touch every file.

Use Case: I want to audit the usage of particular namespaces in the Container using Dry-Monitor decoration, such as producing an audit log of Authorization checks.

In these use-cases, it's important that I want to react to container registration without having knowledge of precisely what keys are being registered. The decorate feature works well when keys are known in advance, but otherwise you have to enumerate the keys in the container after registration.

Enumeration is a problem, because after_finalize is called after the container is frozen, which is too late to decorate key values.

This PR makes two changes:

  1. Establish an after_register hook that fires whenever a key is registered
  2. Move after_finalize to happen prior to freezing the container.

This allows us to either decorate keys dynamically as they are registered, and to enumerate the container keys before the container is frozen in case we want to make alterations.

Due to the complexity of Container value storage between memoization and callable, I opted not to expose the key value directly; the hook code may choose to call resolve if it needs that.

alassek added 2 commits June 15, 2024 16:44
Allows you to take action when a new key is registered, so that you can
decorate the key without knowing its name ahead of time.

See also hanami/hanami#1360
This hook is much less useful that it might be if you could still make
changes to the container when it ran.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant