Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

[core] Add Tags API #295

Merged
merged 4 commits into from
Jan 18, 2019
Merged

Conversation

mayurkale22
Copy link
Member

// A map mapping TagKey to to its respective TagValue.
private readonly registeredTags: Map<TagKey, TagValue> = new Map();

constructor() {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this empty constructor be removed?

/**
* Inserts a key and value in the map if the map does not already contain the
* key.
* @param tagKey The tag key.
Copy link
Contributor

Choose a reason for hiding this comment

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

These @param comments seem to be fully redundant with the parameter names. Can they be removed?

This comment applies to insert, delete and update below too.

* @param tagKey The tag key.
*/
delete(tagKey: TagKey): void {
if (this.registeredTags.has(tagKey)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you need call to has? Map.delete just returns false if the key was not deleted but otherwise is a no-op.

if (!isValidTagValue(tagValue)) {
throw Error(`Invalid TagValue: ${tagValue.value}`);
}
this.registeredTags.set(tagKey, tagValue);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you need to check for a valid tag value here?

* @param tagKey A tag key to be updated.
* @param tagValue The value to update the key to in the map.
*/
update(tagKey: TagKey, tagValue: TagValue): void {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why have separate insert and update functions and not just a set function that does insert and update?

That would enable combining both functions and you also would not need the if (this.registeredTags.has(tagKey)) { check.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 to this. In Java we use this approach in TagContextBuilder.put().

Copy link
Member Author

Choose a reason for hiding this comment

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

Done, thanks for the reference.

const invalidString = /[^\u0020-\u007e]/;

// Max Length of a TagKey
const MAX_LENGTH = 255;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: could this be TAG_KEY_MAX_LENGTH and then remove the comment?

* @return whether the name is valid.
*/
export function isValidTagKey(tagKey: TagKey): boolean {
if (tagKey === null || typeof tagKey === 'undefined') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this be compacted to if (!tagKey) return false;? Given that tagKey needs to be an object with a valid name, all falsely values should be invalid.

* @param tagValue the name to be validated.
* @return whether the name is valid.
*/

Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: should this extra line be removed?

*/

export function isValidTagValue(tagValue: TagValue): boolean {
if (tagValue === null || typeof tagValue === 'undefined') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar question here on whether it makes sense to use falsiness as a check here.

assert.equal(tags.size, 1);
});
});
describe('update()', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit, should this be delete()?

1. removed redundant param comments.
2. removed empty constructor.
3. removed unnecessary map.has check.
4. renamed invalidString -> nonPrintableCharsRegex, MAX_LENGTH -> TAG_KEY_MAX_LENGTH
* @param tagKey A tag key to be updated.
* @param tagValue The value to update the key to in the map.
*/
update(tagKey: TagKey, tagValue: TagValue): void {
Copy link
Contributor

Choose a reason for hiding this comment

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

+1 to this. In Java we use this approach in TagContextBuilder.put().

return false;
}
return isPrintableString(tagValue.value) &&
tagValue.value.length <= TAG_KEY_MAX_LENGTH;
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

I have compared with Java and Python, I see we have enforced length constaint on TagValue. Let me know if you still want me to remove it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. We can leave the constraint for now, it's easy to relax it in the future.

@mayurkale22
Copy link
Member Author

@vigneshtdev #268 (comment) FYI, we have decided to add separate package for Tags API.

@mayurkale22
Copy link
Member Author

@draffensperger thanks for the reviews, I have fixed all the mentioned comments.

@mayurkale22 mayurkale22 merged commit 7a37197 into census-instrumentation:master Jan 18, 2019
@mayurkale22 mayurkale22 deleted the tags branch January 18, 2019 20:57
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants