Skip to content

Instantly share code, notes, and snippets.

View Paxa's full-sized avatar

Pavel Evstigneev Paxa

View GitHub Profile
@Paxa
Paxa / 0. readme.md
Last active August 15, 2022 19:44
Rails 6 JSON logger (logstash format)

What I want to have: all logs in JSON format, with additional tags and per-request tags (similar to java's MDC)

How to make it:

  1. Use lograge with logstash-event to change default request logs
  2. Make custom log formatter
  3. Override ActiveSupport::TaggedLogging format
  4. Add own error handlers with config.exceptions_app = self.routes
  5. Override default logger at the right time

Use environment variables JSON_LOGGER = true/false and RAILS_LOG_LEVEL = info / debug / etc

@Paxa
Paxa / swagger.slim
Created April 24, 2019 08:38
swagger ui dist
doctype html
html[lang="en"]
head
meta(charset="UTF-8")
title Installments API
link[rel="stylesheet" type="text/css" href="//unpkg.com/swagger-ui-dist@3/swagger-ui.css"]
body
#swagger-ui
@Paxa
Paxa / debug.conf
Created January 21, 2019 11:41
Nginx minimal config for debugging
# run with:
# nginx -c $(pwd)/debug.conf
daemon off;
worker_processes 1;
events {
worker_connections 128;
}
@Paxa
Paxa / icco_example.rb
Created March 29, 2018 18:27
fog-backblaze example
gem 'fog-core', '>2.0'
gem 'fog-backblaze'
require 'fog/core'
require 'fog/backblaze'
TEST_BUCKET = ENV['B2_BUCKET'] || 'fog-demo-1505931432'
if !ENV['B2_ACCOUNT_ID'] || ENV['B2_ACCOUNT_ID'] == ""
puts "Missing env B2_ACCOUNT_ID"
tcp_socket = Socket.tcp("mysite.com", 32500, connect_timeout: 60)
ssl_context = OpenSSL::SSL::SSLContext.new()
ssl_context.client_cert_cb = Proc.new do
[
OpenSSL::X509::Certificate.new(File.open("./client.crt")),
OpenSSL::PKey.read(File.open("./client.key"))
]
end
const express = require('express');
const asyncRouter = require('../lib/async_router');
var router = asyncRouter(express.Router());
router.get('/', async (req, res, next) => {
// mkaing async errors
});
@Paxa
Paxa / 1.rb
Created July 13, 2017 09:29
rabbit migrate
desc "Create rabbitmq setup"
task :setup_rabbit => :environment do
QueueWorker::HostSelector.mute_logger
Rails.logger = Logger.new(STDOUT)
options = {}
if ENV['UNDO']
options = {delete_all: true, raise_errors: false}
end
if ENV['RECREATE']
@Paxa
Paxa / 1_output_jakarta_to_singapore.txn
Last active May 12, 2017 09:34
Persistent HTTPS vs not persistent
Run from cloud server in Jakarta, sending requests to cloud server in Singapore
Warming up --------------------------------------
excon 1.000 i/100ms
net-http2 1.000 i/100ms
net-http 1.000 i/100ms
net-http2 persistent 6.000 i/100ms
excon persistent 5.000 i/100ms
http persistent 6.000 i/100ms
Calculating -------------------------------------
@Paxa
Paxa / async.rb
Last active May 12, 2017 08:38
Safe way to run threads on rails
module Async
##
# Run code in thread, handle exceptions, release DB connections
#
# Async(:send_email) { Mailer.send }
#
def self.run(name)
parent_t = Thread.current
working_thread = Thread.new do
begin
@Paxa
Paxa / ar_pool_stats.rb
Created April 20, 2017 12:24
ActiveRecord pool stats for Rails < 5.1
unless ActiveRecord::ConnectionAdapters::ConnectionPool.method_defined?(:stat) # will be in Rails 5.1
class ActiveRecord::ConnectionAdapters::ConnectionPool
def stat
synchronize do
{
size: size,
connections: @connections.size,
busy: @connections.count { |c| c.in_use? && c.owner.alive? },
dead: @connections.count { |c| c.in_use? && !c.owner.alive? },
idle: @connections.count { |c| !c.in_use? },