Add AppInfo metric & automatic build time app info population (#178)

This commit is contained in:
Russell Troxel 2023-06-07 14:21:35 -07:00 committed by GitHub
parent 7c3680cca3
commit c7ce467508
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 84 additions and 19 deletions

View File

@ -39,10 +39,18 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
- name: Prepare
id: prep
run: |
echo ::set-output name=version::${GITHUB_REF##*/}
- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: ghcr.io/${{ github.repository }}
flavor: |
latest=true
prefix=v
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
@ -73,5 +81,9 @@ jobs:
file: ./Dockerfile
platforms: linux/amd64,linux/arm64
push: true
tags: |
ghcr.io/${{ github.repository_owner }}/exportarr:${{ steps.prep.outputs.version }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
VERSION=${{ steps.meta.outputs.version }}
BUILDTIME=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}
REVISION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }}

View File

@ -7,6 +7,10 @@ archives:
builds:
- main: "./cmd/exportarr"
binary: "exportarr"
ldflags:
- -s -w -X main.version={{.Version}} -X main.revision={{.Commit}} -X main.buildTime={{.CommitTimestamp}}
goarch:
- "386"
- "amd64"

View File

@ -2,6 +2,11 @@ FROM golang:1.20.5-alpine as builder
ARG TARGETOS
ARG TARGETARCH
ARG TARGETVARIANT=""
ARG VERSION="development"
ARG BUILDTIME=""
ARG REVISION=""
ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=${TARGETOS} \
@ -11,7 +16,7 @@ RUN apk add --no-cache ca-certificates tini-static \
&& update-ca-certificates
WORKDIR /build
COPY . .
RUN go build -a -tags netgo -ldflags '-w -extldflags "-static"' -o exportarr /build/cmd/exportarr/.
RUN go build -a -tags netgo -ldflags "-w -extldflags '-static' -X main.version=${VERSION} -X main.buildTime=${BUILDTIME} -X main.revision=${REVISION}" -o exportarr /build/cmd/exportarr/.
FROM gcr.io/distroless/static:nonroot
ENV PORT="9707"

View File

@ -2,6 +2,18 @@ package main
import "github.com/onedr0p/exportarr/internal/commands"
var (
appName = "exportarr"
version = "development"
buildTime = ""
revision = ""
)
func main() {
commands.Execute()
commands.Execute(commands.AppInfo{
Name: appName,
Version: version,
BuildTime: buildTime,
Revision: revision,
})
}

View File

@ -49,7 +49,7 @@ var radarrCmd = &cobra.Command{
c.ApiVersion = "v3"
UsageOnError(cmd, c.Validate())
serveHttp(func(r *prometheus.Registry) {
serveHttp(func(r prometheus.Registerer) {
r.MustRegister(
collector.NewRadarrCollector(c),
collector.NewQueueCollector(c),
@ -76,7 +76,7 @@ var sonarrCmd = &cobra.Command{
c.ApiVersion = "v3"
UsageOnError(cmd, c.Validate())
serveHttp(func(r *prometheus.Registry) {
serveHttp(func(r prometheus.Registerer) {
r.MustRegister(
collector.NewSonarrCollector(c),
collector.NewQueueCollector(c),
@ -102,7 +102,7 @@ var lidarrCmd = &cobra.Command{
c.ApiVersion = "v1"
UsageOnError(cmd, c.Validate())
serveHttp(func(r *prometheus.Registry) {
serveHttp(func(r prometheus.Registerer) {
r.MustRegister(
collector.NewLidarrCollector(c),
collector.NewQueueCollector(c),
@ -129,7 +129,7 @@ var readarrCmd = &cobra.Command{
c.ApiVersion = "v1"
UsageOnError(cmd, c.Validate())
serveHttp(func(r *prometheus.Registry) {
serveHttp(func(r prometheus.Registerer) {
r.MustRegister(
collector.NewReadarrCollector(c),
collector.NewQueueCollector(c),
@ -161,7 +161,7 @@ var prowlarrCmd = &cobra.Command{
UsageOnError(cmd, c.Validate())
UsageOnError(cmd, c.Prowlarr.Validate())
serveHttp(func(r *prometheus.Registry) {
serveHttp(func(r prometheus.Registerer) {
r.MustRegister(
collector.NewProwlarrCollector(c),
collector.NewHistoryCollector(c),

View File

@ -22,8 +22,8 @@ import (
var GRACEFUL_TIMEOUT = 5 * time.Second
var (
conf = &config.Config{}
conf = &config.Config{}
appInfo = &AppInfo{}
rootCmd = &cobra.Command{
Use: "exportarr",
Short: "exportarr is a AIO Prometheus exporter for *arr applications",
@ -36,7 +36,15 @@ More information available at the Github Repo (https://github.com/onedr0p/export
}
)
func Execute() error {
type AppInfo struct {
Name string
Version string
BuildTime string
Revision string
}
func Execute(a AppInfo) error {
appInfo = &a
return rootCmd.Execute()
}
@ -83,7 +91,13 @@ func initLogger() {
}
atom.SetLevel(lvl)
zap.S().Debug("Logger initialized")
zap.S().Infow(
fmt.Sprintf("Starting %s", appInfo.Name),
"app_name", appInfo.Name,
"version", appInfo.Version,
"buildTime", appInfo.BuildTime,
"revision", appInfo.Revision,
)
}
func finalizeLogger() {
@ -91,7 +105,7 @@ func finalizeLogger() {
zap.S().Sync()
}
type registerFunc func(registry *prometheus.Registry)
type registerFunc func(registry prometheus.Registerer)
func serveHttp(fn registerFunc) {
var srv http.Server
@ -116,6 +130,7 @@ func serveHttp(fn registerFunc) {
}()
registry := prometheus.NewRegistry()
registerAppInfoMetric(registry)
fn(registry)
mux := http.NewServeMux()
@ -140,3 +155,20 @@ func serveHttp(fn registerFunc) {
}
<-idleConnsClosed
}
func registerAppInfoMetric(registry prometheus.Registerer) {
registry.MustRegister(prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Namespace: appInfo.Name,
Name: "app_info",
Help: "A metric with a constant '1' value labeled by app name, version, build time, and revision.",
ConstLabels: prometheus.Labels{
"app_name": appInfo.Name,
"version": appInfo.Version,
"build_time": appInfo.BuildTime,
"revision": appInfo.Revision,
},
},
func() float64 { return 1 },
))
}

View File

@ -29,7 +29,7 @@ var sabnzbdCmd = &cobra.Command{
if err != nil {
return err
}
serveHttp(func(r *prometheus.Registry) {
serveHttp(func(r prometheus.Registerer) {
r.MustRegister(collector)
})
return nil