Git Product home page Git Product logo

x-ray-machine's Introduction

X Ray Machine

X-Ray

Ever wanted to log and profile your external API calls in a Rails app the same way Rails does with active record? Look no further, as the x-ray-machine is the thing that will enable you to do that!

Usage

As per usual add this to your Gemfile

gem 'x-ray-machine'

Then just call XRay.whatevers with some marker you wanna see in the logs (it can be an url or anything stringy) and then give it a block to measure.

class MyThing
  def talk_to_elastic_search
    url = figure_the_url

    XRay.elastic_search url do
      make_the_actual_request url
    end
  end

  def talk_to_twitter_api
    XRay.twitter "loading recent tweets" do
      load_some_tweets_for_fun_and_profit
    end
  end

  def craaaazy_stuff
    XRay.baaacooon "fat acids hitting the brain" do
      i_wonder_if_spacemen_eat_bacon
    end
  end
end

This will show something like this in your rails console

ElasticSearch (5.1ms)  /url?bla=bla&bla
Twitter (100.2ms) loading recent tweets
Baaacooon (10.1ms) fat acids hitting the brain

Marking Cached Results

In case you want to mark a query as cached, you can use the ray object that is passed down the block, and mark it as cached

url = "some/url.thing"

XRay.heavy_request url do |ray|
  if result = find_in_cache(url)
    ray.cached = true
  else
    result = make_the_actual_request(url)
  end
end

After that your log entry will look like so

HeavyRequest CACHE (0.1ms)  /url?bla=bla&bla

Customization & Configuration

Normally, the XRay object will use method_missing and automatically guess the name for the entry and use a random color for it, but you can customize things

XRayMachine.config do |config|
  config.elastic_search = {
    color: :yellow,         # color for the line
    title: "ES",            # title for the entries
    show_in_summary: false  # show/hide the results in the summary
  }
end

NOTE: the name you use with the config should match the one that you use on the XRay class to track your queries.

Copyright & License

All code in this repository is released under the terms of the MIT License

Copyright (C) 2014 Nikolay Nemshilov

x-ray-machine's People

Contributors

kaievns avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.