Skip to content

Commit 74a0d67

Browse files
authored
Ignore any calls to increment_metric as irrelevant (#93)
* Ignore any calls to increment_metric as irrelevant * Let's output the metrics data instead of swallowing it
1 parent 52e598b commit 74a0d67

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

internal/model/update.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,8 @@ type RecordUpdateJobError struct {
5858
ErrorType string `json:"error-type" yaml:"error-type"`
5959
ErrorDetails map[string]any `json:"error-details" yaml:"error-details"`
6060
}
61+
62+
type IncrementMetric struct {
63+
Metric string `json:"metric" yaml:"metric"`
64+
Tags map[string]any `json:"tags" yaml:"tags"`
65+
}

internal/server/api.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,19 @@ func (a *API) ServeHTTP(_ http.ResponseWriter, r *http.Request) {
118118
a.pushError(err)
119119
}
120120

121+
if kind == "increment_metric" {
122+
// Let's just output the metrics data and stop
123+
a.outputRequestData(kind, actual)
124+
return
125+
}
126+
121127
if err := a.pushResult(kind, actual); err != nil {
122128
a.pushError(err)
123129
return
124130
}
125131

126132
if !a.hasExpectations {
127-
if a.writer != nil {
128-
// output the data received to stdout
129-
if err = json.NewEncoder(a.writer).Encode(map[string]any{
130-
"type": kind,
131-
"data": actual.Data,
132-
}); err != nil {
133-
// Fail so the user knows stdout is not working
134-
log.Panicln("Failed to write to stdout: ", err)
135-
}
136-
}
133+
a.outputRequestData(kind, actual)
137134
return
138135
}
139136

@@ -167,6 +164,19 @@ func (a *API) assertExpectation(kind string, actual *model.UpdateWrapper) {
167164
}
168165
}
169166

167+
func (a *API) outputRequestData(kind string, actual *model.UpdateWrapper) {
168+
if a.writer != nil {
169+
// output the data received to stdout
170+
if err := json.NewEncoder(a.writer).Encode(map[string]any{
171+
"type": kind,
172+
"data": actual.Data,
173+
}); err != nil {
174+
// Fail so the user knows stdout is not working
175+
log.Panicln("Failed to write to stdout: ", err)
176+
}
177+
}
178+
}
179+
170180
func (a *API) pushError(err error) {
171181
escapedError := strings.ReplaceAll(err.Error(), "\n", "")
172182
escapedError = strings.ReplaceAll(escapedError, "\r", "")
@@ -213,6 +223,8 @@ func decodeWrapper(kind string, data []byte) (actual *model.UpdateWrapper, err e
213223
actual.Data, err = decode[model.RecordPackageManagerVersion](data)
214224
case "record_update_job_error":
215225
actual.Data, err = decode[model.RecordUpdateJobError](data)
226+
case "increment_metric":
227+
actual.Data, err = decode[model.IncrementMetric](data)
216228
default:
217229
return nil, fmt.Errorf("unexpected output type: %s", kind)
218230
}

0 commit comments

Comments
 (0)