Skip to content

Commit 466aaa8

Browse files
authored
chore(otlp): add integration tests with fixtures (#3808)
1 parent cd06698 commit 466aaa8

18 files changed

+53624
-45
lines changed

pkg/ingester/otlp/convert.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ func (p *profileBuilder) convertMappingBack(om *otelProfile.Mapping) uint64 {
244244
buildID := ""
245245
for _, attributeIndex := range om.AttributeIndices {
246246
attr := p.src.AttributeTable[attributeIndex]
247-
fmt.Printf("%s %s\n", attr.Key, attr.Value.GetStringValue())
248247
if attr.Key == "process.executable.build_id.gnu" {
249248
buildID = attr.Value.GetStringValue()
250249
}

pkg/ingester/otlp/ingest_handler_test.go

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package otlp
22

33
import (
44
"context"
5-
"fmt"
6-
"strings"
5+
"os"
76
"testing"
87

8+
phlaremodel "github.com/grafana/pyroscope/pkg/model"
9+
910
"github.com/grafana/pyroscope/pkg/distributor/model"
1011

1112
"github.com/prometheus/prometheus/util/testutil"
@@ -14,7 +15,7 @@ import (
1415

1516
v1experimental2 "github.com/grafana/pyroscope/api/otlp/collector/profiles/v1development"
1617
v1experimental "github.com/grafana/pyroscope/api/otlp/profiles/v1development"
17-
"github.com/grafana/pyroscope/pkg/og/convert/pprof/bench"
18+
"github.com/grafana/pyroscope/pkg/og/convert/pprof/strprofile"
1819
"github.com/grafana/pyroscope/pkg/test/mocks/mockotlp"
1920

2021
"github.com/stretchr/testify/assert"
@@ -160,6 +161,12 @@ func TestAppendAttributesUnique(t *testing.T) {
160161
}
161162
}
162163

164+
func readJSONFile(t *testing.T, filename string) string {
165+
data, err := os.ReadFile(filename)
166+
require.NoError(t, err)
167+
return string(data)
168+
}
169+
163170
func TestSymbolizedFunctionNames(t *testing.T) {
164171
// Create two unsymbolized locations at 0x1e0 and 0x2f0
165172
// Expect both of them to be present in the converted pprof
@@ -191,6 +198,7 @@ func TestSymbolizedFunctionNames(t *testing.T) {
191198
LocationsLength: 2,
192199
Value: []int64{0xef},
193200
}}
201+
otlpb.profile.TimeNanos = 239
194202
req := &v1experimental2.ExportProfilesServiceRequest{
195203
ResourceProfiles: []*v1experimental.ResourceProfiles{{
196204
ScopeProfiles: []*v1experimental.ScopeProfiles{{
@@ -201,17 +209,15 @@ func TestSymbolizedFunctionNames(t *testing.T) {
201209
h := NewOTLPIngestHandler(svc, logger, false)
202210
_, err := h.Export(context.Background(), req)
203211
assert.NoError(t, err)
204-
require.Equal(t, 1, len(profiles))
212+
assert.Equal(t, 1, len(profiles))
205213

206214
gp := profiles[0].Series[0].Samples[0].Profile.Profile
207215

208-
ss := bench.StackCollapseProtoWithOptions(gp, bench.StackCollapseOptions{
209-
ValueIdx: 0,
210-
Scale: 1,
211-
WithLabels: true,
212-
})
213-
require.Equal(t, 1, len(ss))
214-
require.Equal(t, " ||| file1.so 0x2f0;file1.so 0x1e0 239", ss[0])
216+
jsonStr, err := strprofile.Stringify(gp, strprofile.Options{})
217+
assert.NoError(t, err)
218+
expectedJSON := readJSONFile(t, "testdata/TestSymbolizedFunctionNames.json")
219+
assert.JSONEq(t, expectedJSON, jsonStr)
220+
215221
}
216222

217223
func TestSampleAttributes(t *testing.T) {
@@ -276,6 +282,7 @@ func TestSampleAttributes(t *testing.T) {
276282
},
277283
},
278284
}}
285+
otlpb.profile.TimeNanos = 239
279286
req := &v1experimental2.ExportProfilesServiceRequest{
280287
ResourceProfiles: []*v1experimental.ResourceProfiles{{
281288
ScopeProfiles: []*v1experimental.ScopeProfiles{{
@@ -299,15 +306,11 @@ func TestSampleAttributes(t *testing.T) {
299306

300307
gp := profiles[0].Series[0].Samples[0].Profile.Profile
301308

302-
ss := bench.StackCollapseProtoWithOptions(gp, bench.StackCollapseOptions{
303-
ValueIdx: 0,
304-
Scale: 1,
305-
WithLabels: true,
306-
})
307-
fmt.Printf("%s \n", strings.Join(ss, "\n"))
308-
require.Equal(t, 2, len(ss))
309-
assert.Equal(t, "(process = chrome) ||| chrome.so 0x4e;chrome.so 0x3e 61423", ss[0])
310-
assert.Equal(t, "(process = firefox) ||| firefox.so 0x2e;firefox.so 0x1e 239", ss[1])
309+
jsonStr, err := strprofile.Stringify(gp, strprofile.Options{})
310+
assert.NoError(t, err)
311+
expectedJSON := readJSONFile(t, "testdata/TestSampleAttributes.json")
312+
assert.Equal(t, expectedJSON, jsonStr)
313+
311314
}
312315

313316
func TestDifferentServiceNames(t *testing.T) {
@@ -438,7 +441,7 @@ func TestDifferentServiceNames(t *testing.T) {
438441
UnitStrindex: otlpb.addstr("nanoseconds"),
439442
}
440443
otlpb.profile.Period = 10000000 // 10ms
441-
444+
otlpb.profile.TimeNanos = 239
442445
req := &v1experimental2.ExportProfilesServiceRequest{
443446
ResourceProfiles: []*v1experimental.ResourceProfiles{{
444447
ScopeProfiles: []*v1experimental.ScopeProfiles{{
@@ -453,22 +456,18 @@ func TestDifferentServiceNames(t *testing.T) {
453456

454457
require.Equal(t, 3, len(profiles))
455458

456-
expectedStacks := map[string]string{
457-
"service-a": " ||| serviceA_func2;serviceA_func1 1000000000",
458-
"service-b": " ||| serviceB_func2;serviceB_func1 2000000000",
459-
"unknown": " ||| serviceC_func3;serviceC_func3 7000000000",
459+
expectedProfiles := map[string]string{
460+
"{__name__=\"process_cpu\", __delta__=\"false\", __otel__=\"true\", pyroscope_spy=\"unknown\", service_name=\"service-a\"}": "testdata/TestDifferentServiceNames_service_a_profile.json",
461+
"{__name__=\"process_cpu\", __delta__=\"false\", __otel__=\"true\", pyroscope_spy=\"unknown\", service_name=\"service-b\"}": "testdata/TestDifferentServiceNames_service_b_profile.json",
462+
"{__name__=\"process_cpu\", __delta__=\"false\", __otel__=\"true\", pyroscope_spy=\"unknown\", service_name=\"unknown\"}": "testdata/TestDifferentServiceNames_unknown_profile.json",
460463
}
461464

462465
for _, p := range profiles {
463466
require.Equal(t, 1, len(p.Series))
464-
seriesLabelsMap := make(map[string]string)
465-
for _, label := range p.Series[0].Labels {
466-
seriesLabelsMap[label.Name] = label.Value
467-
}
468-
469-
serviceName := seriesLabelsMap["service_name"]
470-
require.Contains(t, []string{"service-a", "service-b", "unknown"}, serviceName)
471-
assert.NotContains(t, seriesLabelsMap, "service.name")
467+
series := phlaremodel.Labels(p.Series[0].Labels).ToPrometheusLabels().String()
468+
assert.Contains(t, expectedProfiles, series)
469+
expectedJsonPath := expectedProfiles[series]
470+
expectedJson := readJSONFile(t, expectedJsonPath)
472471

473472
gp := p.Series[0].Samples[0].Profile.Profile
474473

@@ -481,14 +480,11 @@ func TestDifferentServiceNames(t *testing.T) {
481480
assert.Equal(t, "nanoseconds", gp.StringTable[gp.PeriodType.Unit])
482481
assert.Equal(t, int64(10000000), gp.Period)
483482

484-
ss := bench.StackCollapseProtoWithOptions(gp, bench.StackCollapseOptions{
485-
ValueIdx: 0,
486-
Scale: 1,
487-
WithLabels: true,
488-
})
489-
require.Equal(t, 1, len(ss))
490-
assert.Equal(t, expectedStacks[serviceName], ss[0])
491-
assert.NotContains(t, ss[0], "service.name")
483+
jsonStr, err := strprofile.Stringify(gp, strprofile.Options{})
484+
assert.NoError(t, err)
485+
assert.JSONEq(t, expectedJson, jsonStr)
486+
assert.NotContains(t, jsonStr, "service.name")
487+
492488
}
493489
}
494490

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"sample_types": [
3+
{
4+
"type": "cpu",
5+
"unit": "nanoseconds"
6+
}
7+
],
8+
"samples": [
9+
{
10+
"locations": [
11+
{
12+
"address": "0x1100",
13+
"lines": [
14+
"serviceA_func1[serviceA_func1]@service_a.go:10"
15+
],
16+
"mapping": "0x1000-0x2000@0x0 service-a.so()"
17+
},
18+
{
19+
"address": "0x1200",
20+
"lines": [
21+
"serviceA_func2[serviceA_func2]@service_a.go:20"
22+
],
23+
"mapping": "0x1000-0x2000@0x0 service-a.so()"
24+
}
25+
],
26+
"values": "1000000000"
27+
}
28+
],
29+
"time_nanos": "239",
30+
"duration_nanos": "10000000000",
31+
"period": "10000000"
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"sample_types": [
3+
{
4+
"type": "cpu",
5+
"unit": "nanoseconds"
6+
}
7+
],
8+
"samples": [
9+
{
10+
"locations": [
11+
{
12+
"address": "0x2100",
13+
"lines": [
14+
"serviceB_func1[serviceB_func1]@service_b.go:30"
15+
],
16+
"mapping": "0x2000-0x3000@0x0 service-b.so()"
17+
},
18+
{
19+
"address": "0x2200",
20+
"lines": [
21+
"serviceB_func2[serviceB_func2]@service_b.go:40"
22+
],
23+
"mapping": "0x2000-0x3000@0x0 service-b.so()"
24+
}
25+
],
26+
"values": "2000000000"
27+
}
28+
],
29+
"time_nanos": "239",
30+
"duration_nanos": "10000000000",
31+
"period": "10000000"
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"sample_types": [
3+
{
4+
"type": "cpu",
5+
"unit": "nanoseconds"
6+
}
7+
],
8+
"samples": [
9+
{
10+
"locations": [
11+
{
12+
"address": "0xef0",
13+
"lines": [
14+
"serviceC_func3[serviceC_func3]@service_c.go:50"
15+
],
16+
"mapping": "0x4000-0x5000@0x0 service-c.so()"
17+
},
18+
{
19+
"address": "0xef0",
20+
"lines": [
21+
"serviceC_func3[serviceC_func3]@service_c.go:50"
22+
],
23+
"mapping": "0x4000-0x5000@0x0 service-c.so()"
24+
}
25+
],
26+
"values": "7000000000"
27+
}
28+
],
29+
"time_nanos": "239",
30+
"duration_nanos": "10000000000",
31+
"period": "10000000"
32+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"sample_types": [
3+
{
4+
"type": "samples",
5+
"unit": "ms"
6+
}
7+
],
8+
"samples": [
9+
{
10+
"locations": [
11+
{
12+
"address": "0x1e",
13+
"lines": [
14+
"firefox.so 0x1e[]@:0"
15+
],
16+
"mapping": "0x1000-0x1000@0x0 firefox.so()"
17+
},
18+
{
19+
"address": "0x2e",
20+
"lines": [
21+
"firefox.so 0x2e[]@:0"
22+
],
23+
"mapping": "0x1000-0x1000@0x0 firefox.so()"
24+
}
25+
],
26+
"values": "239",
27+
"labels": "process=firefox"
28+
},
29+
{
30+
"locations": [
31+
{
32+
"address": "0x3e",
33+
"lines": [
34+
"chrome.so 0x3e[]@:0"
35+
],
36+
"mapping": "0x1000-0x1000@0x0 chrome.so()"
37+
},
38+
{
39+
"address": "0x4e",
40+
"lines": [
41+
"chrome.so 0x4e[]@:0"
42+
],
43+
"mapping": "0x1000-0x1000@0x0 chrome.so()"
44+
}
45+
],
46+
"values": "61423",
47+
"labels": "process=chrome"
48+
}
49+
],
50+
"time_nanos": "239",
51+
"duration_nanos": "10000000000",
52+
"period": "0"
53+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"sample_types": [
3+
{
4+
"type": "samples",
5+
"unit": "ms"
6+
}
7+
],
8+
"samples": [
9+
{
10+
"locations": [
11+
{
12+
"address": "0x1e0",
13+
"lines": [
14+
"file1.so 0x1e0[]@:0"
15+
],
16+
"mapping": "0x1000-0x1000@0x0 file1.so()"
17+
},
18+
{
19+
"address": "0x2f0",
20+
"lines": [
21+
"file1.so 0x2f0[]@:0"
22+
],
23+
"mapping": "0x1000-0x1000@0x0 file1.so()"
24+
}
25+
],
26+
"values": "239"
27+
}
28+
],
29+
"time_nanos": "239",
30+
"duration_nanos": "10000000000",
31+
"period": "0"
32+
}

pkg/og/convert/pprof/bench/utils.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ func ReadGzipFile(f string) ([]byte, error) {
2222
return nil, err
2323
}
2424
return io.ReadAll(g)
25-
2625
}
2726

2827
func WriteGzipFile(f string, data []byte) error {
@@ -113,7 +112,6 @@ func StackCollapseProtoWithOptions(p *profilev1.Profile, opt StackCollapseOption
113112
continue
114113
}
115114
unique = append(unique, s)
116-
117115
}
118116

119117
res := []string{}

0 commit comments

Comments
 (0)