Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
Fix some small stats inconsistencies (#569)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo authored Sep 18, 2020
1 parent 91ad2fa commit aa95228
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
17 changes: 9 additions & 8 deletions pkg/controller/issueapi/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ func (c *Controller) HandleIssue() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

// Record the issue attempt.
stats.Record(ctx, c.metrics.IssueAttempts.M(1))

var request api.IssueCodeRequest
if err := controller.BindJSON(w, r, &request); err != nil {
stats.Record(ctx, c.metrics.CodeIssueErrors.M(1))
Expand Down Expand Up @@ -118,21 +121,19 @@ func (c *Controller) HandleIssue() http.Handler {
return
}

// Add realm so that metrics are groupable on a per-realm basis.
ctx, err = tag.New(ctx, tag.Upsert(observability.RealmTagKey, realm.Name))
if err != nil {
logger.Errorw("unable to record metrics for realm", "realmID", realm.ID, "error", err)
}

// If this realm requires a date but no date was specified, return an error.
if request.SymptomDate == "" && realm.RequireDate {
stats.Record(ctx, c.metrics.IssueAttempts.M(1), c.metrics.CodeIssueErrors.M(1))
c.h.RenderJSON(w, http.StatusBadRequest, api.Errorf("missing either test or symptom date").WithCode(api.ErrMissingDate))
return
}

// Add realm so that metrics are groupable on a per-realm basis.
ctx, err = tag.New(ctx,
tag.Upsert(observability.RealmTagKey, realm.Name))
if err != nil {
logger.Errorw("unable to record metrics for realm", "realmID", realm.ID, "error", err)
}
stats.Record(ctx, c.metrics.IssueAttempts.M(1))

// Validate that the request with the provided test type is valid for this
// realm.
if !realm.ValidTestType(request.TestType) {
Expand Down
9 changes: 7 additions & 2 deletions pkg/controller/issueapi/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func registerMetrics() (*Metrics, error) {
}); err != nil {
return nil, fmt.Errorf("stat view registration failure: %w", err)
}

mCodesIssued := stats.Int64(MetricPrefix+"/codes_issued", "The number of verification codes issued", stats.UnitDimensionless)
if err := view.Register(&view.View{
Name: MetricPrefix + "/codes_issued_count",
Expand All @@ -57,7 +58,8 @@ func registerMetrics() (*Metrics, error) {
}); err != nil {
return nil, fmt.Errorf("stat view registration failure: %w", err)
}
mCodeIssueErrors := stats.Int64(MetricPrefix+"code_issue_error", "The number of failed code issues", stats.UnitDimensionless)

mCodeIssueErrors := stats.Int64(MetricPrefix+"/code_issue_error", "The number of failed code issues", stats.UnitDimensionless)
if err := view.Register(&view.View{
Name: MetricPrefix + "/code_issue_error_count",
Measure: mCodeIssueErrors,
Expand All @@ -67,6 +69,7 @@ func registerMetrics() (*Metrics, error) {
}); err != nil {
return nil, fmt.Errorf("stat view registration failure: %w", err)
}

mSMSSent := stats.Int64(MetricPrefix+"/sms_sent", "The number of SMS messages sent", stats.UnitDimensionless)
if err := view.Register(&view.View{
Name: MetricPrefix + "/sms_sent_count",
Expand All @@ -77,7 +80,8 @@ func registerMetrics() (*Metrics, error) {
}); err != nil {
return nil, fmt.Errorf("stat view registration failure: %w", err)
}
mSMSSendErrors := stats.Int64(MetricPrefix+"sms_send_error", "The number of failed SMS sends", stats.UnitDimensionless)

mSMSSendErrors := stats.Int64(MetricPrefix+"/sms_send_error", "The number of failed SMS sends", stats.UnitDimensionless)
if err := view.Register(&view.View{
Name: MetricPrefix + "/sms_send_error_count",
Measure: mSMSSendErrors,
Expand All @@ -87,6 +91,7 @@ func registerMetrics() (*Metrics, error) {
}); err != nil {
return nil, fmt.Errorf("stat view registration failure: %w", err)
}

return &Metrics{
IssueAttempts: mIssueAttempts,
CodesIssued: mCodesIssued,
Expand Down

0 comments on commit aa95228

Please sign in to comment.