Skip to content

Package buildinfo provides basic building blocks and instructions to easily add build and release information to your app.

License

Notifications You must be signed in to change notification settings

go-pogo/buildinfo

Repository files navigation

buildinfo

Latest release Build status Go Report Card Documentation

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"

Using ldflags

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

Observability usage

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 metric collector

prometheus.MustRegister(prometheus.NewGaugeFunc(
    prometheus.GaugeOpts{
        Namespace:   "myapp",
        Name:        buildinfo.MetricName,
        Help:        buildinfo.MetricHelp,
        ConstLabels: bld.Map(),
    },
    func() float64 { return 1 },
))

OTEL resource

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)),
    ),
)

Documentation

Additional detailed documentation is available at pkg.go.dev

Created with

License

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.

About

Package buildinfo provides basic building blocks and instructions to easily add build and release information to your app.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Languages