Skip to content

Commit

Permalink
Updated influxdb
Browse files Browse the repository at this point in the history
  • Loading branch information
djthorpe committed Dec 12, 2020
1 parent b38500b commit c6b2000
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
8 changes: 4 additions & 4 deletions etc/systemd/argonone.env
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ DEBUG=false
# when empty string no measurements are written
MEASUREMENT="cpufan"

# Set host and database to write metrics to
INFLUXDB="localhost/metrics"


# Set host, database, username and password to write metrics to
INFLUX_DB="https://localhost/metrics"
INFLUX_USERNAME=""
INFLUX_PASSWORD=""
2 changes: 1 addition & 1 deletion etc/systemd/argonone.service
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ EnvironmentFile=/opt/gopi/etc/argonone.env
ExecStart=/opt/gopi/bin/argonone \
-debug=${DEBUG} \
-argonone.measurement=${MEASUREMENT} \
-influxdb.url=${INFLUXDB}
-influxdb.url=${INFLUX_DB}
Restart=on-failure
12 changes: 10 additions & 2 deletions pkg/db/influxdb/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -54,6 +55,9 @@ const (
DefaultScheme = "http"
DefaultEndpoint = DefaultScheme + "://localhost/metrics"
DefaultPort = 8086

EnvUsername = "INFLUX_USERNAME"
EnvPassword = "INFLUX_PASSWORD"
)

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -294,6 +298,8 @@ func parseUrl(value string) (endpoint, error) {
// Check various styles
if value == "" {
value = DefaultEndpoint
} else if strings.HasPrefix(value, "http://") || strings.HasPrefix(value, "https://") {
// Skip
} else if host, port, err := net.SplitHostPort(value); err == nil {
value = DefaultScheme + "://" + host
if port != "" {
Expand Down Expand Up @@ -330,12 +336,14 @@ func parseUrl(value string) (endpoint, error) {
} else if db, err := parseDatabase(u); err != nil {
return endpoint{}, err
} else {
user, password := "", ""
if u.User != nil {
user := os.Getenv(EnvUsername)
password := os.Getenv(EnvPassword)
if u.User != nil && u.User.Username() != "" {
user = u.User.Username()
password, _ = u.User.Password()
}
u.Path = "/"
u.User = nil
return endpoint{*u, db, user, password}, nil
}
}
Expand Down
24 changes: 24 additions & 0 deletions pkg/db/influxdb/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,27 @@ func Test_Writer_008(t *testing.T) {
}
})
}

func Test_Writer_009(t *testing.T) {
tool.Test(t, []string{"-influxdb.url=https://rpi4b/metrics"}, new(WriterApp), func(app *WriterApp) {
if ep := app.Writer.Endpoint(); ep == nil {
t.Error("Unexpected nil")
} else if ep.String() != "https://rpi4b:8086/" {
t.Error("Unexpected endpoint", ep)
} else {
t.Log("endpoint=", ep)
}
})
}

func Test_Writer_010(t *testing.T) {
tool.Test(t, []string{"-influxdb.url=http://user:pass@rpi4:99/metrics"}, new(WriterApp), func(app *WriterApp) {
if ep := app.Writer.Endpoint(); ep == nil {
t.Error("Unexpected nil")
} else if ep.User.String() != "" {
t.Error("Unexpected username/password", ep)
} else {
t.Log("endpoint=", ep)
}
})
}

0 comments on commit c6b2000

Please sign in to comment.