Skip to content

Commit

Permalink
Introduce SampleCount to ProfilesSink (#11225)
Browse files Browse the repository at this point in the history
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description

This is an extraction from
#11131.

cc @mx-psi
  • Loading branch information
dmathieu authored Sep 20, 2024
1 parent 0c7d347 commit 18e4b49
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
20 changes: 20 additions & 0 deletions .chloggen/consumertest-profiles-samplecount.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'enhancement'

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: consumertest

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Introduce SampleCount method in ProfilesSink struct.

# One or more tracking issues or pull requests related to the change
issues: [11225]

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
14 changes: 12 additions & 2 deletions consumer/consumertest/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,9 @@ func (sle *LogsSink) Reset() {
// stores all profiles and allows querying them for testing.
type ProfilesSink struct {
nonMutatingConsumer
mu sync.Mutex
profiles []pprofile.Profiles
mu sync.Mutex
profiles []pprofile.Profiles
sampleCount int
}

var _ consumerprofiles.Profiles = (*ProfilesSink)(nil)
Expand All @@ -175,6 +176,7 @@ func (ste *ProfilesSink) ConsumeProfiles(_ context.Context, td pprofile.Profiles
defer ste.mu.Unlock()

ste.profiles = append(ste.profiles, td)
ste.sampleCount += td.SampleCount()

return nil
}
Expand All @@ -189,10 +191,18 @@ func (ste *ProfilesSink) AllProfiles() []pprofile.Profiles {
return copyProfiles
}

// ProfileRecordCount returns the number of profiles stored by this sink since last Reset.
func (ste *ProfilesSink) SampleCount() int {
ste.mu.Lock()
defer ste.mu.Unlock()
return ste.sampleCount
}

// Reset deletes any stored data.
func (ste *ProfilesSink) Reset() {
ste.mu.Lock()
defer ste.mu.Unlock()

ste.profiles = nil
ste.sampleCount = 0
}
2 changes: 2 additions & 0 deletions consumer/consumertest/sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func TestProfilesSink(t *testing.T) {
want = append(want, td)
}
assert.Equal(t, want, sink.AllProfiles())
assert.Equal(t, len(want), sink.SampleCount())
sink.Reset()
assert.Empty(t, sink.AllProfiles())
assert.Empty(t, sink.SampleCount())
}

0 comments on commit 18e4b49

Please sign in to comment.