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

Analytics Library #53

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

Analytics Library #53

wants to merge 2 commits into from

Conversation

vcai122
Copy link

@vcai122 vcai122 commented Jan 17, 2025

No description provided.

analytics_recorder, # should be self but consitency
*entries: FuncEntry | UnaryFuncEntry,
get_username: Optional[Callable[[FunctionParam, FunctionReturn], Any]] = None,
) -> Callable[[Callable[..., Any]], Callable[..., Any]]:

Choose a reason for hiding this comment

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

Why is ... used along with Any? Isn't ... equivalent to Any?

Copy link
Author

Choose a reason for hiding this comment

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

the first is the arguments so ... just means any number of arguments, and Any means any return type

def submit_transaction(self, txn: AnalyticsTxn):
# TODO: We should make this fail safely, as to not interrupt the everyday
# business logic products do.
# However, given that we do not have appropriate logging infrastructure
Copy link

Choose a reason for hiding this comment

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

What's the appropriate infra we'd need to change here? Agree on not failing silently rn

Copy link
Author

Choose a reason for hiding this comment

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

We should be surrounding this with a try catch and logging on failure so we can "silently" fail but be notified its failing.

Think about what we would want if the analytics server went down for example; we wouldn't want all penn labs products routes to start failing because this function fails

# Offer a 30 second buffer to refresh
if time.time() >= self.expires_at - 30:
# TODO: According to @judtinzhang there is a subtle bug here with regards
Copy link

Choose a reason for hiding this comment

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

Should probably comment what the bug is so we can be aware of it (if you know what it is)

Copy link
Author

Choose a reason for hiding this comment

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

No clue. But it's the reason why we are doing the 30 second check. I will make the comment more clear tho

_refresh_if_outdated()
self._refresh_expires_at()
self._refresh_headers()

self.executor.submit(self.send_message, txn.to_json())

def send_message(self, json):
def _send_message(self, json):
Copy link

Choose a reason for hiding this comment

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

Why the name change?

Copy link
Author

Choose a reason for hiding this comment

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

_ is convention for private for python

elif self.get_value is not None:
ret = self.get_value(func_param, func_return)
elif self.get_value_use_before is not None:
ret = self.get_value_use_before(func_param, func_return, before_context)
Copy link

Choose a reason for hiding this comment

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

Shouldn't we always enforce that the before context is already computed? For safety

Copy link
Author

Choose a reason for hiding this comment

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

wdym? it is up to the caller to provide the before_context for this function

if you look at where it is called, you are right that before_context may not be computed in the case that compute_before is not provided, but it just defaults to None in that case. That doesn't seem too bad because if someone is using the before_context by passing get_value_use_before, they should be passing a compute_before anyway

try:
return self.filter_do_record(func_param, func_return)
except Exception:
return False
Copy link

Choose a reason for hiding this comment

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

Just to be clear is False record? Seems to be implied by the above documentation

Copy link
Author

Choose a reason for hiding this comment

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

You are right False should be do not record as implied by the function being called filter (since for filters True means keep and False means throw away). Good shout!

if self._compute_filter_do_record(func_param, func_return):
return {
"key": key_template.substitute(
name=f".{self.name}" if self.name else ""
Copy link

Choose a reason for hiding this comment

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

Should we fail more loudly here? Empty key is sus

Copy link
Author

Choose a reason for hiding this comment

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

It's not a failure mode. Users are allowed to not provide a name. Name is just for further customization on the analytics key on the user end, but the library auto generates keys based off of the function name/ route name anyway

Copy link
Contributor

@joyliu-q joyliu-q left a comment

Choose a reason for hiding this comment

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

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants