Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make logging under control #3

Open
wants to merge 5 commits into
base: rabbitmq_synchro
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/config/settings.local.yml
/config/settings/*.local.yml

# Used instead of dotenv library to load environment variables.
/.env.rb
Expand All @@ -8,3 +9,4 @@
/Gemfile.lock

/log
!/log/.keep
7 changes: 7 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,10 @@ Metrics/BlockLength:
Exclude:
- Rakefile
- app/routes/**/*

Style/ClassVars:
Enabled: false

Style/FrozenStringLiteralComment:
Exclude:
- .env.rb
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ gem 'faraday'

gem 'bunny'

gem 'ougai', require: 'ougai'

group :development do
gem 'pry'
end

group :development, :test do
gem 'amazing_print'
gem 'rubocop', require: false
gem 'rubocop-rspec', require: false
end
Expand Down
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Geocoder Microservice (synchronous RabbitMQ)
Geocoder microservice for RabbitMQ synchronous [Ads Microservice](https://github.com/rubygitflow/ads_microservice/tree/rabbitmq_synchro) from Ruby Microservices course
# Geocoder Microservice (on RabbitMQ)
Geocoder microservice for [Ads Microservice](https://github.com/rubygitflow/ads_microservice/tree/logging) on RabbitMQ from Ruby Microservices course

It's set up so you can clone this repository and base your application on it:
```bash
$ git clone git@github.com:rubygitflow/geocoder_microservice.git app_geo --single-branch --branch rabbitmq_synchro && cd app_geo && rm -r -f .git/
$ git clone git@github.com:rubygitflow/geocoder_microservice.git app_geo --single-branch --branch logging && cd app_geo && rm -r -f .git/
```
Initialize and configure a new Git repository (you need to have a [personal access token](https://github.com/settings/tokens)):
```bash
Expand All @@ -24,6 +24,14 @@ $ git push -u origin master
```
For more details, see the [github docs](https://docs.github.com/en/rest/repos/repos#create-a-repository-for-the-authenticated-user)

Add config file `.env.rb` with correct data:
```ruby
ENV['RABBITMQ_HOST']='127.0.0.1'
ENV['RABBITMQ_USER']='RabbitMQ_UserName'
ENV['RABBITMQ_PASSWORD']='RabbitMQ_Password'
ENV['ADS_URL'] = 'http://localhost:3001/api/v1'
```

## Environment setup
```bash
$ bundle install
Expand All @@ -33,16 +41,13 @@ $ bundle install
You can either set up configuration into `config/initializers/config.rb`, `config/settings/*.yml` and `config/settings.yml` or `config/settings.local.yml` before running

```bash
$ bin/app
$ bin/console
```
or run the application with modified configuration using environment variables as well
```bash
$ RACK_ENV=test bin/console
```

## HTTP-requests to the app
```bash
$ bin/app
$ LOG_SERVICE=stdout bin/app
```

## Run tests
Expand Down
2 changes: 1 addition & 1 deletion app/lib/ads_service/rpc_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def publish(payload, opts = {})
@queue.publish(
payload,
opts.merge(
app_id: 'geocoder',
app_id: Settings.app.name,
correlation_id: @correlation_id,
reply_to: @reply_queue.name
)
Expand Down
2 changes: 1 addition & 1 deletion app/lib/geocoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def geocode(city)
end

def data
@data ||= load_data!
@@data ||= load_data!
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вообще, переменные класса лучше не использовать из-за их побочных эффектов. Если у нас модуль Geocoder делает extend self и будет вызываться из разных трэдов, в этом нет проблемы, потому что данные будут только вычитываться и race condition здесь не страшен.

end

private
Expand Down
6 changes: 5 additions & 1 deletion app/lib/rabbit_mq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ module RabbitMq

def connection
@mutex.synchronize do
@connection ||= Bunny.new.start
@connection ||= Bunny.new(
host: Settings.rabbitmq.host,
username: Settings.rabbitmq.username,
password: Settings.rabbitmq.password
).start
end
end

Expand Down
14 changes: 9 additions & 5 deletions config/application.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# frozen_string_literal: true

class Application
def self.root
ApplicationLoader.root
end
class << self
attr_accessor :logger

def root
ApplicationLoader.root
end

def self.environment
ENV.fetch('RACK_ENV').to_sym
def environment
ENV.fetch('RACK_ENV').to_sym
end
end
end
5 changes: 5 additions & 0 deletions config/application_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module ApplicationLoader

def load_app!
init_config
init_logger
require_app
init_app
end
Expand All @@ -19,6 +20,10 @@ def init_config
require_file 'config/initializers/config'
end

def init_logger
require_file 'config/initializers/logger'
end

def require_app
require_file 'config/application'
require_dir 'app/contracts'
Expand Down
6 changes: 6 additions & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

ENV['RACK_ENV'] ||= 'development'

begin
require_relative '../.env'
rescue LoadError
# do nothing
end

require 'bundler/setup'
Bundler.require(:default, ENV['RACK_ENV'])

Expand Down
9 changes: 8 additions & 1 deletion config/initializers/consumer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@
queue = channel.queue('geocoding', durable: true)

# https://www.rabbitmq.com/tutorials/tutorial-two-ruby.html
queue.subscribe(manual_ack: true) do |delivery_info, _properties, payload|
queue.subscribe(manual_ack: true) do |delivery_info, properties, payload|
Thread.current[:request_id] = properties.headers['request_id']
payload = JSON(payload)
coordinates = Geocoder.geocode(payload['city'])

Application.logger.info(
'Update city coordinates',
city: payload['city'],
coordinates: coordinates
)

if coordinates.present?
client = AdsService::HttpClient.new
client.update_coordinates(payload['id'], coordinates)
Expand Down
30 changes: 30 additions & 0 deletions config/initializers/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require_relative '../application'

def stdout?
ENV['LOG_SERVICE'] == 'stdout'
end

logger_path = case ENV['LOG_SERVICE']
when 'stdout'
$stdout
when nil
"#{Application.root}/#{Settings.logger.path}"
else
"#{Application.root}/#{ENV['LOG_SERVICE']}"
end

logger = Ougai::Logger.new(
logger_path,
level: Settings.logger.level
)

logger.formatter = Ougai::Formatters::Readable.new if stdout?

logger.before_log = lambda do |data|
data[:service] = { name: Settings.app.name }
data[:request_id] ||= Thread.current[:request_id]
end

Application.logger = logger
2 changes: 2 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
app:
name: geocoder
rabbitmq:
consumer_pool: 10
7 changes: 7 additions & 0 deletions config/settings/development.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
logger:
path: log/development.log
level: <%= Logger::DEBUG %>
rabbitmq:
host: <%= ENV.fetch('RABBITMQ_HOST') %>
username: <%= ENV.fetch('RABBITMQ_USER') %>
password: <%= ENV.fetch('RABBITMQ_PASSWORD') %>
7 changes: 7 additions & 0 deletions config/settings/production.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
logger:
path: log/production.log
level: <%= Logger::INFO %>
rabbitmq:
host: <%= ENV.fetch('RABBITMQ_HOST') %>
username: <%= ENV.fetch('RABBITMQ_USER') %>
password: <%= ENV.fetch('RABBITMQ_PASSWORD') %>
7 changes: 7 additions & 0 deletions config/settings/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
logger:
path: log/test.log
level: <%= Logger::FATAL %>
rabbitmq:
host: <%= ENV.fetch('RABBITMQ_HOST') %>
username: <%= ENV.fetch('RABBITMQ_USER') %>
password: <%= ENV.fetch('RABBITMQ_PASSWORD') %>
Empty file added rakelib/.keep
Empty file.