Skip to content

Commit

Permalink
Revert "Revert "wip: error code monitoring/metric recording""
Browse files Browse the repository at this point in the history
This reverts commit 44c31be.
  • Loading branch information
jake-engelberg committed Oct 15, 2024
1 parent 77fc2d7 commit fde1844
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 2 deletions.
4 changes: 4 additions & 0 deletions charts/dapr/crds/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ spec:
description: If true (default is false) HTTP verbs (e.g., GET, POST) are excluded from the metrics.
type: boolean
type: object
recordErrorCodes:
type: boolean
rules:
items:
description: MetricsRule defines configuration options for a
Expand Down Expand Up @@ -329,6 +331,8 @@ spec:
items:
type: integer
type: array
recordErrorCodes:
type: boolean
rules:
items:
description: MetricsRule defines configuration options for a
Expand Down
1 change: 1 addition & 0 deletions pkg/api/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ func (a *api) onBulkGetState(reqCtx *fasthttp.RequestCtx) {
status := apierrors.StateStore(storeName).InvalidKeyName(k, err.Error())
universalFastHTTPErrorResponder(reqCtx, status)
log.Debug(status)
diag.DefaultErrorCodeMonitoring.RecordErrorCode(errorcodes.IllegalKey)
return
}
r := state.GetRequest{
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/configuration/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ type ZipkinSpec struct {
type MetricSpec struct {
Enabled *bool `json:"enabled"`
// +optional
RecordErrorCodes *bool `json:"recordErrorCodes,omitempty"`
// +optional
HTTP *MetricHTTP `json:"http,omitempty"`
// +optional
Rules []MetricsRule `json:"rules,omitempty"`
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/configuration/v1alpha1/zz_generated.deepcopy.go

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

14 changes: 12 additions & 2 deletions pkg/config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,9 @@ func (o OtelSpec) GetIsSecure() bool {
// MetricSpec configuration for metrics.
type MetricSpec struct {
// Defaults to true
Enabled *bool `json:"enabled,omitempty" yaml:"enabled,omitempty"`
HTTP *MetricHTTP `json:"http,omitempty" yaml:"http,omitempty"`
Enabled *bool `json:"enabled,omitempty" yaml:"enabled,omitempty"`
RecordErrorCodes *bool `json:"recordErrorCodes,omitempty"`
HTTP *MetricHTTP `json:"http,omitempty" yaml:"http,omitempty"`
// Latency distribution buckets. If not set, the default buckets are used.
LatencyDistributionBuckets *[]int `json:"latencyDistributionBuckets,omitempty" yaml:"latencyDistributionBuckets,omitempty"`
Rules []MetricsRule `json:"rules,omitempty" yaml:"rules,omitempty"`
Expand Down Expand Up @@ -317,6 +318,15 @@ func (m MetricSpec) GetHTTPPathMatching() []string {
return m.HTTP.PathMatching
}

// GetRecordErrorCodes returns true if `recordErrorCodes` is enabled for metrics
func (m MetricSpec) GetRecordErrorCodes() bool {
if m.RecordErrorCodes == nil {
// The default is false
return false
}
return *m.RecordErrorCodes
}

// MetricHTTP defines configuration for metrics for the HTTP server
type MetricHTTP struct {
// If false, metrics for the HTTP server are collected with increased cardinality.
Expand Down
51 changes: 51 additions & 0 deletions pkg/diagnostics/errorcode_monitoring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package diagnostics

import (
"context"

"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"

diagUtils "github.com/dapr/dapr/pkg/diagnostics/utils"
)

type errorCodeMetrics struct {
errorCodeCount *stats.Int64Measure

appID string
ctx context.Context
enabled bool
}

func newErrorCodeMetrics() *errorCodeMetrics {
return &errorCodeMetrics{ //nolint:exhaustruct
errorCodeCount: stats.Int64(
"error_code/count",
"Number of times an error with a specific errorcode was encountered.",
stats.UnitDimensionless),

ctx: context.Background(),
enabled: false,
}
}

// Init registers the errorcode metrics view.
func (m *errorCodeMetrics) Init(id string) error {
m.enabled = true
m.appID = id

return view.Register(
diagUtils.NewMeasureView(m.errorCodeCount, []tag.Key{appIDKey, errorCodeKey}, view.Count()),
)
}

func (m *errorCodeMetrics) RecordErrorCode(code string) {
if m.enabled {
_ = stats.RecordWithTags(
m.ctx,
diagUtils.WithTags(m.errorCodeCount.Name(), appIDKey, m.appID, errorCodeKey, code),
m.errorCodeCount.M(1),
)
}
}
14 changes: 14 additions & 0 deletions pkg/diagnostics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/dapr/dapr/pkg/config"
"github.com/dapr/dapr/pkg/diagnostics/utils"
"github.com/dapr/dapr/pkg/messages/errorcodes"
)

// appIDKey is a tag key for App ID.
Expand All @@ -42,6 +43,8 @@ var (
DefaultResiliencyMonitoring = newResiliencyMetrics()
// DefaultWorkflowMonitoring holds workflow specific metrics.
DefaultWorkflowMonitoring = newWorkflowMetrics()
// DefaultErrorCodeMonitoring holds error code specific metrics.
DefaultErrorCodeMonitoring = newErrorCodeMetrics()
)

// <<10 -> KBs; <<20 -> MBs; <<30 -> GBs
Expand Down Expand Up @@ -79,6 +82,17 @@ func InitMetrics(appID, namespace string, metricSpec config.MetricSpec) error {
return err
}

log.Info("jake::: my build!!!!!!!!!!!!!")
if metricSpec.GetRecordErrorCodes() {
if err := DefaultErrorCodeMonitoring.Init(appID); err != nil {
return err
}
log.Info("jake::: error code monitoring success")
DefaultErrorCodeMonitoring.RecordErrorCode(errorcodes.ActorInstanceMissing)
DefaultErrorCodeMonitoring.RecordErrorCode(errorcodes.ActorInstanceMissing)
DefaultErrorCodeMonitoring.RecordErrorCode(errorcodes.PubsubEmpty)
}

// Set reporting period of views
view.SetReportingPeriod(DefaultReportingPeriod)
return utils.CreateRulesMap(metricSpec.Rules)
Expand Down
1 change: 1 addition & 0 deletions pkg/diagnostics/service_monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
namespaceKey = tag.MustNewKey("namespace")
resiliencyNameKey = tag.MustNewKey("name")
policyKey = tag.MustNewKey("policy")
errorCodeKey = tag.MustNewKey("errorcode")
componentNameKey = tag.MustNewKey("componentName")
destinationAppIDKey = tag.MustNewKey("dst_app_id")
sourceAppIDKey = tag.MustNewKey("src_app_id")
Expand Down

0 comments on commit fde1844

Please sign in to comment.