From 96c93cc6264f0fb73ae586766999452cb2f22f7e Mon Sep 17 00:00:00 2001 From: Quang-Minh Nguyen Date: Mon, 27 Feb 2023 09:42:53 +0700 Subject: Fix gitaly-hooks not exporting traces --- cmd/gitaly-hooks/hooks.go | 15 ++++++++++----- internal/git/hooks_options.go | 6 ++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/cmd/gitaly-hooks/hooks.go b/cmd/gitaly-hooks/hooks.go index 11601d29a..b29554e1b 100644 --- a/cmd/gitaly-hooks/hooks.go +++ b/cmd/gitaly-hooks/hooks.go @@ -4,11 +4,6 @@ import ( "context" "errors" "fmt" - "io" - "net" - "os" - "path/filepath" - gitalyauth "gitlab.com/gitlab-org/gitaly/v15/auth" "gitlab.com/gitlab-org/gitaly/v15/client" "gitlab.com/gitlab-org/gitaly/v15/internal/git" @@ -20,8 +15,13 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" "gitlab.com/gitlab-org/gitaly/v15/streamio" "gitlab.com/gitlab-org/labkit/tracing" + grpctracing "gitlab.com/gitlab-org/labkit/tracing/grpc" "google.golang.org/grpc" "google.golang.org/grpc/metadata" + "io" + "net" + "os" + "path/filepath" ) type hookError struct { @@ -85,6 +85,7 @@ func run(args []string) error { switch subCmd { case "git": + tracing.Initialize(tracing.WithServiceName("gitaly-hooks-git")) return executeHook(hookCommand{ exec: packObjectsHook, hookType: git.PackObjectsHook, @@ -99,6 +100,7 @@ func run(args []string) error { return fmt.Errorf("subcommand name invalid: %q", hookName) } + tracing.Initialize(tracing.WithServiceName(fmt.Sprintf("gitaly-hooks-%s", hookName))) return executeHook(hookCommand, args[1:]) } } @@ -151,6 +153,9 @@ func dialGitaly(payload git.HooksPayload) (*grpc.ClientConn, error) { dialOpts = append(dialOpts, grpc.WithPerRPCCredentials(gitalyauth.RPCCredentialsV2(payload.InternalSocketToken))) } + dialOpts = append(dialOpts, grpc.WithUnaryInterceptor(grpctracing.UnaryClientTracingInterceptor())) + dialOpts = append(dialOpts, grpc.WithStreamInterceptor(grpctracing.StreamClientTracingInterceptor())) + conn, err := client.Dial("unix://"+payload.InternalSocket, dialOpts) if err != nil { return nil, fmt.Errorf("error when dialing: %w", err) diff --git a/internal/git/hooks_options.go b/internal/git/hooks_options.go index c99f87f92..54c40a37b 100644 --- a/internal/git/hooks_options.go +++ b/internal/git/hooks_options.go @@ -12,8 +12,13 @@ import ( "gitlab.com/gitlab-org/gitaly/v15/internal/metadata/featureflag" "gitlab.com/gitlab-org/gitaly/v15/internal/transaction/txinfo" "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" + labkittracing "gitlab.com/gitlab-org/labkit/tracing" ) +// envInjector is responsible for injecting environment variables required for tracing into +// the child process. +var envInjector = labkittracing.NewEnvInjector() + // WithDisabledHooks returns an option that satisfies the requirement to set up // hooks, but won't in fact set up hook execution. func WithDisabledHooks() CmdOpt { @@ -124,6 +129,7 @@ func (cc *cmdCfg) configureHooks( payload, fmt.Sprintf("%s=%s", log.GitalyLogDirEnvKey, cfg.Logging.Dir), ) + cc.env = envInjector(ctx, cc.env) cc.globals = append(cc.globals, ConfigPair{Key: "core.hooksPath", Value: gitCmdFactory.HooksPath(ctx)}) cc.hooksConfigured = true -- cgit v1.2.3