Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ pkg/manager,metrics: Expose ServingMetrics func #367

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 2 additions & 24 deletions pkg/manager/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ import (
"context"
"fmt"
"net"
"net/http"
"sync"
"time"

"github.com/prometheus/client_golang/prometheus/promhttp"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -199,28 +197,8 @@ func (cm *controllerManager) GetWebhookServer() *webhook.Server {
}

func (cm *controllerManager) serveMetrics(stop <-chan struct{}) {
handler := promhttp.HandlerFor(metrics.Registry, promhttp.HandlerOpts{
ErrorHandling: promhttp.HTTPErrorOnError,
})
// TODO(JoelSpeed): Use existing Kubernetes machinery for serving metrics
mux := http.NewServeMux()
mux.Handle("/metrics", handler)
server := http.Server{
Handler: mux,
}
// Run the server
go func() {
if err := server.Serve(cm.metricsListener); err != nil && err != http.ErrServerClosed {
cm.errChan <- err
}
}()

// Shutdown the server when stop is closed
select {
case <-stop:
if err := server.Shutdown(context.Background()); err != nil {
cm.errChan <- err
}
if err := metrics.ServeMetrics(cm.metricsListener, stop); err != nil {
cm.errChan <- err
}
}

Expand Down
59 changes: 59 additions & 0 deletions pkg/metrics/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package metrics

import (
"context"
"net"
"net/http"

"github.com/prometheus/client_golang/prometheus/promhttp"
)

// ServeMetrics serves the metrics until the stop channel is closed.
// It serves the metrics on `/metrics` on the port configured through the MetricsBindAddress.
func ServeMetrics(listener net.Listener, stop <-chan struct{}) error {
handler := promhttp.HandlerFor(Registry, promhttp.HandlerOpts{
ErrorHandling: promhttp.HTTPErrorOnError,
})
// TODO(JoelSpeed): Use existing Kubernetes machinery for serving metrics
mux := http.NewServeMux()
mux.Handle("/metrics", handler)
server := http.Server{
Handler: mux,
}

errc := make(chan error, 0)
// Run the server
go func() {
if err := server.Serve(listener); err != nil && err != http.ErrServerClosed {
errc <- err
}
}()

// Shutdown the server when stop is closed and catch any errors.
select {
case err := <-errc:
return err
case <-stop:
if err := server.Shutdown(context.Background()); err != nil {
return err
}
}

return nil
}