SlideShare a Scribd company logo
1 of 15
Download to read offline
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

More Related Content

What's hot

MongoDB Chunks - Distribution, Splitting, and Merging
MongoDB Chunks - Distribution, Splitting, and MergingMongoDB Chunks - Distribution, Splitting, and Merging
MongoDB Chunks - Distribution, Splitting, and MergingJason Terpko
 
Introduction to MongoDB and Hadoop
Introduction to MongoDB and HadoopIntroduction to MongoDB and Hadoop
Introduction to MongoDB and HadoopSteven Francia
 
The Aggregation Framework
The Aggregation FrameworkThe Aggregation Framework
The Aggregation FrameworkMongoDB
 
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...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 WebinarOperational Intelligence with MongoDB Webinar
Operational Intelligence with MongoDB WebinarMongoDB
 
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...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 RedisMongoDB: Comparing WiredTiger In-Memory Engine to Redis
MongoDB: Comparing WiredTiger In-Memory Engine to RedisJason Terpko
 
MongoDB - Sharded Cluster Tutorial
MongoDB - Sharded Cluster TutorialMongoDB - Sharded Cluster Tutorial
MongoDB - Sharded Cluster TutorialJason Terpko
 
Streaming using Kafka Flink & Elasticsearch
Streaming using Kafka Flink & ElasticsearchStreaming using Kafka Flink & Elasticsearch
Streaming using Kafka Flink & ElasticsearchKeira Zhou
 
MongoDB Performance Debugging
MongoDB Performance DebuggingMongoDB Performance Debugging
MongoDB Performance DebuggingMongoDB
 
SwiftyJSON 慘痛經驗
SwiftyJSON   慘痛經驗SwiftyJSON   慘痛經驗
SwiftyJSON 慘痛經驗Hokila Jan
 
Beeline Firebase talk - Firebase event Jun 2017
Beeline Firebase talk - Firebase event Jun 2017Beeline Firebase talk - Firebase event Jun 2017
Beeline Firebase talk - Firebase event Jun 2017Chetan Padia
 
Google apps script database abstraction exposed version
Google apps script database abstraction   exposed versionGoogle apps script database abstraction   exposed version
Google apps script database abstraction exposed versionBruce 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 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 Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...MongoDB
 

What's hot (19)

MongoDB Chunks - Distribution, Splitting, and Merging
MongoDB Chunks - Distribution, Splitting, and MergingMongoDB Chunks - Distribution, Splitting, and Merging
MongoDB Chunks - Distribution, Splitting, and Merging
 
Introduction to MongoDB and Hadoop
Introduction to MongoDB and HadoopIntroduction to MongoDB and Hadoop
Introduction to MongoDB and Hadoop
 
The Aggregation Framework
The Aggregation FrameworkThe Aggregation Framework
The Aggregation Framework
 
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...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...
 
Operational Intelligence with MongoDB Webinar
Operational Intelligence with MongoDB WebinarOperational Intelligence with MongoDB Webinar
Operational Intelligence with MongoDB Webinar
 
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
 
MongoDB: Comparing WiredTiger In-Memory Engine to Redis
MongoDB: Comparing WiredTiger In-Memory Engine to RedisMongoDB: Comparing WiredTiger In-Memory Engine to Redis
MongoDB: Comparing WiredTiger In-Memory Engine to Redis
 
Qtp Scripts
Qtp ScriptsQtp Scripts
Qtp Scripts
 
MongoDB - Sharded Cluster Tutorial
MongoDB - Sharded Cluster TutorialMongoDB - Sharded Cluster Tutorial
MongoDB - Sharded Cluster Tutorial
 
Streaming using Kafka Flink & Elasticsearch
Streaming using Kafka Flink & ElasticsearchStreaming using Kafka Flink & Elasticsearch
Streaming using Kafka Flink & Elasticsearch
 
