readmeio / metrics-sdks

SDKs and integrations for ReadMe's Metrics platform
https://readme.com/metrics
10 stars 23 forks source link

Two wishes: Send only some requests & Easier Rails invocation #351

Open sandstrom opened 2 years ago

sandstrom commented 2 years ago

Skipping requests

We'd like to skip requests on a per-request basis. Basically, only requests from our customers sandbox accounts should be sent, for privacy reasons.

If we could return false from the block, or send e.g. { :ignore => true } that would be great.

config.middleware.use Readme::Metrics, options do |env|
  current_user = env['warden'].authenticate

  if current_user && current_user.sandbox?
    {
      api_key: current_user.api_key,
    }
  else
    false # or { :ignore => true }
  end
end

Easier Rails Integration

We have a bunch of logic in our Rails ApplicationController, that will run authentication, etc. We'll lookup the company based on the API key, and only after we've checked which account it is, do we know if we want to sent this to Readme.

More specifically, we only want to send Metrics for certain sandbox accounts (for privacy and contractual reasons we cannot send any production data).

With the middleware approach this gets complicated.

We'd rather do something like this:

class ApplicationController < ActionController::Base

  # our actual controller contains more code, this is a simplified example

  before_action :run_auth
  before_action :send_to_readme

  def run_auth
    # …
  end

  def send_to_readme
    if current_company.sandbox? && request_type == 'api'
      Readme::Metrics.send(request, api_key: current_api_key)
    end
  end

end

This would also solve the first item, sending only some requests, since in this example sending a request is opt-in.

Dashron commented 2 years ago

Hi @sandstrom,

We've been working on improving our metrics SDKs and are going to slip the first request into the upcoming update. I think it's a great idea.

The second request, a direct API call without middleware, is one we will eventually be adding to all of our SDKs. We've already added it to the node sdk but I don't have a timeline on when we'll get to it for the ruby sdk.

Let me know if this unblocks you, or if the second request is necessary to move forward.

sandstrom commented 2 years ago

@Dashron Thanks for a quick reply!

We'll probably wait for the direct call. But great to know that it's on the horizon!

Hopefully there will still be the batching + some mechanism such that failed submission of metrics won't interfere with the ruby process in any way. Always an important thing with stuff running in-memory like this.

In case it's helpful, the Ruby tracing product Skylight has a gem which does something similar to you. They send trace data for basically every request (they are not a competitor, but their product is very similar in that it will ingest request data from running Ruby applications).

Maybe their gem has something interesting that could be of use to you: https://github.com/skylightio/skylight-ruby

Dashron commented 2 years ago

Thanks for the tip, we'll keep it in mind!