Skip to content

Commit

Permalink
Merge pull request #31 from Comcast/feature/prometheusEndpoint
Browse files Browse the repository at this point in the history
Feature/prometheus endpoint (WIP)
  • Loading branch information
schmidtw authored Nov 28, 2017
2 parents e63e384 + 78ccd7f commit 515519a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
35 changes: 33 additions & 2 deletions src/glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ import:
version: v3.3
- package: gopkg.in/h2non/gock.v1
version: v1.0.6
- package: github.com/prometheus/client_golang
version: v0.9.0-pre1
subpackages:
- prometheus
4 changes: 4 additions & 0 deletions src/tr1d1um/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type ConversionHandler struct {

//ConversionHandler handles the different incoming tr1 requests
func (ch *ConversionHandler) ServeHTTP(origin http.ResponseWriter, req *http.Request) {
requestsReceived.Inc()

var (
debugLogger = logging.Debug(ch)
errorLogger = logging.Error(ch)
Expand Down Expand Up @@ -138,6 +140,8 @@ func (ch *ConversionHandler) ServeHTTP(origin http.ResponseWriter, req *http.Req

//HandleStat handles the differentiated STAT command
func (ch *ConversionHandler) HandleStat(origin http.ResponseWriter, req *http.Request) {
requestsReceived.Inc()

logging.Debug(ch).Log(logging.MessageKey(), "HandleStat called")
var errorLogger = logging.Error(ch)

Expand Down
29 changes: 26 additions & 3 deletions src/tr1d1um/tr1d1um.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import (
"github.com/go-kit/kit/log"
"github.com/gorilla/mux"
"github.com/justinas/alice"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
Expand All @@ -53,6 +55,7 @@ const (
defaultNetDialerTimeout = "5s"
defaultRetryInterval = "2s"
defaultMaxRetries = 2
defaultMetricsAddress = ":8080"

supportedServicesKey = "supportedServices"
targetURLKey = "targetURL"
Expand All @@ -61,13 +64,18 @@ const (
reqRetryIntervalKey = "requestRetryInterval"
reqMaxRetriesKey = "requestMaxRetries"
respWaitTimeoutKey = "respWaitTimeout"
metricsAddressKey = "metricsAddress"

releaseKey = "release"
)

var (
release = "-"
hostname = "-"
release = "-"
hostname = "-"
requestsReceived = prometheus.NewCounter(prometheus.CounterOpts{
Name: "requests_received",
Help: "Number of requests (that hit correct endpoints) received by tr1d1um.",
})
)

func tr1d1um(arguments []string) (exitCode int) {
Expand All @@ -83,6 +91,7 @@ func tr1d1um(arguments []string) (exitCode int) {
v.SetDefault(reqRetryIntervalKey, defaultRetryInterval)
v.SetDefault(reqMaxRetriesKey, defaultMaxRetries)
v.SetDefault(netDialerTimeoutKey, defaultNetDialerTimeout)
v.SetDefault(metricsAddressKey, defaultMetricsAddress)

//release and internal OS info set up
if releaseVal := v.GetString(releaseKey); releaseVal != "" {
Expand All @@ -99,7 +108,8 @@ func tr1d1um(arguments []string) (exitCode int) {
}

var (
infoLogger = logging.Info(logger)
infoLogger = logging.Info(logger)
errorLogger = logging.Error(logger)
)

infoLogger.Log("configurationFile", v.ConfigFileUsed())
Expand Down Expand Up @@ -136,6 +146,14 @@ func tr1d1um(arguments []string) (exitCode int) {

go snsFactory.PrepareAndStart()

// prometheus metrics set up
http.Handle("/metrics", preHandler.Then(promhttp.Handler()))
go func() {
if prometheusEndpointErr := http.ListenAndServe(v.GetString(metricsAddressKey), nil); prometheusEndpointErr != nil {
errorLogger.Log(logging.MessageKey(), "error with prometheus endpoint", logging.ErrorKey(), prometheusEndpointErr)
}
}()

if err := concurrent.Await(tr1d1umServer, signals); err != nil {
fmt.Fprintf(os.Stderr, "Error when starting %s: %s", applicationName, err)
return 4
Expand Down Expand Up @@ -315,3 +333,8 @@ func getSupportedServicesMap(supportedServices []string) (supportedServicesMap m
func main() {
os.Exit(tr1d1um(os.Args))
}

func init() {
// Metrics values must be registered to be exposed:
prometheus.MustRegister(requestsReceived)
}

0 comments on commit 515519a

Please sign in to comment.