Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.18.2 as build
FROM golang:1.24 AS build

COPY ./ /go/src/github.com/chatwork/sendgrid-stats-exporter
WORKDIR /go/src/github.com/chatwork/sendgrid-stats-exporter
Expand Down
55 changes: 50 additions & 5 deletions collect.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package main

import (
"log/slog"
"time"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/jinzhu/now"
"github.com/prometheus/client_golang/prometheus"
)

type Collector struct {
logger log.Logger
logger *slog.Logger

// account metrics
accountType *prometheus.Desc
reputation *prometheus.Desc

// statistics metrics
blocks *prometheus.Desc
bounceDrops *prometheus.Desc
bounces *prometheus.Desc
Expand All @@ -30,10 +34,23 @@ type Collector struct {
unsubscribes *prometheus.Desc
}

func collector(logger log.Logger) *Collector {
func collector(logger *slog.Logger) *Collector {
return &Collector{
logger: logger,

accountType: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "account_type"),
"The type of account for this user, free or paid",
[]string{"user_name", "account_type"},
nil,
),
reputation: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "reputation"),
"The sender reputation for this user.",
[]string{"user_name"},
nil,
),

blocks: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "blocks"),
"blocks",
Expand Down Expand Up @@ -134,6 +151,34 @@ func collector(logger log.Logger) *Collector {
}

func (c *Collector) Collect(ch chan<- prometheus.Metric) {
c.collectAccount(ch)
c.collectStatistics(ch)
}

func (c *Collector) collectAccount(ch chan<- prometheus.Metric) {
account, err := getAccount()
if err != nil {
c.logger.Error("err", err.Error())
return
}

ch <- prometheus.MustNewConstMetric(
c.accountType,
prometheus.UntypedValue,
1,
*sendGridUserName,
account.Type, // send account type as label value
)

ch <- prometheus.MustNewConstMetric(
c.reputation,
prometheus.GaugeValue,
float64(account.Reputation),
*sendGridUserName,
)
}

func (c *Collector) collectStatistics(ch chan<- prometheus.Metric) {
var today time.Time

if *location != "" && *timeOffset != 0 {
Expand All @@ -150,7 +195,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {

statistics, err := collectByDate(queryDate, today)
if err != nil {
level.Error(c.logger).Log(err)
c.logger.Error("err", err)

return
}
Expand Down
32 changes: 16 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
module github.com/chatwork/sendgrid-stats-exporter

go 1.18
go 1.24.0

require (
github.com/go-kit/kit v0.12.0
github.com/alecthomas/kingpin/v2 v2.4.0
github.com/jinzhu/now v1.1.5
github.com/prometheus/client_golang v1.12.2
github.com/prometheus/common v0.34.0
gopkg.in/alecthomas/kingpin.v2 v2.2.6
github.com/prometheus/client_golang v1.23.2
github.com/prometheus/common v0.67.4
github.com/sendgrid/sendgrid-go v3.16.1+incompatible
)

require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
github.com/alecthomas/units v0.0.0-20240927000941-0f3dac36c52b // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/go-kit/log v0.2.0 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
google.golang.org/protobuf v1.27.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus/client_model v0.6.2 // indirect
github.com/prometheus/procfs v0.19.2 // indirect
github.com/sendgrid/rest v2.6.9+incompatible // indirect
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
go.yaml.in/yaml/v2 v2.4.3 // indirect
golang.org/x/sys v0.38.0 // indirect
google.golang.org/protobuf v1.36.10 // indirect
)
Loading