Skip to content

Commit

Permalink
[app] Improve HTTP Clients
Browse files Browse the repository at this point in the history
We are now using our default transporter for all http clients which
comes with tracing out of the box.

We are now also using NewRequestWithContext instead of NewRequest in all
places.
  • Loading branch information
ricoberger committed Jul 22, 2022
1 parent 3ec93d2 commit 4490c9f
Show file tree
Hide file tree
Showing 44 changed files with 124 additions and 76 deletions.
10 changes: 5 additions & 5 deletions pkg/hub/api/applications/applications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestGetApplications(t *testing.T) {

router := Router{chi.NewRouter(), mockStoreClient, otel.Tracer("applications")}

req, _ := http.NewRequest(http.MethodGet, tt.url, nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil)
rctx := chi.NewRouteContext()
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

Expand Down Expand Up @@ -192,7 +192,7 @@ func TestGetApplicationsCount(t *testing.T) {

router := Router{chi.NewRouter(), mockStoreClient, otel.Tracer("applications")}

req, _ := http.NewRequest(http.MethodGet, tt.url, nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil)
rctx := chi.NewRouteContext()
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

Expand Down Expand Up @@ -246,7 +246,7 @@ func TestGetTags(t *testing.T) {

router := Router{chi.NewRouter(), mockStoreClient, otel.Tracer("applications")}

req, _ := http.NewRequest(http.MethodGet, tt.url, nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil)
rctx := chi.NewRouteContext()
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

Expand Down Expand Up @@ -340,7 +340,7 @@ func TestGetApplication(t *testing.T) {

router := Router{chi.NewRouter(), mockStoreClient, otel.Tracer("applications")}

req, _ := http.NewRequest(http.MethodGet, tt.url, nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil)
rctx := chi.NewRouteContext()
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

Expand Down Expand Up @@ -467,7 +467,7 @@ func TestGetApplicationsByTeam(t *testing.T) {

router := Router{chi.NewRouter(), mockStoreClient, otel.Tracer("applications")}

req, _ := http.NewRequest(http.MethodGet, tt.url, nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil)
rctx := chi.NewRouteContext()
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

Expand Down
6 changes: 3 additions & 3 deletions pkg/hub/api/clusters/clusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestGetClusters(t *testing.T) {

router := Router{chi.NewRouter(), mockStoreClient}

req, _ := http.NewRequest(http.MethodGet, tt.url, nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil)
rctx := chi.NewRouteContext()
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

Expand Down Expand Up @@ -117,7 +117,7 @@ func TestGetNamespaces(t *testing.T) {

router := Router{chi.NewRouter(), mockStoreClient}

req, _ := http.NewRequest(http.MethodGet, tt.url, nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil)
rctx := chi.NewRouteContext()
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

Expand Down Expand Up @@ -171,7 +171,7 @@ func TestGetResources(t *testing.T) {

router := Router{chi.NewRouter(), mockStoreClient}

req, _ := http.NewRequest(http.MethodGet, tt.url, nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil)
rctx := chi.NewRouteContext()
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

Expand Down
5 changes: 3 additions & 2 deletions pkg/hub/api/dashboards/dashboards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dashboards

import (
"bytes"
"context"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -48,7 +49,7 @@ func TestGetDashboardsFromReferences(t *testing.T) {
router := Router{chi.NewRouter(), mockStoreClient}
router.Get("/dashboards", router.getDashboardsFromReferences)

req, _ := http.NewRequest(http.MethodPost, "/dashboards", bytes.NewBuffer(tt.body))
req, _ := http.NewRequestWithContext(context.Background(), http.MethodPost, "/dashboards", bytes.NewBuffer(tt.body))
w := httptest.NewRecorder()

router.getDashboardsFromReferences(w, req)
Expand Down Expand Up @@ -87,7 +88,7 @@ func TestGetDashboard(t *testing.T) {
router := Router{chi.NewRouter(), mockStoreClient}
router.Get("/dashboard", router.getDashboard)

req, _ := http.NewRequest(http.MethodGet, tt.url, nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil)
w := httptest.NewRecorder()

router.getDashboard(w, req)
Expand Down
2 changes: 1 addition & 1 deletion pkg/hub/api/navigation/navigation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestGetNavigationGroups(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
router := Router{chi.NewRouter(), tt.config}

req, _ := http.NewRequest(http.MethodGet, "/groups", nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/groups", nil)
rctx := chi.NewRouteContext()
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

Expand Down
2 changes: 1 addition & 1 deletion pkg/hub/api/notifications/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestGetNotificationGroups(t *testing.T) {

router := Router{chi.NewRouter(), tt.config, mockStoreClient, otel.Tracer("notifications")}

req, _ := http.NewRequest(http.MethodGet, "/groups", nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/groups", nil)
rctx := chi.NewRouteContext()
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

Expand Down
4 changes: 2 additions & 2 deletions pkg/hub/api/teams/teams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestGetTeams(t *testing.T) {

router := Router{chi.NewRouter(), mockStoreClient}

req, _ := http.NewRequest(http.MethodGet, tt.url, nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil)
rctx := chi.NewRouteContext()
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

Expand Down Expand Up @@ -197,7 +197,7 @@ func TestGetTeam(t *testing.T) {

router := Router{chi.NewRouter(), mockStoreClient}

req, _ := http.NewRequest(http.MethodGet, tt.url, nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil)
rctx := chi.NewRouteContext()
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

Expand Down
2 changes: 1 addition & 1 deletion pkg/hub/middleware/userauth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func TestAuthHandler(t *testing.T) {
router.Use(tt.auth.Handler)
router.Mount(url, Mount(""))

req, _ := http.NewRequest(http.MethodGet, url, nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil)
tt.prepareRequest(req)

w := httptest.NewRecorder()
Expand Down
4 changes: 2 additions & 2 deletions pkg/hub/middleware/userauth/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestUserAuthHandler(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
router := Router{chi.NewRouter(), ""}

req, _ := http.NewRequest(http.MethodGet, "/user", nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/user", nil)
req = req.WithContext(context.WithValue(req.Context(), authContext.UserKey, tt.user))

w := httptest.NewRecorder()
Expand All @@ -49,7 +49,7 @@ func TestUserAuthHandler(t *testing.T) {
func TestUserLogoutHandler(t *testing.T) {
router := Router{chi.NewRouter(), ""}

req, _ := http.NewRequest(http.MethodGet, "/user", nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/user", nil)

w := httptest.NewRecorder()
router.userLogoutHandler(w, req)
Expand Down
5 changes: 4 additions & 1 deletion pkg/hub/satellites/satellite/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
authContext "github.com/kobsio/kobs/pkg/hub/middleware/userauth/context"
"github.com/kobsio/kobs/pkg/middleware/errresponse"

"github.com/go-chi/chi/v5/middleware"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
)
Expand All @@ -24,13 +25,15 @@ func doRequest[T any](ctx context.Context, user *authContext.User, client *http.
}

otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header))

req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
if user != nil {
req.Header.Set("x-kobs-user", user.ToString())
} else {
req.Header.Set("x-kobs-user", "{\"email\": \"\"}")
}
if requestID := middleware.GetReqID(ctx); requestID != "" {
req.Header.Set("requestID", requestID)
}

resp, err := client.Do(req)
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions pkg/hub/satellites/satellite/satellite.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"time"

authContext "github.com/kobsio/kobs/pkg/hub/middleware/userauth/context"
applicationv1 "github.com/kobsio/kobs/pkg/kube/apis/application/v1"
Expand All @@ -16,9 +15,9 @@ import (
"github.com/kobsio/kobs/pkg/kube/clusters/cluster"
"github.com/kobsio/kobs/pkg/log"
"github.com/kobsio/kobs/pkg/middleware/errresponse"
"github.com/kobsio/kobs/pkg/middleware/roundtripper"
"github.com/kobsio/kobs/pkg/satellite/plugins/plugin"

"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
Expand Down Expand Up @@ -221,8 +220,7 @@ func NewClient(config Config) (Client, error) {
return &client{
config: config,
httpClient: &http.Client{
Transport: otelhttp.NewTransport(http.DefaultTransport),
Timeout: 60 * time.Second,
Transport: roundtripper.DefaultRoundTripper,
},
proxyURL: proxyURL,
tracer: otel.Tracer("satellite"),
Expand Down
6 changes: 4 additions & 2 deletions pkg/middleware/roundtripper/roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import (
"net"
"net/http"
"time"

"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

// DefaultRoundTripper is our default RoundTripper.
var DefaultRoundTripper http.RoundTripper = &http.Transport{
var DefaultRoundTripper http.RoundTripper = otelhttp.NewTransport(&http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 10 * time.Second,
}
})

// BasicAuthTransport is the struct to add basic auth to a RoundTripper.
type BasicAuthTransport struct {
Expand Down
5 changes: 3 additions & 2 deletions pkg/middleware/roundtripper/roundtripper_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package roundtripper

import (
"context"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -31,7 +32,7 @@ func TestBasicAuthTransport(t *testing.T) {
Password: "admin",
}

req, _ := http.NewRequest(http.MethodGet, "/", nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/", nil)
roundTripper.RoundTrip(req)

w := httptest.NewRecorder()
Expand All @@ -49,7 +50,7 @@ func TestTokenAuthTransporter(t *testing.T) {
Token: "admin",
}

req, _ := http.NewRequest(http.MethodGet, "/", nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/", nil)
roundTripper.RoundTrip(req)

w := httptest.NewRecorder()
Expand Down
3 changes: 2 additions & 1 deletion pkg/satellite/api/applications/applications_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package applications

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -57,7 +58,7 @@ func TestGetApplications(t *testing.T) {
router := Router{chi.NewRouter(), Config{}, mockClustersClient}
router.Get("/applications", router.getApplications)

req, _ := http.NewRequest(http.MethodGet, tt.url, nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil)
w := httptest.NewRecorder()

router.getApplications(w, req)
Expand Down
7 changes: 4 additions & 3 deletions pkg/satellite/api/clusters/clusters_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package clusters

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -40,7 +41,7 @@ func TestGetClusters(t *testing.T) {
router := Router{chi.NewRouter(), Config{}, mockClustersClient}
router.Get("/clusters", router.getClusters)

req, _ := http.NewRequest(http.MethodGet, tt.url, nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil)
w := httptest.NewRecorder()

router.getClusters(w, req)
Expand Down Expand Up @@ -93,7 +94,7 @@ func TestGetNamespaces(t *testing.T) {
}
router.Get("/namespaces", router.getNamespaces)

req, _ := http.NewRequest(http.MethodGet, "/namespaces", nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/namespaces", nil)
w := httptest.NewRecorder()

router.getNamespaces(w, req)
Expand Down Expand Up @@ -121,7 +122,7 @@ func TestGetCRDs(t *testing.T) {
}
router.Get("/crds", router.getCRDs)

req, _ := http.NewRequest(http.MethodGet, "/crds", nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/crds", nil)
w := httptest.NewRecorder()

router.getCRDs(w, req)
Expand Down
3 changes: 2 additions & 1 deletion pkg/satellite/api/dashboards/dashboards_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dashboards

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -59,7 +60,7 @@ func TestGetDashboards(t *testing.T) {
}
router.Get("/dashboards", router.getDashboards)

req, _ := http.NewRequest(http.MethodGet, "/dashboards", nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/dashboards", nil)
w := httptest.NewRecorder()

router.getDashboards(w, req)
Expand Down
3 changes: 2 additions & 1 deletion pkg/satellite/api/teams/teams_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package teams

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -52,7 +53,7 @@ func TestGetTeams(t *testing.T) {
router := Router{Mux: chi.NewRouter(), clustersClient: mockClustersClient, config: Config{}}
router.Get("/teams", router.getTeams)

req, _ := http.NewRequest(http.MethodGet, "/teams", nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/teams", nil)
w := httptest.NewRecorder()

router.getTeams(w, req)
Expand Down
3 changes: 2 additions & 1 deletion pkg/satellite/api/users/users_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package users

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -51,7 +52,7 @@ func TestGetUsers(t *testing.T) {
router := Router{Mux: chi.NewRouter(), clustersClient: mockClustersClient, config: Config{}}
router.Get("/users", router.getUsers)

req, _ := http.NewRequest(http.MethodGet, "/users", nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/users", nil)
w := httptest.NewRecorder()

router.getUsers(w, req)
Expand Down
3 changes: 2 additions & 1 deletion pkg/satellite/middleware/tokenauth/tokenauth_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tokenauth

import (
"context"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -47,7 +48,7 @@ func TestHandler(t *testing.T) {
render.JSON(w, r, nil)
})

req, _ := http.NewRequest(http.MethodGet, "/", nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/", nil)
tt.prepareRequest(req)

w := httptest.NewRecorder()
Expand Down
3 changes: 2 additions & 1 deletion pkg/satellite/middleware/user/user_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package user

import (
"context"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -47,7 +48,7 @@ func TestHandler(t *testing.T) {
render.JSON(w, r, nil)
})

req, _ := http.NewRequest(http.MethodGet, "/", nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/", nil)
tt.prepareRequest(req)

w := httptest.NewRecorder()
Expand Down
Loading

0 comments on commit 4490c9f

Please sign in to comment.