Merge branch 'issue_50488' into 'master'

Move project services log to a separate file

See merge request gitlab-org/gitlab-ce!21316
This commit is contained in:
Stan Hu 2018-09-07 04:19:08 +00:00
commit c9116b6de5
13 changed files with 92 additions and 13 deletions

View File

@ -12,7 +12,8 @@ class Admin::LogsController < Admin::ApplicationController
Gitlab::GitLogger, Gitlab::GitLogger,
Gitlab::EnvironmentLogger, Gitlab::EnvironmentLogger,
Gitlab::SidekiqLogger, Gitlab::SidekiqLogger,
Gitlab::RepositoryCheckLogger Gitlab::RepositoryCheckLogger,
Gitlab::ProjectServiceLogger
] ]
end end
end end

View File

@ -0,0 +1,26 @@
module ProjectServicesLoggable
def log_info(message, params = {})
message = build_message(message, params)
logger.info(message)
end
def log_error(message, params = {})
message = build_message(message, params)
logger.error(message)
end
def build_message(message, params = {})
{
service_class: self.class.name,
project_id: project.id,
project_path: project.full_path,
message: message
}.merge(params)
end
def logger
Gitlab::ProjectServiceLogger
end
end

View File

@ -101,7 +101,7 @@ http://app.asana.com/-/account_api'
task.update(completed: true) task.update(completed: true)
end end
rescue => e rescue => e
Rails.logger.error(e.message) log_error(e.message)
next next
end end
end end

View File

@ -104,7 +104,7 @@ class IrkerService < Service
new_recipient = URI.join(default_irc_uri, '/', recipient).to_s new_recipient = URI.join(default_irc_uri, '/', recipient).to_s
uri = consider_uri(URI.parse(new_recipient)) uri = consider_uri(URI.parse(new_recipient))
rescue rescue
Rails.logger.error("Unable to create a valid URL from #{default_irc_uri} and #{recipient}") log_error("Unable to create a valid URL", default_irc_uri: default_irc_uri, recipient: recipient)
end end
end end

View File

@ -88,7 +88,7 @@ class IssueTrackerService < Service
rescue Gitlab::HTTP::Error, Timeout::Error, SocketError, Errno::ECONNRESET, Errno::ECONNREFUSED, OpenSSL::SSL::SSLError => error rescue Gitlab::HTTP::Error, Timeout::Error, SocketError, Errno::ECONNRESET, Errno::ECONNREFUSED, OpenSSL::SSL::SSLError => error
message = "#{self.type} had an error when trying to connect to #{self.project_url}: #{error.message}" message = "#{self.type} had an error when trying to connect to #{self.project_url}: #{error.message}"
end end
Rails.logger.info(message) log_info(message)
result result
end end

View File

@ -205,7 +205,7 @@ class JiraService < IssueTrackerService
begin begin
issue.transitions.build.save!(transition: { id: transition_id }) issue.transitions.build.save!(transition: { id: transition_id })
rescue => error rescue => error
Rails.logger.info "#{self.class.name} Issue Transition failed message ERROR: #{client_url} - #{error.message}" log_error("Issue transition failed", error: error.message, client_url: client_url)
return false return false
end end
end end
@ -257,9 +257,8 @@ class JiraService < IssueTrackerService
new_remote_link.save!(remote_link_props) new_remote_link.save!(remote_link_props)
end end
result_message = "#{self.class.name} SUCCESS: Successfully posted to #{client_url}." log_info("Successfully posted", client_url: client_url)
Rails.logger.info(result_message) "SUCCESS: Successfully posted to http://jira.example.net."
result_message
end end
end end
@ -317,7 +316,7 @@ class JiraService < IssueTrackerService
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, URI::InvalidURIError, JIRA::HTTPError, OpenSSL::SSL::SSLError => e rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, URI::InvalidURIError, JIRA::HTTPError, OpenSSL::SSL::SSLError => e
@error = e.message @error = e.message
Rails.logger.info "#{self.class.name} Send message ERROR: #{client_url} - #{@error}" log_error("Error sending message", client_url: client_url, error: @error)
nil nil
end end

View File

