From abf508470ef1e667bcfa42c4e9b825bcce06e60c Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Thu, 6 Aug 2020 03:10:05 +0000 Subject: [PATCH] Add latest changes from gitlab-org/gitlab@master --- app/services/branches/create_service.rb | 5 +- .../log_pre_receive_on_create_branch.yml | 5 ++ doc/ci/cloud_deployment/index.md | 54 +++++++++++++++---- .../metrics/elasticsearch_rack_middleware.rb | 4 +- .../elasticsearch_rack_middleware_spec.rb | 10 ++++ spec/services/branches/create_service_spec.rb | 20 +++++++ 6 files changed, 84 insertions(+), 14 deletions(-) create mode 100644 changelogs/unreleased/log_pre_receive_on_create_branch.yml diff --git a/app/services/branches/create_service.rb b/app/services/branches/create_service.rb index 958dd5c9965..8684da701db 100644 --- a/app/services/branches/create_service.rb +++ b/app/services/branches/create_service.rb @@ -16,8 +16,9 @@ module Branches else error("Invalid reference name: #{ref}") end - rescue Gitlab::Git::PreReceiveError => ex - error(ex.message) + rescue Gitlab::Git::PreReceiveError => e + Gitlab::ErrorTracking.track_exception(e, pre_receive_message: e.raw_message, branch_name: branch_name, ref: ref) + error(e.message) end def success(branch) diff --git a/changelogs/unreleased/log_pre_receive_on_create_branch.yml b/changelogs/unreleased/log_pre_receive_on_create_branch.yml new file mode 100644 index 00000000000..d50cc188536 --- /dev/null +++ b/changelogs/unreleased/log_pre_receive_on_create_branch.yml @@ -0,0 +1,5 @@ +--- +title: Log raw pre receive error for create branch service +merge_request: 38749 +author: +type: other diff --git a/doc/ci/cloud_deployment/index.md b/doc/ci/cloud_deployment/index.md index a2dea5006ca..9ebb07b2e91 100644 --- a/doc/ci/cloud_deployment/index.md +++ b/doc/ci/cloud_deployment/index.md @@ -97,11 +97,25 @@ Before getting started with this process, you need a cluster on AWS ECS, as well components, like an ECS service, ECS task definition, a database on AWS RDS, etc. [Read more about AWS ECS](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/Welcome.html). -After you're all set up on AWS ECS, follow these steps: +The ECS task definition can be: + +- An existing task definition in AWS ECS +- A JSON file containing a task definition. Create the JSON file by using the template provided in + the [AWS documentation](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/create-task-definition.html#task-definition-template). + Copy the task definition into a new file in your project, for example `/ci/aws/task-definition.json`. + [Available](https://gitlab.com/gitlab-org/gitlab/-/issues/222618) in GitLab 13.3 and later. + +After you have these prerequisites ready, follow these steps: 1. Make sure your AWS credentials are set up as environment variables for your project. You can follow [the steps above](#run-aws-commands-from-gitlab-cicd) to complete this setup. -1. Add these variables to your project's `.gitlab-ci.yml` file: +1. Add these variables to your project's `.gitlab-ci.yml` file, or in the project's + [CI/CD settings](../variables/README.md#create-a-custom-variable-in-the-ui): + + - `CI_AWS_ECS_CLUSTER`: The name of the AWS ECS cluster that you're targeting for your deployments. + - `CI_AWS_ECS_SERVICE`: The name of the targeted service tied to your AWS ECS cluster. + - `CI_AWS_ECS_TASK_DEFINITION`: The name of an existing task definition in ECS tied + to the service mentioned above. ```yaml variables: @@ -110,19 +124,37 @@ After you're all set up on AWS ECS, follow these steps: CI_AWS_ECS_TASK_DEFINITION: my-task-definition ``` - Three variables are defined in this snippet: - - - `CI_AWS_ECS_CLUSTER`: The name of your AWS ECS cluster that you're - targeting for your deployments. - - `CI_AWS_ECS_SERVICE`: The name of the targeted service tied to - your AWS ECS cluster. - - `CI_AWS_ECS_TASK_DEFINITION`: The name of the task definition tied - to the service mentioned above. - You can find these names after selecting the targeted cluster on your [AWS ECS dashboard](https://console.aws.amazon.com/ecs/home): ![AWS ECS dashboard](../img/ecs_dashboard_v12_9.png) + Alternatively, if you want to use a task definition defined in a JSON file, use + `CI_AWS_ECS_TASK_DEFINITION_FILE` instead: + + ```yaml + variables: + CI_AWS_ECS_CLUSTER: my-cluster + CI_AWS_ECS_SERVICE: my-service + CI_AWS_ECS_TASK_DEFINITION_FILE: ci/aws/my_task_definition.json + ``` + + You can create your `CI_AWS_ECS_TASK_DEFINITION_FILE` variable as a + [file-typed environment variable](../variables/README.md#custom-environment-variables-of-type-file) instead of a + regular environment variable. If you choose to do so, set the variable value to be the full contents of + the JSON task definition. You can then remove the JSON file from your project. + + In both cases, make sure that the value for the `containerDefinitions[].name` attribute is + the same as the `Container name` defined in your targeted ECS service. + + CAUTION: **Warning:** + `CI_AWS_ECS_TASK_DEFINITION_FILE` takes precedence over `CI_AWS_ECS_TASK_DEFINITION` if both these environment + variables are defined within your project. + + NOTE: **Note:** + If the name of the task definition you wrote in your JSON file is the same name + as an existing task definition on AWS, then a new revision is created for it. + Otherwise, a brand new task definition is created, starting at revision 1. + 1. Include this template in `.gitlab-ci.yml`: ```yaml diff --git a/lib/gitlab/metrics/elasticsearch_rack_middleware.rb b/lib/gitlab/metrics/elasticsearch_rack_middleware.rb index 5e99fb3c75f..f4bdb08437e 100644 --- a/lib/gitlab/metrics/elasticsearch_rack_middleware.rb +++ b/lib/gitlab/metrics/elasticsearch_rack_middleware.rb @@ -4,7 +4,7 @@ module Gitlab module Metrics # Rack middleware for tracking Elasticsearch metrics from Grape and Web requests. class ElasticsearchRackMiddleware - HISTOGRAM_BUCKETS = [0.1, 0.25, 0.5, 1, 2.5, 5, 10, 60].freeze + HISTOGRAM_BUCKETS = [0.1, 0.5, 1, 10, 50].freeze def initialize(app) @app = app @@ -28,6 +28,8 @@ module Gitlab docstring 'Amount of calls to Elasticsearch servers during web requests' end + return unless request_count > 0 + transaction.observe(:http_elasticsearch_requests_duration_seconds, query_time) do docstring 'Query time for Elasticsearch servers during web requests' buckets HISTOGRAM_BUCKETS diff --git a/spec/lib/gitlab/metrics/elasticsearch_rack_middleware_spec.rb b/spec/lib/gitlab/metrics/elasticsearch_rack_middleware_spec.rb index b6ad9c3e54e..94f99f3ef64 100644 --- a/spec/lib/gitlab/metrics/elasticsearch_rack_middleware_spec.rb +++ b/spec/lib/gitlab/metrics/elasticsearch_rack_middleware_spec.rb @@ -38,5 +38,15 @@ RSpec.describe Gitlab::Metrics::ElasticsearchRackMiddleware do expect { middleware.call(env) }.to raise_error(StandardError) end + + context 'when there are no elasticsearch requests' do + let(:elasticsearch_requests_count) { 0 } + + it 'does not record duration if there were no ES calls' do + expect(transaction).not_to receive(:observe).with(:http_elasticsearch_requests_duration_seconds) + + middleware.call(env) + end + end end end diff --git a/spec/services/branches/create_service_spec.rb b/spec/services/branches/create_service_spec.rb index b682a3f26ec..5cf0d5af75f 100644 --- a/spec/services/branches/create_service_spec.rb +++ b/spec/services/branches/create_service_spec.rb @@ -44,5 +44,25 @@ RSpec.describe Branches::CreateService do expect(result[:message]).to eq('Invalid reference name: unknown') end end + + it 'logs and returns an error if there is a PreReceiveError exception' do + error_message = 'pre receive error' + raw_message = "GitLab: #{error_message}" + pre_receive_error = Gitlab::Git::PreReceiveError.new(raw_message) + + allow(project.repository).to receive(:add_branch).and_raise(pre_receive_error) + + expect(Gitlab::ErrorTracking).to receive(:track_exception).with( + pre_receive_error, + pre_receive_message: raw_message, + branch_name: 'new-feature', + ref: 'unknown' + ) + + result = service.execute('new-feature', 'unknown') + + expect(result[:status]).to eq(:error) + expect(result[:message]).to eq(error_message) + end end end