-
Notifications
You must be signed in to change notification settings - Fork 25
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
Prometheus encoder added similar to hostd #1068
Changes from all commits
220a570
6c4813c
f7603d6
f6660c1
a250853
7bf0202
68744f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
"go.sia.tech/renterd/api" | ||
"go.sia.tech/renterd/build" | ||
"go.sia.tech/renterd/hostdb" | ||
"go.sia.tech/renterd/internal/prometheus" | ||
"go.sia.tech/renterd/object" | ||
"go.sia.tech/renterd/wallet" | ||
"go.sia.tech/renterd/webhooks" | ||
|
@@ -175,6 +176,34 @@ | |
}) | ||
} | ||
|
||
func (ap *Autopilot) writeResponse(c jape.Context, code int, resp any) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hey I think @n8maninger wrote this method not sure what it does I just use it |
||
var responseFormat string | ||
if err := c.DecodeForm("response", &responseFormat); err != nil { | ||
return | ||
} | ||
|
||
if resp != nil { | ||
switch responseFormat { | ||
case "prometheus": | ||
v, ok := resp.(prometheus.Marshaller) | ||
if !ok { | ||
err := fmt.Errorf("response does not implement prometheus.Marshaller %T", resp) | ||
c.Error(err, http.StatusInternalServerError) | ||
ap.logger.Error("response does not implement prometheus.Marshaller", zap.Stack("stack"), zap.Error(err)) | ||
return | ||
} | ||
|
||
enc := prometheus.NewEncoder(c.ResponseWriter) | ||
if err := enc.Append(v); err != nil { | ||
ap.logger.Error("failed to marshal prometheus response", zap.Error(err)) | ||
return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You return here without calling |
||
} | ||
default: | ||
c.Encode(resp) | ||
} | ||
} | ||
} | ||
|
||
func (ap *Autopilot) configHandlerPOST(jc jape.Context) { | ||
ctx := jc.Request.Context() | ||
|
||
|
@@ -691,7 +720,7 @@ | |
jc.Encode(host) | ||
} | ||
|
||
func (ap *Autopilot) stateHandlerGET(jc jape.Context) { | ||
Check failure on line 723 in autopilot/autopilot.go GitHub Actions / test (ubuntu-latest, 1.21)
|
||
pruning, pLastStart := ap.c.Status() | ||
migrating, mLastStart := ap.m.Status() | ||
scanning, sLastStart := ap.s.Status() | ||
|
@@ -701,7 +730,7 @@ | |
return | ||
} | ||
|
||
jc.Encode(api.AutopilotStateResponse{ | ||
ap.writeResponse(jc, http.StatusOK, AutopilotStateResp(api.AutopilotStateResponse{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't work with our static code analysis tool There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should seriously consider extending |
||
Configured: err == nil, | ||
Migrating: migrating, | ||
MigratingLastStart: api.TimeRFC3339(mLastStart), | ||
|
@@ -719,7 +748,7 @@ | |
OS: runtime.GOOS, | ||
BuildTime: api.TimeRFC3339(build.BuildTime()), | ||
}, | ||
}) | ||
})) | ||
} | ||
|
||
func (ap *Autopilot) hostsHandlerPOST(jc jape.Context) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package autopilot | ||
|
||
import ( | ||
"go.sia.tech/renterd/api" | ||
"go.sia.tech/renterd/internal/prometheus" | ||
) | ||
|
||
type AutopilotStateResp api.AutopilotStateResponse | ||
|
||
func (asr AutopilotStateResp) PrometheusMetric() (metrics []prometheus.Metric) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd implement If a return type doesn't exist yet because it's directly returning a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @n8maninger wrote this I can'ti would run it by him before I make any changes to it. |
||
labels := map[string]any{ | ||
"network": asr.Network, | ||
"version": asr.Version, | ||
"commit": asr.Commit, | ||
"os": asr.OS, | ||
"buildTime": asr.BuildTime.String(), | ||
} | ||
// labels := fmt.Sprintf(`network="%s", version="%s", commit="%s", os="%s", buildTime="%s"`, | ||
// build.NetworkName(), build.Version(), build.Commit(), runtime.GOOS, api.TimeRFC3339(build.BuildTime())) | ||
metrics = append(metrics, prometheus.Metric{ | ||
Name: "renterd_autopilot_state_uptime", | ||
Labels: labels, | ||
Value: float64(asr.UptimeMS), | ||
}) | ||
metrics = append(metrics, prometheus.Metric{ | ||
Name: "renterd_autopilot_state_configured", | ||
Labels: labels, | ||
Value: func() float64 { | ||
if asr.Configured { | ||
return 1 | ||
} | ||
return 0 | ||
}(), | ||
}) | ||
metrics = append(metrics, prometheus.Metric{ | ||
Name: "renterd_autopilot_state_migrating", | ||
Labels: labels, | ||
Value: func() float64 { | ||
if asr.Migrating { | ||
return 1 | ||
} | ||
return 0 | ||
}(), | ||
}) | ||
metrics = append(metrics, prometheus.Metric{ | ||
Name: "renterd_autopilot_state_pruning", | ||
Labels: labels, | ||
Value: func() float64 { | ||
if asr.Pruning { | ||
return 1 | ||
} | ||
return 0 | ||
}(), | ||
}) | ||
metrics = append(metrics, prometheus.Metric{ | ||
Name: "renterd_autopilot_state_scanning", | ||
Labels: labels, | ||
Value: func() float64 { | ||
if asr.Scanning { | ||
return 1 | ||
} | ||
return 0 | ||
}(), | ||
}) | ||
return | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move the content of this to a function in a new file
api/utils.go
like soand then call that function in here, check its error, and log it? Right now there is a lot of duplicate code.