MongoDB Performance Debugging
MongoDB Performance DebuggingMongoDB Performance Debugging
MongoDB Performance Debugging
 
SwiftyJSON 慘痛經驗
SwiftyJSON   慘痛經驗SwiftyJSON   慘痛經驗
SwiftyJSON 慘痛經驗
 
Mongodb workshop
Mongodb workshopMongodb workshop
Mongodb workshop
 
Indexing
IndexingIndexing
Indexing
 
Beeline Firebase talk - Firebase event Jun 2017
Beeline Firebase talk - Firebase event Jun 2017Beeline Firebase talk - Firebase event Jun 2017
Beeline Firebase talk - Firebase event Jun 2017
 
Mongo db dla administratora
Mongo db dla administratoraMongo db dla administratora
Mongo db dla administratora
 
Google apps script database abstraction exposed version
Google apps script database abstraction   exposed versionGoogle apps script database abstraction   exposed version
Google apps script database abstraction exposed version
 
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 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 Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
 

Similar to Logging rails application behavior to MongoDB

MongoDB Stich Overview
MongoDB Stich OverviewMongoDB Stich Overview
MongoDB Stich OverviewMongoDB
 
I Can See Clearly Now - Observing & understanding your Spring applications at...
I Can See Clearly Now - Observing & understanding your Spring applications at...I Can See Clearly Now - Observing & understanding your Spring applications at...
I Can See Clearly Now - Observing & understanding your Spring applications at...Joris Kuipers
 
How to leverage what's new in MongoDB 3.6
How to leverage what's new in MongoDB 3.6How to leverage what's new in MongoDB 3.6
How to leverage what's new in MongoDB 3.6Maxime Beugnet
 
Building an api using golang and postgre sql v1.0
Building an api using golang and postgre sql v1.0Building an api using golang and postgre sql v1.0
Building an api using golang and postgre sql v1.0Frost
 
mongodb-introduction
mongodb-introductionmongodb-introduction
mongodb-introductionTse-Ching Ho
 
Tame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapperTame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapperGiordano Scalzo
 
Scalable Event Analytics with MongoDB & Ruby on Rails
Scalable Event Analytics with MongoDB & Ruby on RailsScalable Event Analytics with MongoDB & Ruby on Rails
Scalable Event Analytics with MongoDB & Ruby on RailsJared Rosoff
 
Cross Domain Web
Mashups with JQuery and Google App Engine
Cross Domain Web
Mashups with JQuery and Google App EngineCross Domain Web
Mashups with JQuery and Google App Engine
Cross Domain Web
Mashups with JQuery and Google App EngineAndy McKay
 
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010Alex Sharp
 
MongoSF 2011 - Using MongoDB for IGN's Social Platform
MongoSF 2011 - Using MongoDB for IGN's Social PlatformMongoSF 2011 - Using MongoDB for IGN's Social Platform
MongoSF 2011 - Using MongoDB for IGN's Social PlatformManish Pandit
 
PostgreSQL - масштабирование в моде, Valentine Gogichashvili (Zalando SE)
PostgreSQL - масштабирование в моде, Valentine Gogichashvili (Zalando SE)PostgreSQL - масштабирование в моде, Valentine Gogichashvili (Zalando SE)
PostgreSQL - масштабирование в моде, Valentine Gogichashvili (Zalando SE)Ontico
 
Crafting Evolvable Api Responses
Crafting Evolvable Api ResponsesCrafting Evolvable Api Responses
Crafting Evolvable Api Responsesdarrelmiller71
 
Cross-Platform Data Access for Android and iPhone
Cross-Platform Data Access for Android and iPhoneCross-Platform Data Access for Android and iPhone
Cross-Platform Data Access for Android and iPhonePeter Friese
 
Practical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo DbPractical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo DbAlex Sharp
 
Introduction to source{d} Engine and source{d} Lookout
Introduction to source{d} Engine and source{d} Lookout Introduction to source{d} Engine and source{d} Lookout
Introduction to source{d} Engine and source{d} Lookout source{d}
 
