Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce SampleCount to ProfilesSink #11225

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}
Loading