Skip to content

Commit

Permalink
fix: assertion for ClusterSizer (#1329)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?

- ClusterSize was not properly set for samplers that supports
`UseClusterSize`

## Short description of the changes

- implement `ClusterSizer` for rules sampler
- convert a `Sampler` to `ClusterSizer` by going through `any`
  • Loading branch information
VinozzZ authored Sep 13, 2024
1 parent f4ca704 commit aeea1d4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
13 changes: 13 additions & 0 deletions sample/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/tidwall/gjson"
)

var _ ClusterSizer = (*RulesBasedSampler)(nil)

type RulesBasedSampler struct {
Config *config.RulesBasedSamplerConfig
Logger logger.Logger
Expand Down Expand Up @@ -79,6 +81,17 @@ func (s *RulesBasedSampler) Start() error {
return nil
}

func (s *RulesBasedSampler) SetClusterSize(size int) {
for _, sampler := range s.samplers {
// Sampler does not implement ClusterSizer.
// By asserting Sampler to an empty interface, we will have access to the underlying pointer.
// We can then assert that pointer to the ClusterSizer.
if sampler, ok := sampler.(any).(ClusterSizer); ok {
sampler.SetClusterSize(size)
}
}
}

func (s *RulesBasedSampler) GetSampleRate(trace *types.Trace) (rate uint, keep bool, reason string, key string) {
logger := s.Logger.Debug().WithFields(map[string]interface{}{
"trace_id": trace.TraceID,
Expand Down
5 changes: 4 additions & 1 deletion sample/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ func (s *SamplerFactory) updatePeerCounts() {

// all the samplers who want it should use the stored count
for _, sampler := range s.samplers {
if clusterSizer, ok := sampler.(ClusterSizer); ok {
// Sampler does not implement ClusterSizer.
// By asserting Sampler to an empty interface, we will have access to the underlying pointer.
// We can then assert that pointer to the ClusterSizer.
if clusterSizer, ok := sampler.(any).(ClusterSizer); ok {
clusterSizer.SetClusterSize(s.peerCount)
}
}
Expand Down

0 comments on commit aeea1d4

Please sign in to comment.