-
Notifications
You must be signed in to change notification settings - Fork 62
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
WIP: Add initial metrics #573
Open
eranra
wants to merge
11
commits into
project-codeflare:main
Choose a base branch
from
eranra:Add_initial_metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
72fb349
refactor: addition of root ctx to main
dimakis 81c2ff6
refactor: addition of metrics address
dimakis f11e9aa
refactor: edit of the deployment and service to expose metrics ports
dimakis cb5f178
refactor: edit of run to start controller and health + metrics concur…
dimakis 96ffc09
refactor: making sure that the health and metrics servers only start …
dimakis 33fc50b
refactor: update health and metric port defaults from strings to ints
dimakis 1cd33a7
refactor: addition of a generic server to use for health, metrics etc
dimakis d1703e6
refactor: use errgroup and new generic-server
dimakis 2406744
Merge branch 'main' into expose-metrics-endpoint
dimakis 3f5b957
Initial version of custom metrics
eranra 7e33c69
add update prometheous cluster metrics
eranra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
|
||
logger "k8s.io/klog/v2" | ||
) | ||
|
||
type ServerOption func(*Server) | ||
|
||
// WithTimeout sets the shutdown timeout for the server. | ||
func WithTimeout(timeout time.Duration) ServerOption { | ||
return func(s *Server) { | ||
s.shutdownTimeout = timeout | ||
} | ||
} | ||
|
||
type Server struct { | ||
httpServer http.Server | ||
listener net.Listener | ||
endpoint string | ||
shutdownTimeout time.Duration | ||
} | ||
|
||
func NewServer(port int, endpoint string, handler http.Handler, options ...ServerOption) (*Server, error) { | ||
addr := "0" | ||
if port != 0 { | ||
addr = ":" + strconv.Itoa(port) | ||
} | ||
|
||
listener, err := newListener(addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
mux := http.NewServeMux() | ||
mux.Handle(endpoint, handler) | ||
|
||
s := &Server{ | ||
endpoint: endpoint, | ||
listener: listener, | ||
httpServer: http.Server{Handler: mux}, | ||
shutdownTimeout: 30 * time.Second, // Default value | ||
} | ||
|
||
for _, opt := range options { | ||
opt(s) | ||
} | ||
|
||
return s, nil | ||
} | ||
|
||
func (s *Server) Start() (err error) { | ||
if s.listener == nil { | ||
logger.Infof("Serving endpoint %s is disabled", s.endpoint) | ||
return | ||
} | ||
|
||
defer func() { | ||
if r := recover(); r != nil { | ||
err = fmt.Errorf("serving endpoint %s failed: %v", s.endpoint, r) | ||
} | ||
}() | ||
|
||
logger.Infof("Started serving endpoint %s at %s", s.endpoint, s.listener.Addr()) | ||
if e := s.httpServer.Serve(s.listener); e != http.ErrServerClosed { | ||
return fmt.Errorf("serving endpoint %s failed: %v", s.endpoint, e) | ||
} | ||
return | ||
} | ||
|
||
func (s *Server) Shutdown() error { | ||
if s.listener == nil { | ||
return nil | ||
} | ||
|
||
logger.Info("Stopping server") | ||
|
||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
defer cancel() | ||
|
||
// Try graceful shutdown | ||
if err := s.httpServer.Shutdown(shutdownCtx); err != nil { | ||
return fmt.Errorf("failed to shutdown server gracefully: %v", err) | ||
} | ||
return s.httpServer.Shutdown(shutdownCtx) | ||
} | ||
|
||
// newListener creates a new TCP listener bound to the given address. | ||
func newListener(addr string) (net.Listener, error) { | ||
// Add a case to disable serving altogether | ||
if addr == "0" { | ||
return nil, nil | ||
} | ||
|
||
listener, err := net.Listen("tcp", addr) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create listener: %v", err) | ||
} | ||
|
||
return listener, nil | ||
} |
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,77 @@ | ||
// ------------------------------------------------------ {COPYRIGHT-TOP} --- | ||
// Copyright 2022 The Multi-Cluster App Dispatcher 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. | ||
// ------------------------------------------------------ {COPYRIGHT-END} --- | ||
package metrics | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/collectors" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
) | ||
|
||
var ( | ||
dummyCounter = prometheus.NewCounter(prometheus.CounterOpts{ | ||
Name: "dummy_counter", | ||
Help: "A dummy Prometheus counter", | ||
}) | ||
|
||
gpuGauge = prometheus.NewGauge(prometheus.GaugeOpts{ | ||
Name: "gpu", | ||
Help: "Number of Available GPUs", | ||
}) | ||
) | ||
|
||
// Global Prometheus Registry | ||
var globalPromRegistry = prometheus.NewRegistry() | ||
|
||
// MetricsHandler returns a http.Handler that serves the prometheus metrics | ||
func Handler() http.Handler { | ||
|
||
// register standrad metrics | ||
globalPromRegistry.MustRegister(collectors.NewBuildInfoCollector()) | ||
globalPromRegistry.MustRegister(collectors.NewGoCollector()) | ||
globalPromRegistry.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{})) | ||
|
||
// register custom metrics | ||
registerCustomMetrics() | ||
|
||
// update custom metrics | ||
go foreverUpdateCustomMetrics() | ||
|
||
handlerOpts := promhttp.HandlerOpts{ | ||
ErrorHandling: promhttp.HTTPErrorOnError, | ||
} | ||
|
||
return promhttp.HandlerFor(globalPromRegistry, handlerOpts) | ||
} | ||
|
||
// register the custom metrics | ||
func registerCustomMetrics() { | ||
|
||
globalPromRegistry.MustRegister(dummyCounter) | ||
globalPromRegistry.MustRegister(gpuGauge) | ||
} | ||
|
||
// forever thread that updates the custom metrics | ||
func foreverUpdateCustomMetrics() { | ||
for { | ||
dummyCounter.Inc() | ||
gpuGauge.Inc() | ||
time.Sleep(time.Second) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eranra Helm chart may be producing a string here. Could you check and validate/fix helm chart. See
deployment/mcad-controller