github.com/grafana/pyroscope@v1.18.0/cmd/profilecli/tsdb.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  
     8  	"github.com/prometheus/prometheus/model/labels"
     9  
    10  	phlaremodel "github.com/grafana/pyroscope/pkg/model"
    11  	"github.com/grafana/pyroscope/pkg/phlaredb"
    12  	"github.com/grafana/pyroscope/pkg/phlaredb/tsdb/index"
    13  )
    14  
    15  func tsdbSeries(ctx context.Context, path string) error {
    16  	r, err := index.NewFileReader(path)
    17  	if err != nil {
    18  		return err
    19  	}
    20  
    21  	it, err := phlaredb.PostingsForMatchers(r, nil, labels.MustNewMatcher(labels.MatchNotEqual, "__name__", ""))
    22  	if err != nil {
    23  		return err
    24  	}
    25  
    26  	var (
    27  		lbls      phlaremodel.Labels
    28  		chunkMeta []index.ChunkMeta
    29  	)
    30  	line := struct {
    31  		SeriesRef   uint64
    32  		SeriesIndex *uint32
    33  		Labels      json.RawMessage
    34  	}{}
    35  	enc := json.NewEncoder(output(ctx))
    36  
    37  	for it.Next() {
    38  		if ctx.Err() != nil {
    39  			return ctx.Err()
    40  		}
    41  
    42  		_, err = r.Series(it.At(), &lbls, &chunkMeta)
    43  		if err != nil {
    44  			return fmt.Errorf("error retrieving seriesRef: %w", err)
    45  		}
    46  
    47  		line.Labels, err = json.Marshal(lbls)
    48  		if err != nil {
    49  			return fmt.Errorf("error marshalling labels: %w", err)
    50  		}
    51  
    52  		if len(chunkMeta) > 0 {
    53  			line.SeriesIndex = &chunkMeta[0].SeriesIndex
    54  		} else {
    55  			line.SeriesIndex = nil
    56  		}
    57  		line.SeriesRef = uint64(it.At())
    58  		if err := enc.Encode(&line); err != nil {
    59  			return fmt.Errorf("error writing line: %w", err)
    60  		}
    61  	}
    62  
    63  	return nil
    64  }