SlideShare a Scribd company logo
Logging rails application behavior to MongoDB

            Vasiliev Alexey Yuriyovich
                   leopard.in.ua


                          2011




              Vasiliev A.Y.   Logging rails application behavior to MongoDB
MongoDB


 Introduction
 MongoDB is an open source document-oriented database.

 Features
     Consistent UTF-8 encoding;
     Cross-platform support;
     Efficient storage for large binary data, for example photos and
     video;
     Replication and failover;
     MapReduce.




                        Vasiliev A.Y.   Logging rails application behavior to MongoDB
Why application should log to DB?




                                           Centralized application
                                           logging;
                                           Easy remote access,
                                           compared to files;
                                           Can be fast and scalable if
                                           you go with database
                                           partitioning and/or multi-log
                                           databases.




                    Vasiliev A.Y.   Logging rails application behavior to MongoDB
What are the advantages appear logging in MongoDB?




    Flexible schema;
    Asynchronous inserts;
    Capped collection;
    Fast enough compared to
    files;
    Indexed and searched;
    Analyze data in-place.




                         Vasiliev A.Y.   Logging rails application behavior to MongoDB
Gem for Rails
MongoDB as Rails logger




                             Central Logger (https://github.com/
                             customink/central_logger). You should do
                             only several steps:
                                    gem ’central_logger’;
                                    include CentralLogger::Filter;
                                    Config the connection to mongodb;
                                    That’s all!




                          Vasiliev A.Y.   Logging rails application behavior to MongoDB
Rails log structure
MongoDB as Rails logger



   {
       ’ action ’           : action_name ,
       ’ application_name ’ : application_name ( r a i l s root ) ,
       ’ controller ’       : controller_name ,
       ’ ip ’               : ip_address ,
       ’ messages ’         : {
                                 ’ info ’  : [ ],
                                 ’ debug ’ : [ ] ,
                                 ’ error ’ : [ ] ,
                                 ’ warn ’  : [ ],
                                 ’ fatal ’ : [ ]
                              },
       ’ params ’           : { },
       ’ path ’             : path ,
       ’ request_time ’     : date_of_request ,
       ’ runtime ’          : execution_time_in_milliseconds ,
       ’ url ’              : full_url
   }




                           Vasiliev A.Y.   Logging rails application behavior to MongoDB
Add data to log
MongoDB as Rails logger




   Add additional data to log
   # make s u r e we ’ r e u s i n g t h e C e n t r a l L o g g e r i n t h i s e n v i r o n m e n t
   i f R a i l s . l o g g e r . r e s p o n d _ t o ? ( : add_metadata )
      R a i l s . l o g g e r . add_metadata ( : u s e r _ g u i d => @ u s e r . i d )
   end




                                     Vasiliev A.Y.    Logging rails application behavior to MongoDB
Querying via the Rails console
MongoDB as Rails logger



   Handle on the MongoDB
   >> db = R a i l s . l o g g e r . mongo_connection
   => #&l t ; Mongo : : DB: 0 x 1 0 2 f 1 9 a c 0 @ s l a v e _ o k= n i l          ...

   >> c o l l e c t i o n = db [ R a i l s . l o g g e r . m o n g o _ c o l l e c t i o n _ n a m e ]
   => #&l t ; Mongo : : C o l l e c t i o n : 0 x 1 0 3 1 b 3 e e 8 . . .



   Find all requests for a specific user (with guid)
   >>   c u r s o r = c o l l e c t i o n . f i n d ( : u s e r _ g u i d => 1 2 3 5 5 )
   =>   #&l t ; Mongo : : C u r s o r : 0 x 1 0 3 1 a 3 e 3 0 . . . &g t ;
   >>   c u r s o r . count
   =>   255




                                          Vasiliev A.Y.      Logging rails application behavior to MongoDB
Querying via the Rails console
MongoDB as Rails logger




   Find all requests that took more that two second to complete
   >> c o l l e c t i o n . f i n d ( { : r u n t i m e => { ’ $gt ’ => 2 0 0 0 } } ) . c o u n t
   => 3



   Find all requests with an exception
   >> c o l l e c t i o n . f i n d ( { " m e s s a g e s . e r r o r " => / R o u t i n g E r r o r / } )




                                          Vasiliev A.Y.      Logging rails application behavior to MongoDB
