diff --git a/internal/model/update.go b/internal/model/update.go index fb6d82f8..fd65abae 100644 --- a/internal/model/update.go +++ b/internal/model/update.go @@ -58,3 +58,8 @@ type RecordUpdateJobError struct { ErrorType string `json:"error-type" yaml:"error-type"` ErrorDetails map[string]any `json:"error-details" yaml:"error-details"` } + +type IncrementMetric struct { + Metric string `json:"metric" yaml:"metric"` + Tags map[string]any `json:"tags" yaml:"tags"` +} diff --git a/internal/server/api.go b/internal/server/api.go index 0a1817b6..090b4dd1 100644 --- a/internal/server/api.go +++ b/internal/server/api.go @@ -118,22 +118,19 @@ func (a *API) ServeHTTP(_ http.ResponseWriter, r *http.Request) { a.pushError(err) } + if kind == "increment_metric" { + // Let's just output the metrics data and stop + a.outputRequestData(kind, actual) + return + } + if err := a.pushResult(kind, actual); err != nil { a.pushError(err) return } if !a.hasExpectations { - if a.writer != nil { - // output the data received to stdout - if err = json.NewEncoder(a.writer).Encode(map[string]any{ - "type": kind, - "data": actual.Data, - }); err != nil { - // Fail so the user knows stdout is not working - log.Panicln("Failed to write to stdout: ", err) - } - } + a.outputRequestData(kind, actual) return } @@ -167,6 +164,19 @@ func (a *API) assertExpectation(kind string, actual *model.UpdateWrapper) { } } +func (a *API) outputRequestData(kind string, actual *model.UpdateWrapper) { + if a.writer != nil { + // output the data received to stdout + if err := json.NewEncoder(a.writer).Encode(map[string]any{ + "type": kind, + "data": actual.Data, + }); err != nil { + // Fail so the user knows stdout is not working + log.Panicln("Failed to write to stdout: ", err) + } + } +} + func (a *API) pushError(err error) { escapedError := strings.ReplaceAll(err.Error(), "\n", "") escapedError = strings.ReplaceAll(escapedError, "\r", "") @@ -213,6 +223,8 @@ func decodeWrapper(kind string, data []byte) (actual *model.UpdateWrapper, err e actual.Data, err = decode[model.RecordPackageManagerVersion](data) case "record_update_job_error": actual.Data, err = decode[model.RecordUpdateJobError](data) + case "increment_metric": + actual.Data, err = decode[model.IncrementMetric](data) default: return nil, fmt.Errorf("unexpected output type: %s", kind) }