deadtrickster / prometheus-phoenix

Prometheus.io Phoenix instrumenter
71 stars 20 forks source link

[Question] Recommendation for Exporting Metrics #1

Closed geowa4 closed 8 years ago

geowa4 commented 8 years ago

Do you have a recommendation for how I should export metrics within a Phoenix application? Your other repo deadtrickster/prometheus-plugs doesn't seem like it's built for Phoenix since it's checking the path, and Phoenix wants controllers configured for everything. I could reimplement that as a Phoenix controller, but I wanted to reach out to you first. I'm new to Elixir and Phoenix so I could just be missing something obvious. Thanks.

deadtrickster commented 8 years ago

Hey,

Simply put Phoenix has two different parts. One is your 'regular' MVC-like part and it's based in Plugs. The other one is infamous channels.

Right now your HTTP processing pipeline split between lib/<app_name>/endpoint.ex and web/router.ex. Both endpoint and router are fancy plugs. HTTP request has to go through endpoint plugs first where it can be intercepted, modified and/or aborted (just like what plug exporter does) then it enters router pipeline and only there it has a chance to be dispatched to a controller. In case of a Phoenix app you can instrument whole pipeline (well, how much pipeline is instrumented actually depends on where you place your PipelineInstrumenter), individual plug, and Phoenix controllers, views, channels.

defmodule appApi.Endpoint do
  use Phoenix.Endpoint, otp_app: :app_api
  #require Prometheus.PlugsInstrumenter

  socket "/socket", appApi.UserSocket

  plug appApi.MetricsPlugExporter
  plug appApi.PlugPipelineInstrumenter

  # Serve at "/" the static files from "priv/static" directory.
  #
  # You should set gzip to true if you are running phoenix.digest
  # when deploying your static files in production.
  plug Plug.Static,
    at: "/", from: :myusa_customer_api, gzip: false,
    only: ~w(css fonts images js favicon.ico robots.txt)

  ### the rest of the endpoint

Here I put my pipeline instrumenter and exporter just below socket macro call. Sockets aren't plugs and implemented as raw cowboy handlers so I'm not missing anything here and Instrument whole pipeline, i.e. if request still dispatched to a controller then http_request_duration_microseconds of the appApi.PlugPipelineInstrumenter will include phoenix_controller_call_duration_microseconds of the appApi.PhoenixInstrumenter.

Let me know if something still unclear.

geowa4 commented 8 years ago

That's perfect; everything works as expected. Thank you for the quick reply!