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

feature: optimise the logic of using prometheus #2429

Merged
Merged
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
22 changes: 11 additions & 11 deletions apis/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"sync"

util_metrics "github.com/alibaba/pouch/pkg/utils/metrics"

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

func init() {
Expand Down Expand Up @@ -47,15 +45,17 @@ var registerMetrics sync.Once

// Register all metrics.
func Register() {
// Register the metrics.
// Get a custom prometheus registry.
registry := util_metrics.GetCustomPrometheusRegistry()
registerMetrics.Do(func() {
prometheus.MustRegister(ImagePullSummary)
prometheus.MustRegister(EngineVersion)
prometheus.MustRegister(ContainerActionsCounter)
prometheus.MustRegister(ContainerSuccessActionsCounter)
prometheus.MustRegister(ImageActionsCounter)
prometheus.MustRegister(ImageSuccessActionsCounter)
prometheus.MustRegister(ContainerActionsTimer)
prometheus.MustRegister(ImageActionsTimer)
// Register the custom metrics.
registry.MustRegister(ImagePullSummary)
registry.MustRegister(EngineVersion)
registry.MustRegister(ContainerActionsCounter)
registry.MustRegister(ContainerSuccessActionsCounter)
registry.MustRegister(ImageActionsCounter)
registry.MustRegister(ImageSuccessActionsCounter)
registry.MustRegister(ContainerActionsTimer)
registry.MustRegister(ImageActionsTimer)
})
}
6 changes: 3 additions & 3 deletions apis/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/pkg/errtypes"
"github.com/alibaba/pouch/pkg/httputils"
util_metrics "github.com/alibaba/pouch/pkg/utils/metrics"

"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -88,8 +88,8 @@ func initRoute(s *Server) http.Handler {
s.addRoute(r, http.MethodPost, "/networks/{id:.*}/disconnect", s.disconnectNetwork)

// metrics
r.Path(versionMatcher + "/metrics").Methods(http.MethodGet).Handler(prometheus.Handler())
r.Path("/metrics").Methods(http.MethodGet).Handler(prometheus.Handler())
r.Path(versionMatcher + "/metrics").Methods(http.MethodGet).Handler(util_metrics.GetPrometheusHandler())
r.Path("/metrics").Methods(http.MethodGet).Handler(util_metrics.GetPrometheusHandler())

// CRI stream server related handlers
if s.StreamRouter != nil {
Expand Down
37 changes: 20 additions & 17 deletions cri/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
util_metrics "github.com/alibaba/pouch/pkg/utils/metrics"

"github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/prometheus/client_golang/prometheus"
)

func init() {
Expand Down Expand Up @@ -75,22 +74,26 @@ var registerMetrics sync.Once

// Register all metrics.
func Register() {
// Get a custom prometheus registry.
registry := util_metrics.GetCustomPrometheusRegistry()
registerMetrics.Do(func() {
prometheus.MustRegister(PodActionsCounter)
prometheus.MustRegister(PodSuccessActionsCounter)
prometheus.MustRegister(PodActionsTimer)
prometheus.MustRegister(ContainerActionsCounter)
prometheus.MustRegister(ContainerSuccessActionsCounter)
prometheus.MustRegister(ContainerActionsTimer)
prometheus.MustRegister(ImagePullSummary)
prometheus.MustRegister(ImageActionsCounter)
prometheus.MustRegister(ImageSuccessActionsCounter)
prometheus.MustRegister(ImageActionsTimer)
prometheus.MustRegister(VolumeActionsCounter)
prometheus.MustRegister(VolumeSuccessActionsCounter)
prometheus.MustRegister(VolumeActionsTimer)
prometheus.MustRegister(RuntimeActionsCounter)
prometheus.MustRegister(RuntimeSuccessActionsCounter)
prometheus.MustRegister(RuntimeActionsTimer)
// Register the custom metrics.
registry.MustRegister(PodActionsCounter)
registry.MustRegister(PodSuccessActionsCounter)
registry.MustRegister(PodActionsTimer)
registry.MustRegister(ContainerActionsCounter)
registry.MustRegister(ContainerSuccessActionsCounter)
registry.MustRegister(ContainerActionsTimer)
registry.MustRegister(ImagePullSummary)
registry.MustRegister(ImageActionsCounter)
registry.MustRegister(ImageSuccessActionsCounter)
registry.MustRegister(ImageActionsTimer)
registry.MustRegister(VolumeActionsCounter)
registry.MustRegister(VolumeSuccessActionsCounter)
registry.MustRegister(VolumeActionsTimer)
registry.MustRegister(RuntimeActionsCounter)
registry.MustRegister(RuntimeSuccessActionsCounter)
registry.MustRegister(RuntimeActionsTimer)
registry.MustRegister(GRPCMetrics)
})
}
35 changes: 35 additions & 0 deletions pkg/utils/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,32 @@ package metrics

import (
"fmt"
"net/http"
"os"
"sync"
"time"

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

const (
namespace = "engine"
)

// CustomPrometheusRegistry creates a custom prometheus registry.
var (
customPrometheusRegistry *prometheus.Registry
prometheusHandler http.Handler
registerMetrics sync.Once
)

func init() {
customPrometheusRegistry = prometheus.NewRegistry()
prometheusHandler = promhttp.HandlerFor(customPrometheusRegistry, promhttp.HandlerOpts{})
registerDefaultMetrics(customPrometheusRegistry)
}

// SinceInMicroseconds gets the time since the specified start in microseconds.
func SinceInMicroseconds(start time.Time) float64 {
return float64(time.Since(start).Nanoseconds() / time.Microsecond.Nanoseconds())
Expand Down Expand Up @@ -63,3 +80,21 @@ func NewLabelTimer(subsystem, name, help string, labels ...string) *prometheus.H
ConstLabels: nil,
}, labels)
}

// GetCustomPrometheusRegistry create a custom resigtry of Prometheus.
func GetCustomPrometheusRegistry() *prometheus.Registry {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use GetPrometheusRegistry here. The Custom is for the prometheus package, not for pouch. Right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm, that is right. As a developer of pouch, he(she) should not care about custom or default.

return customPrometheusRegistry
}

// GetPrometheusHandler return the prometheus handler.
func GetPrometheusHandler() http.Handler {
return prometheusHandler
}

func registerDefaultMetrics(registry *prometheus.Registry) {
//Register the default metrics to the custom registry in prometheus.
registerMetrics.Do(func() {
registry.MustRegister(prometheus.NewProcessCollector(os.Getpid(), ""))
registry.MustRegister(prometheus.NewGoCollector())
})
}
67 changes: 53 additions & 14 deletions vendor/github.com/prometheus/client_golang/prometheus/counter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 53 additions & 14 deletions vendor/github.com/prometheus/client_golang/prometheus/gauge.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading