-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom metrics are added for HTTP traffic and exposed by default on port 8081. This has been tested more extensively in the classic watchdog and copied / duplicated into the of-watchdog. Local e2e testing was done on MacOS outside of a container and the new http_* fields and metrics were reported as expected. Signed-off-by: Alex Ellis <[email protected]>
- Loading branch information
Showing
124 changed files
with
33,036 additions
and
5 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,34 @@ | ||
# Gopkg.toml example | ||
# | ||
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html | ||
# for detailed Gopkg.toml documentation. | ||
# | ||
# required = ["github.com/user/thing/cmd/thing"] | ||
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] | ||
# | ||
# [[constraint]] | ||
# name = "github.com/user/project" | ||
# version = "1.0.0" | ||
# | ||
# [[constraint]] | ||
# name = "github.com/user/project2" | ||
# branch = "dev" | ||
# source = "github.com/myfork/project2" | ||
# | ||
# [[override]] | ||
# name = "github.com/x/y" | ||
# version = "2.4.0" | ||
# | ||
# [prune] | ||
# non-go = false | ||
# go-tests = true | ||
# unused-packages = true | ||
|
||
|
||
[[constraint]] | ||
name = "github.com/prometheus/client_golang" | ||
version = "0.9.2" | ||
|
||
[prune] | ||
go-tests = true | ||
unused-packages = true |
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,27 @@ | ||
package metrics | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
) | ||
|
||
type Http struct { | ||
RequestsTotal *prometheus.CounterVec | ||
RequestDurationHistogram *prometheus.HistogramVec | ||
} | ||
|
||
func NewHttp() Http { | ||
return Http{ | ||
RequestsTotal: promauto.NewCounterVec(prometheus.CounterOpts{ | ||
Subsystem: "http", | ||
Name: "requests_total", | ||
Help: "total HTTP requests processed", | ||
}, []string{"code", "method"}), | ||
RequestDurationHistogram: promauto.NewHistogramVec(prometheus.HistogramOpts{ | ||
Subsystem: "http", | ||
Name: "request_duration_seconds", | ||
Help: "Seconds spent serving HTTP requests.", | ||
Buckets: prometheus.DefBuckets, | ||
}, []string{"code", "method"}), | ||
} | ||
} |
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,65 @@ | ||
package metrics | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
) | ||
|
||
// MetricsServer provides instrumentation for HTTP calls | ||
type MetricsServer struct { | ||
s *http.Server | ||
port int | ||
} | ||
|
||
// Register binds a HTTP server to expose Prometheus metrics | ||
func (m *MetricsServer) Register(metricsPort int) { | ||
|
||
m.port = metricsPort | ||
|
||
readTimeout := time.Millisecond * 500 | ||
writeTimeout := time.Millisecond * 500 | ||
|
||
metricsMux := http.NewServeMux() | ||
metricsMux.Handle("/metrics", promhttp.Handler()) | ||
|
||
m.s = &http.Server{ | ||
Addr: fmt.Sprintf(":%d", metricsPort), | ||
ReadTimeout: readTimeout, | ||
WriteTimeout: writeTimeout, | ||
MaxHeaderBytes: 1 << 20, // Max header of 1MB | ||
Handler: metricsMux, | ||
} | ||
|
||
} | ||
|
||
// Serve http traffic in go routine, non-blocking | ||
func (m *MetricsServer) Serve(cancel chan bool) { | ||
log.Printf("Metrics server. Port: %d\n", m.port) | ||
|
||
go func() { | ||
if err := m.s.ListenAndServe(); err != http.ErrServerClosed { | ||
panic(fmt.Sprintf("metrics error ListenAndServe: %v\n", err)) | ||
} | ||
}() | ||
|
||
go func() { | ||
select { | ||
case <-cancel: | ||
log.Printf("metrics server shutdown\n") | ||
|
||
m.s.Shutdown(context.Background()) | ||
} | ||
}() | ||
} | ||
|
||
// InstrumentHandler returns a handler which records HTTP requests | ||
// as they are made | ||
func InstrumentHandler(next http.HandlerFunc, _http Http) http.HandlerFunc { | ||
return promhttp.InstrumentHandlerCounter(_http.RequestsTotal, | ||
promhttp.InstrumentHandlerDuration(_http.RequestDurationHistogram, next)) | ||
} |
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,57 @@ | ||
package metrics | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func Test_Register_ProvidesBytes(t *testing.T) { | ||
|
||
metricsPort := 31111 | ||
|
||
metricsServer := MetricsServer{} | ||
metricsServer.Register(metricsPort) | ||
|
||
cancel := make(chan bool) | ||
go metricsServer.Serve(cancel) | ||
|
||
defer func() { | ||
cancel <- true | ||
}() | ||
|
||
retries := 10 | ||
|
||
for i := 0; i < retries; i++ { | ||
req, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("http://127.0.0.1:%d/metrics", metricsPort), nil) | ||
|
||
res, err := http.DefaultClient.Do(req) | ||
|
||
if err != nil { | ||
t.Logf("cannot get metrics, or not ready: %s", err.Error()) | ||
|
||
time.Sleep(time.Millisecond * 100) | ||
continue | ||
} | ||
|
||
wantStatus := http.StatusOK | ||
if res.StatusCode != wantStatus { | ||
t.Errorf("metrics gave wrong status, want: %d, got: %d", wantStatus, res.StatusCode) | ||
t.Fail() | ||
return | ||
} | ||
|
||
if res.Body == nil { | ||
t.Errorf("metrics response should have a body") | ||
t.Fail() | ||
return | ||
} | ||
defer res.Body.Close() | ||
|
||
return | ||
} | ||
|
||
t.Errorf("unable to get expected response from metrics server") | ||
t.Fail() | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.