Skip to content

Commit

Permalink
Add information about build to Prometheus publisher
Browse files Browse the repository at this point in the history
  • Loading branch information
koesie10 committed Dec 12, 2021
1 parent b50729b commit ececad0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ builds:
- goos: windows
goarch: arm64
main: ./cmd/smartmeter
ldflags:
- -s -w
- -X github.com/koesie10/smartmeter/version.Version={{.Version}}
- -X github.com/koesie10/smartmeter/version.Commit={{.Commit}}
- -X github.com/koesie10/smartmeter/version.BuildDate={{.Date}}
checksum:
name_template: 'checksums.txt'
snapshot:
Expand Down
6 changes: 0 additions & 6 deletions cmd/smartmeter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ import (
"os"
)

var (
version = "dev"
commit = "none"
date = "unknown"
)

func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
Expand Down
7 changes: 4 additions & 3 deletions cmd/smartmeter/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/jacobsa/go-serial/serial"
"github.com/koesie10/pflagenv"
"github.com/koesie10/smartmeter/serialinput"
"github.com/koesie10/smartmeter/version"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -44,9 +45,9 @@ var logger, _ = zap.NewDevelopment()
var rootCmd = &cobra.Command{
Use: "smartmeter",
Version: fmt.Sprintf("%s (%s at %s)",
version,
commit,
date,
version.Version,
version.Commit,
version.BuildDate,
),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := pflagenv.Parse(&config); err != nil {
Expand Down
30 changes: 27 additions & 3 deletions prometheus/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package prometheus

import (
"fmt"
"github.com/koesie10/smartmeter/smartmeter"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"net"
"net/http"
"strconv"

"github.com/koesie10/smartmeter/smartmeter"
"github.com/koesie10/smartmeter/version"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

var _ smartmeter.Publisher = (*publisher)(nil)
Expand Down Expand Up @@ -146,6 +149,25 @@ func NewPublisher(options PublisherOptions) (smartmeter.Publisher, error) {

registry.MustRegister(p.gasConsumed)

if !options.DisableGoCollector {
registry.MustRegister(collectors.NewGoCollector())
registry.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
}

buildInfo := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "info",
Help: "Information about the smartmeter build",
Namespace: "smartmeter",
ConstLabels: map[string]string{
"version": version.Version,
"commit": version.Commit,
"build_date": version.BuildDate,
},
})
buildInfo.Set(1)

registry.MustRegister(buildInfo)

mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))

Expand All @@ -170,6 +192,8 @@ func NewPublisher(options PublisherOptions) (smartmeter.Publisher, error) {

type PublisherOptions struct {
Addr string `env:"PROMETHEUS_ADDR" flag:"addr" desc:"Prometheus HTTP server address, set empty to disable"`

DisableGoCollector bool `env:"DISABLE_GO_COLLECTOR" flag:"disable-go-collector" desc:"Disable Go collector"`
}

func (p *publisher) Publish(packet *smartmeter.P1Packet) error {
Expand Down
33 changes: 33 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package version

import (
"fmt"
"time"
)

var (
// Version is the version of this build
Version string
// Commit is the commit of this build
Commit string
// BuildDate is the build date of this build in RFC3339 format
BuildDate string
// BuildTime is the build date of this build as a Go time.Time
BuildTime time.Time
)

func init() {
if Version == "" {
Version = "SNAPSHOT"
}

if BuildDate == "" {
BuildDate = time.Time{}.Format(time.RFC3339)
}

var err error
BuildTime, err = time.ParseInLocation(time.RFC3339, BuildDate, time.UTC)
if err != nil {
panic(fmt.Sprintf("failed to parse build time: %v", err))
}
}

0 comments on commit ececad0

Please sign in to comment.