Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Support environment variables via dotenv in development #84

Open
nathany opened this issue Mar 20, 2014 · 7 comments
Open

Support environment variables via dotenv in development #84

nathany opened this issue Mar 20, 2014 · 7 comments

Comments

@nathany
Copy link

nathany commented Mar 20, 2014

We usually use dotenv 0.10 to configure environment variables via an .env.development file.

I get no librato-rails logging or metrics sent in the following situations:

  • Environment variables specified on the command line to rails s (no librato.yml)
  • Environment variables in .env.development file (no librato.yml)
  • Environment variables in .env.development file referenced in librato.yml

It only works with librato.yml with the token and email under the development section in that file.

Verified that dotenv environment variables are available when running rails c.

@nextmat
Copy link
Contributor

nextmat commented Mar 20, 2014

A quick point of clarification: environment variables are supported in development, the issue is with how dotenv loads them.

Dotenv loads the environment variables in your .env.development file into the ruby environment hash as the rails process is starting up, via the railtie here:

https://github.com/bkeepers/dotenv/blob/master/lib/dotenv/railtie.rb

So your .env.development vars aren't actually available at process startup.

If our railtie runs before the dotenv railtie the librato-rails configuration is built before your env vars are available for use.

Unfortunately we need to do this early (before the rails initializers run) to ensure that we're available for certain types of instrumentation during rails startup/initialization.

Most of the things you would configure with env vars check them in rails initializers or later, so dotenv works fine for them.

I don't know that there is an easy fix for this, unfortunately. Have you considered using something like foreman to manage sourcing in the environment before you start the app in development? It basically uses the same convention, a .env file, but it will actually set them all before the ruby process starts.

@nextmat nextmat changed the title Support environment variables (and dotenv) in development Support environment variables via dotenv in development Mar 20, 2014
@nathany
Copy link
Author

nathany commented Mar 20, 2014

Thanks for the quick & thorough response.

In that case, I think the easiest fix is to add a note to readme regarding dotenv.

@nextmat
Copy link
Contributor

nextmat commented Mar 20, 2014

Yeah, I guess the question is is this resolvable and if not which side it should be documented on. @bkeepers, any advice/thoughts on this?

@bkeepers
Copy link

See the note on https://github.com/bkeepers/dotenv#rails

[dotenv] should be listed in the Gemfile before any other gems that use environment variables, otherwise those gems will get initialized with the wrong values.

Does that fix it for you?

@nathany
Copy link
Author

nathany commented Mar 28, 2014

@bkeepers It doesn't appear to help. If I remove my librato.yml and specify them in .env.development it doesn't log any [librato-rails].

Happy enough using the YML file, it just was confusing to setup.

@stujo
Copy link

stujo commented Jul 18, 2017

I think this behaviour is because Librato::Rails::Railtie reads the configuration when the file is loaded (by the bundler) and this is before the Dotenv railtie load method is called in before_configuration. This happens even if Dotenv is loaded first and before rails has started

@nathany
Copy link
Author

nathany commented Jul 18, 2017

I haven't used Rails in a few years now. Feel free to close this issue if it's no longer relevant.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants