Skip to content

Commit

Permalink
Revert to current
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Oct 26, 2022
1 parent 9388222 commit 2ea7546
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 146 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.19
require (
github.com/fatih/color v1.13.0
github.com/gkampitakis/go-snaps v0.4.0
github.com/go-json-experiment/json v0.0.0-20221017203807-c5ed296b8c92
github.com/google/go-cmp v0.5.9
github.com/google/go-github/v37 v37.0.0
github.com/hashicorp/golang-lru v0.5.4
Expand All @@ -15,6 +14,7 @@ require (
github.com/prometheus/client_model v0.3.0
github.com/prometheus/common v0.37.0
github.com/prometheus/prometheus v0.39.1
github.com/prymitive/current v0.1.0
github.com/rogpeppe/go-internal v1.9.0
github.com/rs/zerolog v1.28.0
github.com/stretchr/testify v1.8.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ github.com/gkampitakis/go-snaps v0.4.0/go.mod h1:xYclGIA7Al0CoYwehW0dd/NEr6oJge+
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-json-experiment/json v0.0.0-20221017203807-c5ed296b8c92 h1:eoE7yxLELqDQVlHGoYYxXLFZqF8NcdOnrukTm4ObJaY=
github.com/go-json-experiment/json v0.0.0-20221017203807-c5ed296b8c92/go.mod h1:I+I5/LT2lLP0eZsBNaVDrOrYASx9h7o7mRHmy+535/A=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
Expand Down Expand Up @@ -395,6 +393,8 @@ github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5
github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4=
github.com/prometheus/prometheus v0.39.1 h1:abZM6A+sKAv2eKTbRIaHq4amM/nT07MuxRm0+QTaTj0=
github.com/prometheus/prometheus v0.39.1/go.mod h1:GjQjgLhHMc0oo4Ko7qt/yBSJMY4hUoiAZwsYQgjaePA=
github.com/prymitive/current v0.1.0 h1:j0qvhMUKEz4rZE7YgftTYnBcaujmv6RVGvvmEC+p+6E=
github.com/prymitive/current v0.1.0/go.mod h1:ZKbTBHjDMGAM3YPcnkA2I4L5U/vYfbXyVTKZJWhTCoc=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
Expand Down
62 changes: 37 additions & 25 deletions internal/promapi/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,19 @@ package promapi
import (
"context"
"crypto/sha1"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"

"github.com/go-json-experiment/json"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prymitive/current"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3"
)

const promAPIStatusSuccess = "success"

type ConfigResponse struct {
Status string `json:"status"`
Error string `json:"error"`
ErrorType string `json:"errorType"`
Data struct {
YAML string `json:"yaml"`
} `json:"data"`
}

type ConfigSectionGlobal struct {
ScrapeInterval time.Duration `yaml:"scrape_interval"`
ScrapeTimeout time.Duration `yaml:"scrape_timeout"`
Expand Down Expand Up @@ -70,19 +61,7 @@ func (q configQuery) Run() queryResult {
return qr
}

var decoded ConfigResponse
err = json.UnmarshalFull(resp.Body, &decoded)
if err != nil {
qr.err = APIError{Status: decoded.Status, ErrorType: decodeErrorType(decoded.ErrorType), Err: decoded.Error}
return qr
}

if decoded.Status != promAPIStatusSuccess {
qr.err = APIError{Status: decoded.Status, ErrorType: decodeErrorType(decoded.ErrorType), Err: decoded.Error}
return qr
}

qr.value = decoded.Data.YAML
qr.value, qr.err = streamConfig(resp.Body)
return qr
}

Expand Down Expand Up @@ -140,3 +119,36 @@ func (p *Prometheus) Config(ctx context.Context) (*ConfigResult, error) {

return &r, nil
}

func streamConfig(r io.Reader) (cfg string, err error) {
defer dummyReadAll(r)

var status, errType, errText string
decoder := current.Object(
current.Key("status", current.Value(func(s string, isNil bool) {
status = s
})),
current.Key("error", current.Value(func(s string, isNil bool) {
errText = s
})),
current.Key("errorType", current.Value(func(s string, isNil bool) {
errType = s
})),
current.Key("data", current.Object(
current.Key("yaml", current.Value(func(s string, isNil bool) {
cfg = s
})),
)),
)

dec := json.NewDecoder(r)
if err = decoder.Stream(dec); err != nil {
return cfg, APIError{Status: status, ErrorType: v1.ErrBadResponse, Err: fmt.Sprintf("JSON parse error: %s", err)}
}

if status != "success" {
return cfg, APIError{Status: status, ErrorType: decodeErrorType(errType), Err: errText}
}

return cfg, nil
}
42 changes: 25 additions & 17 deletions internal/promapi/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package promapi

import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"syscall"

"github.com/go-json-experiment/json"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prymitive/current"
)

func IsUnavailableError(err error) bool {
Expand Down Expand Up @@ -79,23 +80,30 @@ func decodeError(err error) string {
return err.Error()
}

type FailedResponse struct {
Status string `json:"status"`
Error string `json:"error"`
ErrorType string `json:"errorType"`
}

func tryDecodingAPIError(resp *http.Response) error {
var decoded FailedResponse
if json.UnmarshalFull(resp.Body, &decoded) == nil {
return APIError{Status: decoded.Status, ErrorType: decodeErrorType(decoded.ErrorType), Err: decoded.Error}
var status, errType, errText string
decoder := current.Object(
current.Key("status", current.Value(func(s string, isNil bool) {
status = s
})),
current.Key("error", current.Value(func(s string, isNil bool) {
errText = s
})),
current.Key("errorType", current.Value(func(s string, isNil bool) {
errType = s
})),
)

dec := json.NewDecoder(resp.Body)
if err := decoder.Stream(dec); err != nil {
switch resp.StatusCode / 100 {
case 4:
return APIError{Status: "error", ErrorType: v1.ErrClient, Err: fmt.Sprintf("client error: %d", resp.StatusCode)}
case 5:
return APIError{Status: "error", ErrorType: v1.ErrServer, Err: fmt.Sprintf("server error: %d", resp.StatusCode)}
}
return APIError{Status: "error", ErrorType: v1.ErrBadResponse, Err: fmt.Sprintf("bad response code: %d", resp.StatusCode)}
}

switch resp.StatusCode / 100 {
case 4:
return APIError{Status: "error", ErrorType: v1.ErrClient, Err: fmt.Sprintf("client error: %d", resp.StatusCode)}
case 5:
return APIError{Status: "error", ErrorType: v1.ErrServer, Err: fmt.Sprintf("server error: %d", resp.StatusCode)}
}
return APIError{Status: "error", ErrorType: v1.ErrBadResponse, Err: fmt.Sprintf("bad response code: %d", resp.StatusCode)}
return APIError{Status: status, ErrorType: decodeErrorType(errType), Err: errText}
}
56 changes: 35 additions & 21 deletions internal/promapi/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,18 @@ package promapi
import (
"context"
"crypto/sha1"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"

"github.com/go-json-experiment/json"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prymitive/current"
"github.com/rs/zerolog/log"
)

type FlagsResponse struct {
Status string `json:"status"`
Error string `json:"error"`
ErrorType string `json:"errorType"`
Data v1.FlagsResult `json:"data"`
}

type FlagsResult struct {
URI string
Flags v1.FlagsResult
Expand Down Expand Up @@ -55,19 +49,7 @@ func (q flagsQuery) Run() queryResult {
return qr
}

var decoded FlagsResponse
err = json.UnmarshalFull(resp.Body, &decoded)
if err != nil {
qr.err = APIError{Status: decoded.Status, ErrorType: v1.ErrBadResponse, Err: fmt.Sprintf("JSON parse error: %s", err)}
return qr
}

if decoded.Status != promAPIStatusSuccess {
qr.err = APIError{Status: decoded.Status, ErrorType: decodeErrorType(decoded.ErrorType), Err: decoded.Error}
return qr
}

qr.value = decoded.Data
qr.value, qr.err = streamFlags(resp.Body)
return qr
}

Expand Down Expand Up @@ -109,3 +91,35 @@ func (p *Prometheus) Flags(ctx context.Context) (*FlagsResult, error) {

return &r, nil
}

func streamFlags(r io.Reader) (flags v1.FlagsResult, err error) {
defer dummyReadAll(r)

var status, errType, errText string
flags = v1.FlagsResult{}
decoder := current.Object(
current.Key("status", current.Value(func(s string, isNil bool) {
status = s
})),
current.Key("error", current.Value(func(s string, isNil bool) {
errText = s
})),
current.Key("errorType", current.Value(func(s string, isNil bool) {
errType = s
})),
current.Key("data", current.Map(func(k, v string) {
flags[k] = v
})),
)

dec := json.NewDecoder(r)
if err = decoder.Stream(dec); err != nil {
return nil, APIError{Status: status, ErrorType: v1.ErrBadResponse, Err: fmt.Sprintf("JSON parse error: %s", err)}
}

if status != "success" {
return nil, APIError{Status: status, ErrorType: decodeErrorType(errType), Err: errText}
}

return flags, nil
}
2 changes: 1 addition & 1 deletion internal/promapi/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestFlags(t *testing.T) {
{
prefix: "/badYaml",
timeout: time.Second,
err: `bad_response: JSON parse error: json: missing character ':' after object name`,
err: `bad_response: JSON parse error: expected colon after object key`,
},
}

Expand Down
56 changes: 35 additions & 21 deletions internal/promapi/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,18 @@ package promapi
import (
"context"
"crypto/sha1"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"

"github.com/go-json-experiment/json"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prymitive/current"
"github.com/rs/zerolog/log"
)

type MedatataResponse struct {
Status string `json:"status"`
Error string `json:"error"`
ErrorType string `json:"errorType"`
Data map[string][]v1.Metadata `json:"data"`
}

type MetadataResult struct {
URI string
Metadata []v1.Metadata
Expand Down Expand Up @@ -58,19 +52,7 @@ func (q metadataQuery) Run() queryResult {
return qr
}

var decoded MedatataResponse
err = json.UnmarshalFull(resp.Body, &decoded)
if err != nil {
qr.err = APIError{Status: decoded.Status, ErrorType: decodeErrorType(decoded.ErrorType), Err: decoded.Error}
return qr
}

if decoded.Status != promAPIStatusSuccess {
qr.err = APIError{Status: decoded.Status, ErrorType: decodeErrorType(decoded.ErrorType), Err: decoded.Error}
return qr
}

qr.value = decoded.Data
qr.value, qr.err = streamMetadata(resp.Body)
return qr
}

Expand Down Expand Up @@ -114,3 +96,35 @@ func (p *Prometheus) Metadata(ctx context.Context, metric string) (*MetadataResu

return &metadata, nil
}

func streamMetadata(r io.Reader) (meta map[string][]v1.Metadata, err error) {
defer dummyReadAll(r)

var status, errType, errText string
meta = map[string][]v1.Metadata{}
decoder := current.Object(
current.Key("status", current.Value(func(s string, isNil bool) {
status = s
})),
current.Key("error", current.Value(func(s string, isNil bool) {
errText = s
})),
current.Key("errorType", current.Value(func(s string, isNil bool) {
errType = s
})),
current.Key("data", current.Map(func(k string, v []v1.Metadata) {
meta[k] = v
})),
)

dec := json.NewDecoder(r)
if err = decoder.Stream(dec); err != nil {
return nil, APIError{Status: status, ErrorType: v1.ErrBadResponse, Err: fmt.Sprintf("JSON parse error: %s", err)}
}

if status != "success" {
return nil, APIError{Status: status, ErrorType: decodeErrorType(errType), Err: errText}
}

return meta, nil
}
4 changes: 4 additions & 0 deletions internal/promapi/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,7 @@ func processJob(prom *Prometheus, job queryRequest) queryResult {
func formatTime(t time.Time) string {
return strconv.FormatFloat(float64(t.Unix())+float64(t.Nanosecond())/1e9, 'f', -1, 64)
}

func dummyReadAll(r io.Reader) {
_, _ = io.Copy(io.Discard, r)
}
Loading

0 comments on commit 2ea7546

Please sign in to comment.