Skip to content

Commit 0b4fd0a

Browse files
committed
Merge branch '59-git-tracing' into 'master'
Enable GIT_TRACE_PERFORMANCE through a config variable. The value of the variable must an absolute path needs to exist so we’re able to check if we can write in that file. Because in the case we cannot write we’ll throw a warning to the output of the users. ```sh ~/dev/gitlab/local/pacoguzman/gitlab-ce (master=)$ git push origin master warning: could not open '/wadus' for tracing: Permission denied Everything up-to-date ``` Closes #59 See merge request !91
2 parents b71ca5d + 192e2bd commit 0b4fd0a

File tree

5 files changed

+107
-1
lines changed

5 files changed

+107
-1
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
v3.6.2
2+
- Enable GIT_TRACE/GIT_TRACE_PACKET/GIT_TRACE_PERFORMANCE by providing the git_trace_log_file config key
3+
14
v3.6.1
25
- Set a low IO priority for storage moves to lower performance impact
36

config.yml.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,10 @@ audit_usernames: false
6868
# For Debian and Ubuntu systems this can be done with: sudo apt-get install git-annex
6969
# For CentOS: sudo yum install epel-release && sudo yum install git-annex
7070
git_annex_enabled: false
71+
72+
# Git trace log file.
73+
# If set, git commands receive GIT_TRACE* environment variables
74+
# See https://git-scm.com/book/es/v2/Git-Internals-Environment-Variables#Debugging for documentation
75+
# An absolute path starting with / – the trace output will be appended to that file.
76+
# It needs to exist so we can check permissions and avoid to throwing warnings to the users.
77+
git_trace_log_file:

lib/gitlab_config.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,8 @@ def audit_usernames
5050
def git_annex_enabled?
5151
@config['git_annex_enabled'] ||= false
5252
end
53+
54+
def git_trace_log_file
55+
@config['git_trace_log_file']
56+
end
5357
end

lib/gitlab_shell.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'shellwords'
2+
require 'pathname'
23

34
require_relative 'gitlab_net'
45

@@ -150,6 +151,14 @@ def exec_cmd(*args)
150151
env.merge!({ 'GIT_ANNEX_SHELL_LIMITED' => '1' })
151152
end
152153

154+
if git_trace_available?
155+
env.merge!({
156+
'GIT_TRACE' => @config.git_trace_log_file,
157+
'GIT_TRACE_PACKET' => @config.git_trace_log_file,
158+
'GIT_TRACE_PERFORMANCE' => @config.git_trace_log_file,
159+
})
160+
end
161+
153162
Kernel::exec(env, *args, unsetenv_others: true)
154163
end
155164

@@ -232,6 +241,23 @@ def api_2fa_recovery_codes
232241
end
233242
end
234243

244+
def git_trace_available?
245+
return false unless @config.git_trace_log_file
246+
247+
if Pathname(@config.git_trace_log_file).relative?
248+
$logger.warn "gitlab-shell: is configured to trace git commands with #{@config.git_trace_log_file.inspect} but an absolute path needs to be provided"
249+
return false
250+
end
251+
252+
begin
253+
File.open(@config.git_trace_log_file, 'a') { nil }
254+
return true
255+
rescue => ex
256+
$logger.warn "gitlab-shell: is configured to trace git commands with #{@config.git_trace_log_file.inspect} but it's not possible to write in that path #{ex.message}"
257+
return false
258+
end
259+
end
260+
235261
def repo_path=(repo_path)
236262
raise ArgumentError, "Repository path not provided. Please make sure you're using GitLab v8.10 or later." unless repo_path
237263
raise InvalidRepositoryPathError if File.absolute_path(repo_path) != repo_path

spec/gitlab_shell_spec.rb

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@
361361

362362
describe :exec_cmd do
363363
let(:shell) { GitlabShell.new(key_id) }
364-
before { Kernel.stub!(:exec) }
364+
before { Kernel.stub(:exec) }
365365

366366
it "uses Kernel::exec method" do
367367
Kernel.should_receive(:exec).with(kind_of(Hash), 1, 2, unsetenv_others: true).once
@@ -376,6 +376,72 @@
376376
Kernel.should_receive(:exec).with(kind_of(Hash), [1, 2], unsetenv_others: true).once
377377
shell.send :exec_cmd, [1, 2]
378378
end
379+
380+
context "when specifying a git_tracing log file" do
381+
let(:git_trace_log_file) { '/tmp/git_trace_performance.log' }
382+
383+
before do
384+
GitlabConfig.any_instance.stub(git_trace_log_file: git_trace_log_file)
385+
shell
386+
end
387+
388+
it "uses GIT_TRACE_PERFORMANCE" do
389+
expected_hash = hash_including(
390+
'GIT_TRACE' => git_trace_log_file,
391+
'GIT_TRACE_PACKET' => git_trace_log_file,
392+
'GIT_TRACE_PERFORMANCE' => git_trace_log_file
393+
)
394+
Kernel.should_receive(:exec).with(expected_hash, [1, 2], unsetenv_others: true).once
395+
396+
shell.send :exec_cmd, [1, 2]
397+
end
398+
399+
context "when provides a relative path" do
400+
let(:git_trace_log_file) { 'git_trace_performance.log' }
401+
402+
it "does not uses GIT_TRACE*" do
403+
# If we try to use it we'll show a warning to the users
404+
expected_hash = hash_excluding(
405+
'GIT_TRACE', 'GIT_TRACE_PACKET', 'GIT_TRACE_PERFORMANCE'
406+
)
407+
Kernel.should_receive(:exec).with(expected_hash, [1, 2], unsetenv_others: true).once
408+
409+
shell.send :exec_cmd, [1, 2]
410+
end
411+
412+
it "writes an entry on the log" do
413+
expect($logger).to receive(:warn).
414+
with("gitlab-shell: is configured to trace git commands with #{git_trace_log_file.inspect} but an absolute path needs to be provided")
415+
416+
Kernel.should_receive(:exec).with(kind_of(Hash), [1, 2], unsetenv_others: true).once
417+
shell.send :exec_cmd, [1, 2]
418+
end
419+
end
420+
421+
context "when provides a file not writable" do
422+
before do
423+
expect(File).to receive(:open).with(git_trace_log_file, 'a').and_raise(Errno::EACCES)
424+
end
425+
426+
it "does not uses GIT_TRACE*" do
427+
# If we try to use it we'll show a warning to the users
428+
expected_hash = hash_excluding(
429+
'GIT_TRACE', 'GIT_TRACE_PACKET', 'GIT_TRACE_PERFORMANCE'
430+
)
431+
Kernel.should_receive(:exec).with(expected_hash, [1, 2], unsetenv_others: true).once
432+
433+
shell.send :exec_cmd, [1, 2]
434+
end
435+
436+
it "writes an entry on the log" do
437+
expect($logger).to receive(:warn).
438+
with("gitlab-shell: is configured to trace git commands with #{git_trace_log_file.inspect} but it's not possible to write in that path Permission denied")
439+
440+
Kernel.should_receive(:exec).with(kind_of(Hash), [1, 2], unsetenv_others: true).once
441+
shell.send :exec_cmd, [1, 2]
442+
end
443+
end
444+
end
379445
end
380446

381447
describe :api do

0 commit comments

Comments
 (0)