Skip to content

Commit

Permalink
makes http getJson to use Client instance
Browse files Browse the repository at this point in the history
Signed-off-by: Afzal <94980910+afzalbin64@users.noreply.github.com>
  • Loading branch information
afzalbin64 committed Jul 10, 2023
1 parent f52a46a commit da16e3c
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions examples/hotrod/pkg/tracing/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package tracing
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"

"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
Expand All @@ -40,11 +42,27 @@ func NewHTTPClient(tp trace.TracerProvider) *HTTPClient {
// GetJSON executes HTTP GET against specified url and tried to parse
// the response into out object.
func (c *HTTPClient) GetJSON(ctx context.Context, url string, out interface{}) error {
resp, err := otelhttp.Get(ctx, url)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return err
}
req = req.WithContext(ctx)

decoder := json.NewDecoder(resp.Body)
res, err := c.Client.Do(req)
if err != nil {
return err
}

defer res.Body.Close()

if res.StatusCode >= 400 {
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
return errors.New(string(body))
}

decoder := json.NewDecoder(res.Body)
return decoder.Decode(out)
}

0 comments on commit da16e3c

Please sign in to comment.