Skip to content

Commit

Permalink
fix: bump d2r2/go-logger version
Browse files Browse the repository at this point in the history
  • Loading branch information
lukibahr committed Oct 4, 2023
1 parent 658a977 commit 339eb79
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 27 deletions.
31 changes: 30 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
project_name: Prometheus-BME280-exporter
before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy
builds:
- env: [CGO_ENABLED=0]
goos:
Expand All @@ -7,11 +11,36 @@ builds:
- amd64
- arm64
- arm
ldflags:
- "-X 'github.com/lukibahr/prometheus-bme280-exporter/cmd.buildVersion={{.Version}}'"
- "-X 'github.com/lukibahr/prometheus-bme280-exporter/cmd.buildCommit={{.ShortCommit}}'"
archives:
- replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ incpatch .Version }}-next"
changelog:
sort: asc
filters:
exclude:
- "^docs:"
- "^test:"
- Merge pull request
- Merge branch
dockers:
- image_templates: ["ghcr.io/lukibahr/Prometheus-BME280-exporter:{{ .Version }}"]
dockerfile: Dockerfile
build_flag_templates:
- --label=org.opencontainers.image.source=github.com/lukibahr/Prometheus-BME280-exporter
- --label=org.opencontainers.image.version={{ .Version }}
- --label=org.opencontainers.image.created={{ time "2006-01-02T15:04:05Z07:00" }}
- --label=org.opencontainers.image.revision={{ .FullCommit }}
- --label=org.opencontainers.image.revision={{ .FullCommit }}
- --label=org.opencontainers.image.title={{ .ProjectName }}
- --label=org.opencontainers.image.description={{ .ProjectName }}
- --label=org.opencontainers.image.url=https://github.com/lukibahr/rclone-daemon
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ cleanup:

index:
$(CR) index --charts-repo $(GHBASE)$(REPO)

