Skip to content

Commit

Permalink
tenant: don't log missing tenant for watchdog
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhengl committed Dec 9, 2024
1 parent 5cad1d8 commit f5b4fee
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
20 changes: 16 additions & 4 deletions internal/tenant/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,29 @@ func FromContext(ctx context.Context) (*tenanttype.Tenant, error) {
return tnt, nil
}

type contextKey int

const (
skipLogging contextKey = iota
)

func WithSkipMissingLogging(ctx context.Context) context.Context {
return context.WithValue(ctx, skipLogging, skipLogging)
}

// Log logs the tenant ID to the trace. If tenant logging is enabled, it also
// logs a stack trace to a pprof profile.
func Log(ctx context.Context, tr *trace.Trace) {
tnt, ok := tenanttype.GetTenant(ctx)
if !ok {
if profile := pprofMissingTenant(); profile != nil {
// We want to track every stack trace, so need a unique value for the event
eventValue := pprofUniqID.Add(1)
if _, ok := ctx.Value(skipLogging).(contextKey); !ok {
// We want to track every stack trace, so need a unique value for the event
eventValue := pprofUniqID.Add(1)

// skip stack for Add and this function (2).
profile.Add(eventValue, 2)
// skip stack for Add and this function (2).
profile.Add(eventValue, 2)
}
}
tr.LazyPrintf("tenant: missing")
return
Expand Down
45 changes: 45 additions & 0 deletions internal/tenant/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package tenant

import (
"context"
"testing"

"github.com/stretchr/testify/require"

"github.com/sourcegraph/zoekt/internal/tenant/tenanttest"
"github.com/sourcegraph/zoekt/trace"
)

func TestLog(t *testing.T) {
tenanttest.MockEnforce(t)

cases := []struct {
name string
ctx context.Context
expectedCount int64
}{
{
name: "With Tenant",
ctx: tenanttest.NewTestContext(),
expectedCount: 0,
},
{
name: "Skip Logging",
ctx: WithSkipMissingLogging(context.Background()),
expectedCount: 0,
},
{
name: "Missing Tenant",
ctx: context.Background(),
expectedCount: 1,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
tr, ctx := trace.New(tc.ctx, "test", "test")
Log(ctx, tr)
require.Equal(t, tc.expectedCount, pprofUniqID.Load())
})
}
}
4 changes: 3 additions & 1 deletion web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ import (
"time"

"github.com/grafana/regexp"

"github.com/sourcegraph/zoekt"
"github.com/sourcegraph/zoekt/internal/tenant"
zjson "github.com/sourcegraph/zoekt/json"
"github.com/sourcegraph/zoekt/query"
)
Expand Down Expand Up @@ -206,7 +208,7 @@ func (s *Server) serveHealthz(w http.ResponseWriter, r *http.Request) {
q := &query.Const{Value: true}
opts := &zoekt.SearchOptions{ShardMaxMatchCount: 1, TotalMaxMatchCount: 1, MaxDocDisplayCount: 1}

result, err := s.Searcher.Search(r.Context(), q, opts)
result, err := s.Searcher.Search(tenant.WithSkipMissingLogging(r.Context()), q, opts)
if err != nil {
http.Error(w, fmt.Sprintf("not ready: %v", err), http.StatusInternalServerError)
return
Expand Down

0 comments on commit f5b4fee

Please sign in to comment.