Skip to content

Commit

Permalink
add metrics.
Browse files Browse the repository at this point in the history
Signed-off-by: morvencao <[email protected]>
  • Loading branch information
morvencao committed Sep 2, 2024
1 parent f4d9da7 commit bf2425b
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 19 deletions.
36 changes: 18 additions & 18 deletions cmd/maestro/server/metrics_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ func MetricsMiddleware(handler http.Handler) http.Handler {

// Create the set of labels that we will add to all the requests:
labels := prometheus.Labels{
metricsMethodLabel: r.Method,
metricsPathLabel: path,
metricsCodeLabel: strconv.Itoa(wrapper.code),
restMetricsMethodLabel: r.Method,
restMetricsPathLabel: path,
restMetricsCodeLabel: strconv.Itoa(wrapper.code),
}

// Update the metric containing the number of requests:
Expand All @@ -112,20 +112,20 @@ var metricsPathVarRE = regexp.MustCompile(`{[^}]*}`)
var PathVarSub = "-"

// Subsystem used to define the metrics:
const metricsSubsystem = "api_inbound"
const restMetricsSubsystem = "rest_api_inbound"

// Names of the labels added to metrics:
const (
metricsMethodLabel = "method"
metricsPathLabel = "path"
metricsCodeLabel = "code"
restMetricsMethodLabel = "method"
restMetricsPathLabel = "path"
restMetricsCodeLabel = "code"
)

// MetricsLabels - Array of labels added to metrics:
var MetricsLabels = []string{
metricsMethodLabel,
metricsPathLabel,
metricsCodeLabel,
// RestMetricsLabels - Array of labels added to metrics:
var RestMetricsLabels = []string{
restMetricsMethodLabel,
restMetricsPathLabel,
restMetricsCodeLabel,
}

// Names of the metrics:
Expand All @@ -134,26 +134,26 @@ const (
requestDuration = "request_duration"
)

// MetricsNames - Array of Names of the metrics:
var MetricsNames = []string{
// RestMetricsNames - Array of Names of the metrics:
var RestMetricsNames = []string{
requestCount,
requestDuration,
}

// Description of the requests count metric:
var requestCountMetric = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: metricsSubsystem,
Subsystem: restMetricsSubsystem,
Name: requestCount,
Help: "Number of requests served.",
},
MetricsLabels,
RestMetricsLabels,
)

// Description of the request duration metric:
var requestDurationMetric = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: metricsSubsystem,
Subsystem: restMetricsSubsystem,
Name: requestDuration,
Help: "Request duration in seconds.",
Buckets: []float64{
Expand All @@ -163,7 +163,7 @@ var requestDurationMetric = prometheus.NewHistogramVec(
30.0,
},
},
MetricsLabels,
RestMetricsLabels,
)

// metricsResponseWrapper is an extension of the HTTP response writer that remembers the status code,
Expand Down
5 changes: 5 additions & 0 deletions pkg/db/advisory_locks.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import (
"gorm.io/gorm"
)

func init() {
// Register the metrics for advisory locks
RegisterAdvisoryLockMetrics()
}

type (
LockType string
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/db/metrics_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func UnregisterAdvisoryLockMetrics() {
prometheus.Unregister(advisoryLockDurationMetric)
}

// ResetMetricCollectors resets all collectors
// ResetAdvisoryLockMetricsCollectors resets all collectors
func ResetAdvisoryLockMetricsCollectors() {
advisoryLockCountMetric.Reset()
advisoryLockDurationMetric.Reset()
Expand Down
92 changes: 92 additions & 0 deletions pkg/services/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/openshift-online/maestro/pkg/dao"
"github.com/openshift-online/maestro/pkg/db"
logger "github.com/openshift-online/maestro/pkg/logger"
"github.com/prometheus/client_golang/prometheus"

cegeneric "open-cluster-management.io/sdk-go/pkg/cloudevents/generic"
cetypes "open-cluster-management.io/sdk-go/pkg/cloudevents/generic/types"
Expand Down Expand Up @@ -42,6 +43,11 @@ func NewResourceService(lockFactory db.LockFactory, resourceDao dao.ResourceDao,
}
}

func init() {
// Register the metrics for resource service
RegisterResourceMetrics()
}

var _ ResourceService = &sqlResourceService{}

type sqlResourceService struct {
Expand Down Expand Up @@ -87,6 +93,15 @@ func (s *sqlResourceService) Create(ctx context.Context, resource *api.Resource)
return nil, handleCreateError("Resource", err)
}

// Create the set of labels that we will add to all the resource process:
labels := prometheus.Labels{
metricsIDLabel: resource.ID,
metricsActionLabel: "create",
}

// Update the metric containing the number of resource process:
resourceProcessCountMetric.With(labels).Inc()

return resource, nil
}

Expand Down Expand Up @@ -141,6 +156,15 @@ func (s *sqlResourceService) Update(ctx context.Context, resource *api.Resource)
return nil, handleUpdateError("Resource", err)
}

// Create the set of labels that we will add to all the resource process:
labels := prometheus.Labels{
metricsIDLabel: updated.ID,
metricsActionLabel: "update",
}

// Update the metric containing the number of resource process:
resourceProcessCountMetric.With(labels).Inc()

return updated, nil
}

Expand Down Expand Up @@ -214,6 +238,15 @@ func (s *sqlResourceService) UpdateStatus(ctx context.Context, resource *api.Res
return nil, false, handleUpdateError("Resource", err)
}

// Create the set of labels that we will add to all the resource process:
labels := prometheus.Labels{
metricsIDLabel: updated.ID,
metricsActionLabel: "update",
}

// Update the metric containing the number of resource process:
resourceProcessCountMetric.With(labels).Inc()

return updated, true, nil
}

Expand Down Expand Up @@ -254,6 +287,15 @@ func (s *sqlResourceService) Delete(ctx context.Context, id string) *errors.Serv
return handleDeleteError("Resource", errors.GeneralError("Unable to delete resource: %s", err))
}

// Create the set of labels that we will add to all the resource process:
labels := prometheus.Labels{
metricsIDLabel: id,
metricsActionLabel: "delete",
}

// Update the metric containing the number of resource process:
resourceProcessCountMetric.With(labels).Inc()

return nil
}

Expand Down Expand Up @@ -323,3 +365,53 @@ func (s *sqlResourceService) syncTimestampsFromResourceMeta(resource *api.Resour
}
}
}

// Subsystem used to define the metrics:
const metricsSubsystem = "resource"

// Names of the labels added to metrics:
const (
metricsIDLabel = "id"
metricsActionLabel = "action"
)

// MetricsLabels - Array of labels added to metrics:
var MetricsLabels = []string{
metricsIDLabel,
metricsActionLabel,
}

// Names of the metrics:
const (
processCountMetric = "process_total"
)

// MetricsNames - Array of Names of the metrics:
var MetricsNames = []string{
processCountMetric,
}

// Register the metrics:
func RegisterResourceMetrics() {
prometheus.MustRegister(resourceProcessCountMetric)
}

// Unregister the metrics:
func UnregisterResourceMetrics() {
prometheus.Unregister(resourceProcessCountMetric)
}

// Reset the metrics:
func ResetResourceMetrics() {
resourceProcessCountMetric.Reset()
}

// Description of the resource process count metric:
var resourceProcessCountMetric = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: metricsSubsystem,
Name: processCountMetric,
Help: "Number of resource process.",
},
MetricsLabels,
)

0 comments on commit bf2425b

Please sign in to comment.