Querying via the Rails console
MongoDB as Rails logger




   Find by params values
   >> c o l l e c t i o n . f i n d (
       " params . a c t i v i t y " => { ’ $ e x i s t s ’ => t r u e } ,
       " params . a c t i v i t y . s t o r i e s " => { ’ $ e x i s t s ’ => t r u e } ,
       " r e q u e s t _ t i m e " => {
                ’ $ g t e ’ => s t a r t _ d a t e . u t c ,
                ’ $ l t e ’ => end_date . u t c
       }
      ) . s o r t ( " params . a c t i v i t y . o c c u r r e d _ a t " , 1 ) . c o u n t


   => 542




                                    Vasiliev A.Y.    Logging rails application behavior to MongoDB
Querying via the Rails console
MongoDB as Rails logger




   Find current status of story using logs
   >> c o l l e c t i o n . f i n d (
              " params . a c t i v i t y . s t o r i e s . i d " => s t o r y _ i d . to_i ,
              " params . a c t i v i t y . s t o r i e s . c u r r e n t _ s t a t e " =>
              { ’ $ e x i s t s ’ => t r u e }
      ) . s o r t ( " params . a c t i v i t y . o c c u r r e d _ a t " , −1). f i r s t


   => {" _id"=>BSON : : O b j e c t I d ( ’ 4 e 7 c 6 2 e d 7 6 1 2 6 8 0 4 d 4 0 0 0 0 0 f ’ ) ,
   " a c t i o n "=>"hook " ,
   " c o n t r o l l e r "=>" s e r v i c e s " , " p a t h "=>"/ a p i / v1 / hook " ,
   " params"=>{" a c t i v i t y "=>{ . . . ,
   " s t o r i e s " = > [ { . . . , " c u r r e n t _ s t a t e "=>" f i n i s h e d " } ] } }




                                        Vasiliev A.Y.     Logging rails application behavior to MongoDB
Querying via the Rails console
MongoDB as Rails logger

   Tailable Cursors
   v a r c o l l = db . some . c a p p e d . c o l l e c t i o n ;
   var l a s t V a l = c o l l . f i n d ( ) . s o r t ({ ’ $ n a t u r a l ’ : 1 })
                                             . l i m i t ( 1 ) . next ( ) [ ’ increasing ’          ];
   while (1){

       c u r s o r = c o l l . f i n d ({ ’ i n c r e a s i n g ’ : {
             ’ $gte ’ : l a s t V a l
       } });

       // t a i l a b l e
       c u r s o r . addOption ( 2 ) ;
       // a w a i t d a t a
       c u r s o r . a d d O p t i o n ( 32 ) ;

       // W a i t s s e v e r a l s e c f o r more d a t a
       w h i l e ( c u r s o r . hasNext ( ) ){
          v a r doc = c u r s o r . n e x t ( ) ;
          l a s t V a l = doc [ ’ i n c r e a s i n g ’ ] ;
          p r i n t j s o n ( doc ) ;
       }

   }                                    Vasiliev A.Y.    Logging rails application behavior to MongoDB
Using MapReduce
MongoDB as Rails logger

   Find average time of page loading
   >> c o l l e c t i o n . g r o u p ( " f u n c t i o n ( x ) {
     r e t u r n { month : x . r e q u e s t _ t i m e . getMonth ( ) ,
     year : x . request_time . g e t F u l l Y e a r ( ) } ; }" ,
     { : r e q u e s t _ t i m e => {
         ’ $ g t e ’ => Time . u t c ( 2 0 1 1 , 0 1 , 0 1 , 0 0 , 0 0 , 0 0 ) ,
         " $ l t " => Time . u t c ( 2 0 1 1 , 8 , 0 1 , 0 0 , 0 0 , 0 0 ) }
     } , { : c o u n t => 0 , : t o t a l _ t i m e => 0 } ,
     " f u n c t i o n ( doc , o u t ) {
         o u t . c o u n t ++; o u t . t o t a l _ t i m e+=doc . r u n t i m e } " ,
     " f u n c t i o n ( out ){
         o u t . avg_time = o u t . t o t a l _ t i m e / o u t . c o u n t } " )

   => [
     {" month "= >4.0 ,
     " y e a r "= >2011.0 ,
     " c o u n t "= >5385.0 ,
     " t o t a l _ t i m e "= >1027015.0 ,
     " avg_time "= >190.717734447539} ,
     ...
     ]

                                      Vasiliev A.Y.     Logging rails application behavior to MongoDB
