generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 62
Set up a metrics server with data served on /metrics #371
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
Merged
+73,435
−26
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,84 @@ | ||
package metrics | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"strings" | ||
|
||
grpcprom "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"google.golang.org/grpc" | ||
"k8s.io/component-base/metrics/legacyregistry" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
var ( | ||
// metricsList is a list of all raw metrics that should be registered always, regardless of any feature gate's value. | ||
metricsList []prometheus.Collector | ||
grpcServerMetrics *grpcprom.ServerMetrics | ||
) | ||
|
||
// Register registers a list of metrics. | ||
func Register() { | ||
grpcServerMetrics = grpcprom.NewServerMetrics( | ||
grpcprom.WithServerHandlingTimeHistogram( | ||
grpcprom.WithHistogramBuckets([]float64{0.001, 0.01, 0.1, 0.3, 0.6, 1, 3, 6, 9, 20, 30, 60, 90, 120}), | ||
), | ||
) | ||
|
||
metricsList = []prometheus.Collector{ | ||
grpcServerMetrics, | ||
} | ||
|
||
for _, metric := range metricsList { | ||
legacyregistry.RawMustRegister(metric) | ||
} | ||
} | ||
|
||
// SetupMetricsServer creates an HTTP server to expose the gRPC server metrics | ||
// at the`endpoint at `metricsAddress` | ||
func SetupMetricsServer(metricsAddress string) error { | ||
if metricsAddress == "" { | ||
return nil | ||
} | ||
l, err := net.Listen("tcp", metricsAddress) | ||
if err != nil { | ||
klog.Warningf("failed to get listener for metrics endpoint: %v", err) | ||
mauriciopoppe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return err | ||
} | ||
path := l.Addr().String() | ||
klog.V(2).Infof("set up prometheus server on %v", path) | ||
go func() { | ||
defer l.Close() | ||
|
||
m := http.NewServeMux() | ||
m.Handle("/metrics", legacyregistry.Handler()) | ||
if err := trapClosedConnErr(http.Serve(l, m)); err != nil { | ||
klog.Fatalf("serve failure(%v), address(%v)", err, path) | ||
} | ||
}() | ||
return nil | ||
} | ||
|
||
func trapClosedConnErr(err error) error { | ||
if err == nil { | ||
return nil | ||
} | ||
if strings.Contains(err.Error(), "use of closed network connection") { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
// GRPCServerMetricsOptions returns the ServerOption applying on gRPC server | ||
// to collect server metrics | ||
func GRPCServerMetricsOptions() []grpc.ServerOption { | ||
return []grpc.ServerOption{ | ||
grpc.ChainUnaryInterceptor( | ||
grpcServerMetrics.UnaryServerInterceptor(), | ||
), | ||
grpc.ChainStreamInterceptor( | ||
grpcServerMetrics.StreamServerInterceptor(), | ||
), | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.