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

Decorators #45

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

Decorators #45

wants to merge 2 commits into from

Conversation

SimonLdj
Copy link
Contributor

Which issues does it affect?

What it does

  • enables us to use decorators to monitor methods instead of boiler plait wrapping methods

@SimonLdj SimonLdj self-assigned this Jul 11, 2022
@SimonLdj SimonLdj added enhancement New feature or request small labels Jul 11, 2022
return function (_target: Object, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
const metricName = `${prefix}${prefix ? '.' : ''}${propertyKey}`;
descriptor.value = monitored(metricName, originalMethod, options);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
descriptor.value = monitored(metricName, originalMethod, options);
descriptor.value = (...args) => monitored(metricName, () => originalMethod(...args), options);

monitored calls the method, you need descriptor.value to be a method

},
"include": ["src/**/*", "types/*.d.ts"],
"include": ["src/**/*", "types/*.d.ts", "example/**/*"],
Copy link
Contributor

Choose a reason for hiding this comment

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

ensure that it does not cause examples to be part of the dist

/**
* @deprecated since version 2.0
*/
export const getStatsdClient: Monitor['getStatsdClient'] = (...args) => getGlobalInstance().getStatsdClient(...args);
export const increment: Monitor['increment'] = (...args) => getGlobalInstance().increment(...args);
export const gauge: Monitor['gauge'] = (...args) => getGlobalInstance().gauge(...args);
export const timing: Monitor['timing'] = (...args) => getGlobalInstance().timing(...args);

export const monitorMethod = <T>(prefix?: string, options: MonitoredOptions<T> = {}) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd replace monitorMethod with a function (instead of const) and provide an overload which does not take the prefix parameter.

I want to avoid @monitorMethod(undefined, {...}).

Or, just make prefix required. What use cases do we have that we don't add some prefix to your monitor call?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request small
Projects
None yet
Development

Successfully merging this pull request may close these issues.

support Decorators
3 participants