-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from systemli/metrics
Add support for basic metric exporting
- Loading branch information
Showing
8 changed files
with
144 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package api | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/prometheus/client_golang/prometheus" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
reqCnt = prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
Name: "http_requests_total", | ||
Help: "The total number of requests", | ||
}, []string{"handler", "origin", "code"}) | ||
|
||
reqDur = prometheus.NewSummaryVec(prometheus.SummaryOpts{ | ||
Name: "http_request_duration_seconds", | ||
Help: "The HTTP requests latency in seconds", | ||
}, []string{"handler", "origin", "code"}) | ||
) | ||
|
||
// NewPrometheus returns the Gin Middleware for collecting basic http metrics. | ||
func NewPrometheus() gin.HandlerFunc { | ||
err := prometheus.Register(reqCnt) | ||
if err != nil { | ||
log.WithError(err).Error(`"reqCnt" could not be registered in Prometheus`) | ||
} | ||
err = prometheus.Register(reqDur) | ||
if err != nil { | ||
log.WithError(err).Error(`"reqDur" could not be registered in Prometheus`) | ||
} | ||
|
||
return func(c *gin.Context) { | ||
start := time.Now() | ||
|
||
c.Next() | ||
|
||
handler := prepareHandler(c.HandlerName()) | ||
origin := prepareOrigin(c) | ||
code := strconv.Itoa(c.Writer.Status()) | ||
|
||
elapsed := float64(time.Since(start)) / float64(time.Second) | ||
reqDur.WithLabelValues(handler, origin, code).Observe(elapsed) | ||
reqCnt.WithLabelValues(handler, origin, code).Inc() | ||
} | ||
} | ||
|
||
func prepareHandler(h string) string { | ||
s := strings.Split(h, ".") | ||
|
||
return s[len(s)-1] | ||
} | ||
|
||
func prepareOrigin(c *gin.Context) string { | ||
domain, err := GetDomain(c) | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
return domain | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,11 @@ package model | |
|
||
import ( | ||
"fmt" | ||
"github.com/sethvargo/go-password/password" | ||
"github.com/spf13/viper" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/sethvargo/go-password/password" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var Config *config | ||
|
@@ -18,18 +19,20 @@ type config struct { | |
Database string `mapstructure:"database"` | ||
TwitterConsumerKey string `mapstructure:"twitter_consumer_key"` | ||
TwitterConsumerSecret string `mapstructure:"twitter_consumer_secret"` | ||
MetricsListen string `mapstructure:"metrics_listen"` | ||
} | ||
|
||
//NewConfig returns config with default values. | ||
func NewConfig() *config { | ||
secret, _ := password.Generate(64, 12, 12, false, false) | ||
|
||
return &config{ | ||
Listen: ":8080", | ||
LogLevel: "debug", | ||
Initiator: "[email protected]", | ||
Secret: secret, | ||
Database: "ticker.db", | ||
Listen: ":8080", | ||
LogLevel: "debug", | ||
Initiator: "[email protected]", | ||
Secret: secret, | ||
Database: "ticker.db", | ||
MetricsListen: ":8181", | ||
} | ||
} | ||
|
||
|
@@ -49,6 +52,7 @@ func LoadConfig(path string) *config { | |
viper.SetDefault("initiator", c.Initiator) | ||
viper.SetDefault("secret", c.Secret) | ||
viper.SetDefault("database", c.Database) | ||
viper.SetDefault("metrics_listen", c.MetricsListen) | ||
viper.SetDefault("twitter_consumer_key", "") | ||
viper.SetDefault("twitter_consumer_secret", "") | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters