Log your slow queries in slack using Rails - Veltris
Veltris
MENU
  • Company
      • AboutusAbout Us
      • __CSR_mainCSR
      • LeadershipLeadership
      • __Life at Veltris-mainLife at Veltris
      • ChannelPartnerships
      • Contact usContact Us
      • veltrisPressroom
      • EventsEvents
  • AI Services
    • Digital-CloudAI-Augmented Engineering
    • Data-AIAI Readiness & Data
      Modernization
    • __Connectivity & Industry X.0_1 (1)AI Monetization
    • __Product Lifecyle & Managed Services_1Product Lifecycle &
      Managed Services
  • Industries
    • HealthcareHealthcare
      • __Health TechHealthTech & MedTech Platforms
      • __Dental CareDental & Multi-Unit Retail Health
      • __Pharmacy Benefit Management (PBM)Pharmacy Benefit Management (PBM)
    • Hi TechTechnology
      • __Technology Products & PlatformsSoftware / XTech / AI Platforms
      • __Communications Service ProvidersSecure Networking & Infrastructure
    • Diversified-IndustrialsIndustrials
      • __Autonomous Vehicles and LogisticsLogistics & Mobility Tech
      • __Engineering and ConstructionEngineering & Construction Tech
  • Insights
    • BlogsBlogs
    • White papersWhitepapers
    • Glossary-svgAI Guides and Innovations
    • CasestudiesClient Case Studies
  • Careers
  • Vertical AI Platform
    (VeltrisOne™)
Get My AI Readiness Score
Veltris
Get My AI Readiness Score
  • Company
    • About Us
    • Leadership
    • Partnerships
    • Pressroom
    • Events
    • CSR
    • Life at Veltris
  • Services
    • AI-Augmented Engineering
    • AI Readiness
    • AI Monetization
    • Product Lifecycle & Managed Services
  • Industries
    • Healthcare
      • Healthtech Platforms
      • Dental & Multi-Unit Retail Health
      • Pharmacy Benefit Management (PBM)
    • Technology
      • Software / XTech / AI Platforms
      • Secure Networking & Infrastructure
    • Industrials
      • Logistics & Mobility Tech
      • Engineering & Construction Tech
  • Insights
    • Blogs
    • Whitepapers
    • AI Guides and Innovations
    • Client Case Studies
  • Careers
  • Get My AI Readiness Score
Blogs

Log your slow queries in slack using Rails

October 31, 2017 | Team Veltris
  • Blogs
  • Product Lifecycle & Managed Services
  • Technology, Communications & Media

This post is inspired by Monitoring Slow SQL Queries via Slack. After reading that, I thought, how we can implement this in Rails. Then after a thorough search, I found that ActiveSupport:: Notifications could help us in achieving this. ActiveSupport:: Notifications provides an instrumentation API to track not only SQL queries but also view rendering information. Prior monitoring SQL queries, one should subscribe eventsql.active_record along with callback as shown below.

callback = lambda {|*args| ... }
ActiveSupport::Notifications
  .subscribed(callback,"sql.active_record") do
  # your code is here
end

For every SQL that fires by an active record, callback will get executed.

callback = lambda do |_, start_time, end_time, *args|
  puts args.last[:sql]
  puts "#{(end_time - start_time) * 1000} ms"
end
ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do
   Farmer.accessible_by(current_ability).first(10)
end

When you run above code you would following output:

Log SQL queries on the console

Now let’s notify these slow queries on to slack. First, you have to register slack webhook URL. After that please follow steps below:

# Gemfile
gem 'slack-notifier'

in your codebase, please add following code:

if duration > 2.0
  url = <slack webhook URL>
  notifier = Slack::Notifier.new url, 
             channel: "#ror", 
             username: "SQL-bot"
  attachment = {
     fallback: '',
     text: "#{sql} took #{duration}",
     color: "warning"
  }
  notifier.post text: "Slow SQL Queries", attachments: [attachment]
end

Final code will appear as below once you merged instrumentation and notifications:

callback = lambda do |_, start_time, end_time, *args|
  sql = args.last[:sql]
  duration = (end_time - start_time) * 1000
  if duration > 2.0
    url = <slack webhook URL>
    notifier = Slack::Notifier.new url, channel: "#ror", 
                                        username: "SQL-bot"
  attachment = {
      fallback: '',
      text: "#{sql} took #{duration}",
      color: "warning"
  }
  notifier.post text: "Slow SQL Queries", attachments: [attachment]
end
ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do
     Farmer.accessible_by(current_ability).first(10)
   end
end

And on your slack

Notification in slack

This is a basic version of instrumenting your code base. For detailed metrics, it’s better to use some tools like new-relic or skylight.

Thanks for reading. Happy query logging!!!.

FOUND THIS USEFUL? SHARE WITH

Related Articles

RCM Automation: The Path to Faster Claim Cycles, Fewer Denials, and Stronger Cash Flow
AIOps: Enhancing Banking Core Systems Reliability and Transactions Availability
AIOps Self-Healing for Investment Management Platform Stability & Growth
AI-Driven Modernization: Transforming Legacy Trading and Wealth Management Platforms
Navigating the Future of Healthcare: Top 6 Technology Trends for 2025 and Beyond
The Connected Cure: Exploring the Potential of IoMT Ecosystems
Veltris
Company
  • About Us
  • Leadership
  • Partnerships
  • Pressroom
  • Events
  • CSR
  • Life at Veltris
  • Careers
  • Contact Us
Services
  • AI-Augmented Engineering
  • AI Readiness & Data Modernization
  • AI Monetization
  • Product Lifecycle & Managed Services
Industries
  • Healthcare
  • Technology
  • Industrials
Insights
  • Blogs
  • Whitepapers
  • AI Guides and Innovations
  • Client Case Studies
Copyright © 2026 Veltris. All Rights Reserved.
Cookie Policy | Disclaimer | Privacy Policy | Sitemap
Veltris
  • About Us
  • Leadership
  • Pressroom
  • Events
  • CSR
  • Life at Veltris
  • Careers
  • Contact Us
  • AI-Augmented Engineering
  • AI Readiness & Data Modernization
  • AI Monetization
  • Product Lifecycle & Managed Services
  • Healthcare
  • Technology
  • Industrials
  • Blogs
  • Whitepapers
  • AI Guides and Innovations
  • Client Case Studies
Copyright © 2026 Veltris. All Rights Reserved.
Cookie Policy | Disclaimer | Privacy Policy | Sitemap