feat(analytics): support explicit count values in Count method#3619
feat(analytics): support explicit count values in Count method#3619wolf-06 wants to merge 2 commits intohatchet-dev:mainfrom
Conversation
Signed-off-by: wolf-06 <Uajjawal.06@gmail.com>
Signed-off-by: wolf-06 <Uajjawal.06@gmail.com>
|
@wolf-06 is attempting to deploy a commit to the Hatchet Team on Vercel. A member of the Team first needs to authorize it. |
gregfurman
left a comment
There was a problem hiding this comment.
Thanks for picking this up! I think the current approach needs some more consideration since (IMO) it's prone to incorrect usage and is not very self-documenting.
Wdyt of the approach outlined in #3382 that extends the analytics.Properties and uses a more explicit (and structured) typing system to determine values?
| // Extract count from last properties arg if multiple are provided. | ||
| // Allows passing aggregated counts instead of always using 1. |
There was a problem hiding this comment.
Tbh I still think we should be passing in a structured type to determine/override this value, where necessary instead of relying on a positional arg in the varadic params.
Like, it's more explicit that way i.e
a.Count(ctx, analytics.Log, analytics.Get, analytics.Properties{}, analytics.Increment(5))versus:
a.Count(ctx, analytics.Log, analytics.Get, analytics.Properties{}, analytics.Properties{"": 5})where the latter could be error prone as well since it means we'd need to document this positional behaviour.
This typed-approach also means we can extend supported operations i.e
a.Count(ctx, analytics.Log, analytics.Get, analytics.Properties{}, analytics.Decrement(5))
a.Count(ctx, analytics.Log, analytics.Get, analytics.Properties{}, analytics.Set(0)) // or maybe .Reset() ?Then if we have other metric types (i.e a gauge or whatever) we can extend this approach to suit.
There was a problem hiding this comment.
Fair enough,
will move with original approach of using structured property, which ig will also solve to other concerns automatically.
| // Allows passing aggregated counts instead of always using 1. | ||
| var n int64 | ||
| if len(props) > 1 && props[len(props)-1] != nil { | ||
| for _, v := range props[len(props)-1] { |
There was a problem hiding this comment.
Unsure this is desirable due to the typing of analytics.Properties.
Like, analytics.Properties is a map[string]any which means (in the case of your changes) the last arg would still have to be correctly typed as such. So if I wanted to increment the count by 5 I'd need to pass a contrived map type where the keys will be ignored i.e
a.Count(ctx, analytics.Log, analytics.Get, analytics.Properties{}, analytics.Properties{"foo": 5}))Also, if we pass in two (or more) properties, the caller has no way of knowing the final arg will have all of its values used for incrementing. This ambigious behaviour could lead to unintentionally passing props in that we expect to be included as tags/labels but get used to increment metrics.
| var n int64 | ||
| if len(props) > 1 && props[len(props)-1] != nil { | ||
| for _, v := range props[len(props)-1] { | ||
| if value, ok := v.(int64); ok { |
There was a problem hiding this comment.
This conversion means we're only ever supporting an int64 being explicitly passed in, meaning other numeric types being used would get skipped over.
i.e
a.Count(ctx, analytics.Log, analytics.Get, analytics.Properties{}, analytics.Properties{"foo": uint64(5)}))Would see 1 being added since a uint64 is not an int64 -- hence it's skipped as type-checking fails.
Description
Adds support for explicit count values in
analytics.Count()by extracting int64 values from the lastPropertiesargument when multiple are provided.What's Changed
Type of change
FIXES #3382