github.com/GuanceCloud/cliutils@v1.1.21/pprofparser/service/parsing/metadata.go (about)

     1  package parsing
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"github.com/GuanceCloud/cliutils/pprofparser/domain/events"
     7  	"github.com/GuanceCloud/cliutils/pprofparser/domain/parameter"
     8  	"github.com/GuanceCloud/cliutils/pprofparser/service/storage"
     9  	"github.com/GuanceCloud/cliutils/pprofparser/tools/filepathtoolkit"
    10  	"os"
    11  	"strings"
    12  )
    13  
    14  const (
    15  	FlameGraph Format = "flamegraph" // see https://github.com/brendangregg/FlameGraph
    16  	//Deprecated use Collapsed instead
    17  	RawFlameGraph Format = "rawflamegraph" // flamegraph collapse
    18  	Collapsed     Format = "collapse"      // flamegraph collapse format see https://github.com/brendangregg/FlameGraph/blob/master/stackcollapse.pl
    19  	SpeedScope    Format = "speedscope"    // see https://github.com/jlfwong/speedscope
    20  	JFR           Format = "jfr"           // see https://github.com/openjdk/jmc#core-api-example
    21  	PPROF         Format = "pprof"         // see https://github.com/google/pprof/blob/main/proto/profile.proto
    22  )
    23  
    24  type Profiler string
    25  
    26  const (
    27  	Unknown       Profiler = "unknown"
    28  	DDtrace                = "ddtrace"
    29  	AsyncProfiler          = "async-profiler"
    30  	PySpy
    31  	Pyroscope = "pyroscope"
    32  )
    33  
    34  type Format string
    35  
    36  type MetaData struct {
    37  	Format      Format   `json:"format"`
    38  	Profiler    Profiler `json:"profiler"`
    39  	Attachments []string `json:"attachments"`
    40  }
    41  
    42  func ReadMetaDataFile(f string) (*MetaData, error) {
    43  	if !filepathtoolkit.FileExists(f) && !strings.HasSuffix(f, ".json") {
    44  		f += ".json"
    45  	}
    46  	data, err := os.ReadFile(f)
    47  	if err != nil {
    48  		return nil, fmt.Errorf("open metadata file fail: %w", err)
    49  	}
    50  	m := new(MetaData)
    51  	if err := json.Unmarshal(data, m); err != nil {
    52  		return nil, fmt.Errorf("metadata json unmarshal fail: %w", err)
    53  	}
    54  	return m, nil
    55  }
    56  
    57  func ReadMetaData(prof *parameter.Profile, workspaceUUID string) (*MetaData, error) {
    58  	startNanos, err := prof.StartTime()
    59  	if err != nil {
    60  		return nil, fmt.Errorf("resolve Profile start timestamp fail: %w", err)
    61  	}
    62  
    63  	file := storage.DefaultDiskStorage.GetProfilePath(workspaceUUID, prof.ProfileID, startNanos, events.DefaultMetaFileName)
    64  
    65  	return ReadMetaDataFile(file)
    66  }