Giter VIP home page Giter VIP logo

optics-agent-ruby's Introduction

optics-agent-ruby

Optics Agent for GraphQL Monitoring in Ruby.

This is an alpha release, suitable for use in development contexts. There are still some outstanding improvements to make it ready for production contexts; see the known limitations section below.

Gem Version Build Status

Installing

Add

gem 'optics-agent'

To your Gemfile

Setup

API key

You'll need to run your app with the OPTICS_API_KEY environment variable set to the API key of your Apollo Optics service; at the moment Optics is in early access alpha--get in touch if you want to be part of our early access program.

Basic Rack/Sinatra

Create an agent

# we expect one day there'll be some options
agent = OpticsAgent::Agent.instance

Register the Rack middleware (say in a config.ru):

use agent.rack_middleware

Register the Graphql middleware:

agent.instrument_schema(YourSchema)

Add something like this to your route:

post '/graphql' do
  request.body.rewind
  params = JSON.parse request.body.read
  document = params['query']
  variables = params['variables']

  result = Schema.execute(
    document,
    variables: variables,
    context: { optics_agent: env[:optics_agent].with_document(document) }
  )

  JSON.generate(result)
end

Rails

The equivalent of the above for Rails is:

Create an agent in config/apllication.rb, and register the rack middleware:

module YourApplicationRails
  class Application < Rails::Application
    # ...

    config.optics_agent = OpticsAgent::Agent.instance
    config.middleware.use config.optics_agent.rack_middleware
  end
end

Register the Graphql middleware when you create your schema:

Rails.application.config.optics_agent.instrument_schema(YourSchema)

Register Optics Agent on the GraphQL context within your graphql action as below:

def create
  query_string = params[:query]
  query_variables = ensure_hash(params[:variables])

  result = YourSchema.execute(
    query_string,
    variables: query_variables,
    context: {
      optics_agent: env[:optics_agent].with_document(query_string)
    }
  )

  render json: result
end

You can check out the GitHunt Rails API server example here: https://github.com/apollostack/githunt-api-rails

Known limitations

Currently the agent is in alpha state; it is intended for early access use in development or basic (non-performance oriented) staging testing.

We are working on resolving a list of issues to put together a production-ready beta launch. The headline issues as things stand are:

  • The agent is overly chatty and uses a naive threading mechanism that may lose reporting data when threads exit/etc.
  • Instrumentation timings may not be correct in all uses cases, and query times include the full rack request time.

You can follow along with our Beta Release Project, or even get in touch if you want to help out getting there!

Development

Running tests

bundle install
bundle exec rspec

Building protobuf definitions

Ensure you've installed protobuf and development dependencies.

# this works on OSX
brew install protobuf

# ensure it's version 3
protoc --version

bundle install

Compile the .proto definitions with

bundle exec rake protobuf:compile

optics-agent-ruby's People

Contributors

tmeasday avatar xuorig avatar zol avatar glasser avatar n1mmy avatar

Watchers

James Cloos avatar Robert Mosolgo avatar  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.