Redispresentation apac2012
Redispresentation apac2012Redispresentation apac2012
Redispresentation apac2012Ankur Gupta
 
Practical Ruby Projects (Alex Sharp)
Practical Ruby Projects (Alex Sharp)Practical Ruby Projects (Alex Sharp)
Practical Ruby Projects (Alex Sharp)MongoSF
 

Similar to Logging rails application behavior to MongoDB (20)

MongoDB Stich Overview
MongoDB Stich OverviewMongoDB Stich Overview
MongoDB Stich Overview
 
I Can See Clearly Now - Observing & understanding your Spring applications at...
I Can See Clearly Now - Observing & understanding your Spring applications at...I Can See Clearly Now - Observing & understanding your Spring applications at...
I Can See Clearly Now - Observing & understanding your Spring applications at...
 
How to leverage what's new in MongoDB 3.6
How to leverage what's new in MongoDB 3.6How to leverage what's new in MongoDB 3.6
How to leverage what's new in MongoDB 3.6
 
Building an api using golang and postgre sql v1.0
Building an api using golang and postgre sql v1.0Building an api using golang and postgre sql v1.0
Building an api using golang and postgre sql v1.0
 
mongodb-introduction
mongodb-introductionmongodb-introduction
mongodb-introduction
 
Tame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapperTame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapper
 
Scalable Event Analytics with MongoDB & Ruby on Rails
Scalable Event Analytics with MongoDB & Ruby on RailsScalable Event Analytics with MongoDB & Ruby on Rails
Scalable Event Analytics with MongoDB & Ruby on Rails
 
Cross Domain Web
Mashups with JQuery and Google App Engine
Cross Domain Web
Mashups with JQuery and Google App EngineCross Domain Web
Mashups with JQuery and Google App Engine
Cross Domain Web
Mashups with JQuery and Google App Engine
 
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
 
MongoSF 2011 - Using MongoDB for IGN's Social Platform
MongoSF 2011 - Using MongoDB for IGN's Social PlatformMongoSF 2011 - Using MongoDB for IGN's Social Platform
MongoSF 2011 - Using MongoDB for IGN's Social Platform
 
Getting Input from User
Getting Input from UserGetting Input from User
Getting Input from User
 
Unit-2 Getting Input from User.pptx
Unit-2 Getting Input from User.pptxUnit-2 Getting Input from User.pptx
Unit-2 Getting Input from User.pptx
 
huhu
huhuhuhu
huhu
 
PostgreSQL - масштабирование в моде, Valentine Gogichashvili (Zalando SE)
PostgreSQL - масштабирование в моде, Valentine Gogichashvili (Zalando SE)PostgreSQL - масштабирование в моде, Valentine Gogichashvili (Zalando SE)
PostgreSQL - масштабирование в моде, Valentine Gogichashvili (Zalando SE)
 
Crafting Evolvable Api Responses
Crafting Evolvable Api ResponsesCrafting Evolvable Api Responses
Crafting Evolvable Api Responses
 
Cross-Platform Data Access for Android and iPhone
Cross-Platform Data Access for Android and iPhoneCross-Platform Data Access for Android and iPhone
Cross-Platform Data Access for Android and iPhone
 
Practical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo DbPractical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo Db
 
Introduction to source{d} Engine and source{d} Lookout
Introduction to source{d} Engine and source{d} Lookout Introduction to source{d} Engine and source{d} Lookout
Introduction to source{d} Engine and source{d} Lookout
 
Redispresentation apac2012
Redispresentation apac2012Redispresentation apac2012
Redispresentation apac2012
 
Practical Ruby Projects (Alex Sharp)
Practical Ruby Projects (Alex Sharp)Practical Ruby Projects (Alex Sharp)
Practical Ruby Projects (Alex Sharp)
 

Recently uploaded

SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 

Recently uploaded (20)

SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 

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