2023-09-20 09:31:22 -05:00
|
|
|
package pyroscope
|
2023-04-25 09:08:18 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/bufbuild/connect-go"
|
|
|
|
googlev1 "github.com/grafana/phlare/api/gen/proto/go/google/v1"
|
|
|
|
querierv1 "github.com/grafana/phlare/api/gen/proto/go/querier/v1"
|
|
|
|
typesv1 "github.com/grafana/phlare/api/gen/proto/go/types/v1"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-09-20 09:31:22 -05:00
|
|
|
func Test_PyroscopeClient(t *testing.T) {
|
|
|
|
connectClient := &FakePyroscopeConnectClient{}
|
|
|
|
client := &PyroscopeClient{
|
2023-04-25 09:08:18 -05:00
|
|
|
connectClient: connectClient,
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("GetSeries", func(t *testing.T) {
|
|
|
|
resp, err := client.GetSeries(context.Background(), "memory:alloc_objects:count:space:bytes", "{}", 0, 100, []string{}, 15)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
series := &SeriesResponse{
|
|
|
|
Series: []*Series{
|
|
|
|
{Labels: []*LabelPair{{Name: "foo", Value: "bar"}}, Points: []*Point{{Timestamp: int64(1000), Value: 30}, {Timestamp: int64(2000), Value: 10}}},
|
|
|
|
},
|
|
|
|
Units: "short",
|
|
|
|
Label: "alloc_objects",
|
|
|
|
}
|
|
|
|
require.Equal(t, series, resp)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("GetProfile", func(t *testing.T) {
|
2023-04-27 03:50:11 -05:00
|
|
|
maxNodes := int64(-1)
|
|
|
|
resp, err := client.GetProfile(context.Background(), "memory:alloc_objects:count:space:bytes", "{}", 0, 100, &maxNodes)
|
2023-04-25 09:08:18 -05:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
series := &ProfileResponse{
|
|
|
|
Flamebearer: &Flamebearer{
|
|
|
|
Names: []string{"foo", "bar", "baz"},
|
|
|
|
Levels: []*Level{
|
|
|
|
{Values: []int64{0, 10, 0, 0}},
|
|
|
|
{Values: []int64{0, 9, 0, 1}},
|
|
|
|
{Values: []int64{0, 8, 8, 2}},
|
|
|
|
},
|
|
|
|
Total: 100,
|
|
|
|
MaxSelf: 56,
|
|
|
|
},
|
|
|
|
Units: "short",
|
|
|
|
}
|
|
|
|
require.Equal(t, series, resp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-20 09:31:22 -05:00
|
|
|
type FakePyroscopeConnectClient struct {
|
2023-08-30 10:46:47 -05:00
|
|
|
Req any
|
2023-04-25 09:08:18 -05:00
|
|
|
}
|
|
|
|
|
2023-09-20 09:31:22 -05:00
|
|
|
func (f *FakePyroscopeConnectClient) ProfileTypes(ctx context.Context, c *connect.Request[querierv1.ProfileTypesRequest]) (*connect.Response[querierv1.ProfileTypesResponse], error) {
|
2023-04-25 09:08:18 -05:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2023-09-20 09:31:22 -05:00
|
|
|
func (f *FakePyroscopeConnectClient) LabelValues(ctx context.Context, c *connect.Request[querierv1.LabelValuesRequest]) (*connect.Response[querierv1.LabelValuesResponse], error) {
|
2023-04-25 09:08:18 -05:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2023-09-20 09:31:22 -05:00
|
|
|
func (f *FakePyroscopeConnectClient) LabelNames(context.Context, *connect.Request[querierv1.LabelNamesRequest]) (*connect.Response[querierv1.LabelNamesResponse], error) {
|
2023-04-25 09:08:18 -05:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2023-09-20 09:31:22 -05:00
|
|
|
func (f *FakePyroscopeConnectClient) Series(ctx context.Context, c *connect.Request[querierv1.SeriesRequest]) (*connect.Response[querierv1.SeriesResponse], error) {
|
2023-04-25 09:08:18 -05:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2023-09-20 09:31:22 -05:00
|
|
|
func (f *FakePyroscopeConnectClient) SelectMergeStacktraces(ctx context.Context, c *connect.Request[querierv1.SelectMergeStacktracesRequest]) (*connect.Response[querierv1.SelectMergeStacktracesResponse], error) {
|
2023-04-25 09:08:18 -05:00
|
|
|
f.Req = c
|
|
|
|
return &connect.Response[querierv1.SelectMergeStacktracesResponse]{
|
|
|
|
Msg: &querierv1.SelectMergeStacktracesResponse{
|
|
|
|
Flamegraph: &querierv1.FlameGraph{
|
|
|
|
Names: []string{"foo", "bar", "baz"},
|
|
|
|
Levels: []*querierv1.Level{
|
|
|
|
{Values: []int64{0, 10, 0, 0}},
|
|
|
|
{Values: []int64{0, 9, 0, 1}},
|
|
|
|
{Values: []int64{0, 8, 8, 2}},
|
|
|
|
},
|
|
|
|
Total: 100,
|
|
|
|
MaxSelf: 56,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-09-20 09:31:22 -05:00
|
|
|
func (f *FakePyroscopeConnectClient) SelectSeries(ctx context.Context, req *connect.Request[querierv1.SelectSeriesRequest]) (*connect.Response[querierv1.SelectSeriesResponse], error) {
|
2023-04-25 09:08:18 -05:00
|
|
|
f.Req = req
|
|
|
|
return &connect.Response[querierv1.SelectSeriesResponse]{
|
|
|
|
Msg: &querierv1.SelectSeriesResponse{
|
|
|
|
Series: []*typesv1.Series{
|
|
|
|
{
|
|
|
|
Labels: []*typesv1.LabelPair{{Name: "foo", Value: "bar"}},
|
|
|
|
Points: []*typesv1.Point{{Timestamp: int64(1000), Value: 30}, {Timestamp: int64(2000), Value: 10}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-09-20 09:31:22 -05:00
|
|
|
func (f *FakePyroscopeConnectClient) SelectMergeProfile(ctx context.Context, c *connect.Request[querierv1.SelectMergeProfileRequest]) (*connect.Response[googlev1.Profile], error) {
|
2023-04-25 09:08:18 -05:00
|
|
|
panic("implement me")
|
|
|
|
}
|