From 4490c9f49a12cb74c3dbbb69b9e812bab85f443b Mon Sep 17 00:00:00 2001 From: ricoberger Date: Fri, 22 Jul 2022 16:05:43 +0200 Subject: [PATCH] [app] Improve HTTP Clients 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. --- pkg/hub/api/applications/applications_test.go | 10 +++++----- pkg/hub/api/clusters/clusters_test.go | 6 +++--- pkg/hub/api/dashboards/dashboards_test.go | 5 +++-- pkg/hub/api/navigation/navigation_test.go | 2 +- pkg/hub/api/notifications/notifications_test.go | 2 +- pkg/hub/api/teams/teams_test.go | 4 ++-- pkg/hub/middleware/userauth/auth_test.go | 2 +- pkg/hub/middleware/userauth/router_test.go | 4 ++-- pkg/hub/satellites/satellite/request.go | 5 ++++- pkg/hub/satellites/satellite/satellite.go | 6 ++---- pkg/middleware/roundtripper/roundtripper.go | 6 ++++-- pkg/middleware/roundtripper/roundtripper_test.go | 5 +++-- pkg/satellite/api/applications/applications_test.go | 3 ++- pkg/satellite/api/clusters/clusters_test.go | 7 ++++--- pkg/satellite/api/dashboards/dashboards_test.go | 3 ++- pkg/satellite/api/teams/teams_test.go | 3 ++- pkg/satellite/api/users/users_test.go | 3 ++- pkg/satellite/middleware/tokenauth/tokenauth_test.go | 3 ++- pkg/satellite/middleware/user/user_test.go | 3 ++- plugins/plugin-azure/cmd/containerinstances_test.go | 8 ++++---- plugins/plugin-azure/cmd/costmanagement_test.go | 3 ++- plugins/plugin-azure/cmd/kubernetesservices_test.go | 6 +++--- plugins/plugin-azure/cmd/monitor_test.go | 2 +- plugins/plugin-azure/cmd/resourcegroups_test.go | 2 +- .../plugin-azure/cmd/virtualmachinescalesets_test.go | 6 +++--- plugins/plugin-elasticsearch/cmd/elasticsearch_test.go | 2 +- plugins/plugin-elasticsearch/pkg/instance/instance.go | 7 +++++-- plugins/plugin-github/pkg/instance/instance_test.go | 7 ++++--- plugins/plugin-grafana/cmd/grafana_test.go | 2 +- plugins/plugin-grafana/pkg/instance/instance.go | 5 +---- plugins/plugin-grafana/pkg/instance/request.go | 4 ++++ plugins/plugin-harbor/pkg/instance/instance.go | 5 +++-- plugins/plugin-harbor/pkg/instance/request.go | 4 ++++ plugins/plugin-helm/cmd/helm_test.go | 6 +++--- plugins/plugin-jaeger/pkg/instance/instance.go | 7 +++++-- plugins/plugin-kiali/pkg/instance/instance.go | 3 +-- plugins/plugin-kiali/pkg/instance/request.go | 4 ++++ plugins/plugin-opsgenie/cmd/opsgenie_test.go | 2 +- .../plugin-opsgenie/pkg/instance/incident/incident.go | 7 +++++++ plugins/plugin-opsgenie/pkg/instance/instance.go | 5 +++++ plugins/plugin-rss/cmd/rss.go | 6 ++---- plugins/plugin-rss/cmd/rss_test.go | 3 ++- plugins/plugin-sonarqube/pkg/instance/instance.go | 3 +-- plugins/plugin-sonarqube/pkg/instance/request.go | 9 +++++++++ 44 files changed, 124 insertions(+), 76 deletions(-) diff --git a/pkg/hub/api/applications/applications_test.go b/pkg/hub/api/applications/applications_test.go index 602fd4f38..9ab45993a 100644 --- a/pkg/hub/api/applications/applications_test.go +++ b/pkg/hub/api/applications/applications_test.go @@ -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)) @@ -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)) @@ -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)) @@ -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)) @@ -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)) diff --git a/pkg/hub/api/clusters/clusters_test.go b/pkg/hub/api/clusters/clusters_test.go index 38c5e5b1e..7a5b5c59c 100644 --- a/pkg/hub/api/clusters/clusters_test.go +++ b/pkg/hub/api/clusters/clusters_test.go @@ -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)) @@ -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)) @@ -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)) diff --git a/pkg/hub/api/dashboards/dashboards_test.go b/pkg/hub/api/dashboards/dashboards_test.go index a0268284a..235557778 100644 --- a/pkg/hub/api/dashboards/dashboards_test.go +++ b/pkg/hub/api/dashboards/dashboards_test.go @@ -2,6 +2,7 @@ package dashboards import ( "bytes" + "context" "fmt" "net/http" "net/http/httptest" @@ -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) @@ -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) diff --git a/pkg/hub/api/navigation/navigation_test.go b/pkg/hub/api/navigation/navigation_test.go index 65604e6d9..30fc1ea58 100644 --- a/pkg/hub/api/navigation/navigation_test.go +++ b/pkg/hub/api/navigation/navigation_test.go @@ -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)) diff --git a/pkg/hub/api/notifications/notifications_test.go b/pkg/hub/api/notifications/notifications_test.go index 243aef052..df4cf58c0 100644 --- a/pkg/hub/api/notifications/notifications_test.go +++ b/pkg/hub/api/notifications/notifications_test.go @@ -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)) diff --git a/pkg/hub/api/teams/teams_test.go b/pkg/hub/api/teams/teams_test.go index 3211b22bc..e750c2bdf 100644 --- a/pkg/hub/api/teams/teams_test.go +++ b/pkg/hub/api/teams/teams_test.go @@ -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)) @@ -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)) diff --git a/pkg/hub/middleware/userauth/auth_test.go b/pkg/hub/middleware/userauth/auth_test.go index 7c3ceeb7c..6b7530aeb 100644 --- a/pkg/hub/middleware/userauth/auth_test.go +++ b/pkg/hub/middleware/userauth/auth_test.go @@ -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() diff --git a/pkg/hub/middleware/userauth/router_test.go b/pkg/hub/middleware/userauth/router_test.go index 3375762bb..55a14b2dc 100644 --- a/pkg/hub/middleware/userauth/router_test.go +++ b/pkg/hub/middleware/userauth/router_test.go @@ -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() @@ -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) diff --git a/pkg/hub/satellites/satellite/request.go b/pkg/hub/satellites/satellite/request.go index fd3e08d2a..6a3bdb48a 100644 --- a/pkg/hub/satellites/satellite/request.go +++ b/pkg/hub/satellites/satellite/request.go @@ -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" ) @@ -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 { diff --git a/pkg/hub/satellites/satellite/satellite.go b/pkg/hub/satellites/satellite/satellite.go index 53d0f3d84..a4fd22d39 100644 --- a/pkg/hub/satellites/satellite/satellite.go +++ b/pkg/hub/satellites/satellite/satellite.go @@ -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" @@ -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" @@ -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"), diff --git a/pkg/middleware/roundtripper/roundtripper.go b/pkg/middleware/roundtripper/roundtripper.go index 602ff14c5..2548496f9 100644 --- a/pkg/middleware/roundtripper/roundtripper.go +++ b/pkg/middleware/roundtripper/roundtripper.go @@ -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 { diff --git a/pkg/middleware/roundtripper/roundtripper_test.go b/pkg/middleware/roundtripper/roundtripper_test.go index b12ffe5c5..cc833ab43 100644 --- a/pkg/middleware/roundtripper/roundtripper_test.go +++ b/pkg/middleware/roundtripper/roundtripper_test.go @@ -1,6 +1,7 @@ package roundtripper import ( + "context" "net/http" "net/http/httptest" "testing" @@ -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() @@ -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() diff --git a/pkg/satellite/api/applications/applications_test.go b/pkg/satellite/api/applications/applications_test.go index 7ac39f080..9753bfd1d 100644 --- a/pkg/satellite/api/applications/applications_test.go +++ b/pkg/satellite/api/applications/applications_test.go @@ -1,6 +1,7 @@ package applications import ( + "context" "fmt" "net/http" "net/http/httptest" @@ -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) diff --git a/pkg/satellite/api/clusters/clusters_test.go b/pkg/satellite/api/clusters/clusters_test.go index 22132b872..a5e1f89aa 100644 --- a/pkg/satellite/api/clusters/clusters_test.go +++ b/pkg/satellite/api/clusters/clusters_test.go @@ -1,6 +1,7 @@ package clusters import ( + "context" "fmt" "net/http" "net/http/httptest" @@ -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) @@ -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) @@ -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) diff --git a/pkg/satellite/api/dashboards/dashboards_test.go b/pkg/satellite/api/dashboards/dashboards_test.go index be845b50e..d1d9d7b37 100644 --- a/pkg/satellite/api/dashboards/dashboards_test.go +++ b/pkg/satellite/api/dashboards/dashboards_test.go @@ -1,6 +1,7 @@ package dashboards import ( + "context" "fmt" "net/http" "net/http/httptest" @@ -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) diff --git a/pkg/satellite/api/teams/teams_test.go b/pkg/satellite/api/teams/teams_test.go index fcd70e1b7..edf320051 100644 --- a/pkg/satellite/api/teams/teams_test.go +++ b/pkg/satellite/api/teams/teams_test.go @@ -1,6 +1,7 @@ package teams import ( + "context" "fmt" "net/http" "net/http/httptest" @@ -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) diff --git a/pkg/satellite/api/users/users_test.go b/pkg/satellite/api/users/users_test.go index 96adef8ba..5a8353bfd 100644 --- a/pkg/satellite/api/users/users_test.go +++ b/pkg/satellite/api/users/users_test.go @@ -1,6 +1,7 @@ package users import ( + "context" "fmt" "net/http" "net/http/httptest" @@ -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) diff --git a/pkg/satellite/middleware/tokenauth/tokenauth_test.go b/pkg/satellite/middleware/tokenauth/tokenauth_test.go index 1dbf2a4be..03dbdb7b9 100644 --- a/pkg/satellite/middleware/tokenauth/tokenauth_test.go +++ b/pkg/satellite/middleware/tokenauth/tokenauth_test.go @@ -1,6 +1,7 @@ package tokenauth import ( + "context" "net/http" "net/http/httptest" "testing" @@ -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() diff --git a/pkg/satellite/middleware/user/user_test.go b/pkg/satellite/middleware/user/user_test.go index f8ab88bf2..8308aae9e 100644 --- a/pkg/satellite/middleware/user/user_test.go +++ b/pkg/satellite/middleware/user/user_test.go @@ -1,6 +1,7 @@ package user import ( + "context" "net/http" "net/http/httptest" "testing" @@ -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() diff --git a/plugins/plugin-azure/cmd/containerinstances_test.go b/plugins/plugin-azure/cmd/containerinstances_test.go index d7cafe71f..5706f6ea9 100644 --- a/plugins/plugin-azure/cmd/containerinstances_test.go +++ b/plugins/plugin-azure/cmd/containerinstances_test.go @@ -123,7 +123,7 @@ func TestGetContainerGroups(t *testing.T) { containerInstancesRouter.Put("/containergroup/restart", router.restartContainerGroup) }) - req, _ := http.NewRequest(http.MethodGet, tt.url, nil) + req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil) req.Header.Set("x-kobs-plugin", tt.pluginName) w := httptest.NewRecorder() @@ -240,7 +240,7 @@ func TestGetContainerGroup(t *testing.T) { containerInstancesRouter.Put("/containergroup/restart", router.restartContainerGroup) }) - req, _ := http.NewRequest(http.MethodGet, tt.url, nil) + req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil) req.Header.Set("x-kobs-plugin", tt.pluginName) w := httptest.NewRecorder() @@ -357,7 +357,7 @@ func TestRestartContainerGroup(t *testing.T) { containerInstancesRouter.Put("/containergroup/restart", router.restartContainerGroup) }) - req, _ := http.NewRequest(http.MethodGet, tt.url, nil) + req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil) req.Header.Set("x-kobs-plugin", tt.pluginName) w := httptest.NewRecorder() @@ -501,7 +501,7 @@ func TestGetContainerLogs(t *testing.T) { containerInstancesRouter.Put("/containergroup/restart", router.restartContainerGroup) }) - req, _ := http.NewRequest(http.MethodGet, tt.url, nil) + req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil) req.Header.Set("x-kobs-plugin", tt.pluginName) w := httptest.NewRecorder() diff --git a/plugins/plugin-azure/cmd/costmanagement_test.go b/plugins/plugin-azure/cmd/costmanagement_test.go index bfa157ef4..38ac8c6ed 100644 --- a/plugins/plugin-azure/cmd/costmanagement_test.go +++ b/plugins/plugin-azure/cmd/costmanagement_test.go @@ -1,6 +1,7 @@ package azure import ( + "context" "fmt" "net/http" "net/http/httptest" @@ -111,7 +112,7 @@ func TestGetActualCosts(t *testing.T) { costManagementRouter.Get("/actualcosts", router.getActualCosts) }) - req, _ := http.NewRequest(http.MethodGet, tt.url, nil) + req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil) req.Header.Set("x-kobs-plugin", tt.pluginName) w := httptest.NewRecorder() diff --git a/plugins/plugin-azure/cmd/kubernetesservices_test.go b/plugins/plugin-azure/cmd/kubernetesservices_test.go index 9d461955e..93333936d 100644 --- a/plugins/plugin-azure/cmd/kubernetesservices_test.go +++ b/plugins/plugin-azure/cmd/kubernetesservices_test.go @@ -123,7 +123,7 @@ func TestGetManagedClusters(t *testing.T) { kubernetesServicesRouter.Get("/managedcluster/nodepools", router.getNodePools) }) - req, _ := http.NewRequest(http.MethodGet, tt.url, nil) + req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil) req.Header.Set("x-kobs-plugin", tt.pluginName) w := httptest.NewRecorder() @@ -239,7 +239,7 @@ func TestGetManagedCluster(t *testing.T) { kubernetesServicesRouter.Get("/managedcluster/nodepools", router.getNodePools) }) - req, _ := http.NewRequest(http.MethodGet, tt.url, nil) + req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil) req.Header.Set("x-kobs-plugin", tt.pluginName) w := httptest.NewRecorder() @@ -355,7 +355,7 @@ func TestGetNodePools(t *testing.T) { kubernetesServicesRouter.Get("/managedcluster/nodepools", router.getNodePools) }) - req, _ := http.NewRequest(http.MethodGet, tt.url, nil) + req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil) req.Header.Set("x-kobs-plugin", tt.pluginName) w := httptest.NewRecorder() diff --git a/plugins/plugin-azure/cmd/monitor_test.go b/plugins/plugin-azure/cmd/monitor_test.go index dfd886cce..b8e2979f2 100644 --- a/plugins/plugin-azure/cmd/monitor_test.go +++ b/plugins/plugin-azure/cmd/monitor_test.go @@ -148,7 +148,7 @@ func TestGetMetrics(t *testing.T) { monitorRouter.Get("/metrics", router.getMetrics) }) - req, _ := http.NewRequest(http.MethodGet, tt.url, nil) + req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil) req.Header.Set("x-kobs-plugin", tt.pluginName) w := httptest.NewRecorder() diff --git a/plugins/plugin-azure/cmd/resourcegroups_test.go b/plugins/plugin-azure/cmd/resourcegroups_test.go index c26272af9..f985fbabd 100644 --- a/plugins/plugin-azure/cmd/resourcegroups_test.go +++ b/plugins/plugin-azure/cmd/resourcegroups_test.go @@ -86,7 +86,7 @@ func TestGetResourceGroups(t *testing.T) { router := Router{chi.NewRouter(), []instance.Instance{mockInstance}} router.Get("/resourcegroups", router.getResourceGroups) - req, _ := http.NewRequest(http.MethodGet, tt.url, nil) + req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil) req.Header.Set("x-kobs-plugin", tt.pluginName) w := httptest.NewRecorder() diff --git a/plugins/plugin-azure/cmd/virtualmachinescalesets_test.go b/plugins/plugin-azure/cmd/virtualmachinescalesets_test.go index 1d35a9571..c67538d52 100644 --- a/plugins/plugin-azure/cmd/virtualmachinescalesets_test.go +++ b/plugins/plugin-azure/cmd/virtualmachinescalesets_test.go @@ -122,7 +122,7 @@ func TestGetVirtualMachineScaleSets(t *testing.T) { virtualMachineScaleSetsRouter.Get("/virtualmachines", router.getVirtualMachines) }) - req, _ := http.NewRequest(http.MethodGet, tt.url, nil) + req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil) req.Header.Set("x-kobs-plugin", tt.pluginName) w := httptest.NewRecorder() @@ -238,7 +238,7 @@ func TestGetVirtualMachineScaleSetDetails(t *testing.T) { virtualMachineScaleSetsRouter.Get("/virtualmachines", router.getVirtualMachines) }) - req, _ := http.NewRequest(http.MethodGet, tt.url, nil) + req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil) req.Header.Set("x-kobs-plugin", tt.pluginName) w := httptest.NewRecorder() @@ -354,7 +354,7 @@ func TestGetVirtualMachines(t *testing.T) { virtualMachineScaleSetsRouter.Get("/virtualmachines", router.getVirtualMachines) }) - req, _ := http.NewRequest(http.MethodGet, tt.url, nil) + req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil) req.Header.Set("x-kobs-plugin", tt.pluginName) w := httptest.NewRecorder() diff --git a/plugins/plugin-elasticsearch/cmd/elasticsearch_test.go b/plugins/plugin-elasticsearch/cmd/elasticsearch_test.go index 292f360e9..c93fe26ea 100644 --- a/plugins/plugin-elasticsearch/cmd/elasticsearch_test.go +++ b/plugins/plugin-elasticsearch/cmd/elasticsearch_test.go @@ -103,7 +103,7 @@ func TestGetLogs(t *testing.T) { r.Get("/logs", router.getLogs) }) - req, _ := http.NewRequest(http.MethodGet, tt.url, nil) + req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil) rctx := chi.NewRouteContext() rctx.URLParams.Add("name", strings.Split(tt.url, "/")[1]) req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx)) diff --git a/plugins/plugin-elasticsearch/pkg/instance/instance.go b/plugins/plugin-elasticsearch/pkg/instance/instance.go index 7fc17d588..42fc5b96e 100644 --- a/plugins/plugin-elasticsearch/pkg/instance/instance.go +++ b/plugins/plugin-elasticsearch/pkg/instance/instance.go @@ -11,8 +11,8 @@ import ( "github.com/kobsio/kobs/pkg/log" "github.com/kobsio/kobs/pkg/middleware/roundtripper" + "github.com/go-chi/chi/v5/middleware" "github.com/mitchellh/mapstructure" - "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/propagation" "go.uber.org/zap" @@ -62,6 +62,9 @@ func (i *instance) GetLogs(ctx context.Context, query string, timeStart, timeEnd otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header)) req.Header.Set("Content-Type", "application/json") + if requestID := middleware.GetReqID(ctx); requestID != "" { + req.Header.Set("requestID", requestID) + } resp, err := i.client.Do(req) if err != nil { @@ -138,7 +141,7 @@ func New(name string, options map[string]any) (Instance, error) { name: name, address: config.Address, client: &http.Client{ - Transport: otelhttp.NewTransport(roundTripper), + Transport: roundtripper.DefaultRoundTripper, }, }, nil } diff --git a/plugins/plugin-github/pkg/instance/instance_test.go b/plugins/plugin-github/pkg/instance/instance_test.go index 97fe5339f..301e4a7c0 100644 --- a/plugins/plugin-github/pkg/instance/instance_test.go +++ b/plugins/plugin-github/pkg/instance/instance_test.go @@ -1,6 +1,7 @@ package instance import ( + "context" "net/http" "testing" @@ -33,7 +34,7 @@ func TestTokenFromCookie(t *testing.T) { i := instance{name: "github", config: Config{Organization: "kobsio"}} t.Run("no error", func(t *testing.T) { - r, _ := http.NewRequest(http.MethodGet, "", nil) + r, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "", nil) r.AddCookie(&http.Cookie{Name: "kobs-oauth-github-kobsio", Value: "eyJhY2Nlc3NfdG9rZW4iOiIxMjM0IiwiZXhwaXJ5IjoiMDAwMS0wMS0wMVQwMDowMDowMFoifQ==", Path: "/", Secure: false, HttpOnly: false}) token, err := i.TokenFromCookie(r) @@ -42,7 +43,7 @@ func TestTokenFromCookie(t *testing.T) { }) t.Run("with error invalid cookie value", func(t *testing.T) { - r, _ := http.NewRequest(http.MethodGet, "", nil) + r, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "", nil) r.AddCookie(&http.Cookie{Name: "kobs-oauth-github-kobsio", Value: "eyJhY2Nlc3NfdG9rZW4iOiIxMjM0IiwiZXhwaXJ5IjoiMDAwMS0wMS0wMVQwMDowMDowMFoifQ", Path: "/", Secure: false, HttpOnly: false}) token, err := i.TokenFromCookie(r) @@ -51,7 +52,7 @@ func TestTokenFromCookie(t *testing.T) { }) t.Run("with error invalid cookie name", func(t *testing.T) { - r, _ := http.NewRequest(http.MethodGet, "", nil) + r, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "", nil) r.AddCookie(&http.Cookie{Name: "kobs-oauth-github", Value: "eyJhY2Nlc3NfdG9rZW4iOiIxMjM0IiwiZXhwaXJ5IjoiMDAwMS0wMS0wMVQwMDowMDowMFoifQ==", Path: "/", Secure: false, HttpOnly: false}) token, err := i.TokenFromCookie(r) diff --git a/plugins/plugin-grafana/cmd/grafana_test.go b/plugins/plugin-grafana/cmd/grafana_test.go index 564aa27cf..abc09b576 100644 --- a/plugins/plugin-grafana/cmd/grafana_test.go +++ b/plugins/plugin-grafana/cmd/grafana_test.go @@ -117,7 +117,7 @@ func TestGetDashboards(t *testing.T) { r.Get("/dashboards", router.getDashboards) }) - req, _ := http.NewRequest(http.MethodGet, tt.url, nil) + req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil) rctx := chi.NewRouteContext() rctx.URLParams.Add("name", strings.Split(tt.url, "/")[1]) req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx)) diff --git a/plugins/plugin-grafana/pkg/instance/instance.go b/plugins/plugin-grafana/pkg/instance/instance.go index 350b7041f..bd2fa798c 100644 --- a/plugins/plugin-grafana/pkg/instance/instance.go +++ b/plugins/plugin-grafana/pkg/instance/instance.go @@ -4,12 +4,10 @@ import ( "context" "fmt" "net/http" - "time" "github.com/kobsio/kobs/pkg/middleware/roundtripper" "github.com/mitchellh/mapstructure" - "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" ) // Config is the structure of the configuration for a single Grafana instance. @@ -91,8 +89,7 @@ func New(name string, options map[string]any) (Instance, error) { name: name, address: config.Address, client: &http.Client{ - Timeout: 30 * time.Second, - Transport: otelhttp.NewTransport(roundTripper), + Transport: roundTripper, }, }, nil } diff --git a/plugins/plugin-grafana/pkg/instance/request.go b/plugins/plugin-grafana/pkg/instance/request.go index f7070b89f..0b7d129d7 100644 --- a/plugins/plugin-grafana/pkg/instance/request.go +++ b/plugins/plugin-grafana/pkg/instance/request.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" + "github.com/go-chi/chi/v5/middleware" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/propagation" ) @@ -21,6 +22,9 @@ func doRequest[T any](ctx context.Context, client *http.Client, url string) (T, } otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header)) + if requestID := middleware.GetReqID(ctx); requestID != "" { + req.Header.Set("requestID", requestID) + } resp, err := client.Do(req) if err != nil { diff --git a/plugins/plugin-harbor/pkg/instance/instance.go b/plugins/plugin-harbor/pkg/instance/instance.go index 888683248..226ca0337 100644 --- a/plugins/plugin-harbor/pkg/instance/instance.go +++ b/plugins/plugin-harbor/pkg/instance/instance.go @@ -5,11 +5,11 @@ import ( "fmt" "net/http" "net/url" + "time" "github.com/kobsio/kobs/pkg/middleware/roundtripper" "github.com/mitchellh/mapstructure" - "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" ) // Config is the structure of the configuration for a single Harbor database instance. @@ -154,7 +154,8 @@ func New(name string, options map[string]any) (Instance, error) { name: name, address: config.Address, client: &http.Client{ - Transport: otelhttp.NewTransport(roundTripper), + Timeout: 60 * time.Second, + Transport: roundTripper, }, }, nil } diff --git a/plugins/plugin-harbor/pkg/instance/request.go b/plugins/plugin-harbor/pkg/instance/request.go index 93a60d16d..f6e24196b 100644 --- a/plugins/plugin-harbor/pkg/instance/request.go +++ b/plugins/plugin-harbor/pkg/instance/request.go @@ -7,6 +7,7 @@ import ( "net/http" "strconv" + "github.com/go-chi/chi/v5/middleware" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/propagation" ) @@ -23,6 +24,9 @@ func doRequest[T any](ctx context.Context, client *http.Client, url string) (T, otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header)) req.Header.Set("X-Accept-Vulnerabilities", "application/vnd.security.vulnerability.report; version=1.1, application/vnd.scanner.adapter.vuln.report.harbor+json; version=1.0") + if requestID := middleware.GetReqID(ctx); requestID != "" { + req.Header.Set("requestID", requestID) + } resp, err := client.Do(req) if err != nil { diff --git a/plugins/plugin-helm/cmd/helm_test.go b/plugins/plugin-helm/cmd/helm_test.go index 24d80081b..c4111fe73 100644 --- a/plugins/plugin-helm/cmd/helm_test.go +++ b/plugins/plugin-helm/cmd/helm_test.go @@ -146,7 +146,7 @@ func TestGetReleases(t *testing.T) { router := Router{chi.NewRouter(), mockClustersClient, tt.permissionsEnabled} router.Get("/releases", router.getReleases) - req, _ := http.NewRequest(http.MethodGet, tt.url, nil) + req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil) w := httptest.NewRecorder() tt.do(router, w, req) @@ -254,7 +254,7 @@ func TestGetRelease(t *testing.T) { router := Router{chi.NewRouter(), mockClustersClient, tt.permissionsEnabled} router.Get("/release", router.getRelease) - req, _ := http.NewRequest(http.MethodGet, tt.url, nil) + req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil) w := httptest.NewRecorder() tt.do(router, w, req) @@ -352,7 +352,7 @@ func TestGetReleaseHistory(t *testing.T) { router := Router{chi.NewRouter(), mockClustersClient, tt.permissionsEnabled} router.Get("/release/history", router.getReleaseHistory) - req, _ := http.NewRequest(http.MethodGet, tt.url, nil) + req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil) w := httptest.NewRecorder() tt.do(router, w, req) diff --git a/plugins/plugin-jaeger/pkg/instance/instance.go b/plugins/plugin-jaeger/pkg/instance/instance.go index 4af0169b5..1e9ca61f4 100644 --- a/plugins/plugin-jaeger/pkg/instance/instance.go +++ b/plugins/plugin-jaeger/pkg/instance/instance.go @@ -9,8 +9,8 @@ import ( "github.com/kobsio/kobs/pkg/middleware/roundtripper" + "github.com/go-chi/chi/v5/middleware" "github.com/mitchellh/mapstructure" - "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/propagation" ) @@ -59,6 +59,9 @@ func (i *instance) doRequest(ctx context.Context, url string) (map[string]any, e } otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header)) + if requestID := middleware.GetReqID(ctx); requestID != "" { + req.Header.Set("requestID", requestID) + } resp, err := i.client.Do(req) if err != nil { @@ -137,7 +140,7 @@ func New(name string, options map[string]any) (Instance, error) { name: name, address: config.Address, client: &http.Client{ - Transport: otelhttp.NewTransport(roundTripper), + Transport: roundTripper, }, }, nil } diff --git a/plugins/plugin-kiali/pkg/instance/instance.go b/plugins/plugin-kiali/pkg/instance/instance.go index ce94349b6..bab6b07be 100644 --- a/plugins/plugin-kiali/pkg/instance/instance.go +++ b/plugins/plugin-kiali/pkg/instance/instance.go @@ -11,7 +11,6 @@ import ( "github.com/kiali/kiali/models" "github.com/mitchellh/mapstructure" - "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" ) // Config is the structure of the configuration for a single Harbor database instance. @@ -191,7 +190,7 @@ func New(name string, options map[string]any) (Instance, error) { name: name, address: config.Address, client: &http.Client{ - Transport: otelhttp.NewTransport(roundTripper), + Transport: roundTripper, }, }, nil } diff --git a/plugins/plugin-kiali/pkg/instance/request.go b/plugins/plugin-kiali/pkg/instance/request.go index a77391a27..b24df8338 100644 --- a/plugins/plugin-kiali/pkg/instance/request.go +++ b/plugins/plugin-kiali/pkg/instance/request.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" + "github.com/go-chi/chi/v5/middleware" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/propagation" ) @@ -21,6 +22,9 @@ func doRequest[T any](ctx context.Context, client *http.Client, url string) (T, } otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header)) + if requestID := middleware.GetReqID(ctx); requestID != "" { + req.Header.Set("requestID", requestID) + } resp, err := client.Do(req) if err != nil { diff --git a/plugins/plugin-opsgenie/cmd/opsgenie_test.go b/plugins/plugin-opsgenie/cmd/opsgenie_test.go index 2528609a7..514314d28 100644 --- a/plugins/plugin-opsgenie/cmd/opsgenie_test.go +++ b/plugins/plugin-opsgenie/cmd/opsgenie_test.go @@ -785,7 +785,7 @@ func Test(t *testing.T) { r.Get("/incident/close", router.closeIncident) }) - req, _ := http.NewRequest(http.MethodGet, tt.url, nil) + req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, tt.url, nil) rctx := chi.NewRouteContext() rctx.URLParams.Add("name", strings.Split(tt.url, "/")[1]) req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx)) diff --git a/plugins/plugin-opsgenie/pkg/instance/incident/incident.go b/plugins/plugin-opsgenie/pkg/instance/incident/incident.go index abcccf2a2..29e8e3bbf 100644 --- a/plugins/plugin-opsgenie/pkg/instance/incident/incident.go +++ b/plugins/plugin-opsgenie/pkg/instance/incident/incident.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "net/http" + "github.com/go-chi/chi/v5/middleware" "github.com/opsgenie/opsgenie-go-sdk-v2/client" ) @@ -25,6 +26,9 @@ func (c *Client) GetTimeline(ctx context.Context, id string) ([]Entry, error) { } req.Header.Set("Authorization", fmt.Sprintf("GenieKey %s", c.client.Config.ApiKey)) + if requestID := middleware.GetReqID(ctx); requestID != "" { + req.Header.Set("requestID", requestID) + } resp, err := c.client.RetryableClient.HTTPClient.Do(req) if err != nil { @@ -62,6 +66,9 @@ func (c *Client) Resolve(ctx context.Context, id, note string) error { } req.Header.Set("Authorization", fmt.Sprintf("GenieKey %s", c.client.Config.ApiKey)) + if requestID := middleware.GetReqID(ctx); requestID != "" { + req.Header.Set("requestID", requestID) + } resp, err := c.client.RetryableClient.HTTPClient.Do(req) if err != nil { diff --git a/plugins/plugin-opsgenie/pkg/instance/instance.go b/plugins/plugin-opsgenie/pkg/instance/instance.go index d3adb4fbc..0a471cf32 100644 --- a/plugins/plugin-opsgenie/pkg/instance/instance.go +++ b/plugins/plugin-opsgenie/pkg/instance/instance.go @@ -3,9 +3,11 @@ package instance import ( "context" "fmt" + "net/http" "time" authContext "github.com/kobsio/kobs/pkg/hub/middleware/userauth/context" + "github.com/kobsio/kobs/pkg/middleware/roundtripper" extendedIncident "github.com/kobsio/kobs/plugins/plugin-opsgenie/pkg/instance/incident" "github.com/mitchellh/mapstructure" @@ -223,6 +225,9 @@ func New(name string, options map[string]any) (Instance, error) { opsgenieConfig := &client.Config{ ApiKey: config.APIKey, OpsGenieAPIURL: client.ApiUrl(config.APIUrl), + HttpClient: &http.Client{ + Transport: roundtripper.DefaultRoundTripper, + }, } opsgenieConfig.ConfigureLogLevel("error") diff --git a/plugins/plugin-rss/cmd/rss.go b/plugins/plugin-rss/cmd/rss.go index a50fcbe4d..ebba99c91 100644 --- a/plugins/plugin-rss/cmd/rss.go +++ b/plugins/plugin-rss/cmd/rss.go @@ -3,11 +3,11 @@ package rss import ( "net/http" "sync" - "time" "github.com/kobsio/kobs/pkg/kube/clusters" "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" "github.com/kobsio/kobs/plugins/plugin-rss/pkg/feed" "github.com/kobsio/kobs/plugins/plugin-rss/pkg/instance" @@ -15,7 +15,6 @@ import ( "github.com/go-chi/chi/v5" "github.com/go-chi/render" "github.com/mmcdole/gofeed" - "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "go.uber.org/zap" ) @@ -101,8 +100,7 @@ func Mount(instances []plugin.Instance, clustersClient clusters.Client) (chi.Rou for _, i := range instances { rssInstance := instance.New(i.Name, &http.Client{ - Transport: otelhttp.NewTransport(http.DefaultTransport), - Timeout: 30 * time.Second, + Transport: roundtripper.DefaultRoundTripper, }) rssInstances = append(rssInstances, rssInstance) } diff --git a/plugins/plugin-rss/cmd/rss_test.go b/plugins/plugin-rss/cmd/rss_test.go index 1b3f9e5b1..ac1b559c2 100644 --- a/plugins/plugin-rss/cmd/rss_test.go +++ b/plugins/plugin-rss/cmd/rss_test.go @@ -1,6 +1,7 @@ package rss import ( + "context" "net/http" "net/http/httptest" "testing" @@ -125,7 +126,7 @@ func TestGetFeed(t *testing.T) { router := Router{chi.NewRouter(), []instance.Instance{i}} router.Get("/feed", router.getFeed) - req, _ := http.NewRequest(http.MethodGet, "/feed?url="+ts.URL, nil) + req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/feed?url="+ts.URL, nil) w := httptest.NewRecorder() tt.do(router, w, req) diff --git a/plugins/plugin-sonarqube/pkg/instance/instance.go b/plugins/plugin-sonarqube/pkg/instance/instance.go index a5fc88ae6..908061be9 100644 --- a/plugins/plugin-sonarqube/pkg/instance/instance.go +++ b/plugins/plugin-sonarqube/pkg/instance/instance.go @@ -9,7 +9,6 @@ import ( "github.com/kobsio/kobs/pkg/middleware/roundtripper" "github.com/mitchellh/mapstructure" - "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" ) // Config is the structure of the configuration for a single SonarQube instance. @@ -90,7 +89,7 @@ func New(name string, options map[string]any) (Instance, error) { address: config.Address, organization: config.Organization, client: &http.Client{ - Transport: otelhttp.NewTransport(roundTripper), + Transport: roundTripper, }, metricKeys: metricKeys, }, nil diff --git a/plugins/plugin-sonarqube/pkg/instance/request.go b/plugins/plugin-sonarqube/pkg/instance/request.go index e4c0e783f..d1139913d 100644 --- a/plugins/plugin-sonarqube/pkg/instance/request.go +++ b/plugins/plugin-sonarqube/pkg/instance/request.go @@ -5,6 +5,10 @@ import ( "encoding/json" "fmt" "net/http" + + "github.com/go-chi/chi/v5/middleware" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/propagation" ) // doRequest runs a http request against the given url with the given client. It decodes the returned result in the @@ -17,6 +21,11 @@ func doRequest[T any](ctx context.Context, client *http.Client, url string) (T, return result, err } + otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header)) + if requestID := middleware.GetReqID(ctx); requestID != "" { + req.Header.Set("requestID", requestID) + } + resp, err := client.Do(req) if err != nil { return result, err