Skip to content
Draft
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
16 changes: 15 additions & 1 deletion example/run.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dotenv/config';

import {setGlobalInstance, StatsdPlugin, monitored, Monitor} from '../src';
import {setGlobalInstance, StatsdPlugin, monitored, Monitor, monitorMethod} from '../src';

setGlobalInstance(
new Monitor({
Expand Down Expand Up @@ -97,3 +97,17 @@ monitored(
},
{logErrorAsInfo: true}
).catch(() => {});

// monitor method using decorator
class Person {
firstName: string = 'Jon';
lastName: string = 'Doe';

@monitorMethod('person', {logErrorAsInfo: true})
getFullName() {
return `${this.firstName} ${this.lastName}`;
}
}
const person = new Person();
const name = person.getFullName();
console.log(`person's name is ${name}`);
10 changes: 10 additions & 0 deletions src/globalInstance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Monitor from './Monitor';
import {MonitoredOptions} from './types';

let instance: Monitor | undefined;

Expand All @@ -15,10 +16,19 @@ export function getGlobalInstance(): Monitor {
}

export const monitored: Monitor['monitored'] = (...args) => getGlobalInstance().monitored(...args);

/**
* @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?

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

};
};
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
"lib": ["dom", "es2017"],
"skipLibCheck": true,
"noImplicitAny": false,
"useUnknownInCatchVariables": false
"useUnknownInCatchVariables": false,
"experimentalDecorators": true
},
"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

"exclude": ["__tests__", "dist"]
}