63 lines
2.3 KiB
Go
63 lines
2.3 KiB
Go
package prometheus
|
|
|
|
import (
|
|
"slices"
|
|
|
|
client "github.com/prometheus/client_golang/prometheus"
|
|
)
|
|
|
|
// otherLabelValue replaces a label value outside its closed set, so a caller
|
|
// mistake cannot create unbounded series.
|
|
const otherLabelValue = "other"
|
|
|
|
// assessmentMetrics count the saving of assessment answers, which PRD 6.6
|
|
// names as a product signal next to API errors and queues. The answer
|
|
// command (SubmitMyAssessmentAnswer) is not implemented yet, so nothing
|
|
// calls the hook and no series exists; its HTTP latency and errors are
|
|
// already counted per route template by the request metrics.
|
|
type assessmentMetrics struct {
|
|
answerSaves *client.CounterVec
|
|
}
|
|
|
|
var (
|
|
// answerFormats mirror the answer formats of the public contract.
|
|
answerFormats = []string{"choice", "number", "string", "order", "find_error", "configuration", "practice"}
|
|
// answerSaveOutcomes: stored (a new answer was stored), unchanged (the
|
|
// same answer was already stored; nothing changed), rejected (a stale
|
|
// version, a state conflict, or an invalid answer), or failed (an
|
|
// internal error; nothing was stored).
|
|
answerSaveOutcomes = []string{"stored", "unchanged", "rejected", "failed"}
|
|
)
|
|
|
|
func newAssessmentMetrics() assessmentMetrics {
|
|
return assessmentMetrics{
|
|
answerSaves: client.NewCounterVec(client.CounterOpts{
|
|
Name: "lincodex_assessment_answer_saves_total",
|
|
Help: "Assessment answer save commands by answer format and outcome: stored, unchanged, rejected, or failed.",
|
|
}, []string{"format", "outcome"}),
|
|
}
|
|
}
|
|
|
|
func (assessment assessmentMetrics) collectors() []client.Collector {
|
|
return []client.Collector{assessment.answerSaves}
|
|
}
|
|
|
|
// ObserveAssessmentAnswerSave counts one answer save command. It is the hook
|
|
// for the assessment usecase: call it once per command after its transaction
|
|
// ended, with the format of the answer and one of the outcomes above. A
|
|
// value outside these sets is counted as "other". Never pass an attempt,
|
|
// item, or candidate identifier.
|
|
func (metrics *Metrics) ObserveAssessmentAnswerSave(format, outcome string) {
|
|
metrics.assessment.answerSaves.WithLabelValues(
|
|
boundedLabel(format, answerFormats),
|
|
boundedLabel(outcome, answerSaveOutcomes),
|
|
).Inc()
|
|
}
|
|
|
|
func boundedLabel(value string, allowed []string) string {
|
|
if slices.Contains(allowed, value) {
|
|
return value
|
|
}
|
|
return otherLabelValue
|
|
}
|