Heroku Review App config and logging (vs. production config/logging) - node.js

I'm looking into some issues I'm having with my Heroku Review Apps.
It appears that from Bash, there's no way to access the config (environment variables) or logging. If I run:
heroku logs --tail
This just comes back with logs from the production app (which makes sense, since there doesn't seem to be a way to keep the scope at the review app). I can see the Review App logs from within the Heroku dashboard, but even this seems a bit off, as subsequent refreshes will omit different lines from the log (which is why I'm trying to just see them from Bash).
Also, if I do:
heroku config
It returns back the expected config values (i.e. DATABASE_URL), but again, I'm not sure if this is just for production.
For example, running this from the review app:
console.log("process.env.PORT: " + process.env.PORT);
console.log("process.env.DATABASE_URL: " + process.env.DATABASE_URL);
console.log("process.env.PAPERTRAIL_API_TOKEN: " + process.env.PAPERTRAIL_API_TOKEN);
The PORT is returning a value, but both DATABASE_URL AND PAPERTRAIL_API_TOKEN are coming back undefined, even though these are both populated in heroku config.
So my questions are:
1) How do I view a Review App's logs from Bash (or any other reliable method other than the Heroku dashboard)?
2) Are the config values different between Review Apps and production, and if so, how do I configure them for Review Apps?

Use --app to point to your review app, e.g:
heroku config --app my_review_app
heroku config:set VAR=VALUE --app my_review_app

Yoni is exactly right, but here are more details.
When running heroku config, the first line of the response shows the app it is currently pointing to. This should've been my first clue, and I missed it initially.
$ heroku config
=== fathomless-meadow-38193 Config Vars
DATABASE_URL: postgres://xxxxxxxxxx...
Once I ran the config for my review app, it indeed returned empty.
$ heroku config --app blooming-river-14132-pr-2
=== blooming-river-14132-pr-2 Config Vars
As Yoni said, you can use the -app switch to specify an app when setting config variables. But I wanted my default app to point to my blooming-river app, which lead me to this post on how to change this:
$ heroku git:remote -a blooming-river-14132-pr-2
Now I can set DATABASE_URL using:
$ heroku config:set DATABASE_URL=POSTGRES://xxxxxxxx...
I don't really like having to manually set it for each new pull request, so I found out that Review Apps can inherit config vars from a parent app. Unfortunately, I never could get this work, at least not for DATABASE_URL.
So I'm still trying to figure that out, and will update this answer if I do.

There are addons to collect the logs and have so many other features like alerts.
So you can consider using one of them. I am just giving one that actually we are using in our app called Logentries
https://elements.heroku.com/addons/logentries

Related

getting 401 response codes on a deployed React App that worked fine in development?

I am trying to complete a Redux project and got everything to work fine in development on my own machine but when deployed, the browser is failing to recognise my hidden environment variables so not passing client keys to Api calls and thus making unauthorised requests. I have a '.env' file with the keys in them and then refer thus...
const API_URL = https://api.openweathermap.org/data/2.5/weather?appid=${process.env.REACT_APP_OPEN_WEATHER_MAP_API_KEY}&units=metric;
I have looked up the problem on here and think I understand what's going wrong but don't know how to put it right. I am deploying site via Netlify from my Github repo and my build settings appear to be correct. I thought 'npm run build' took care of all that for me?
99% finished a project and now scratching my head furiously...
Try defining variables in .env file starting with REACT_APP like:
REACT_APP_CLIENT_ID=4
REACT_APP_API_URL=/api
Try using it anywhere like:
const apiUrl: process.env.REACT_APP_API_URL;
Set the environment variables specifically within the Netlify UI. So 'Deploy Settings > Environment > Environment Variables' then variable -'REACT_APP....' value - '*****' for each variable you want defined.

Environment variables in NodeJs using cPanel

So I'm using cPanel with Setup Node.js App plugin for a Next.js app. (don't asky why cPanel)
Everything is working as expected in development, except for environment variables in production, I set up them manually from the cPanel interface, I restarted/stopped the app, logging the process.env on the server and I don't see the env variables there (not to say when trying to call them they are undefined).
When doing
res.json(JSON.stringify(process.env)); i get a bunch of variables except for the one I manually wrote in cPanel variables interface.
It is important for me to store these variables as secret key because they are API credentials.
Anyone know what I might have misconfigured or had this problem?
Never mind, found the answer, apparenlty was a Next.js misconfiguration. I had to add the following lines of code inside next.config.js in order to read env variables on build version.
require('dotenv').config();
module.exports = {
env: {
EMAIL_NAME: process.env.EMAIL_NAME,
EMAIL_PASSWORD: process.env.EMAIL_PASSWORD,
GETRESPONSE_API_KEY: process.env.GETRESPONSE_API_KEY
}
};
Where EMAIL_NAME, EMAIL_PASSWORD, GETRESPONSE_API_KEY were the variables defined by me on cPanel interface

Docker environment variables, dokku-redis

