Skip to content

Commit a388c8d

Browse files
committed
feat: Homogenize tracing of requests to other services
The "tracing.GetNewRequest" and "tracing.GetNewRequestWithContext" aims to replace the "http.NewRequest" and "http.NewRequestWithContext" respectively by including tracing data. For requests that have been already created, such as the case of some reva requests, "tracing.InjectTracingHeaders" is provided. Note that some outgoing requests might be required NOT to use tracing headers.
1 parent 8360bdf commit a388c8d

File tree

8 files changed

+54
-24
lines changed

8 files changed

+54
-24
lines changed

ocis-pkg/middleware/tracing.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ var propagator = propagation.NewCompositeTextMapPropagator(
1515
func TraceContext(next http.Handler) http.Handler {
1616
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1717
ctx := propagator.Extract(r.Context(), propagation.HeaderCarrier(r.Header))
18-
propagator.Inject(ctx, propagation.HeaderCarrier(r.Header))
1918
next.ServeHTTP(w, r.WithContext(ctx))
2019
})
2120
}

ocis-pkg/oidc/client.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
goidc "github.com/coreos/go-oidc/v3/oidc"
1818
"github.com/golang-jwt/jwt/v5"
1919
"github.com/owncloud/ocis/v2/ocis-pkg/log"
20+
"github.com/owncloud/ocis/v2/ocis-pkg/tracing"
2021
"github.com/owncloud/ocis/v2/services/proxy/pkg/config"
21-
"go.opentelemetry.io/otel/propagation"
2222
"golang.org/x/oauth2"
2323
)
2424

