Skip to content

Commit

Permalink
fix: scrape and data format issues
Browse files Browse the repository at this point in the history
Signed-off-by: Janar Juusu <janar@juusujanar.eu>
  • Loading branch information
juusujanar committed Apr 22, 2024
1 parent 7d25679 commit 7bf5cf0
Show file tree
Hide file tree
Showing 11 changed files with 547 additions and 40 deletions.
10 changes: 7 additions & 3 deletions collector/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
)

const userAgent = "ttn-prometheus-exporter/1.0.0"
Expand All @@ -16,15 +17,18 @@ type GatewayData struct {
}

func GetInfo(client *http.Client, uri string, apiKey string, logger log.Logger) ([]GatewayData, error) {
gateways, err := getGatewayList(client, uri, apiKey, logger)
gateways, err := getGatewayList(client, uri, apiKey)
if err != nil {
return nil, err
}

allStats := []GatewayData{}
for _, gateway := range gateways {
for _, gateway := range *gateways {
gatewayID := gateway.IDs.GatewayID
gatewayStats, err := getGatewayStats(client, uri, apiKey, gatewayID, logger)
gatewayStats, err := getGatewayStats(client, uri, apiKey, gatewayID)
if err != nil {
_ = level.Warn(logger).Log("msg", "Failed to scrape gateway", "gatewayID", gatewayID, "err", err)
}
connected := err == nil
allStats = append(allStats, GatewayData{
GatewayID: gatewayID,
Expand Down
23 changes: 13 additions & 10 deletions collector/gateway_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"net/http"
"net/url"
"time"

"github.com/go-kit/log"
)

type Duration struct {
Expand All @@ -36,6 +34,7 @@ func (d *Duration) UnmarshalJSON(b []byte) error {
}

type GatewayStatsResponse struct {
Code int `json:"code"`
Protocol string `json:"protocol"`
LastStatus struct {
Time time.Time `json:"time"`
Expand All @@ -46,14 +45,14 @@ type GatewayStatsResponse struct {
Platform *string `json:"platform"`
Station *string `json:"station"`
} `json:"versions"`
IP string `json:"ip"`
IP []string `json:"ip"`
Metrics struct {
Ackr int64 `json:"ackr"`
TxIn int64 `json:"txin"`
TxOk int64 `json:"txok"`
RxIn int64 `json:"rxin"`
RxOk int64 `json:"rxok"`
RxFw int64 `json:"rxfw"`
Ackr float64 `json:"ackr"`
TxIn float64 `json:"txin"`
TxOk float64 `json:"txok"`
RxIn float64 `json:"rxin"`
RxOk float64 `json:"rxok"`
RxFw float64 `json:"rxfw"`
} `json:"metrics"`
} `json:"last_status"`
UplinkCount string `json:"uplink_count"`
Expand All @@ -70,7 +69,7 @@ type GatewayStatsResponse struct {
} `json:"gateway_remote_address"`
}

func getGatewayStats(client *http.Client, uri string, apiKey string, gatewayID string, logger log.Logger) (*GatewayStatsResponse, error) {
func getGatewayStats(client *http.Client, uri string, apiKey string, gatewayID string) (*GatewayStatsResponse, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, err
Expand Down Expand Up @@ -110,5 +109,9 @@ func getGatewayStats(client *http.Client, uri string, apiKey string, gatewayID s
return nil, err
}

if gatewayStats.Code != 0 {
return nil, fmt.Errorf("Gateway stats response code is %d", gatewayStats.Code)
}

return gatewayStats, nil
}
6 changes: 2 additions & 4 deletions collector/gateways_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"io"
"net/http"
"net/url"

"github.com/go-kit/log"
)

type Gateway struct {
Expand All @@ -22,7 +20,7 @@ type GatewayListResponse struct {
Gateways []Gateway `json:"gateways"`
}

func getGatewayList(client *http.Client, uri string, apiKey string, logger log.Logger) ([]Gateway, error) {
func getGatewayList(client *http.Client, uri string, apiKey string) (*[]Gateway, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, err
Expand Down Expand Up @@ -63,5 +61,5 @@ func getGatewayList(client *http.Client, uri string, apiKey string, logger log.L
return nil, err
}

return gatewayList.Gateways, nil
return &gatewayList.Gateways, nil
}
61 changes: 38 additions & 23 deletions ttn_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/go-kit/log/level"
"github.com/juusujanar/ttn-exporter/collector"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
Expand All @@ -34,7 +35,7 @@ var (
gatewayConnected = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "gateway", "connected"),
"Gateway connection status",
[]string{"gateway_id", "name"}, nil,
[]string{"gateway_id", "name", "protocol"}, nil,
)
uplinkCount = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "gateway", "uplink_count"),
Expand Down Expand Up @@ -148,29 +149,40 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) (up float64) {
e.parseVersion(ch, data)

for _, gw := range data {
if gw.Stats != nil {
ch <- prometheus.MustNewConstMetric(gatewayConnected, prometheus.GaugeValue, BoolToFloat(gw.Connected), gw.GatewayID, gw.Name)
uplinkCountFloat, err := strconv.ParseFloat(gw.Stats.UplinkCount, 64)
if err != nil {
_ = level.Error(e.logger).Log("msg", "Failed to convert UplinkCount to float64", "err", err)
if gw.Connected {
ch <- prometheus.MustNewConstMetric(gatewayConnected, prometheus.GaugeValue, BoolToFloat(gw.Connected), gw.GatewayID, gw.Name, gw.Stats.Protocol)
if gw.Stats.UplinkCount != "" {
uplinkCountFloat, err := strconv.ParseFloat(gw.Stats.UplinkCount, 64)
if err != nil {
_ = level.Error(e.logger).Log("msg", "Failed to convert UplinkCount to float64", "err", err, "value", gw.Stats.UplinkCount)
}
ch <- prometheus.MustNewConstMetric(uplinkCount, prometheus.CounterValue, uplinkCountFloat, gw.GatewayID, gw.Name)
}
downlinkCountFloat, err := strconv.ParseFloat(gw.Stats.DownlinkCount, 64)
if err != nil {
_ = level.Error(e.logger).Log("msg", "Failed to convert DownlinkCount to float64", "err", err)

if gw.Stats.DownlinkCount != "" {
downlinkCountFloat, err := strconv.ParseFloat(gw.Stats.DownlinkCount, 64)
if err != nil {
_ = level.Error(e.logger).Log("msg", "Failed to convert DownlinkCount to float64", "err", err, "value", gw.Stats.DownlinkCount)
}
ch <- prometheus.MustNewConstMetric(downlinkCount, prometheus.CounterValue, downlinkCountFloat, gw.GatewayID, gw.Name)
}
txAcknowledgementCountFloat, err := strconv.ParseFloat(gw.Stats.TxAcknowledgementCount, 64)
if err != nil {
_ = level.Error(e.logger).Log("msg", "Failed to convert TxAcknowledgementCount to float64", "err", err)

if gw.Stats.TxAcknowledgementCount != "" {
txAcknowledgementCountFloat, err := strconv.ParseFloat(gw.Stats.TxAcknowledgementCount, 64)
if err != nil {
_ = level.Error(e.logger).Log("msg", "Failed to convert TxAcknowledgementCount to float64", "err", err, "value", gw.Stats.TxAcknowledgementCount)
}
ch <- prometheus.MustNewConstMetric(txAcknowledgementCount, prometheus.CounterValue, txAcknowledgementCountFloat, gw.GatewayID, gw.Name)
}

if gw.Stats.RoundTripTimes != nil {
ch <- prometheus.MustNewConstMetric(roundTripMin, prometheus.GaugeValue, gw.Stats.RoundTripTimes.Min.Seconds(), gw.GatewayID, gw.Name)
ch <- prometheus.MustNewConstMetric(roundTripMax, prometheus.GaugeValue, gw.Stats.RoundTripTimes.Max.Seconds(), gw.GatewayID, gw.Name)
ch <- prometheus.MustNewConstMetric(roundTripMedian, prometheus.GaugeValue, gw.Stats.RoundTripTimes.Median.Seconds(), gw.GatewayID, gw.Name)
ch <- prometheus.MustNewConstMetric(roundTripCount, prometheus.GaugeValue, float64(gw.Stats.RoundTripTimes.Count), gw.GatewayID, gw.Name)
}
ch <- prometheus.MustNewConstMetric(uplinkCount, prometheus.CounterValue, uplinkCountFloat, gw.GatewayID, gw.Name)
ch <- prometheus.MustNewConstMetric(downlinkCount, prometheus.CounterValue, downlinkCountFloat, gw.GatewayID, gw.Name)
ch <- prometheus.MustNewConstMetric(txAcknowledgementCount, prometheus.CounterValue, txAcknowledgementCountFloat, gw.GatewayID, gw.Name)
ch <- prometheus.MustNewConstMetric(roundTripMin, prometheus.GaugeValue, gw.Stats.RoundTripTimes.Min.Seconds(), gw.GatewayID, gw.Name)
ch <- prometheus.MustNewConstMetric(roundTripMax, prometheus.GaugeValue, gw.Stats.RoundTripTimes.Max.Seconds(), gw.GatewayID, gw.Name)
ch <- prometheus.MustNewConstMetric(roundTripMedian, prometheus.GaugeValue, gw.Stats.RoundTripTimes.Median.Seconds(), gw.GatewayID, gw.Name)
ch <- prometheus.MustNewConstMetric(roundTripCount, prometheus.GaugeValue, float64(gw.Stats.RoundTripTimes.Count), gw.GatewayID, gw.Name)
} else {
ch <- prometheus.MustNewConstMetric(gatewayConnected, prometheus.GaugeValue, 0, gw.GatewayID, gw.Name)
ch <- prometheus.MustNewConstMetric(gatewayConnected, prometheus.GaugeValue, 0, gw.GatewayID, gw.Name, "")
}
}
return 1
Expand Down Expand Up @@ -228,10 +240,13 @@ func main() {
os.Exit(1)
}
registry := prometheus.NewRegistry()
registry.MustRegister(exporter)
registry.MustRegister(version.NewCollector("ttn_exporter"))
registry.MustRegister(
collectors.NewGoCollector(),
exporter,
version.NewCollector("ttn_exporter"),
)

http.Handle(*metricsPath, promhttp.Handler())
http.Handle(*metricsPath, promhttp.HandlerFor(registry, promhttp.HandlerOpts{Registry: registry}))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`<html>
<head><title>TTN Exporter</title></head>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7bf5cf0

Please sign in to comment.