github.com/grafana/pyroscope@v1.18.0/pkg/phlaredb/schemas/v1/testhelper/profile.go (about)

     1  package testhelper
     2  
     3  import (
     4  	"github.com/google/uuid"
     5  	"github.com/prometheus/common/model"
     6  
     7  	typesv1 "github.com/grafana/pyroscope/api/gen/proto/go/types/v1"
     8  	phlaremodel "github.com/grafana/pyroscope/pkg/model"
     9  	"github.com/grafana/pyroscope/pkg/phlaredb/labels"
    10  	schemav1 "github.com/grafana/pyroscope/pkg/phlaredb/schemas/v1"
    11  	"github.com/grafana/pyroscope/pkg/pprof"
    12  	"github.com/grafana/pyroscope/pkg/pprof/testhelper"
    13  )
    14  
    15  func NewProfileSchema(builder *testhelper.ProfileBuilder, name string) ([]schemav1.InMemoryProfile, []phlaremodel.Labels) {
    16  	var (
    17  		p                = builder.Profile
    18  		lbls, seriesRefs = labels.CreateProfileLabels(true, p, &typesv1.LabelPair{Name: model.MetricNameLabel, Value: name})
    19  		ps               = make([]schemav1.InMemoryProfile, len(lbls))
    20  	)
    21  	for idxType := range lbls {
    22  		ps[idxType] = schemav1.InMemoryProfile{
    23  			ID:                uuid.New(),
    24  			TimeNanos:         p.TimeNanos,
    25  			Comments:          p.Comment,
    26  			DurationNanos:     p.DurationNanos,
    27  			DropFrames:        p.DropFrames,
    28  			KeepFrames:        p.KeepFrames,
    29  			Period:            p.Period,
    30  			DefaultSampleType: p.DefaultSampleType,
    31  			Annotations: schemav1.Annotations{
    32  				Keys:   make([]string, 0),
    33  				Values: make([]string, 0),
    34  			},
    35  		}
    36  		hashes := pprof.SampleHasher{}.Hashes(p.Sample)
    37  		ps[idxType].Samples = schemav1.Samples{
    38  			StacktraceIDs: make([]uint32, len(p.Sample)),
    39  			Values:        make([]uint64, len(p.Sample)),
    40  		}
    41  		for i, s := range p.Sample {
    42  			ps[idxType].Samples.Values[i] = uint64(s.Value[idxType])
    43  			ps[idxType].Samples.StacktraceIDs[i] = uint32(hashes[i])
    44  
    45  		}
    46  		ps[idxType].SeriesFingerprint = seriesRefs[idxType]
    47  		for _, a := range builder.Annotations {
    48  			ps[idxType].Annotations.Keys = append(ps[idxType].Annotations.Keys, a.Key)
    49  			ps[idxType].Annotations.Values = append(ps[idxType].Annotations.Values, a.Value)
    50  		}
    51  	}
    52  	return ps, lbls
    53  }