Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 6a28eb8

Browse files
tenant: set pprof label for tenant (#64338)
In the future this will allow us to attribute stack traces collected by pprof to a tenant. This only sets it for the http middleware. I am unsure how to achieve the same thing for grpc, since that uses propogators. Test Plan: captured a goroutine profile and saw some goroutines with a tenant label --------- Co-authored-by: Erik Seliger <[email protected]>
1 parent 520444e commit 6a28eb8

File tree

6 files changed

+51
-4
lines changed

6 files changed

+51
-4
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ require (
284284
github.com/google/go-containerregistry v0.16.1
285285
github.com/google/go-github/v48 v48.2.0
286286
github.com/google/go-github/v55 v55.0.0
287+
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
287288
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.0-rc.0
288289
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0
289290
github.com/hashicorp/cronexpr v1.1.1
@@ -411,7 +412,6 @@ require (
411412
github.com/gosimple/slug v1.12.0 // indirect
412413
github.com/gosimple/unidecode v1.0.1 // indirect
413414
github.com/grafana-tools/sdk v0.0.0-20220919052116-6562121319fc // indirect
414-
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
415415
github.com/hashicorp/errwrap v1.1.0 // indirect
416416
github.com/hashicorp/go-multierror v1.1.1 // indirect
417417
github.com/hashicorp/go-slug v0.12.1 // indirect

internal/grpc/defaults/defaults.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ func buildServerOptions(logger log.Logger, opts serverOptions) []grpc.ServerOpti
185185
propagator.StreamServerPropagator(tenant.TenantPropagator{}),
186186
propagator.StreamServerPropagator(actor.ActorPropagator{}),
187187
propagator.StreamServerPropagator(policy.ShouldTracePropagator{}),
188+
tenant.StreamServerInterceptor,
188189
otelgrpc.StreamServerInterceptor(otelOpts...), //lint:ignore SA1019 the advertised replacement doesn't seem to be a drop-in replacement, use deprecated mechanism for now
189190
contextconv.StreamServerInterceptor,
190191
),
@@ -198,6 +199,7 @@ func buildServerOptions(logger log.Logger, opts serverOptions) []grpc.ServerOpti
198199
propagator.UnaryServerPropagator(tenant.TenantPropagator{}),
199200
propagator.UnaryServerPropagator(actor.ActorPropagator{}),
200201
propagator.UnaryServerPropagator(policy.ShouldTracePropagator{}),
202+
tenant.UnaryServerInterceptor,
201203
otelgrpc.UnaryServerInterceptor(otelOpts...), //lint:ignore SA1019 the advertised replacement doesn't seem to be a drop-in replacement, use deprecated mechanism for now
202204
contextconv.UnaryServerInterceptor,
203205
),

internal/tenant/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ go_library(
1919
"//internal/testutil",
2020
"//internal/trace",
2121
"//lib/errors",
22+
"@com_github_grpc_ecosystem_go_grpc_middleware//:go-grpc-middleware",
2223
"@com_github_sourcegraph_log//:log",
24+
"@org_golang_google_grpc//:go_default_library",
2325
"@org_golang_google_grpc//codes",
2426
"@org_golang_google_grpc//metadata",
2527
"@org_golang_google_grpc//status",

internal/tenant/externalhttp.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7+
"runtime/pprof"
8+
"strconv"
79
"strings"
810

911
"github.com/sourcegraph/sourcegraph/internal/errcode"
@@ -37,7 +39,10 @@ func ExternalTenantFromHostnameMiddleware(tenantForHostname TenantHostnameMapper
3739
}
3840

3941
ctx = withTenant(ctx, tenantID)
40-
next.ServeHTTP(rw, req.WithContext(ctx))
42+
43+
pprof.Do(ctx, pprof.Labels("tenant", strconv.Itoa(tenantID)), func(ctx context.Context) {
44+
next.ServeHTTP(rw, req.WithContext(ctx))
45+
})
4146
})
4247
}
4348

internal/tenant/grpc.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ package tenant
22

33
import (
44
"context"
5+
"runtime/pprof"
56
"strconv"
67

8+
"google.golang.org/grpc"
79
"google.golang.org/grpc/codes"
810
"google.golang.org/grpc/metadata"
911
"google.golang.org/grpc/status"
1012

13+
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
14+
1115
"github.com/sourcegraph/sourcegraph/lib/errors"
1216
)
1317

@@ -48,3 +52,33 @@ func (TenantPropagator) InjectContext(ctx context.Context, md metadata.MD) (cont
4852
return withTenant(ctx, uid), nil
4953
}
5054
}
55+
56+
// UnaryServerInterceptor is a grpc.UnaryServerInterceptor that injects the tenant ID
57+
// from the context into pprof labels.
58+
func UnaryServerInterceptor(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (response any, err error) {
59+
if tnt, err := FromContext(ctx); err == nil {
60+
defer pprof.SetGoroutineLabels(ctx)
61+
ctx = pprof.WithLabels(ctx, pprof.Labels("tenant", strconv.Itoa(tnt.ID())))
62+
pprof.SetGoroutineLabels(ctx)
63+
}
64+
65+
return handler(ctx, req)
66+
}
67+
68+
// StreamServerInterceptor is a grpc.StreamServerInterceptor that injects the tenant ID
69+
// from the context into pprof labels.
70+
func StreamServerInterceptor(srv any, ss grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
71+
if tnt, err := FromContext(ss.Context()); err == nil {
72+
ctx := ss.Context()
73+
defer pprof.SetGoroutineLabels(ctx)
74+
ctx = pprof.WithLabels(ctx, pprof.Labels("tenant", strconv.Itoa(tnt.ID())))
75+
pprof.SetGoroutineLabels(ctx)
76+
77+
ss = &grpc_middleware.WrappedServerStream{
78+
ServerStream: ss,
79+
WrappedContext: ctx,
80+
}
81+
}
82+
83+
return handler(srv, ss)
84+
}

internal/tenant/http.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package tenant
22

33
import (
4+
"context"
45
"fmt"
56
"net/http"
7+
"runtime/pprof"
68
"strconv"
79

810
"github.com/sourcegraph/log"
@@ -84,8 +86,10 @@ func InternalHTTPMiddleware(logger log.Logger, next http.Handler) http.Handler {
8486

8587
// Valid tenant
8688
ctx = withTenant(ctx, tntID)
87-
}
8889

89-
next.ServeHTTP(rw, req.WithContext(ctx))
90+
pprof.Do(ctx, pprof.Labels("tenant", strconv.Itoa(tntID)), func(ctx context.Context) {
91+
next.ServeHTTP(rw, req.WithContext(ctx))
92+
})
93+
}
9094
})
9195
}

0 commit comments

Comments
 (0)