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

chore(otlp): add integration tests with fixtures #3808

Merged
merged 9 commits into from
Jan 3, 2025
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
1 change: 0 additions & 1 deletion pkg/ingester/otlp/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ func (p *profileBuilder) convertMappingBack(om *otelProfile.Mapping) uint64 {
buildID := ""
for _, attributeIndex := range om.AttributeIndices {
attr := p.src.AttributeTable[attributeIndex]
fmt.Printf("%s %s\n", attr.Key, attr.Value.GetStringValue())
if attr.Key == "process.executable.build_id.gnu" {
buildID = attr.Value.GetStringValue()
}
Expand Down
78 changes: 37 additions & 41 deletions pkg/ingester/otlp/ingest_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package otlp

import (
"context"
"fmt"
"strings"
"os"
"testing"

phlaremodel "github.com/grafana/pyroscope/pkg/model"

"github.com/grafana/pyroscope/pkg/distributor/model"

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

v1experimental2 "github.com/grafana/pyroscope/api/otlp/collector/profiles/v1development"
v1experimental "github.com/grafana/pyroscope/api/otlp/profiles/v1development"
"github.com/grafana/pyroscope/pkg/og/convert/pprof/bench"
"github.com/grafana/pyroscope/pkg/og/convert/pprof/strprofile"
"github.com/grafana/pyroscope/pkg/test/mocks/mockotlp"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -160,6 +161,12 @@ func TestAppendAttributesUnique(t *testing.T) {
}
}

func readJSONFile(t *testing.T, filename string) string {
data, err := os.ReadFile(filename)
require.NoError(t, err)
return string(data)
}

func TestSymbolizedFunctionNames(t *testing.T) {
// Create two unsymbolized locations at 0x1e0 and 0x2f0
// Expect both of them to be present in the converted pprof
Expand Down Expand Up @@ -191,6 +198,7 @@ func TestSymbolizedFunctionNames(t *testing.T) {
LocationsLength: 2,
Value: []int64{0xef},
}}
otlpb.profile.TimeNanos = 239
req := &v1experimental2.ExportProfilesServiceRequest{
ResourceProfiles: []*v1experimental.ResourceProfiles{{
ScopeProfiles: []*v1experimental.ScopeProfiles{{
Expand All @@ -201,17 +209,15 @@ func TestSymbolizedFunctionNames(t *testing.T) {
h := NewOTLPIngestHandler(svc, logger, false)
_, err := h.Export(context.Background(), req)
assert.NoError(t, err)
require.Equal(t, 1, len(profiles))
assert.Equal(t, 1, len(profiles))

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

ss := bench.StackCollapseProtoWithOptions(gp, bench.StackCollapseOptions{
ValueIdx: 0,
Scale: 1,
WithLabels: true,
})
require.Equal(t, 1, len(ss))
require.Equal(t, " ||| file1.so 0x2f0;file1.so 0x1e0 239", ss[0])
jsonStr, err := strprofile.Stringify(gp, strprofile.Options{})
assert.NoError(t, err)
expectedJSON := readJSONFile(t, "testdata/TestSymbolizedFunctionNames.json")
assert.JSONEq(t, expectedJSON, jsonStr)

}

func TestSampleAttributes(t *testing.T) {
Expand Down Expand Up @@ -276,6 +282,7 @@ func TestSampleAttributes(t *testing.T) {
},
},
}}
otlpb.profile.TimeNanos = 239
req := &v1experimental2.ExportProfilesServiceRequest{
ResourceProfiles: []*v1experimental.ResourceProfiles{{
ScopeProfiles: []*v1experimental.ScopeProfiles{{
Expand All @@ -299,15 +306,11 @@ func TestSampleAttributes(t *testing.T) {

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

ss := bench.StackCollapseProtoWithOptions(gp, bench.StackCollapseOptions{
ValueIdx: 0,
Scale: 1,
WithLabels: true,
})
fmt.Printf("%s \n", strings.Join(ss, "\n"))
require.Equal(t, 2, len(ss))
assert.Equal(t, "(process = chrome) ||| chrome.so 0x4e;chrome.so 0x3e 61423", ss[0])
assert.Equal(t, "(process = firefox) ||| firefox.so 0x2e;firefox.so 0x1e 239", ss[1])
jsonStr, err := strprofile.Stringify(gp, strprofile.Options{})
assert.NoError(t, err)
expectedJSON := readJSONFile(t, "testdata/TestSampleAttributes.json")
assert.Equal(t, expectedJSON, jsonStr)

}

func TestDifferentServiceNames(t *testing.T) {
Expand Down Expand Up @@ -438,7 +441,7 @@ func TestDifferentServiceNames(t *testing.T) {
UnitStrindex: otlpb.addstr("nanoseconds"),
}
otlpb.profile.Period = 10000000 // 10ms

otlpb.profile.TimeNanos = 239
req := &v1experimental2.ExportProfilesServiceRequest{
ResourceProfiles: []*v1experimental.ResourceProfiles{{
ScopeProfiles: []*v1experimental.ScopeProfiles{{
Expand All @@ -453,22 +456,18 @@ func TestDifferentServiceNames(t *testing.T) {

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

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

for _, p := range profiles {
require.Equal(t, 1, len(p.Series))
seriesLabelsMap := make(map[string]string)
for _, label := range p.Series[0].Labels {
seriesLabelsMap[label.Name] = label.Value
}

serviceName := seriesLabelsMap["service_name"]
require.Contains(t, []string{"service-a", "service-b", "unknown"}, serviceName)
assert.NotContains(t, seriesLabelsMap, "service.name")
series := phlaremodel.Labels(p.Series[0].Labels).ToPrometheusLabels().String()
assert.Contains(t, expectedProfiles, series)
expectedJsonPath := expectedProfiles[series]
expectedJson := readJSONFile(t, expectedJsonPath)

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

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

ss := bench.StackCollapseProtoWithOptions(gp, bench.StackCollapseOptions{
ValueIdx: 0,
Scale: 1,
WithLabels: true,
})
require.Equal(t, 1, len(ss))
assert.Equal(t, expectedStacks[serviceName], ss[0])
assert.NotContains(t, ss[0], "service.name")
jsonStr, err := strprofile.Stringify(gp, strprofile.Options{})
assert.NoError(t, err)
assert.JSONEq(t, expectedJson, jsonStr)
assert.NotContains(t, jsonStr, "service.name")

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"sample_types": [
{
"type": "cpu",
"unit": "nanoseconds"
}
],
"samples": [
{
"locations": [
{
"address": "0x1100",
"lines": [
"serviceA_func1[serviceA_func1]@service_a.go:10"
],
"mapping": "0x1000-0x2000@0x0 service-a.so()"
},
{
"address": "0x1200",
"lines": [
"serviceA_func2[serviceA_func2]@service_a.go:20"
],
"mapping": "0x1000-0x2000@0x0 service-a.so()"
}
],
"values": "1000000000"
}
],
"time_nanos": "239",
"duration_nanos": "10000000000",
"period": "10000000"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"sample_types": [
{
"type": "cpu",
"unit": "nanoseconds"
}
],
"samples": [
{
"locations": [
{
"address": "0x2100",
"lines": [
"serviceB_func1[serviceB_func1]@service_b.go:30"
],
"mapping": "0x2000-0x3000@0x0 service-b.so()"
},
{
"address": "0x2200",
"lines": [
"serviceB_func2[serviceB_func2]@service_b.go:40"
],
"mapping": "0x2000-0x3000@0x0 service-b.so()"
}
],
"values": "2000000000"
}
],
"time_nanos": "239",
"duration_nanos": "10000000000",
"period": "10000000"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"sample_types": [
{
"type": "cpu",
"unit": "nanoseconds"
}
],
"samples": [
{
"locations": [
{
"address": "0xef0",
"lines": [
"serviceC_func3[serviceC_func3]@service_c.go:50"
],
"mapping": "0x4000-0x5000@0x0 service-c.so()"
},
{
"address": "0xef0",
"lines": [
"serviceC_func3[serviceC_func3]@service_c.go:50"
],
"mapping": "0x4000-0x5000@0x0 service-c.so()"
}
],
"values": "7000000000"
}
],
"time_nanos": "239",
"duration_nanos": "10000000000",
"period": "10000000"
}
53 changes: 53 additions & 0 deletions pkg/ingester/otlp/testdata/TestSampleAttributes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"sample_types": [
{
"type": "samples",
"unit": "ms"
}
],
"samples": [
{
"locations": [
{
"address": "0x1e",
"lines": [
"firefox.so 0x1e[]@:0"
],
"mapping": "0x1000-0x1000@0x0 firefox.so()"
},
{
"address": "0x2e",
"lines": [
"firefox.so 0x2e[]@:0"
],
"mapping": "0x1000-0x1000@0x0 firefox.so()"
}
],
"values": "239",
"labels": "process=firefox"
},
{
"locations": [
{
"address": "0x3e",
"lines": [
"chrome.so 0x3e[]@:0"
],
"mapping": "0x1000-0x1000@0x0 chrome.so()"
},
{
"address": "0x4e",
"lines": [
"chrome.so 0x4e[]@:0"
],
"mapping": "0x1000-0x1000@0x0 chrome.so()"
}
],
"values": "61423",
"labels": "process=chrome"
}
],
"time_nanos": "239",
"duration_nanos": "10000000000",
"period": "0"
}
32 changes: 32 additions & 0 deletions pkg/ingester/otlp/testdata/TestSymbolizedFunctionNames.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"sample_types": [
{
"type": "samples",
"unit": "ms"
}
],
"samples": [
{
"locations": [
{
"address": "0x1e0",
"lines": [
"file1.so 0x1e0[]@:0"
],
"mapping": "0x1000-0x1000@0x0 file1.so()"
},
{
"address": "0x2f0",
"lines": [
"file1.so 0x2f0[]@:0"
],
"mapping": "0x1000-0x1000@0x0 file1.so()"
}
],
"values": "239"
}
],
"time_nanos": "239",
"duration_nanos": "10000000000",
"period": "0"
}
2 changes: 0 additions & 2 deletions pkg/og/convert/pprof/bench/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func ReadGzipFile(f string) ([]byte, error) {
return nil, err
}
return io.ReadAll(g)

}

func WriteGzipFile(f string, data []byte) error {
Expand Down Expand Up @@ -113,7 +112,6 @@ func StackCollapseProtoWithOptions(p *profilev1.Profile, opt StackCollapseOption
continue
}
unique = append(unique, s)

}

res := []string{}
Expand Down
Loading
Loading