Restrictions
MongoDB as Rails logger




                               The database doesn’t allow update the
                               existing objects in the collection (but
                               there is possibility).
                               The database doesn’t allow deleting
                               objects from a capped collection.
                               Capped collection are not shardable.
                               More indexes - slower inserts.




                          Vasiliev A.Y.   Logging rails application behavior to MongoDB
Q&A




      Thank you for attention!

        Vasiliev A.Y.   Logging rails application behavior to MongoDB
Ad

Recommended

MongoDB.local DC 2018: Ch-Ch-Ch-Ch-Changes: Taking Your MongoDB Stitch Applic...
MongoDB.local DC 2018: Ch-Ch-Ch-Ch-Changes: Taking Your MongoDB Stitch Applic...
MongoDB
 
Practical JSON in MySQL 5.7
Practical JSON in MySQL 5.7
Ike Walker
 
Practical JSON in MySQL 5.7 and Beyond
Practical JSON in MySQL 5.7 and Beyond
Ike Walker
 
Advanced Redis data structures
Advanced Redis data structures
amix3k
 
Redis data modeling examples
Redis data modeling examples
Terry Cho
 
MongoDB - Aggregation Pipeline
MongoDB - Aggregation Pipeline
Jason Terpko
 
SH 1 - SES 3 - 3.6-Overview-Tel-Aviv.pptx
SH 1 - SES 3 - 3.6-Overview-Tel-Aviv.pptx
MongoDB
 
Large Scale Log Analytics with Solr (from Lucene Revolution 2015)
Large Scale Log Analytics with Solr (from Lucene Revolution 2015)
Sematext Group, Inc.
 
MongoDB Chunks - Distribution, Splitting, and Merging
MongoDB Chunks - Distribution, Splitting, and Merging
Jason Terpko
 
Introduction to MongoDB and Hadoop
Introduction to MongoDB and Hadoop
Steven Francia
 
The Aggregation Framework
The Aggregation Framework
MongoDB
 
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
MongoDB
 
Operational Intelligence with MongoDB Webinar
Operational Intelligence with MongoDB Webinar
MongoDB
 
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB
 
MongoDB: Comparing WiredTiger In-Memory Engine to Redis
MongoDB: Comparing WiredTiger In-Memory Engine to Redis
Jason Terpko
 
Qtp Scripts
Qtp Scripts
G.C Reddy
 
MongoDB - Sharded Cluster Tutorial
MongoDB - Sharded Cluster Tutorial
Jason Terpko
 
Streaming using Kafka Flink & Elasticsearch
Streaming using Kafka Flink & Elasticsearch
Keira Zhou
 
MongoDB Performance Debugging
MongoDB Performance Debugging
MongoDB
 
SwiftyJSON 慘痛經驗
SwiftyJSON 慘痛經驗
Hokila Jan
 
Mongodb workshop
Mongodb workshop
Harun Yardımcı
 
Indexing
Indexing
Mike Dirolf
 
Beeline Firebase talk - Firebase event Jun 2017
Beeline Firebase talk - Firebase event Jun 2017
Chetan Padia
 
Mongo db dla administratora
Mongo db dla administratora
Łukasz Jagiełło
 
Google apps script database abstraction exposed version
Google apps script database abstraction exposed version
Bruce McPherson
 
MongoDB for Time Series Data Part 2: Analyzing Time Series Data Using the Agg...
MongoDB for Time Series Data Part 2: Analyzing Time Series Data Using the Agg...
MongoDB
 
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB
 
