Skip to content

Instantly share code, notes, and snippets.

View mikhailov's full-sized avatar

Anatoly Mikhaylov mikhailov

View GitHub Profile
@mikhailov
mikhailov / datadog_apm_sql_time.rb
Last active October 30, 2019 11:49
Datadog APM instrumentation to have SQL Time for every event
ActiveSupport::Notifications.subscribe('process_action.action_controller') do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
span = Datadog.tracer.active_span
payload = event.payload
if span && payload.key?(:db_runtime)
db_runtime = payload[:db_runtime].to_f.round(3)
span.set_tag('database.sql_time', db_runtime)
end
end
@mikhailov
mikhailov / mysql_process_list_to_stdout.sh
Last active January 19, 2020 13:04
MySQL Process List redirect to STDOUT
#!/bin/bash
set -ex
# Inject APM Trace ID (part of SQL query comment) to a Log event
# https://docs.datadoghq.com/tracing/advanced/connect_logs_and_traces/?tab=ruby
# Input: {"command": "Query", "current_statement": "SELECT ... /* 4572859165692197980 */"}
# Output: {"process_list": {"current_statement": "SELECT ... /* 4572859165692197980 */"}, "dd": {"trace_id": 4572859165692197980}}
function inject_trace_id() {
trace_id=$(echo "$1" |grep -Eo '/\* [0-9]{16,20} \*/' | awk '{print $2}')
log_format json '{'
'"http":{'
'"method":"$request_method",'
'"request_id":"$request_id",'
'"status_code":$status,'
'"content_type":"$content_type",'
'"url":"$request_uri",'
'"url_details":{'
'"path":"$uri",'
'"scheme":"$scheme",'
@mikhailov
mikhailov / nginx.local.conf
Last active April 19, 2021 08:39
Troubleshooting Application Performance and Slow TCP Connections with NGINX Amplify
upstream upstream_close_server_keepalive_timeout_0 {
server upstream:10443;
}
upstream upstream_keepalive_server_keepalive_timeout_0 {
server upstream:11443;
keepalive 64;
}
upstream upstream_close_server_keepalive_timeout_300 {
module ActionDispatch
class Reloader
def call(env)
if env['PATH_INFO'].include?(Rails.application.config.assets.prefix)
@app.call(env)
else
@validated = @condition.call
prepare!
@mikhailov
mikhailov / tests.rb
Created October 2, 2015 18:28
Basic testing framework
class Object
class UnitTestError < StandardError; end
def describe(description, &block)
if Object.class_eval(&block)
print '.'
else
puts 'FAIL ' + description
raise UnitTestError
@mikhailov
mikhailov / reverse_sentence.rb
Created September 26, 2015 20:09
ReverseSentence
class ReverseSentence
def initialize(string)
@array, @array_reversed = string.split(" "), []
end
def process
@array_reversed << @array.pop while @array.any?
@array_reversed.join(" ")
end
@mikhailov
mikhailov / fizz_buzz.rb
Created September 26, 2015 16:52
FizzBuzz
class FizzBuzz
def initialize(array)
@array = array
@new_array = []
end
def process
@array.each do |e|
value = \
if e % 3 == 0 && e % 5 == 0
@mikhailov
mikhailov / decimal_mark.rb
Created September 26, 2015 16:23
DecimalMark
class DecimalMark
def initialize(data)
@array = data.split("")
@new_array = []
end
def process
i = 0
loop do
@mikhailov
mikhailov / valid_parentheses.rb
Last active September 26, 2015 15:19
ValidParentheses
class ValidParentheses
SYMBOLS = {"(" => ")", "[" => "]", "{" => "}"}
def initialize(array)
@array = array.split("")
@stack = []
end
def process
@array.each do |i|