Using redis as my session store in my express.js app. I'm having problems. Narrowed them down to a connection issue.
How do I access a docker environment variable from within an express.js app? I'm using dokku-redis.
It reports that environment variables are automatically set up on the linked app... I've linked my app. running dokku redis:info foo shows that all is linked. I'm trying to pull in REDIS_URL
Thanks, Rob
https://github.com/dokku/dokku-redis
------------
a redis service can be linked to a
container this will use native docker
links via the docker-options plugin
here we link it to our 'playground' app
NOTE: this will restart your app
dokku redis:link lolipop playground
the following environment variables will be set automatically by docker (not on the app itself, so they won’t be listed when calling dokku config)
DOKKU_REDIS_LOLIPOP_NAME=/lolipop/DATABASE
DOKKU_REDIS_LOLIPOP_PORT=tcp://172.17.0.1:6379
DOKKU_REDIS_LOLIPOP_PORT_6379_TCP=tcp://172.17.0.1:6379
DOKKU_REDIS_LOLIPOP_PORT_6379_TCP_PROTO=tcp
DOKKU_REDIS_LOLIPOP_PORT_6379_TCP_PORT=6379
DOKKU_REDIS_LOLIPOP_PORT_6379_TCP_ADDR=172.17.0.1
and the following will be set on the linked application by default
REDIS_URL=redis://lolipop:SOME_PASSWORD#dokku-redis-lolipop:6379
NOTE: the host exposed here only works internally in docker containers. If
you want your container to be reachable from outside, you should use 'expose'.
------------------------------
Edit - sorry, I forgot to add that I have tried process.env
How did you link the redis service with your application? You cannot simply do dokku config:set, and if you did, you should unset it and then use dokku redis:link instead. Once you do that, rebuild your app using dokku ps:rebuild APP and you should get process.env.REDIS_URL set.
you can access environment variables through process.env like the following:
console.log(process.env["DOKKU_REDIS_LOLIPOP_NAME"]);
console.log(process.env["DOKKU_REDIS_LOLIPOP_PORT"]);
console.log(process.env["REDIS_URL"]);
also as long as keys in objects do not harm variable naming conventions you can access them like this too:
console.log(process.env.REDIS_URL);
more info in regards to javascript variable naming rules: What characters are valid for JavaScript variable names?

nodejs/redis auth works locally (w/Foreman), but not on Heroku

It looks like my req.session (.user) doesn't exist when my node app is on Heroku. Things work on my local machine, and when I push to heroku, I have access to my redis-to-go store for other uses, but when I log in and proceed to authenticated pages, I get "Internal Server Error" on the served page, and "Cannot read property 'currentUser' of undefined" in the log, w/undefined = req.session, I believe.
Also, I have the same redistogo code at the start of both files:
if process.env.REDISTOGO_URL
rtg = require("url").parse(process.env.REDISTOGO_URL)
redis = require("redis").createClient(rtg.port, rtg.hostname)
redis.auth(rtg.auth.split(":")[1])
else
redis = require("redis").createClient()
edit:
I'm still rather new to Heroku/express/redis, and I realized that the problem may be that the code above isn't in the function that's being exported and called, so I moved it in, but I can't get it to deploy. After
git add .
git push heroku master
heroku releases
I get the same old list of deploys and rollbacks that I had before…
so, 1) My problem is that I had my code outside of the exported fn??
2) How do I push my changes? Have I possibly run out of memory on Heroku? my program isn't very large…
Edit2 - I figured it out. There were several small changes I needed to make.
Thanks,
John
Try changing your first line to:
if (typeof(process.env.REDISTOGO_URL) !== 'undefined')
If that doesn't work, add console.log statements to see which side of the if..else statement you're hitting on Heroku.

How can I set up a node app (ember app kit) on heroku that reads ENV variables and makes the values available to the application?

Okay, I'm new to node, and really only just using the node server to serve static js, but I can't find any info on this anywhere.
I'm running an application ember app kit, which gets built to a node server.js for deploy, and heroku runs it with node server.js.
It uses grunt for building, testing, etc.
I'd like to know how I can specify configuration variables (i.e. authentication tokens) that can be overridden by heroku config variables.
The closest I've been able to get is a custom task that reads environment variables and writes out a json file that gets built into the site (and assigned to a global var). This works locally, but doesn't take into account heroku configs.
I even wrote a deploy script that gets heroku's configs, exports them as environment variables locally, and does the build--Which works, but the configs only get updated on app deploy. So if I do a heroku config:add CONFIG_TEST=test_value, my app doesn't see that value for CONFIG_TEST until the next time I deploy the app.
I'd like for my app to start embedding that config value in the browser JS immediately.
Any way to do this with node the way my app is set up?
I am not sure I understand what's wrong with simply taking config variables, at run time, from the environment. Use process.env.KEY in your code, and embed that result into whatever template you may have, and serve that as the result.
When you change Heroku config variables your process gets restarted, so it picks up the new values.
Is the problem the fact that you serve static files? If so -- can you simply change it so that you use a template engine to do some processing on them before serving?
OK, here's a solution for ember-app-kit using grunt-sed.
In EMBER_APP_KIT_PROJECT/tasks/options/sed.js
Add something like
module.exports = {
version: {
path: "./dist/",
pattern: '{{env.API_BASE_PATH}}',
replacement: function(){
return process.env.API_BASE_PATH;
},
recursive: true
}
};
then in your code just put
"{{env.API_BASE_PATH}}"
Now, when you run
$ grunt sed
it will replace "{{env.API_BASE_PATH}}" with whatever's in the environment variable.

Resources