Practical Ruby Projects (Alex Sharp)
Practical Ruby Projects (Alex Sharp)
MongoSF
 
Practical Ruby Projects with MongoDB - MongoSF
Practical Ruby Projects with MongoDB - MongoSF
Alex Sharp
 
MongoDB Use Cases: Healthcare, CMS, Analytics
MongoDB Use Cases: Healthcare, CMS, Analytics
MongoDB
 

More Related Content

What's hot (19)

MongoDB Chunks - Distribution, Splitting, and Merging
MongoDB Chunks - Distribution, Splitting, and Merging
Jason Terpko
 
Introduction to MongoDB and Hadoop
Introduction to MongoDB and Hadoop
Steven Francia
 
The Aggregation Framework
The Aggregation Framework
MongoDB
 
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
MongoDB
 
Operational Intelligence with MongoDB Webinar
Operational Intelligence with MongoDB Webinar
MongoDB
 
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB
 
MongoDB: Comparing WiredTiger In-Memory Engine to Redis
MongoDB: Comparing WiredTiger In-Memory Engine to Redis
Jason Terpko
 
Qtp Scripts
Qtp Scripts
G.C Reddy
 
MongoDB - Sharded Cluster Tutorial
MongoDB - Sharded Cluster Tutorial
Jason Terpko
 
Streaming using Kafka Flink & Elasticsearch
Streaming using Kafka Flink & Elasticsearch
Keira Zhou
 
MongoDB Performance Debugging
MongoDB Performance Debugging
MongoDB
 
SwiftyJSON 慘痛經驗
SwiftyJSON 慘痛經驗
Hokila Jan
 
Mongodb workshop
Mongodb workshop
Harun Yardımcı
 
Indexing
Indexing
Mike Dirolf
 
Beeline Firebase talk - Firebase event Jun 2017
Beeline Firebase talk - Firebase event Jun 2017
Chetan Padia
 
Mongo db dla administratora
Mongo db dla administratora
Łukasz Jagiełło
 
Google apps script database abstraction exposed version
Google apps script database abstraction exposed version
Bruce McPherson
 
MongoDB for Time Series Data Part 2: Analyzing Time Series Data Using the Agg...
MongoDB for Time Series Data Part 2: Analyzing Time Series Data Using the Agg...
MongoDB
 
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB
 
MongoDB Chunks - Distribution, Splitting, and Merging
MongoDB Chunks - Distribution, Splitting, and Merging
Jason Terpko
 
Introduction to MongoDB and Hadoop
Introduction to MongoDB and Hadoop
Steven Francia
 
The Aggregation Framework
The Aggregation Framework
MongoDB
 
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
MongoDB
 
Operational Intelligence with MongoDB Webinar
Operational Intelligence with MongoDB Webinar
MongoDB
 
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB
 
MongoDB: Comparing WiredTiger In-Memory Engine to Redis
MongoDB: Comparing WiredTiger In-Memory Engine to Redis
Jason Terpko
 
MongoDB - Sharded Cluster Tutorial
MongoDB - Sharded Cluster Tutorial
Jason Terpko
 
Streaming using Kafka Flink & Elasticsearch
Streaming using Kafka Flink & Elasticsearch
Keira Zhou
 
MongoDB Performance Debugging
MongoDB Performance Debugging
MongoDB
 
SwiftyJSON 慘痛經驗
SwiftyJSON 慘痛經驗
Hokila Jan
 
Beeline Firebase talk - Firebase event Jun 2017
Beeline Firebase talk - Firebase event Jun 2017
Chetan Padia
 
Google apps script database abstraction exposed version
Google apps script database abstraction exposed version
Bruce McPherson
 
MongoDB for Time Series Data Part 2: Analyzing Time Series Data Using the Agg...
MongoDB for Time Series Data Part 2: Analyzing Time Series Data Using the Agg...
MongoDB
 
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB
 

Similar to Logging rails application behavior to MongoDB (20)