armv6:
CC=arm-linux-gnueabi-gcc GOOS=linux GOARM=6 GOARCH=arm go build -o prometheus-bme280-exporter main.go
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,11 @@ For a more customizable setup, refer to the helm chart.
- Releaser action used for releasing the charts: [https://github.com/helm/chart-releaser-action](https://github.com/helm/chart-releaser-action)
- Chart releaser used for publishing helm chart: [https://github.com/helm/chart-releaser](https://github.com/helm/chart-releaser)
## Rebiulding
- rebuilding with mqtt: <https://github.com/hessu/mqtt-bme280/blob/master/mqtt-bme280.py>
## Open ToDos
- create CI/CD Pipeline with drone
44 changes: 44 additions & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# I just wanted to give taskfile.dev another try. This is optional. Get task here https://taskfile.dev/#/
# Next create a .env file

version: '3'

vars:
GOLANGCILINT:
sh: which golangci-lint
GOLANG:
sh: which go

tasks:
default:
cmds:
- task --list
silent: true

lint:
desc: Run golangci-lint on your sourcecode
preconditions:
- sh: "[ '{{.GOLANGCILINT}}' != '<no value>' ]"
msg: "golangci-lint executable not found"
cmds:
- |
golangci-lint run -v
silent: false

build:
desc: build binary
preconditions:
- sh: "[ '{{.GOLANG}}' != '<no value>' ]"
msg: "go executable not found"
cmds:
- |
GOOS=linux GOARCH=arm GOARM=7 go build
GOOS=darwin GOARCH=arm64 go build -ldflags="-X 'github.com/lukibahr/prometheus-bme280-exporter/cmd.buildVersion=v0.0.3' -X 'github.com/lukibahr/prometheus-bme280-exporter/cmd.buildCommit=$(git rev-parse --short HEAD)'" -o release/rclone-daemon-darwin-arm64 main.go
silent: false

test:
desc: Run go test on your sourcecode
cmds:
- |
go test -v
silent: false
26 changes: 21 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import (
"github.com/spf13/cobra"
)

var (
buildVersion string
buildCommit string
)

var rootCmd = &cobra.Command{
Use: "prometheus-bme280-exporter",
Short: "Export metrics from a Bosh BME280 sensor",
Expand All @@ -28,17 +33,19 @@ var rootCmd = &cobra.Command{
},
}

//Execute runs the toor command
// Execute runs the toor command
func Execute() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}

func init() {
rootCmd.PersistentFlags().StringP("loglevel", "l", "info", "Sets loglevel")
rootCmd.Flags().StringP("loglevel", "l", "info", "Sets loglevel")
rootCmd.Flags().StringP("port", "p", "9123", "Sets the port the exporter listens to")
rootCmd.Flags().StringP("accuracy", "a", "ACCURACY_STANDARD", "Sets the sampling rate of the metric")
rootCmd.Flags().StringP("environment", "e", "dev", "set the environment")
rootCmd.Flags().String("location", "local", "sets the location of the exporter")
}

func setLoglevel(level string) {
Expand All @@ -57,8 +64,7 @@ func setLoglevel(level string) {
}

func runRoot(cmd *cobra.Command) error {
c := collectors.NewBMECollector()
prometheus.MustRegister(c)
log.Infof("prometheus-bme280-exporter version %s, commit %s", buildVersion, buildCommit)

port, err := cmd.Flags().GetString("port")
if err != nil {
Expand All @@ -72,8 +78,18 @@ func runRoot(cmd *cobra.Command) error {
if err != nil {
return err
}
conf := config.New(port, accuracy, loglevel)
environment, err := cmd.Flags().GetString("environment")
if err != nil {
return err
}
location, err := cmd.Flags().GetString("location")
if err != nil {
return err
}
conf := config.New(port, accuracy, loglevel, environment, location)
setLoglevel(conf.Loglevel)
c := collectors.NewBMECollector()
prometheus.MustRegister(c)
http.HandleFunc("/", handlers.IndexHandler)
http.HandleFunc("/health", handlers.HealthHandler)
http.Handle("/metrics", promhttp.Handler())
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module github.com/lukibahr/Prometheus-BME280-exporter

go 1.15
go 1.20.2

require (
github.com/antonfisher/nested-logrus-formatter v1.3.1
github.com/d2r2/go-bsbmp v0.0.0-20190515110334-3b4b3aea8375
github.com/d2r2/go-i2c v0.0.0-20191123181816-73a8a799d6bc
github.com/d2r2/go-logger v0.0.0-20181221090742-9998a510495e
//github.com/d2r2/go-logger v0.0.0-20181221090742-9998a510495e
github.com/d2r2/go-logger v0.0.0-20210606094344-60e9d1233e22

github.com/prometheus/client_golang v1.12.2
github.com/shiena/ansicolor v0.0.0-20230509054315-a9deabde6e02
github.com/sirupsen/logrus v1.9.3
Expand Down
26 changes: 14 additions & 12 deletions pkg/collectors/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"github.com/d2r2/go-bsbmp"
"github.com/d2r2/go-i2c"
"github.com/d2r2/go-logger"
"github.com/lukibahr/Prometheus-BME280-exporter/pkg/config"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)

//BMECollector defines the struct for the collector that contains pointers
//to prometheus descriptors for each metric you wish to expose.
// BMECollector defines the struct for the collector that contains pointers
// to prometheus descriptors for each metric you wish to expose.
type BMECollector struct {
temperature *prometheus.Desc
humidity *prometheus.Desc
Expand All @@ -20,33 +21,33 @@ type BMECollector struct {
altitude *prometheus.Desc
}

//NewBMECollector is the constructor for every descriptor and returns a pointer to the collector
// NewBMECollector is the constructor for every descriptor and returns a pointer to the collector
func NewBMECollector() *BMECollector {
return &BMECollector{
temperature: prometheus.NewDesc("bme280_temperature_celcius",
"Returns the measured temperature in celsius",
[]string{"sensor_id", "hostname"}, nil,
[]string{"sensor_id", "hostname", "environment", "location"}, nil,
),
humidity: prometheus.NewDesc("bme280_humidity_percent",
"Returns the measured humidity in percent",
[]string{"sensor_id", "hostname"}, nil,
[]string{"sensor_id", "hostname", "environment", "location"}, nil,
),
pressureMg: prometheus.NewDesc("bme280_pressure_mmHg",
"Returns the measured and calculated air pressure in mmHg (millimeter of mercury)",
[]string{"sensor_id", "hostname"}, nil,
[]string{"sensor_id", "hostname", "environment", "location"}, nil,
),
pressureHPa: prometheus.NewDesc("bme280_pressure_hpa",
"Returns the measured and calculated air pressure in hPA (Pascal)",
[]string{"sensor_id", "hostname"}, nil,
[]string{"sensor_id", "hostname", "environment", "location"}, nil,
),
altitude: prometheus.NewDesc("bme280_altitude_meters",
"Returns shows the measured altitude in meters above sea level (101325 Pa)",
[]string{"sensor_id", "hostname"}, nil,
[]string{"sensor_id", "hostname", "environment", "location"}, nil,
),
}
}

//Describe implements the Describe function of the collector
// Describe implements the Describe function of the collector
func (collector *BMECollector) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.temperature
ch <- collector.humidity
Expand All @@ -55,7 +56,7 @@ func (collector *BMECollector) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.altitude
}

//Collect implements required collect function for all prometheus collectors
// Collect implements required collect function for all prometheus collectors
func (collector *BMECollector) Collect(ch chan<- prometheus.Metric) {
//temporarily used, more a smell than a feature
i2cerr := logger.ChangePackageLogLevel("i2c", logger.InfoLevel)
Expand Down Expand Up @@ -110,8 +111,9 @@ func (collector *BMECollector) Collect(ch chan<- prometheus.Metric) {
log.Fatal(err)
}
}

ch <- prometheus.MustNewConstMetric(collector.temperature, prometheus.GaugeValue, float64(t), string(id), hostname)
location := config.Config.Location
environment := config.Config.Environment
ch <- prometheus.MustNewConstMetric(collector.temperature, prometheus.GaugeValue, float64(t), string(id), hostname, environment, location)
ch <- prometheus.MustNewConstMetric(collector.humidity, prometheus.GaugeValue, float64(h), string(id), hostname)
ch <- prometheus.MustNewConstMetric(collector.pressureMg, prometheus.GaugeValue, float64(pMg), string(id), hostname)
ch <- prometheus.MustNewConstMetric(collector.pressureHPa, prometheus.GaugeValue, float64(pHPa), string(id), hostname)
Expand Down
18 changes: 11 additions & 7 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package config

type Config struct {
Port string
Accuracy string
Loglevel string
Port string
Accuracy string
Loglevel string
Environment string
Location string
}

// New creates a new Config object
func New(port string, accuracy, loglevel string) *Config {
func New(port, accuracy, loglevel, environment, location string) *Config {
return &Config{
Port: port,
Accuracy: accuracy,
Loglevel: loglevel,
Port: port,
Accuracy: accuracy,
Loglevel: loglevel,
Environment: environment,
Location: location,
}
}

0 comments on commit 339eb79

Please sign in to comment.