Skip to content

Commit

Permalink
[rk-prom] Refactor based on rk-boot requirements [commit:dongxuny]
Browse files Browse the repository at this point in the history
  • Loading branch information
dongxuny committed Aug 9, 2020
1 parent 8cb95af commit 1f2a803
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 30 deletions.
2 changes: 1 addition & 1 deletion example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {
rk_prom.SetZapLogger(logger)

// start prom on local
server := rk_prom.StartProm("1608", "/metrics")
server, _ := rk_prom.StartProm(1608, "/metrics")

defer func() {
err := server.Shutdown(context.Background())
Expand Down
40 changes: 21 additions & 19 deletions initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,66 @@
package rk_prom

import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
"net/http"
"strconv"
"strings"
)

var (
logger = zap.NewNop()
// Why 1608? It is the year of first telescope was invented
DefaultPort = "1608"
DefaultPath = "/metrics"
defaultPort = 1608
defaultPath = "/metrics"
)

// Register collectors in default registry
func RegisterCollectors(c ...prometheus.Collector) {
prometheus.MustRegister(c...)
}

func StartProm(port, path string) *http.Server {
func StartProm(port uint64, path string) (*http.Server, error) {
// Trim space by default
port = strings.TrimSpace(port)
path = strings.TrimSpace(path)

if len(port) < 1 {
logger.Warn(fmt.Sprintf("port is empty, using default port:%s", DefaultPort))
port = DefaultPort
if len(path) < 1 {
// Invalid path, use default one
logger.Warn("invalid path, using default path",
zap.String("path", defaultPath))
path = defaultPath
}

if len(path) < 1 || !strings.HasPrefix(path, "/") {
// Invalid path, use default one
logger.Warn(fmt.Sprintf("invalid path, using default path:%s", DefaultPath))
path = DefaultPath
if !strings.HasPrefix(path, "/") {
path = "/" + path
}

// Register by default
err := prometheus.Register(ProcessCollector)
if err != nil {
logger.Warn(fmt.Sprintf("failed to register collector, %v", err))
logger.Error("failed to register collector",
zap.Error(err))
return nil, err
}

httpMux := http.NewServeMux()
httpMux.Handle(path, promhttp.Handler())

server := &http.Server{
Addr: "0.0.0.0:" + port,
Addr: "0.0.0.0:" + strconv.FormatUint(port, 10),
Handler: httpMux,
}

go func() {
err := server.ListenAndServe()
if err != nil {
logger.Error(fmt.Sprintf("failed to serving prometheus client, %v", err))
}
logger.Info("starting prometheus client,",
zap.Uint64("port", port),
zap.String("path", path))

server.ListenAndServe()
}()

return server
return server, err
}

func SetZapLogger(in *zap.Logger) {
Expand Down
18 changes: 9 additions & 9 deletions metrics_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
)

const (
MaxKeyLength = 128
Separator = "::"
NamespaceDefault = "rk"
SubSystemDefault = "service"
maxKeyLength = 128
separator = "::"
namespaceDefault = "rk"
subSystemDefault = "service"
)

var SummaryObjectives = map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001, 0.999: 0.0001}
Expand All @@ -34,11 +34,11 @@ type MetricsSet struct {

func NewMetricsSet(namespace, subSystem string) *MetricsSet {
if len(namespace) < 1 {
namespace = NamespaceDefault
namespace = namespaceDefault
}

if len(subSystem) < 1 {
subSystem = SubSystemDefault
subSystem = subSystemDefault
}

metrics := MetricsSet{
Expand Down Expand Up @@ -638,7 +638,7 @@ func (set *MetricsSet) getKey(name string) string {
key := strings.Join([]string{
set.namespace,
set.subSystem,
name}, Separator)
name}, separator)

return key
}
Expand All @@ -655,8 +655,8 @@ func validateRawName(name string) error {
return errors.New(errMsg)
}

if len(name) > MaxKeyLength {
errMsg := fmt.Sprintf("exceed max name length:%d", MaxKeyLength)
if len(name) > maxKeyLength {
errMsg := fmt.Sprintf("exceed max name length:%d", maxKeyLength)
return errors.New(errMsg)
}

Expand Down
10 changes: 9 additions & 1 deletion publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

// Prometheus metrics publisher
type Publisher interface {
Start() error
Start()

IsRunning() bool

Expand All @@ -28,6 +28,8 @@ type Publisher interface {
type PushGatewayPublisher struct {
pusher *push.Pusher
interval time.Duration
url string
jobName string
isRunning bool
lock *sync.Mutex
}
Expand All @@ -48,6 +50,8 @@ func NewPushGatewayPublisher(interval time.Duration, url, jobName string) (*Push
return &PushGatewayPublisher{
interval: interval,
pusher: push.New(url, jobName),
url: url,
jobName: jobName,
lock: &sync.Mutex{},
isRunning: false,
}, nil
Expand All @@ -59,6 +63,10 @@ func (pub *PushGatewayPublisher) Start() {

pub.isRunning = true

logger.Info("starting pushGateway publisher,",
zap.String("remote_addr", pub.url),
zap.String("job_name", pub.jobName))

go pub.publish()
}

Expand Down

0 comments on commit 1f2a803

Please sign in to comment.