Practical Ruby Projects (Alex Sharp)
Practical Ruby Projects (Alex Sharp)
MongoSF
 
Practical Ruby Projects with MongoDB - MongoSF
Practical Ruby Projects with MongoDB - MongoSF
Alex Sharp
 
MongoDB Use Cases: Healthcare, CMS, Analytics
MongoDB Use Cases: Healthcare, CMS, Analytics
MongoDB
 
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Alex Sharp
 
Practical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo Db
Alex Sharp
 
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
MongoSF
 
MongoDB at RubyEnRails 2009
MongoDB at RubyEnRails 2009
Mike Dirolf
 
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
Johannes Hoppe
 
2013-03-23 - NoSQL Spartakiade
2013-03-23 - NoSQL Spartakiade
Johannes Hoppe
 
Webinar: Building Your First Application with MongoDB
Webinar: Building Your First Application with MongoDB
MongoDB
 
MongoDB - Ruby document store that doesn't rhyme with ouch
MongoDB - Ruby document store that doesn't rhyme with ouch
Wynn Netherland
 
MongoDB at FrozenRails
MongoDB at FrozenRails
Mike Dirolf
 
Tame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapper
Giordano Scalzo
 
Back to Basics: My First MongoDB Application
Back to Basics: My First MongoDB Application
MongoDB
 
Back to Basics 2017 - Your First MongoDB Application
Back to Basics 2017 - Your First MongoDB Application
Joe Drumgoole
 
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
Daniel Cousineau
 
Building Your First MongoDB Application
Building Your First MongoDB Application
Rick Copeland
 
Back to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB Application
MongoDB
 
Back to Basics Webinar 2 - Your First MongoDB Application
Back to Basics Webinar 2 - Your First MongoDB Application
Joe Drumgoole
 
2012 phoenix mug
2012 phoenix mug
Paul Pedersen
 
Practical Ruby Projects (Alex Sharp)
Practical Ruby Projects (Alex Sharp)
MongoSF
 
Practical Ruby Projects with MongoDB - MongoSF
Practical Ruby Projects with MongoDB - MongoSF
Alex Sharp
 
MongoDB Use Cases: Healthcare, CMS, Analytics
MongoDB Use Cases: Healthcare, CMS, Analytics
MongoDB
 
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Alex Sharp
 
Practical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo Db
Alex Sharp
 
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
MongoSF
 
MongoDB at RubyEnRails 2009
MongoDB at RubyEnRails 2009
Mike Dirolf
 
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
Johannes Hoppe
 
2013-03-23 - NoSQL Spartakiade
2013-03-23 - NoSQL Spartakiade
Johannes Hoppe
 
Webinar: Building Your First Application with MongoDB
Webinar: Building Your First Application with MongoDB
MongoDB
 
MongoDB - Ruby document store that doesn't rhyme with ouch
MongoDB - Ruby document store that doesn't rhyme with ouch
Wynn Netherland
 
MongoDB at FrozenRails
MongoDB at FrozenRails
Mike Dirolf
 
Tame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapper
Giordano Scalzo
 
Back to Basics: My First MongoDB Application
Back to Basics: My First MongoDB Application
MongoDB
 
Back to Basics 2017 - Your First MongoDB Application
Back to Basics 2017 - Your First MongoDB Application
Joe Drumgoole
 
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
Daniel Cousineau
 
Building Your First MongoDB Application
Building Your First MongoDB Application
Rick Copeland
 
Back to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB Application
MongoDB
 
Back to Basics Webinar 2 - Your First MongoDB Application
Back to Basics Webinar 2 - Your First MongoDB Application
Joe Drumgoole
 
Ad

Recently uploaded (20)

Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
 
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Safe Software
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
 
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
Priyanka Aash
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
 
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Safe Software
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
 
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
Priyanka Aash
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
Ad