@@ -105,17 +105,11 @@ func (c *oidcClient) lookupWellKnownOpenidConfiguration(ctx context.Context) err
105105
c.providerLock.Lock()
106106
defer c.providerLock.Unlock()
107107
if c.provider == nil {
108-
propagator := propagation.NewCompositeTextMapPropagator(
109-
propagation.Baggage{},
110-
propagation.TraceContext{},
111-
)
112-
113108
wellKnown := strings.TrimSuffix(c.issuer, "/") + wellknownPath
114-
req, err := http.NewRequest("GET", wellKnown, nil)
109+
req, err := tracing.GetNewRequest(ctx, http.MethodGet, wellKnown, nil)
115110
if err != nil {
116111
return err
117112
}
118-
propagator.Inject(ctx, propagation.HeaderCarrier(req.Header))
119113
resp, err := c.httpClient.Do(req.WithContext(ctx))
120114
if err != nil {
121115
return err
@@ -221,11 +215,6 @@ func (u *UserInfo) Claims(v interface{}) error {
221215

222216
// UserInfo retrieves the userinfo from a Token
223217
func (c *oidcClient) UserInfo(ctx context.Context, tokenSource oauth2.TokenSource) (*UserInfo, error) {
224-
propagator := propagation.NewCompositeTextMapPropagator(
225-
propagation.Baggage{},
226-
propagation.TraceContext{},
227-
)
228-
229218
if err := c.lookupWellKnownOpenidConfiguration(ctx); err != nil {
230219
return nil, err
231220
}
@@ -234,11 +223,10 @@ func (c *oidcClient) UserInfo(ctx context.Context, tokenSource oauth2.TokenSourc
234223
return nil, errors.New("oidc: user info endpoint is not supported by this provider")
235224
}
236225

237-
req, err := http.NewRequest("GET", c.provider.UserinfoEndpoint, nil)
226+
req, err := tracing.GetNewRequest(ctx, http.MethodGet, c.provider.UserinfoEndpoint, nil)
238227
if err != nil {
239228
return nil, fmt.Errorf("oidc: create GET request: %v", err)
240229
}
241-
propagator.Inject(ctx, propagation.HeaderCarrier(req.Header))
242230

243231
token, err := tokenSource.Token()
244232
if err != nil {

ocis-pkg/tracing/tracing.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package tracing
33
import (
44
"context"
55
"fmt"
6+
"io"
7+
"net/http"
68
"net/url"
79
"reflect"
810
"strings"
@@ -172,3 +174,43 @@ func parseAgentConfig(ae string) (string, string, error) {
172174
}
173175
return p[0], p[1], nil
174176
}
177+
178+
// GetNewRequest gets a new HTTP request with tracing data coming from the
179+
// provided context. Note that the provided context will NOT be used for the
180+
// request, just to get the data.
181+
// The request will have a new "context.Background()" context associated. This
182+
// means that cancelling the provided context will NOT stop the request.
183+
func GetNewRequest(injectCtx context.Context, method, url string, body io.Reader) (*http.Request, error) {
184+
return GetNewRequestWithDifferentContext(context.Background(), injectCtx, method, url, body)
185+
}
186+
187+
// GetNewRequestWithContext gets a new HTTP request with tracing data coming
188+
// from the provided context. The request will also have the same provided
189+
// context associated (in case the context is cancelled)
190+
func GetNewRequestWithContext(ctx context.Context, method, url string, body io.Reader) (*http.Request, error) {
191+
return GetNewRequestWithDifferentContext(ctx, ctx, method, url, body)
192+
}
193+
194+
// GetNewRequestWithDifferentContext gets a new HTTP request with tracing
195+
// data coming from the "injectCtx" context. The "reqCtx" context will be
196+
// associated with the request.
197+
//
198+
// This method is intended to be used if you want to associate a context
199+
// with a request, and at the same time use a different context to get the
200+
// tracing info.
201+
func GetNewRequestWithDifferentContext(reqCtx, injectCtx context.Context, method, url string, body io.Reader) (*http.Request, error) {
202+
req, err := http.NewRequestWithContext(reqCtx, method, url, body)
203+
if err != nil {
204+
return req, err
205+
}
206+
207+
InjectTracingHeaders(injectCtx, req)
208+
return req, nil
209+
}
210+
211+
// InjectTracingHeaders sets the tracing info from the context as HTTP headers
212+
// in the provided request.
213+
func InjectTracingHeaders(ctx context.Context, req *http.Request) {
214+
propagator := GetPropagator()
215+
propagator.Inject(ctx, propagation.HeaderCarrier(req.Header))
216+
}

services/collaboration/pkg/connector/contentconnector.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
revactx "github.com/owncloud/reva/v2/pkg/ctx"
2222
"github.com/owncloud/reva/v2/pkg/rgrpc/todo/pool"
2323
"github.com/rs/zerolog"
24-
"go.opentelemetry.io/otel/propagation"
2524
)
2625

2726
// ContentConnectorService is the interface to implement the "File contents"
@@ -57,7 +56,7 @@ func NewContentConnector(gws pool.Selectable[gatewayv1beta1.GatewayAPIClient], c
5756
}
5857

5958
func newHttpRequest(ctx context.Context, wopiContext middleware.WopiContext, method, url, transferToken string, body io.Reader) (*http.Request, error) {
60-
httpReq, err := http.NewRequestWithContext(ctx, method, url, body)
59+
httpReq, err := tracing.GetNewRequestWithContext(ctx, method, url, body)
6160
if err != nil {
6261
return nil, err
6362
}
@@ -72,8 +71,6 @@ func newHttpRequest(ctx context.Context, wopiContext middleware.WopiContext, met
7271
} else {
7372
httpReq.Header.Add("X-Access-Token", wopiContext.AccessToken)
7473
}
75-
tracingProp := tracing.GetPropagator()
76-
tracingProp.Inject(ctx, propagation.HeaderCarrier(httpReq.Header))
7774
return httpReq, nil
7875
}
7976

services/search/pkg/content/cs3.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
1212
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
1313
"github.com/owncloud/ocis/v2/ocis-pkg/log"
14+
"github.com/owncloud/ocis/v2/ocis-pkg/tracing"
1415
revactx "github.com/owncloud/reva/v2/pkg/ctx"
1516
"github.com/owncloud/reva/v2/pkg/rgrpc/todo/pool"
1617
)
@@ -66,7 +67,7 @@ func (s cs3) Retrieve(ctx context.Context, rID *provider.ResourceId) (io.ReadClo
6667
ep, tt = res.Protocols[0].DownloadEndpoint, res.Protocols[0].Token
6768
}
6869

69-
req, err := http.NewRequest(http.MethodGet, ep, nil)
70+
req, err := tracing.GetNewRequest(ctx, http.MethodGet, ep, nil)
7071
if err != nil {
7172
return nil, err
7273
}

services/thumbnails/pkg/thumbnail/imgsource/cs3.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
1111
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
1212
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
13+
"github.com/owncloud/ocis/v2/ocis-pkg/tracing"
1314
"github.com/owncloud/ocis/v2/services/thumbnails/pkg/config"
1415
"github.com/owncloud/ocis/v2/services/thumbnails/pkg/errors"
1516
"github.com/owncloud/reva/v2/pkg/bytesize"
@@ -58,7 +59,7 @@ func (s CS3) Get(ctx context.Context, path string) (io.ReadCloser, error) {
5859
}
5960
}
6061

61-
ctx = metadata.AppendToOutgoingContext(context.Background(), revactx.TokenHeader, auth)
62+
ctx = metadata.AppendToOutgoingContext(ctx, revactx.TokenHeader, auth)
6263
err = s.checkImageFileSize(ctx, ref)
6364
if err != nil {
6465
return nil, err
@@ -89,6 +90,7 @@ func (s CS3) Get(ctx context.Context, path string) (io.ReadCloser, error) {
8990
}
9091

9192
httpReq, err := rhttp.NewRequest(ctx, "GET", ep, nil)
93+
tracing.InjectTracingHeaders(ctx, httpReq)
9294
if err != nil {
9395
return nil, err
9496
}

services/thumbnails/pkg/thumbnail/imgsource/webdav.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net/http"
1212
"strconv"
1313

14+
"github.com/owncloud/ocis/v2/ocis-pkg/tracing"
1415
"github.com/owncloud/ocis/v2/services/thumbnails/pkg/config"
1516
thumbnailerErrors "github.com/owncloud/ocis/v2/services/thumbnails/pkg/errors"
1617
"github.com/owncloud/reva/v2/pkg/bytesize"
@@ -34,7 +35,7 @@ type WebDav struct {
3435
// Get downloads the file from a webdav service
3536
// The caller MUST make sure to close the returned ReadCloser
3637
func (s WebDav) Get(ctx context.Context, url string) (io.ReadCloser, error) {
37-
req, err := http.NewRequest(http.MethodGet, url, nil)
38+
req, err := tracing.GetNewRequest(ctx, http.MethodGet, url, nil)
3839
if err != nil {
3940
return nil, errors.Wrapf(err, `could not get the image "%s"`, url)
4041
}

services/webdav/pkg/service/v0/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ func (g Webdav) sendThumbnailResponse(rsp *thumbnailssvc.GetThumbnailResponse, w
471471
// Timeout: time.Second * 5,
472472
}
473473

474-
dlReq, err := http.NewRequest(http.MethodGet, rsp.DataEndpoint, http.NoBody)
474+
dlReq, err := tracing.GetNewRequest(r.Context(), http.MethodGet, rsp.DataEndpoint, http.NoBody)
475475
if err != nil {
476476
renderError(w, r, errInternalError(err.Error()))
477477
logger.Error().Err(err).Msg("could not create download thumbnail request")

0 commit comments

Comments
 (0)