prometheus / client_golang

Prometheus instrumentation library for Go applications
https://pkg.go.dev/github.com/prometheus/client_golang
Apache License 2.0
5.35k stars 1.17k forks source link

AlertingRule and RecordingRule struct in api.go missing Type variable #1557

Open sarthaktyagi-505 opened 2 months ago

sarthaktyagi-505 commented 2 months ago

Problem Description:

While using prometheus api to get the rules using Rule API in api.go I ran into the following issue, While trying to marshall the body into a json. I am trying to cache the results.

 <*errors.errorString | 0xc0002c9c50>: {
          s: "v1.RulesResult.Groups: []v1.RuleGroup: unmarshalerDecoder: failed to decode JSON into an alerting or recording rule, error found in #10 byte of ...|00:00Z\"}]}]}|..., bigger context ...|Time\":0,\"lastEvaluation\":\"0001-01-01T00:00:00Z\"}]}]}|...",
      }
      v1.RulesResult.Groups: []v1.RuleGroup: unmarshalerDecoder: failed to decode JSON into an alerting or recording rule, error found in #10 byte of ...|00:00Z"}]}]}|..., bigger context ...|Time":0,"lastEvaluation":"0001-01-01T00:00:00Z"}]}]}|...
  occurred

// following stacktrace
type field not present in rule

On looking closely in the api.go file I found that Type variable is not defined in the struct for AlertingRule and RecordingRule, hence when we are trying to get the Rules using the api, we get an unmarshall failed error. I also checked in the same file which is specifically checking for the Type field to be present in the json on the following lines https://github.com/prometheus/client_golang/blob/main/api/prometheus/v1/api.go#L739C1-L751C3. I also checked the sample json response which is expected and it also contains the type field. The sample can be found here.

Solution We add the Type field in both the structs and set them alerting and recording which are already defined as constant in the file. I tested it on my local and it works.

sarthaktyagi-505 commented 2 months ago

Hi Guys,

I opened the following PR to fix the above issue https://github.com/prometheus/client_golang/pull/1558 can you guys please review it C.C - @kakkoyun @bwplotka @ArthurSens

Thanks in advance Sarthak