Skip to content

Commit e5a962c

Browse files
authored
[pdata/pprofile] add ProfileCount() (#14239)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Similar to `(Logs) LogRecordCount() int`, `(Metrics) MetricCount() int` and `(Traces) SpanCount() int` add `ProfileCount()`. <!-- Issue number if applicable --> #### Link to tracking issue Fixes # <!--Describe what testing was performed and which tests were added.--> #### Testing <!--Describe the documentation added.--> #### Documentation <!--Please delete paragraphs that you did not use before submitting.--> Signed-off-by: Florian Lehner <[email protected]>
1 parent 152042e commit e5a962c

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

.chloggen/profiles-count.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: 'enhancement'
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. receiver/otlp)
7+
component: pdata/pprofile
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: add ProfileCount()
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [14239]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [api]

pdata/pprofile/profiles.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,17 @@ func (ms Profiles) switchDictionary(src, dst ProfilesDictionary) error {
4444

4545
return nil
4646
}
47+
48+
// ProfileCount calculates the total number of profile records.
49+
func (ms Profiles) ProfileCount() int {
50+
profileCount := 0
51+
rps := ms.ResourceProfiles()
52+
for i := 0; i < rps.Len(); i++ {
53+
rp := rps.At(i)
54+
sps := rp.ScopeProfiles()
55+
for j := 0; j < sps.Len(); j++ {
56+
profileCount += sps.At(j).Profiles().Len()
57+
}
58+
}
59+
return profileCount
60+
}

pdata/pprofile/profiles_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,42 @@ func TestSampleCount(t *testing.T) {
6161
assert.Equal(t, 7, pd.SampleCount())
6262
}
6363

64+
func TestProfileCount(t *testing.T) {
65+
pd := NewProfiles()
66+
assert.Equal(t, 0, pd.ProfileCount())
67+
68+
rs := pd.ResourceProfiles().AppendEmpty()
69+
assert.Equal(t, 0, pd.ProfileCount())
70+
71+
ils := rs.ScopeProfiles().AppendEmpty()
72+
assert.Equal(t, 0, pd.ProfileCount())
73+
74+
ps := ils.Profiles().AppendEmpty()
75+
assert.Equal(t, 1, pd.ProfileCount())
76+
77+
ps.Samples().AppendEmpty()
78+
assert.Equal(t, 1, pd.ProfileCount())
79+
80+
ils2 := rs.ScopeProfiles().AppendEmpty()
81+
assert.Equal(t, 1, pd.ProfileCount())
82+
83+
ps2 := ils2.Profiles().AppendEmpty()
84+
assert.Equal(t, 2, pd.ProfileCount())
85+
86+
ps2.Samples().AppendEmpty()
87+
assert.Equal(t, 2, pd.ProfileCount())
88+
89+
rms := pd.ResourceProfiles()
90+
rms.EnsureCapacity(3)
91+
rms.AppendEmpty().ScopeProfiles().AppendEmpty()
92+
ilss := rms.AppendEmpty().ScopeProfiles().AppendEmpty().Profiles().AppendEmpty().Samples()
93+
for range 5 {
94+
ilss.AppendEmpty()
95+
}
96+
// 5 + 2 (from rms.At(0) and rms.At(1) initialized first)
97+
assert.Equal(t, 3, pd.ProfileCount())
98+
}
99+
64100
func TestSampleCountWithEmpty(t *testing.T) {
65101
assert.Equal(t, 0, newProfiles(&internal.ExportProfilesServiceRequest{
66102
ResourceProfiles: []*internal.ResourceProfiles{{}},

0 commit comments

Comments
 (0)