castai / promwrite

Prometheus Remote Write Go client

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question Regarding `Write` Function

pchang388 opened this issue · comments

First, thanks for your work on putting this together.

I was instrumenting our own code for Prometheus Metrics and I was trying to add a counter_vec for status_codes returned from Prometheus Remote Write API.

I was looking to leverage the WriteError struct methods:

// WriteError returned if HTTP call is finished with response status code, but it was not successful.
type WriteError struct {
	err  error
	code int
}

func (e *WriteError) Error() string {
	return e.err.Error()
}

func (e *WriteError) StatusCode() int {
	return e.code
}

But when I get a response with a non 20X status code, I am unable to call the StatusCode() function (at least in my linter, IDE) since the function itself returns the inbuilt error:

func (p *Client) Write(ctx context.Context, req *WriteRequest, options ...WriteOption) (*WriteResponse, error) {
...
}

Shouldn't this function return: (*WriteResponse, *WriteError) instead so we can access the status_code without parsing the Error() string?

I think this is a misunderstanding/tiredness on my part

I should be able to use errors.Is or errors.As to see if it's a specific error, WriteError, in this case and then access the StatusCode() method.