@ -5,6 +5,7 @@
class Service < ActiveRecord::Base class Service < ActiveRecord::Base
include Sortable include Sortable
include Importable include Importable
include ProjectServicesLoggable
serialize :properties, JSON # rubocop:disable Cop/ActiveRecordSerialize serialize :properties, JSON # rubocop:disable Cop/ActiveRecordSerialize

View File

@ -0,0 +1,5 @@
---
title: Move project services log to a separate file
merge_request:
author:
type: other

View File

@ -113,6 +113,19 @@ October 07, 2014 11:25: User "Claudie Hodkiewicz" (nasir_stehr@olson.co.uk) was
October 07, 2014 11:25: Project "project133" was removed October 07, 2014 11:25: Project "project133" was removed
``` ```
## `integrations_json.log`
This file lives in `/var/log/gitlab/gitlab-rails/integrations_json.log` for
Omnibus GitLab packages or in `/home/git/gitlab/log/integrations_json.log` for
installations from source.
It contains information about [integrations](../user/project/integrations/project_services.md) activities such as JIRA, Asana and Irker services. It uses JSON format like the example below:
``` json
{"severity":"ERROR","time":"2018-09-06T14:56:20.439Z","service_class":"JiraService","project_id":8,"project_path":"h5bp/html5-boilerplate","message":"Error sending message","client_url":"http://jira.gitlap.com:8080","error":"execution expired"}
{"severity":"INFO","time":"2018-09-06T17:15:16.365Z","service_class":"JiraService","project_id":3,"project_path":"namespace2/project2","message":"Successfully posted","client_url":"http://jira.example.net"}
```
## `githost.log` ## `githost.log`
This file lives in `/var/log/gitlab/gitlab-rails/githost.log` for This file lives in `/var/log/gitlab/gitlab-rails/githost.log` for

View File

@ -0,0 +1,7 @@
module Gitlab
class ProjectServiceLogger < Gitlab::JsonLogger
def self.file_name_noext
'integrations_json'
end
end
end

View File

@ -231,12 +231,12 @@ describe JiraService do
end end
it 'logs exception when transition id is not valid' do it 'logs exception when transition id is not valid' do
allow(Rails.logger).to receive(:info) allow(@jira_service).to receive(:log_error)
WebMock.stub_request(:post, @transitions_url).with(basic_auth: %w(gitlab_jira_username gitlab_jira_password)).and_raise('Bad Request') WebMock.stub_request(:post, @transitions_url).with(basic_auth: %w(gitlab_jira_username gitlab_jira_password)).and_raise("Bad Request")
@jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project)) @jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
expect(Rails.logger).to have_received(:info).with('JiraService Issue Transition failed message ERROR: http://jira.example.com - Bad Request') expect(@jira_service).to have_received(:log_error).with("Issue transition failed", error: "Bad Request", client_url: "http://jira.example.com")
end end
it 'calls the api with jira_issue_transition_id' do it 'calls the api with jira_issue_transition_id' do

View File

@ -345,4 +345,31 @@ describe Service do
expect(service.api_field_names).to eq(['safe_field']) expect(service.api_field_names).to eq(['safe_field'])
end end
end end
context 'logging' do
let(:project) { create(:project) }
let(:service) { create(:service, project: project) }
let(:test_message) { "test message" }
let(:arguments) do
{
service_class: service.class.name,
project_path: project.full_path,
project_id: project.id,
message: test_message,
additional_argument: 'some argument'
}
end
it 'logs info messages using json logger' do
expect(Gitlab::JsonLogger).to receive(:info).with(arguments)
service.log_info(test_message, additional_argument: 'some argument')
end
it 'logs error messages using json logger' do
expect(Gitlab::JsonLogger).to receive(:error).with(arguments)
service.log_error(test_message, additional_argument: 'some argument')
end
end
end end

View File

@ -725,7 +725,7 @@ describe SystemNoteService do
let(:jira_tracker) { project.jira_service } let(:jira_tracker) { project.jira_service }
let(:commit) { project.commit } let(:commit) { project.commit }
let(:comment_url) { jira_api_comment_url(jira_issue.id) } let(:comment_url) { jira_api_comment_url(jira_issue.id) }
let(:success_message) { "JiraService SUCCESS: Successfully posted to http://jira.example.net." } let(:success_message) { "SUCCESS: Successfully posted to http://jira.example.net." }
before do before do
stub_jira_urls(jira_issue.id) stub_jira_urls(jira_issue.id)