How can you send `revision` to Appsignal from a Phoenix app?

With Appsignal, you can use deploy markers to flag when a bug happened in the git timeline.

In my appsignal.exs file I have:

use Mix.Config

config :appsignal, :config,
  otp_app: :my_app,
  name: "my_app",
  push_api_key: "my_key",
  env: Mix.env(),
  revision: System.get_env("RENDER_GIT_COMMIT")

However I don’t think it’s pulling the right value during compilation using mix release.

Does anyone have experience with this?

I’m using that setting on a project of mine and it works fine – though not using render for that one.

Interesting, do you mind pasting your appsignal.exs file here to compare? I also tried changing to import Config at the top of the file and it doesn’t seem to grab the revision flag.

It looks the same as the code you posted besides a different system env variable name.

Very weird… I wonder if the ENV var is just not there.

This worked fine:

import Config

config :appsignal, :config,
  otp_app: :my_app,
  name: "my_app",
  push_api_key: "my_key",
  env: Mix.env(),
  revision: "jan-10-2023-test"

I wonder why the ENV value isn’t being loaded in properly…

I got it to work.

For posterity, I delete the appsignal.exs file that Appsignal recommends.

Instead I put the appsignal configuration in runtime.exs where I know System.get_env works fine.

if config_env() == :prod do
  config :appsignal, :config,
    otp_app: :my_app,
    name: "my_app",
    push_api_key: "my_key",
    env: :prod,
    revision: System.get_env("RENDER_GIT_COMMIT")

And now I can see the deploy marker.

image

3 Likes

Just an FYI for folks deploying to Fly.io. You can use the FLY_IMAGE_REF environment variable to get a unique-ish revision ID:

# config/appsignal.exs

{_rest, revision} = System.get_env("FLY_IMAGE_REF") |> String.split_at(-8)

config :appsignal, :config,
  revision: revision,
  # others

The FLY_IMAGE_REF is something like:

registry.fly.io/my-app-name:deployment-01H9RK9EYO9PGNBYAKGXSHV0PH

and this little script will give you the last 8 letters, for example:

GXSHV0PH

This might be good enough for the revision id :slight_smile:

This is what I do to provide the git revision to AppSignal. I deploy from a GitHub Action, so pass the revision as a build argument:

flyctl deploy --build-arg REVISION=${{ github.sha }} --remote-only

Alternatively, if you deploy from the command line, then you could use the following

flyctl deploy --build-arg REVISION=$(git rev-parse HEAD) --remote-only

Then in the Dockerfile, set that build argument as APP_REVISION environment variable in the final runner image, AppSignal’s library checks that env variable:

# ...

FROM ${RUNNER_IMAGE}

ARG REVISION="unknown"
ENV APP_REVISION=$REVISION

# ...
1 Like

Even easier is to inject an environment variable directly on deploy:

fly deploy -e APP_REVISION=$(git rev-parse --short HEAD)

No docker-fu needed (for that part of the process anyway)!

1 Like

Ah, good point. I also use the RELEASE argument while compiling the application in the builder phase.