Skip to content

Commit

Permalink
Add metrics server for receiver and event server
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Laine <[email protected]>
  • Loading branch information
Philip Laine committed Mar 30, 2021
1 parent 59234b1 commit ef07dc0
Show file tree
Hide file tree
Showing 7 changed files with 397 additions and 9 deletions.
5 changes: 4 additions & 1 deletion controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import (
var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment
var promMiddleware *server.PrometheusMiddleware

func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)
Expand All @@ -67,6 +68,8 @@ var _ = BeforeSuite(func(done Done) {
zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)),
)

promMiddleware = server.NewPrometheusMiddleware()

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")},
Expand Down Expand Up @@ -136,7 +139,7 @@ var _ = Describe("Event handlers", func() {
// TODO let OS assign port number
eventServer := server.NewEventServer("127.0.0.1:56789", logf.Log, k8sClient)
stopCh = make(chan struct{})
go eventServer.ListenAndServe(stopCh, store)
go eventServer.ListenAndServe(stopCh, promMiddleware, store)
})

AfterEach(func() {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ require (
github.com/getsentry/sentry-go v0.10.0
github.com/go-logr/logr v0.3.0
github.com/google/go-github/v32 v32.1.0
github.com/gorilla/mux v1.8.0
github.com/hashicorp/go-retryablehttp v0.6.8
github.com/ktrysmt/go-bitbucket v0.6.5
github.com/microsoft/azure-devops-go-api/azuredevops v1.0.0-b5
github.com/onsi/ginkgo v1.14.1
github.com/onsi/gomega v1.10.2
github.com/prometheus/client_golang v1.7.1
github.com/sethvargo/go-limiter v0.6.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.6.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ github.com/googleapis/gnostic v0.4.1/go.mod h1:LRhVm6pbyptWbWbuZ38d1eyptfvIytN3i
github.com/googleapis/gnostic v0.5.1 h1:A8Yhf6EtqTv9RMsU6MQTyrtV1TjWlR6xU9BsZIwuTCM=
github.com/googleapis/gnostic v0.5.1/go.mod h1:6U4PtQXGIEt/Z3h5MAT7FNofLnw9vXk2cUuW7uA/OeU=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
Expand Down
6 changes: 3 additions & 3 deletions internal/server/event_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ func NewEventServer(port string, logger logr.Logger, kubeClient client.Client) *
}

// ListenAndServe starts the HTTP server on the specified port
func (s *EventServer) ListenAndServe(stopCh <-chan struct{}, store limiter.Store) {
middleware, err := httplimit.NewMiddleware(store, eventKeyFunc)
func (s *EventServer) ListenAndServe(stopCh <-chan struct{}, promMiddleware *PrometheusMiddleware, store limiter.Store) {
limitMiddleware, err := httplimit.NewMiddleware(store, eventKeyFunc)
if err != nil {
s.logger.Error(err, "Event server crashed")
os.Exit(1)
}
mux := http.NewServeMux()
mux.Handle("/", middleware.Handle(http.HandlerFunc(s.handleEvent())))
mux.Handle("/", promMiddleware.Handler(limitMiddleware.Handle(http.HandlerFunc(s.handleEvent()))))
srv := &http.Server{
Addr: s.port,
Handler: mux,
Expand Down
Loading

0 comments on commit ef07dc0

Please sign in to comment.