Skip to content

Commit

Permalink
Merge pull request #108 from whywaita/fix/106
Browse files Browse the repository at this point in the history
Switch to dispatcher-worker method
  • Loading branch information
whywaita authored Sep 30, 2021
2 parents 01a1029 + de6e82c commit 9e3ca6d
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 104 deletions.
3 changes: 3 additions & 0 deletions docs/01_01_for_admin_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,8 @@ $ ./myshoes
- `STRICT`
- default: true
- set strict mode
- `MAX_CONNECTIONS_TO_BACKEND`
- default: 50
- The number of max connections to shoes-provider

and more some env values from [shoes provider](https://github.com/whywaita/myshoes-providers).
6 changes: 4 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ type conf struct {
ShoesPluginPath string
RunnerUser string

Debug bool
Strict bool // check to registered runner before delete job
Debug bool
Strict bool // check to registered runner before delete job
MaxConnectionsToBackend int64
}

// Config Environment keys
Expand All @@ -32,4 +33,5 @@ const (
EnvShoesPluginPath = "PLUGIN"
EnvDebug = "DEBUG"
EnvStrict = "STRICT"
EnvMaxConnectionsToBackend = "MAX_CONNECTIONS_TO_BACKEND"
)
9 changes: 9 additions & 0 deletions internal/config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ func Load() {
if os.Getenv(EnvStrict) == "false" {
Config.Strict = false
}

Config.MaxConnectionsToBackend = 50
if os.Getenv(EnvMaxConnectionsToBackend) != "" {
numberPB, err := strconv.ParseInt(os.Getenv(EnvMaxConnectionsToBackend), 10, 64)
if err != nil {
log.Panicf("failed to convert int64 %s: %+v", EnvMaxConnectionsToBackend, err)
}
Config.MaxConnectionsToBackend = numberPB
}
}

func checkBinary(p string) (string, error) {
Expand Down
1 change: 1 addition & 0 deletions pkg/metric/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type Scraper interface {
func NewScrapers() []Scraper {
return []Scraper{
ScraperDatastore{},
ScraperMemory{},
}
}

Expand Down
75 changes: 75 additions & 0 deletions pkg/metric/scrape_memory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package metric

import (
"context"
"fmt"

"github.com/prometheus/client_golang/prometheus"

"github.com/whywaita/myshoes/internal/config"
"github.com/whywaita/myshoes/pkg/datastore"
"github.com/whywaita/myshoes/pkg/starter"
)

const memoryName = "memory"

var (
memoryStarterMaxRunning = prometheus.NewDesc(
prometheus.BuildFQName(namespace, memoryName, "starter_max_running"),
"The number of max running in starter (Config)",
[]string{"starter"}, nil,
)
memoryStarterQueueRunning = prometheus.NewDesc(
prometheus.BuildFQName(namespace, memoryName, "starter_queue_running"),
"running queue in starter",
[]string{"starter"}, nil,
)
memoryStarterQueueWaiting = prometheus.NewDesc(
prometheus.BuildFQName(namespace, memoryName, "starter_queue_waiting"),
"waiting queue in starter",
[]string{"starter"}, nil,
)
)

// ScraperMemory is scraper implement for memory
type ScraperMemory struct{}

// Name return name
func (ScraperMemory) Name() string {
return memoryName
}

// Help return help
func (ScraperMemory) Help() string {
return "Collect from memory"
}

// Scrape scrape metrics
func (ScraperMemory) Scrape(ctx context.Context, ds datastore.Datastore, ch chan<- prometheus.Metric) error {
if err := scrapeStarterValues(ch); err != nil {
return fmt.Errorf("failed to scrape starter values: %w", err)
}

return nil
}

func scrapeStarterValues(ch chan<- prometheus.Metric) error {
configMax := config.Config.MaxConnectionsToBackend

const labelStarter = "starter"

ch <- prometheus.MustNewConstMetric(
memoryStarterMaxRunning, prometheus.GaugeValue, float64(configMax), labelStarter)

countRunning := starter.CountRunning
countWaiting := starter.CountWaiting

ch <- prometheus.MustNewConstMetric(
memoryStarterQueueRunning, prometheus.GaugeValue, float64(countRunning), labelStarter)
ch <- prometheus.MustNewConstMetric(
memoryStarterQueueWaiting, prometheus.GaugeValue, float64(countWaiting), labelStarter)

return nil
}

var _ Scraper = ScraperMemory{}
Loading

0 comments on commit 9e3ca6d

Please sign in to comment.