Logging rails application behavior to MongoDB

  • 1. Logging rails application behavior to MongoDB Vasiliev Alexey Yuriyovich leopard.in.ua 2011 Vasiliev A.Y. Logging rails application behavior to MongoDB
  • 2. MongoDB Introduction MongoDB is an open source document-oriented database. Features Consistent UTF-8 encoding; Cross-platform support; Efficient storage for large binary data, for example photos and video; Replication and failover; MapReduce. Vasiliev A.Y. Logging rails application behavior to MongoDB
  • 3. Why application should log to DB? Centralized application logging; Easy remote access, compared to files; Can be fast and scalable if you go with database partitioning and/or multi-log databases. Vasiliev A.Y. Logging rails application behavior to MongoDB
  • 4. What are the advantages appear logging in MongoDB? Flexible schema; Asynchronous inserts; Capped collection; Fast enough compared to files; Indexed and searched; Analyze data in-place. Vasiliev A.Y. Logging rails application behavior to MongoDB
  • 5. Gem for Rails MongoDB as Rails logger Central Logger (https://github.com/ customink/central_logger). You should do only several steps: gem ’central_logger’; include CentralLogger::Filter; Config the connection to mongodb; That’s all! Vasiliev A.Y. Logging rails application behavior to MongoDB
  • 6. Rails log structure MongoDB as Rails logger { ’ action ’ : action_name , ’ application_name ’ : application_name ( r a i l s root ) , ’ controller ’ : controller_name , ’ ip ’ : ip_address , ’ messages ’ : { ’ info ’ : [ ], ’ debug ’ : [ ] , ’ error ’ : [ ] , ’ warn ’ : [ ], ’ fatal ’ : [ ] }, ’ params ’ : { }, ’ path ’ : path , ’ request_time ’ : date_of_request , ’ runtime ’ : execution_time_in_milliseconds , ’ url ’ : full_url } Vasiliev A.Y. Logging rails application behavior to MongoDB
  • 7. Add data to log MongoDB as Rails logger Add additional data to log # make s u r e we ’ r e u s i n g t h e C e n t r a l L o g g e r i n t h i s e n v i r o n m e n t i f R a i l s . l o g g e r . r e s p o n d _ t o ? ( : add_metadata ) R a i l s . l o g g e r . add_metadata ( : u s e r _ g u i d => @ u s e r . i d ) end Vasiliev A.Y. Logging rails application behavior to MongoDB
  • 8. Querying via the Rails console MongoDB as Rails logger Handle on the MongoDB >> db = R a i l s . l o g g e r . mongo_connection => #&l t ; Mongo : : DB: 0 x 1 0 2 f 1 9 a c 0 @ s l a v e _ o k= n i l ... >> c o l l e c t i o n = db [ R a i l s . l o g g e r . m o n g o _ c o l l e c t i o n _ n a m e ] => #&l t ; Mongo : : C o l l e c t i o n : 0 x 1 0 3 1 b 3 e e 8 . . . Find all requests for a specific user (with guid) >> c u r s o r = c o l l e c t i o n . f i n d ( : u s e r _ g u i d => 1 2 3 5 5 ) => #&l t ; Mongo : : C u r s o r : 0 x 1 0 3 1 a 3 e 3 0 . . . &g t ; >> c u r s o r . count => 255 Vasiliev A.Y. Logging rails application behavior to MongoDB
  • 9. Querying via the Rails console MongoDB as Rails logger Find all requests that took more that two second to complete >> c o l l e c t i o n . f i n d ( { : r u n t i m e => { ’ $gt ’ => 2 0 0 0 } } ) . c o u n t => 3 Find all requests with an exception >> c o l l e c t i o n . f i n d ( { " m e s s a g e s . e r r o r " => / R o u t i n g E r r o r / } ) Vasiliev A.Y. Logging rails application behavior to MongoDB
  • 10. Querying via the Rails console MongoDB as Rails logger Find by params values >> c o l l e c t i o n . f i n d ( " params . a c t i v i t y " => { ’ $ e x i s t s ’ => t r u e } , " params . a c t i v i t y . s t o r i e s " => { ’ $ e x i s t s ’ => t r u e } , " r e q u e s t _ t i m e " => { ’ $ g t e ’ => s t a r t _ d a t e . u t c , ’ $ l t e ’ => end_date . u t c } ) . s o r t ( " params . a c t i v i t y . o c c u r r e d _ a t " , 1 ) . c o u n t => 542 Vasiliev A.Y. Logging rails application behavior to MongoDB
  • 11. Querying via the Rails console MongoDB as Rails logger Find current status of story using logs >> c o l l e c t i o n . f i n d ( " params . a c t i v i t y . s t o r i e s . i d " => s t o r y _ i d . to_i , " params . a c t i v i t y . s t o r i e s . c u r r e n t _ s t a t e " => { ’ $ e x i s t s ’ => t r u e } ) . s o r t ( " params . a c t i v i t y . o c c u r r e d _ a t " , −1). f i r s t => {" _id"=>BSON : : O b j e c t I d ( ’ 4 e 7 c 6 2 e d 7 6 1 2 6 8 0 4 d 4 0 0 0 0 0 f ’ ) , " a c t i o n "=>"hook " , " c o n t r o l l e r "=>" s e r v i c e s " , " p a t h "=>"/ a p i / v1 / hook " , " params"=>{" a c t i v i t y "=>{ . . . , " s t o r i e s " = > [ { . . . , " c u r r e n t _ s t a t e "=>" f i n i s h e d " } ] } } Vasiliev A.Y. Logging rails application behavior to MongoDB
  • 12. Querying via the Rails console MongoDB as Rails logger Tailable Cursors v a r c o l l = db . some . c a p p e d . c o l l e c t i o n ; var l a s t V a l = c o l l . f i n d ( ) . s o r t ({ ’ $ n a t u r a l ’ : 1 }) . l i m i t ( 1 ) . next ( ) [ ’ increasing ’ ]; while (1){ c u r s o r = c o l l . f i n d ({ ’ i n c r e a s i n g ’ : { ’ $gte ’ : l a s t V a l } }); // t a i l a b l e c u r s o r . addOption ( 2 ) ; // a w a i t d a t a c u r s o r . a d d O p t i o n ( 32 ) ; // W a i t s s e v e r a l s e c f o r more d a t a w h i l e ( c u r s o r . hasNext ( ) ){ v a r doc = c u r s o r . n e x t ( ) ; l a s t V a l = doc [ ’ i n c r e a s i n g ’ ] ; p r i n t j s o n ( doc ) ; } } Vasiliev A.Y. Logging rails application behavior to MongoDB
  • 13. Using MapReduce MongoDB as Rails logger Find average time of page loading >> c o l l e c t i o n . g r o u p ( " f u n c t i o n ( x ) { r e t u r n { month : x . r e q u e s t _ t i m e . getMonth ( ) , year : x . request_time . g e t F u l l Y e a r ( ) } ; }" , { : r e q u e s t _ t i m e => { ’ $ g t e ’ => Time . u t c ( 2 0 1 1 , 0 1 , 0 1 , 0 0 , 0 0 , 0 0 ) , " $ l t " => Time . u t c ( 2 0 1 1 , 8 , 0 1 , 0 0 , 0 0 , 0 0 ) } } , { : c o u n t => 0 , : t o t a l _ t i m e => 0 } , " f u n c t i o n ( doc , o u t ) { o u t . c o u n t ++; o u t . t o t a l _ t i m e+=doc . r u n t i m e } " , " f u n c t i o n ( out ){ o u t . avg_time = o u t . t o t a l _ t i m e / o u t . c o u n t } " ) => [ {" month "= >4.0 , " y e a r "= >2011.0 , " c o u n t "= >5385.0 , " t o t a l _ t i m e "= >1027015.0 , " avg_time "= >190.717734447539} , ... ] Vasiliev A.Y. Logging rails application behavior to MongoDB
  • 14. Restrictions MongoDB as Rails logger The database doesn’t allow update the existing objects in the collection (but there is possibility). The database doesn’t allow deleting objects from a capped collection. Capped collection are not shardable. More indexes - slower inserts. Vasiliev A.Y. Logging rails application behavior to MongoDB
  • 15. Q&A Thank you for attention! Vasiliev A.Y. Logging rails application behavior to MongoDB