Package buildinfo
provides basic building blocks and instructions to easily add
build and release information to your app.
go get github.com/go-pogo/buildinfo
import "github.com/go-pogo/buildinfo"
Declare build info variables in your main package:
package main
// this value is changed via ldflags when building a new release
var version string
func main() {
bld, err := buildinfo.New(version)
}
Build your Go project and include the following ldflags
:
go build -ldflags=" \
-X main.version=`$(git describe --tags)` \
main.go
When using a metrics scraper like Prometheus or OpenTelemetry, it is often a good idea to make the build information of your app available. Below example shows just how easy it is to create and register a collector with the build information as constant labels.
prometheus.MustRegister(prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Namespace: "myapp",
Name: buildinfo.MetricName,
Help: buildinfo.MetricHelp,
ConstLabels: bld.Map(),
},
func() float64 { return 1 },
))
resource.Merge(
resource.Default(),
resource.NewSchemaless(
semconv.ServiceName("myapp"),
semconv.ServiceVersion(bld.Version()),
attribute.String("vcs.revision", bld.Revision()),
attribute.String("vcs.time", bld.Time().Format(time.RFC3339)),
),
)
Additional detailed documentation is available at pkg.go.dev
Copyright © 2020-2024 Roel Schut. All rights reserved.
This project is governed by a BSD-style license that can be found in the LICENSE file.