Skip to content

Commit

Permalink
Simplify metrics registration
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-antoniak committed Jul 19, 2024
1 parent f3d31ef commit a7deb82
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 35 deletions.
15 changes: 2 additions & 13 deletions proxy/pkg/metrics/node_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ type NodeMetricsInstance struct {
UnavailableErrors Counter
OtherErrors Counter

RequestDuration map[string]Histogram
ReadDurations Histogram
WriteDurations Histogram

OpenConnections Gauge

Expand Down Expand Up @@ -311,18 +312,6 @@ func CreateHistogramNodeMetric(metricFactory MetricFactory, nodeDescription stri
return m, nil
}

func CreateHistogramNodeRequestDurationMetrics(metricFactory MetricFactory, nodeDescription string, mn Metric, buckets []float64) (map[string]Histogram, error) {
requestDuration := make(map[string]Histogram)
for _, stmtCtg := range StatementCategories {
item, err := CreateHistogramNodeMetric(metricFactory, nodeDescription, mn, buckets, RequestDurationTypeLabel, stmtCtg)
if err != nil {
return nil, err
}
requestDuration[stmtCtg] = item
}
return requestDuration, nil
}

func CreateGaugeNodeMetric(metricFactory MetricFactory, nodeDescription string, mn Metric) (Gauge, error) {
m, err := metricFactory.GetOrCreateGauge(
mn.WithLabels(map[string]string{nodeLabel: nodeDescription}))
Expand Down
4 changes: 0 additions & 4 deletions proxy/pkg/metrics/proxy_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ const (
inFlightRequestsDescription = "Number of requests currently in flight in the proxy"
)

var (
StatementCategories = []string{TypeWrites, TypeReads}
)

var (
FailedReadsOrigin = NewMetricWithLabels(
failedReadsName,
Expand Down
30 changes: 24 additions & 6 deletions proxy/pkg/zdmproxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,12 @@ func (p *ZdmProxy) CreateOriginNodeMetrics(
return nil, err
}

originRequestDuration, err := metrics.CreateHistogramNodeRequestDurationMetrics(metricFactory, originNodeDescription, metrics.OriginRequestDuration, originBuckets)
originReadRequestDuration, err := metrics.CreateHistogramNodeMetric(metricFactory, originNodeDescription, metrics.OriginRequestDuration, originBuckets, metrics.RequestDurationTypeLabel, metrics.TypeReads)
if err != nil {
return nil, err
}

originWriteRequestDuration, err := metrics.CreateHistogramNodeMetric(metricFactory, originNodeDescription, metrics.OriginRequestDuration, originBuckets, metrics.RequestDurationTypeLabel, metrics.TypeWrites)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -857,7 +862,8 @@ func (p *ZdmProxy) CreateOriginNodeMetrics(
OverloadedErrors: originOverloadedErrors,
UnavailableErrors: originUnavailableErrors,
OtherErrors: originOtherErrors,
RequestDuration: originRequestDuration,
ReadDurations: originReadRequestDuration,
WriteDurations: originWriteRequestDuration,
OpenConnections: openOriginConnections,
InFlightRequests: inflightRequests,
UsedStreamIds: originUsedStreamIds,
Expand Down Expand Up @@ -911,7 +917,12 @@ func (p *ZdmProxy) CreateAsyncNodeMetrics(
return nil, err
}

asyncRequestDuration, err := metrics.CreateHistogramNodeRequestDurationMetrics(metricFactory, asyncNodeDescription, metrics.AsyncRequestDuration, asyncBuckets)
asyncReadRequestDuration, err := metrics.CreateHistogramNodeMetric(metricFactory, asyncNodeDescription, metrics.AsyncRequestDuration, asyncBuckets, metrics.RequestDurationTypeLabel, metrics.TypeReads)
if err != nil {
return nil, err
}

asyncWriteRequestDuration, err := metrics.CreateHistogramNodeMetric(metricFactory, asyncNodeDescription, metrics.AsyncRequestDuration, asyncBuckets, metrics.RequestDurationTypeLabel, metrics.TypeWrites)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -941,7 +952,8 @@ func (p *ZdmProxy) CreateAsyncNodeMetrics(
OverloadedErrors: asyncOverloadedErrors,
UnavailableErrors: asyncUnavailableErrors,
OtherErrors: asyncOtherErrors,
RequestDuration: asyncRequestDuration,
ReadDurations: asyncReadRequestDuration,
WriteDurations: asyncWriteRequestDuration,
OpenConnections: openAsyncConnections,
InFlightRequests: inflightRequestsAsync,
UsedStreamIds: asyncUsedStreamIds,
Expand Down Expand Up @@ -995,7 +1007,12 @@ func (p *ZdmProxy) CreateTargetNodeMetrics(
return nil, err
}

targetRequestDuration, err := metrics.CreateHistogramNodeRequestDurationMetrics(metricFactory, targetNodeDescription, metrics.TargetRequestDuration, targetBuckets)
targetReadRequestDuration, err := metrics.CreateHistogramNodeMetric(metricFactory, targetNodeDescription, metrics.TargetRequestDuration, targetBuckets, metrics.RequestDurationTypeLabel, metrics.TypeReads)
if err != nil {
return nil, err
}

targetWriteRequestDuration, err := metrics.CreateHistogramNodeMetric(metricFactory, targetNodeDescription, metrics.TargetRequestDuration, targetBuckets, metrics.RequestDurationTypeLabel, metrics.TypeWrites)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1026,7 +1043,8 @@ func (p *ZdmProxy) CreateTargetNodeMetrics(
OverloadedErrors: targetOverloadedErrors,
UnavailableErrors: targetUnavailableErrors,
OtherErrors: targetOtherErrors,
RequestDuration: targetRequestDuration,
ReadDurations: targetReadRequestDuration,
WriteDurations: targetWriteRequestDuration,
OpenConnections: openTargetConnections,
InFlightRequests: inflightRequests,
UsedStreamIds: targetUsedStreamIds,
Expand Down
29 changes: 17 additions & 12 deletions proxy/pkg/zdmproxy/requestcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,17 @@ func (recv *requestContextImpl) SetResponse(nodeMetrics *metrics.NodeMetrics, f
if recv.GetRequestInfo().ShouldBeTrackedInMetrics() {
switch connectorType {
case ClusterConnectorTypeOrigin:
stmtCtg := getStatementCategory(recv.GetRequestInfo())
nodeMetrics.OriginMetrics.RequestDuration[stmtCtg].Track(recv.startTime)
if isWriteStatement(recv.GetRequestInfo()) {
nodeMetrics.OriginMetrics.WriteDurations.Track(recv.startTime)
} else {
nodeMetrics.OriginMetrics.ReadDurations.Track(recv.startTime)
}
case ClusterConnectorTypeTarget:
stmtCtg := getStatementCategory(recv.GetRequestInfo())
nodeMetrics.TargetMetrics.RequestDuration[stmtCtg].Track(recv.startTime)
if isWriteStatement(recv.GetRequestInfo()) {
nodeMetrics.TargetMetrics.WriteDurations.Track(recv.startTime)
} else {
nodeMetrics.TargetMetrics.ReadDurations.Track(recv.startTime)
}
case ClusterConnectorTypeAsync:
default:
log.Errorf("could not recognize connector type %v", connectorType)
Expand All @@ -207,12 +213,8 @@ func (recv *requestContextImpl) SetResponse(nodeMetrics *metrics.NodeMetrics, f
return finished
}

func getStatementCategory(req RequestInfo) string {
switch req.GetForwardDecision() {
case forwardToBoth:
return metrics.TypeWrites
}
return metrics.TypeReads
func isWriteStatement(req RequestInfo) bool {
return req.GetForwardDecision() == forwardToBoth
}

func (recv *requestContextImpl) updateInternalState(f *frame.RawFrame, cluster common.ClusterType) (state int, updated bool) {
Expand Down Expand Up @@ -341,8 +343,11 @@ func (recv *asyncRequestContextImpl) SetResponse(
}

if recv.GetRequestInfo().ShouldBeTrackedInMetrics() {
stmtCtg := getStatementCategory(recv.GetRequestInfo())
nodeMetrics.AsyncMetrics.RequestDuration[stmtCtg].Track(recv.startTime)
if isWriteStatement(recv.GetRequestInfo()) {
nodeMetrics.AsyncMetrics.WriteDurations.Track(recv.startTime)
} else {
nodeMetrics.AsyncMetrics.ReadDurations.Track(recv.startTime)
}
nodeMetrics.AsyncMetrics.InFlightRequests.Subtract(1)
}

Expand Down

0 comments on commit a7deb82

Please sign in to comment.