Skip to content

Commit

Permalink
Add Prometheus exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
fguillot committed Sep 28, 2020
1 parent 16b7b3b commit c394a61
Show file tree
Hide file tree
Showing 61 changed files with 809 additions and 96 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ windows-x86: generate
@ GOOS=windows GOARCH=386 go build -ldflags=$(LD_FLAGS) -o $(APP)-windows-x86 main.go

run: generate
@ go run main.go -debug
@ LOG_DATE_TIME=1 go run main.go -debug

clean:
@ rm -f $(APP)-* $(APP)
Expand Down
20 changes: 6 additions & 14 deletions cli/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"net/http"
"os"
"os/signal"
"runtime"
"syscall"
"time"

"miniflux.app/config"
"miniflux.app/logger"
"miniflux.app/metric"
"miniflux.app/reader/feed"
"miniflux.app/service/httpd"
"miniflux.app/service/scheduler"
Expand All @@ -32,8 +32,6 @@ func startDaemon(store *storage.Storage) {
feedHandler := feed.NewFeedHandler(store)
pool := worker.NewPool(feedHandler, config.Opts.WorkerPoolSize())

go showProcessStatistics()

if config.Opts.HasSchedulerService() && !config.Opts.HasMaintenanceMode() {
scheduler.Serve(store, pool)
}
Expand All @@ -43,6 +41,11 @@ func startDaemon(store *storage.Storage) {
httpServer = httpd.Serve(store, pool, feedHandler)
}

if config.Opts.HasMetricsCollector() {
collector := metric.NewCollector(store, config.Opts.MetricsRefreshInterval())
go collector.GatherStorageMetrics()
}

<-stop
logger.Info("Shutting down the process...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
Expand All @@ -54,14 +57,3 @@ func startDaemon(store *storage.Storage) {

logger.Info("Process gracefully stopped")
}

func showProcessStatistics() {
for {
var m runtime.MemStats
runtime.ReadMemStats(&m)
logger.Debug("Sys=%vK, InUse=%vK, HeapInUse=%vK, StackSys=%vK, StackInUse=%vK, GoRoutines=%d, NumCPU=%d",
m.Sys/1024, (m.Sys-m.HeapReleased)/1024, m.HeapInuse/1024, m.StackSys/1024, m.StackInuse/1024,
runtime.NumGoroutine(), runtime.NumCPU())
time.Sleep(30 * time.Second)
}
}
27 changes: 27 additions & 0 deletions config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ const (
defaultAuthProxyUserCreation = false
defaultMaintenanceMode = false
defaultMaintenanceMessage = "Miniflux is currently under maintenance"
defaultMetricsCollector = false
defaultMetricsRefreshInterval = 60
defaultMetricsAllowedNetworks = "127.0.0.1/8"
)

// Options contains configuration options.
Expand Down Expand Up @@ -106,6 +109,9 @@ type Options struct {
authProxyUserCreation bool
maintenanceMode bool
maintenanceMessage string
metricsCollector bool
metricsRefreshInterval int
metricsAllowedNetworks []string
}

// NewOptions returns Options with default values.
Expand Down Expand Up @@ -155,6 +161,9 @@ func NewOptions() *Options {
authProxyUserCreation: defaultAuthProxyUserCreation,
maintenanceMode: defaultMaintenanceMode,
maintenanceMessage: defaultMaintenanceMessage,
metricsCollector: defaultMetricsCollector,
metricsRefreshInterval: defaultMetricsRefreshInterval,
metricsAllowedNetworks: []string{defaultMetricsAllowedNetworks},
}
}

Expand Down Expand Up @@ -398,6 +407,21 @@ func (o *Options) IsAuthProxyUserCreationAllowed() bool {
return o.authProxyUserCreation
}

// HasMetricsCollector returns true if metrics collection is enabled.
func (o *Options) HasMetricsCollector() bool {
return o.metricsCollector
}

// MetricsRefreshInterval returns the refresh interval in seconds.
func (o *Options) MetricsRefreshInterval() int {
return o.metricsRefreshInterval
}

// MetricsAllowedNetworks returns the list of networks allowed to connect to the metrics endpoint.
func (o *Options) MetricsAllowedNetworks() []string {
return o.metricsAllowedNetworks
}

func (o *Options) String() string {
var builder strings.Builder
builder.WriteString(fmt.Sprintf("LOG_DATE_TIME: %v\n", o.logDateTime))
Expand Down Expand Up @@ -446,5 +470,8 @@ func (o *Options) String() string {
builder.WriteString(fmt.Sprintf("AUTH_PROXY_USER_CREATION: %v\n", o.authProxyUserCreation))
builder.WriteString(fmt.Sprintf("MAINTENANCE_MODE: %v\n", o.maintenanceMode))
builder.WriteString(fmt.Sprintf("MAINTENANCE_MESSAGE: %v\n", o.maintenanceMessage))
builder.WriteString(fmt.Sprintf("METRICS_COLLECTOR: %v\n", o.metricsCollector))
builder.WriteString(fmt.Sprintf("METRICS_REFRESH_INTERVAL: %v\n", o.metricsRefreshInterval))
builder.WriteString(fmt.Sprintf("METRICS_ALLOWED_NETWORKS: %v\n", o.metricsAllowedNetworks))
return builder.String()
}
20 changes: 20 additions & 0 deletions config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ func (p *Parser) parseLines(lines []string) (err error) {
p.opts.maintenanceMode = parseBool(value, defaultMaintenanceMode)
case "MAINTENANCE_MESSAGE":
p.opts.maintenanceMessage = parseString(value, defaultMaintenanceMessage)
case "METRICS_COLLECTOR":
p.opts.metricsCollector = parseBool(value, defaultMetricsCollector)
case "METRICS_REFRESH_INTERVAL":
p.opts.metricsRefreshInterval = parseInt(value, defaultMetricsRefreshInterval)
case "METRICS_ALLOWED_NETWORKS":
p.opts.metricsAllowedNetworks = parseStringList(value, []string{defaultMetricsAllowedNetworks})
}
}

Expand Down Expand Up @@ -244,6 +250,20 @@ func parseString(value string, fallback string) string {
return value
}

func parseStringList(value string, fallback []string) []string {
if value == "" {
return fallback
}

var strList []string
items := strings.Split(value, ",")
for _, item := range items {
strList = append(strList, strings.TrimSpace(item))
}

return strList
}

func readSecretFile(filename, fallback string) string {
data, err := ioutil.ReadFile(filename)
if err != nil {
Expand Down
11 changes: 7 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ module miniflux.app
require (
github.com/PuerkitoBio/goquery v1.5.1
github.com/coreos/go-oidc v2.2.1+incompatible
github.com/golang/protobuf v1.4.0 // indirect
github.com/gorilla/mux v1.8.0
github.com/lib/pq v1.8.0
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 // indirect
github.com/prometheus/client_golang v1.7.1
github.com/prometheus/common v0.14.0 // indirect
github.com/prometheus/procfs v0.2.0 // indirect
github.com/rylans/getlang v0.0.0-20200505200108-4c3188ff8a2d
github.com/stretchr/testify v1.6.1 // indirect
github.com/tdewolff/minify/v2 v2.9.5 // indirect
golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/net v0.0.0-20200625001655-4c5254603344
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c // indirect
golang.org/x/sys v0.0.0-20200926100807-9d91bd62050c // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/square/go-jose.v2 v2.5.0 // indirect
)

Expand Down
Loading

0 comments on commit c394a61

Please sign in to comment.