Skip to content

Named rule support; v1.4.0#7

Merged
mikeybtn merged 4 commits intomasterfrom
mikey/named-rules
May 15, 2019
Merged

Named rule support; v1.4.0#7
mikeybtn merged 4 commits intomasterfrom
mikey/named-rules

Conversation

@mikeybtn
Copy link
Contributor

@mikeybtn mikeybtn commented May 8, 2019

No description provided.

@mikeybtn mikeybtn requested review from danielmcgrath and i May 8, 2019 21:17
@mikeybtn mikeybtn marked this pull request as ready for review May 8, 2019 21:17
@mikeybtn
Copy link
Contributor Author

mikeybtn commented May 8, 2019

Pushed 'ready for review' too soon, looks like something got lost on the way to external - stand by..

@mikeybtn mikeybtn force-pushed the mikey/named-rules branch from adedc56 to 3a81e07 Compare May 8, 2019 21:22
@mikeybtn
Copy link
Contributor Author

mikeybtn commented May 8, 2019

..ready to go!

src/server.js Outdated
const result = status.isAllowed ? 'accepted' : 'rejected';
const matchType = Server.getMatchType(rule);
this.instrumenter.countHit(result, matchType);
const ruleName = rule && rule.name ? rule.name : '';
Copy link
Contributor

Choose a reason for hiding this comment

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

s/name/label

this.statsd.increment(`hit.${status}`);
this.statsd.increment(`hit.${status}.${type}`);
this.hitCounter.labels(status, type).inc();
this.hitCounter.labels(status, type, name || '').inc();
Copy link
Contributor

Choose a reason for hiding this comment

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

You're handling name || '' both here and at the call-site. Doesn't hurt anything, though.

Separately, rename the name param here to label?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Separately, rename the name param here to label?

good catch, fixed!

README.md Outdated

The following optional fields are also supported:

* `label`: A short, slug-like name for the rule. If set, Prometheus metrics for hits matching the rule will be labeled with `rule_label` as this value.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be helpful to call out explicitly that this value is ignored for statsd metrics. We can consider adding support for additional statsd counters on a per-label basis down the road?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

creditLimit: 1,
resetSeconds: 60,
actorField: '',
label: null,
Copy link
Contributor

Choose a reason for hiding this comment

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

most configs won't explicitly set label to null.
maybe specify one as undefined and another with no key for label for good measure?

* @param {string} comment Optional diagnostic name for this rule.
*/
addRule(operation, creditLimit, resetSeconds, actorField, comment) {
addRule(operation, creditLimit, resetSeconds, actorField, label, comment) {
Copy link
Contributor

@i i May 9, 2019

Choose a reason for hiding this comment

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

since label is an optional argument, why position it second to last?
there's a bunch of places in tests/config-test.js that will need to be updated to reflect this positional change.

alternatively, since the cardinality of this function is getting big, what are your thoughts on having it accept an arguments object instead of positional arguments?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

since label is an optional argument, why position it second to last?

Since both label and comment are optional, am guessing I somewhat arbitrarily decided labels will be more common than comments.

alternatively, since the cardinality of this function is getting big, what are your thoughts on having it accept an arguments object instead of positional arguments?

I like it, that'd definitely make it a bunch more readable! May sheepishly defer this suggestion as am running into limited cycles to iterate further. OK with you to land as is?

Adds test coverage for `countHit` with a label in `server-test`.
@mikeybtn mikeybtn force-pushed the mikey/named-rules branch from 5a32a28 to 29b93f8 Compare May 14, 2019 14:31
Copy link
Contributor Author

@mikeybtn mikeybtn left a comment

Choose a reason for hiding this comment

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

PTAL!

README.md Outdated

The following optional fields are also supported:

* `label`: A short, slug-like name for the rule. If set, Prometheus metrics for hits matching the rule will be labeled with `rule_label` as this value.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

* @param {string} comment Optional diagnostic name for this rule.
*/
addRule(operation, creditLimit, resetSeconds, actorField, comment) {
addRule(operation, creditLimit, resetSeconds, actorField, label, comment) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

since label is an optional argument, why position it second to last?

Since both label and comment are optional, am guessing I somewhat arbitrarily decided labels will be more common than comments.

alternatively, since the cardinality of this function is getting big, what are your thoughts on having it accept an arguments object instead of positional arguments?

I like it, that'd definitely make it a bunch more readable! May sheepishly defer this suggestion as am running into limited cycles to iterate further. OK with you to land as is?

this.statsd.increment(`hit.${status}`);
this.statsd.increment(`hit.${status}.${type}`);
this.hitCounter.labels(status, type).inc();
this.hitCounter.labels(status, type, name || '').inc();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Separately, rename the name param here to label?

good catch, fixed!

src/server.js Outdated
const result = status.isAllowed ? 'accepted' : 'rejected';
const matchType = Server.getMatchType(rule);
this.instrumenter.countHit(result, matchType);
const ruleLabel = rule && rule.label ? rule.label : '';
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: what's the order of operations here? can you clarify with parens?

const ruleLabel = (rule && rule.label) ? rule.label : '';
const ruleLabel = rule && (rule.label ? rule.label : '');

assuming it's the first since, but not clear from reading

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can do. Ternary is pretty much lowest precedence operator in JS so its not uncommon to see 'naked' expressions like this ahead of it. (In fact your suggestion would be rejected by rules/no-extra-parens but we don't enable it.)

Copy link
Contributor

Choose a reason for hiding this comment

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

d'oh! no need to include for my sake if it's an antipattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i think its fine, happy to follow the "if someone asked, its probably worth doing/clarifying" school of thought here :)

@mikeybtn mikeybtn requested a review from i May 14, 2019 18:50
Copy link
Contributor

@i i left a comment

Choose a reason for hiding this comment

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

LGTM. Defer to your heart with regards to leaving those parens in or out.

@mikeybtn mikeybtn merged commit 201ed5e into master May 15, 2019
@mikeybtn mikeybtn deleted the mikey/named-rules branch May 15, 2019 13:19
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.

3 participants