@@ -16,6 +16,8 @@ struct UpscaleGroupInfo
1616 TGroup Group;
1717 uint64_t RealCount;
1818 uint64_t SampledCount;
19+ uint64_t RealValue;
20+ uint64_t SampledValue;
1921};
2022
2123// Template class that support "sampling by group".
@@ -34,24 +36,27 @@ class GroupSampler : public GenericSampler
3436public:
3537 struct GroupInfo
3638 {
37- uint64_t Real;
38- uint64_t Sampled;
39+ uint64_t RealCount;
40+ uint64_t SampledCount;
41+ uint64_t RealValue;
42+ uint64_t SampledValue;
3943 };
4044
4145public:
42- bool Sample (TGroup group)
46+ bool Sample (TGroup group, uint64_t value = 0 )
4347 {
4448 std::unique_lock lock (_groupsMutex);
4549
46- // increment the real count for the given group
50+ // increment the real count and value for the given group
4751 GroupInfo* pInfo;
48- AddInGroup (group, pInfo);
52+ AddInGroup (group, pInfo, value );
4953
5054 auto [it, inserted] = _knownGroups.insert (std::move (group));
5155 if (inserted && _keepAtLeastOne)
5256 {
53- // increment the sampled count for the given group
54- pInfo->Sampled ++;
57+ // increment the sampled count and value for the given group
58+ pInfo->SampledCount ++;
59+ pInfo->SampledValue += value;
5560
5661 // This is the first time we see this group in this time window,
5762 // so force the sampling decision
@@ -61,29 +66,32 @@ class GroupSampler : public GenericSampler
6166 auto sampled = _sampler.Sample ();
6267 if (sampled)
6368 {
64- // increment the sampled count for the given group
65- pInfo->Sampled ++;
69+ // increment the sampled count and value for the given group
70+ pInfo->SampledCount ++;
71+ pInfo->SampledValue += value;
6672 }
6773
6874 return sampled;
6975 }
7076
7177 // MUST be called under the lock
72- void AddInGroup (TGroup group, GroupInfo*& groupInfo)
78+ void AddInGroup (TGroup group, GroupInfo*& groupInfo, uint64_t value = 0 )
7379 {
7480 auto info = _groups.find (group);
7581 if (info != _groups.end ())
7682 {
7783 groupInfo = &(info->second );
78- info->second .Real ++;
79-
84+ info->second .RealCount ++;
85+ info-> second . RealValue += value;
8086 return ;
8187 }
8288
8389 // need to add the info of this new group
8490 GroupInfo gi;
85- gi.Real = 1 ;
86- gi.Sampled = 0 ;
91+ gi.RealCount = 1 ;
92+ gi.SampledCount = 0 ;
93+ gi.RealValue = value;
94+ gi.SampledValue = 0 ;
8795 auto slot = _groups.insert_or_assign (group, gi);
8896 groupInfo = &((*slot.first ).second );
8997 }
@@ -95,17 +103,19 @@ class GroupSampler : public GenericSampler
95103
96104 upscaleGroups.reserve (_groups.size ());
97105
98- for (auto & [name, counts ] : _groups)
106+ for (auto & [name, info ] : _groups)
99107 {
100- auto sampledCount = counts. Sampled ;
108+ auto sampledCount = info. SampledCount ;
101109 if (sampledCount > 0 )
102110 {
103- upscaleGroups.push_back (UpscaleGroupInfo<TGroup>{name, counts. Real , sampledCount});
111+ upscaleGroups.push_back (UpscaleGroupInfo<TGroup>{name, info. RealCount , sampledCount, info. RealValue , info. SampledValue });
104112 }
105113
106114 // reset groups count
107- counts.Real = 0 ;
108- counts.Sampled = 0 ;
115+ info.RealCount = 0 ;
116+ info.SampledCount = 0 ;
117+ info.RealValue = 0 ;
118+ info.SampledValue = 0 ;
109119 }
110120
111121 return upscaleGroups;
0 commit comments