Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions modules/querier/external/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/weaveworks/common/user"
"golang.org/x/oauth2"
)

var (
Expand Down Expand Up @@ -54,20 +53,13 @@ type Client struct {

type CloudRunConfig struct {
Endpoints []string `yaml:"external_endpoints"`
NoAuth bool // For testing
}

type HTTPConfig struct {
Endpoints []string
}

type tokenProvider interface {
// Returns an oauth2 token, leveraging a cache unless the token is expired.
// If expired, the token is renewed and added to the cache.
//
// If this returns nil, the request will be unauthenticated.
getToken(ctx context.Context, endpoint string) (*oauth2.Token, error)
}

type option func(client *Client) error

func withTokenProvider(provider tokenProvider) option {
Expand All @@ -90,7 +82,7 @@ func NewClient(cfg *Config) (*Client, error) {
hedgeRequestsUpTo: cfg.HedgeRequestsUpTo,
})
case "google_cloud_run":
provider, err := newGoogleProvider(ctx, cfg.CloudRunConfig.Endpoints)
provider, err := newGoogleProvider(ctx, cfg.CloudRunConfig.Endpoints, cfg.CloudRunConfig.NoAuth)
if err != nil {
return nil, err
}
Expand Down
14 changes: 13 additions & 1 deletion modules/querier/external/token_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import (
"google.golang.org/api/idtoken"
)

type tokenProvider interface {
// Returns an oauth2 token, leveraging a cache unless the token is expired.
// If expired, the token is renewed and added to the cache.
//
// If this returns nil, the request will be unauthenticated.
getToken(ctx context.Context, endpoint string) (*oauth2.Token, error)
}

// Caches an oauth2.TokenSource to enable efficient auth on each of our external
// endpoints.
type cachedTokenProvider struct {
Expand Down Expand Up @@ -59,7 +67,11 @@ func (t *cachedTokenProvider) getToken(_ context.Context, endpoint string) (*oau
return nil, fmt.Errorf("endpoint is not configured: %s", endpoint)
}

func newGoogleProvider(ctx context.Context, endpoints []string) (*cachedTokenProvider, error) {
func newGoogleProvider(ctx context.Context, endpoints []string, noAuth bool) (tokenProvider, error) {
if noAuth {
return &nilTokenProvider{}, nil
}

return newTokenProvider(ctx, endpoints, func(ctx context.Context, endpoint string) (oauth2.TokenSource, error) {
return idtoken.NewTokenSource(ctx, endpoint)
})
Expand Down
2 changes: 1 addition & 1 deletion modules/querier/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ func valuesToV2Response(distinctValues *util.DistinctValueCollector[tempopb.TagV
// SearchBlock searches the specified subset of the block for the passed tags.
func (q *Querier) SearchBlock(ctx context.Context, req *tempopb.SearchBlockRequest) (*tempopb.SearchResponse, error) {
// if we have no external configuration always search in the querier
if len(q.cfg.Search.ExternalEndpoints) == 0 {
if q.cfg.Search.ExternalBackend == "" && len(q.cfg.Search.ExternalEndpoints) == 0 {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this can be a cleaner check in the future, but i'm fine with it for now.

return q.internalSearchBlock(ctx, req)
}

Expand Down
14 changes: 14 additions & 0 deletions modules/querier/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
generator_client "github.com/grafana/tempo/modules/generator/client"
ingester_client "github.com/grafana/tempo/modules/ingester/client"
"github.com/grafana/tempo/modules/overrides"
"github.com/grafana/tempo/modules/querier/external"
"github.com/grafana/tempo/pkg/tempopb"
"github.com/stretchr/testify/require"
"github.com/uber-go/atomic"
Expand Down Expand Up @@ -47,6 +48,19 @@ func TestQuerierUsesSearchExternalEndpoint(t *testing.T) {
queriesToExecute: 3,
externalExpected: 0,
},
{
cfg: Config{
Search: SearchConfig{
ExternalBackend: "google_cloud_run",
CloudRun: &external.CloudRunConfig{
Endpoints: []string{srv.URL},
NoAuth: true,
},
},
},
queriesToExecute: 1,
externalExpected: 1,
},
// SearchPreferSelf is respected. this test won't pass b/c SearchBlock fails instantly and so
// all 3 queries are executed locally and nothing is proxied to the external endpoint.
// we'd have to mock the storage.Store interface to get this to pass. it's a big interface.
Expand Down