Skip to content

Instantly share code, notes, and snippets.

@mikhailov
mikhailov / datadog_apm_sql_time.rb
Last active Oct 30, 2019
Datadog APM instrumentation to have SQL Time for every event
View datadog_apm_sql_time.rb
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 Jan 19, 2020
MySQL Process List redirect to STDOUT
View mysql_process_list_to_stdout.sh
#!/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}')
View Nginx access log
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 Apr 19, 2021
Troubleshooting Application Performance and Slow TCP Connections with NGINX Amplify
View nginx.local.conf
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 {
View reloader_fix.rb
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 Oct 2, 2015
Basic testing framework
View tests.rb
class Object
class UnitTestError < StandardError; end
def describe(description, &block)
if Object.class_eval(&block)
print '.'
else
puts 'FAIL ' + description
raise UnitTestError
View reverse_sentence.rb
class ReverseSentence
def initialize(string)
@array, @array_reversed = string.split(" "), []
end
def process
@array_reversed << @array.pop while @array.any?
@array_reversed.join(" ")
end
View fizz_buzz.rb
class FizzBuzz
def initialize(array)
@array = array
@new_array = []
end
def process
@array.each do |e|
value = \
if e % 3 == 0 && e % 5 == 0
View decimal_mark.rb
class DecimalMark
def initialize(data)
@array = data.split("")
@new_array = []
end
def process
i = 0
loop do
View valid_parentheses.rb
class ValidParentheses
SYMBOLS = {"(" => ")", "[" => "]", "{" => "}"}
def initialize(array)
@array = array.split("")
@stack = []
end
def process
@array.each do |i|