github.com/grafana/pyroscope@v1.18.0/pkg/og/build/build.go (about)

     1  // Package build contains build-related variables set at compile time.
     2  package build
     3  
     4  import (
     5  	"encoding/json"
     6  	"fmt"
     7  	"runtime"
     8  	"strconv"
     9  	"strings"
    10  
    11  	"github.com/prometheus/client_golang/prometheus"
    12  )
    13  
    14  var (
    15  	Version = "N/A"
    16  	ID      = "N/A"
    17  	Time    = "N/A"
    18  
    19  	GitSHA      = "N/A"
    20  	GitDirtyStr = "-1"
    21  	GitDirty    int
    22  )
    23  
    24  func init() {
    25  	GitDirty, _ = strconv.Atoi(GitDirtyStr)
    26  }
    27  
    28  const tmplt = `
    29  GENERAL
    30    GOARCH:             %s
    31    GOOS:               %s
    32    Go Version:         %s
    33    Version:            %s
    34    Build ID:           %s
    35    Build Time:         %s
    36    Git SHA:            %s
    37    Git Dirty Files:    %d
    38    Embedded Assets:    %t
    39  `
    40  
    41  func Summary() string {
    42  	return fmt.Sprintf(strings.TrimSpace(tmplt),
    43  		runtime.GOARCH,
    44  		runtime.GOOS,
    45  		runtime.Version(),
    46  		Version,
    47  		ID,
    48  		Time,
    49  		GitSHA,
    50  		GitDirty,
    51  	)
    52  }
    53  
    54  type buildInfoJSON struct {
    55  	GOOS              string `json:"goos"`
    56  	GOARCH            string `json:"goarch"`
    57  	GoVersion         string `json:"goVersion"`
    58  	Version           string `json:"version"`
    59  	ID                string `json:"id"`
    60  	Time              string `json:"time"`
    61  	GitSHA            string `json:"gitSHA"`
    62  	GitDirty          int    `json:"gitDirty"`
    63  	UseEmbeddedAssets bool   `json:"useEmbeddedAssets"`
    64  	PhpspyGitSHA      string `json:"phpspyGitSHA"`
    65  }
    66  
    67  func generateBuildInfoJSON() buildInfoJSON {
    68  	return buildInfoJSON{
    69  		GOOS:      runtime.GOOS,
    70  		GOARCH:    runtime.GOARCH,
    71  		GoVersion: runtime.Version(),
    72  		Version:   Version,
    73  		ID:        ID,
    74  		Time:      Time,
    75  		GitSHA:    GitSHA,
    76  		GitDirty:  GitDirty,
    77  	}
    78  }
    79  
    80  func JSON() string {
    81  	b, _ := json.Marshal(generateBuildInfoJSON())
    82  	return string(b)
    83  }
    84  
    85  func PrettyJSON() string {
    86  	b, _ := json.MarshalIndent(generateBuildInfoJSON(), "", "  ")
    87  	return string(b)
    88  }
    89  
    90  // PrometheusBuildLabels returns a map of the labels
    91  // that will be exposed in the build_info metric
    92  func PrometheusBuildLabels() prometheus.Labels {
    93  	return prometheus.Labels{
    94  		"GOOS":     runtime.GOOS,
    95  		"GOARCH":   runtime.GOARCH,
    96  		"version":  Version,
    97  		"time":     Time,
    98  		"revision": GitSHA,
    99  	}
   100  }