ForzaElixir / rollbax

Exception tracking and logging from Elixir to Rollbar
https://hexdocs.pm/rollbax
ISC License
243 stars 52 forks source link

{m, f, a} configuration access? #112

Closed PragTob closed 5 years ago

PragTob commented 5 years ago

:wave:

First of all, thanks a lot for open sourcing this and enabling everyone to use rollbar with elixir :tada: :clap: :pray:

Trying to integrate it, I noticed that the following configuration didn't work:

config :rollbax,
  enabled: true,
  access_token: {System, :get_env, ["ROLLBAR_ACCESS_TOKEN"]},
  environment: {System, :get_env, ["BASE_HOST"]}

(we wanted to have the secret only on the production host not the build host)

and resulted in:

** (Mix) Could not start application rollbax: Rollbax.start(:normal, []) returned an error: shutdown: failed to start child: Rollbax.Client
    ** (EXIT) an exception was raised:
        ** (FunctionClauseError) no function clause matching in Rollbax.Item.draft/3
            (rollbax) lib/rollbax/item.ex:9: Rollbax.Item.draft({System, :get_env, ["ROLLBAR_ACCESS_TOKEN"]}, {System, :get_env, ["BASE_HOST"]}, %{})

I'm rather used to it working though :D Although, probably because we don't have any of the more advanced distillery configuration solutions set up.

Would you be open to a PR implementing {m, f, a} environment access or alternatively am I missing an easier way to do this?

Thanks and cheers! Tobi

edit: I just realized that the distillery docs might have confused me and that ${VAR} still works but I'm not 100% sure :) Question still stands regardless :)

whatyouhide commented 5 years ago

Hey @PragTob, yeah that's not supported. If you want to have dynamic configuration, you can use:

config :rollbax,
  config_callback: {MyModule, :my_function}

and read the env variables in MyModule.my_function. Does that work for you?

thbar commented 5 years ago

@PragTob in case it helps (since I implemented it recently for a client), typically this will give something along the lines of:

# https://hexdocs.pm/rollbax/Rollbax.html#module-runtime-configuration
defmodule MyApp.Rollbax.RuntimeConfig do
  def config(c) do
    # keys taken from https://hexdocs.pm/rollbax/Rollbax.html#module-configuration
    c
    |> Keyword.put(:access_token, System.get_env("ROLLBAR_ACCESS_TOKEN"))
    # NOTE: `System.get_env/2` requires Elixir 1.9.0+
    |> Keyword.put(:enabled, (System.get_env("ROLLBAR_ENABLED", "0") == "1")
    |> Keyword.put(:environment, System.get_env("ROLLBAR_ENV"))
  end
end

Then later:

config :rollbax,
  config_callback: {MyApp.Rollbax.RuntimeConfig, :config}

Happy error tracking :smile:

PragTob commented 5 years ago

@whatyouhide @thbar thanks a lot :tada:

Yeah that works. I also realized that I misread the distillery docs and "${ENV}" is supported (it seemed like that was "outdated" in the docs, but I'll open another issue there about it). so it worked for me either way.

This is flexible enough of a way to work with this and quite clever, thank you! I might send a PR to add it to the README as it seems at least I didn't find it and people should find it more easily :tada:

Thanks again!