deploy_feedback icon indicating copy to clipboard operation
deploy_feedback copied to clipboard

How to detect in Deno if it is currently a `prod` deployment?

Open Sleepful opened this issue 1 year ago • 4 comments

What problem are you trying to solve?

I need to configure logging for my app and I need to differentiate whether a message originated from a prod environment or from one of the preview environments.

Describe the solution you'd like

Something akin to checking an ENV var that updates as the status of the Deno isolate/instance changes

Describe alternatives you've considered

Tracing the URL of requests that generate messages as a proxy for this info

Documentation, Adoption, Migration Strategy

No response

Sleepful avatar Nov 24 '24 05:11 Sleepful

Currently there is no smart way to determine if a particular deployment is run as a prod deployment. One idea I've come up with is looking at host header. Let's say the project name is magurotuna-prod-host, and you deploy the following code:

Deno.serve((req) => {
  const host = req.headers.get("host");
  const isProd = host === "magurotuna-prod-host.deno.dev";
  return Response.json({ isProd });
});

Then, if you make a request to the prod deployment via https://magurotuna-prod-host.deno.dev, you'll see isProd to be true. In contrast, if you do to a preview deployment via, say, https://magurotuna-prod-host-abc1234defg.deno.dev, isProd should be false.

magurotuna avatar Nov 25 '24 11:11 magurotuna

Ah cool, thanks!

There is another way, which is useful if you are trying to determine the environment outside of an HTTP Request going through the middleware (or if it might be too much trouble trickling down the origin value).

  1. Make an API endpoint that returns the Deno.env.get('DENO_DEPLOYMENT_ID') [1]
  2. From your code, make a fetch request to this endpoint that you just made, using the ORIGIN domain in which you expect your prod deployment to reside fetch('https://myRealProdURL')
  3. Compare the result of the API with the Deno.env.get('DENO_DEPLOYMENT_ID'). If they are equal that means the current instance is the prod deployment.

Kinda like asking yourself your own name, and waiting to see if it is the one you remember.

Sleepful avatar Dec 14 '24 20:12 Sleepful

Actually you will get an error if you try to request your own API...

This is such a hack but it works... silly.

export async function isProd() {
  const url = `${DenoDeploy_ORIGIN}`;
  const response = await fetch(url);

  if (!response.ok) {
    if (response.status === 508) {
      // Deno Deploy does not allow the server to send a request to itself... -_-
      // https://github.com/denoland/deploy_feedback/issues/243
      return true;
    }
    throw new Error(`isProd() HTTP error! Status: ${response.status}`);
  }
  return false
}

Sleepful avatar Dec 18 '24 08:12 Sleepful

  1. Make an API endpoint that returns the Deno.env.get('DENO_DEPLOYMENT_ID') [1]

It will cause

Env vars cannot begin with DENO_

error

westtrade avatar Dec 24 '24 13:12 westtrade