How to run new relic command on koyeb to enable infrastructure and logs metrics

I want add new relic to my fastapi in koyeb I tried to use console but it keeps throwing sudo error. how can I implement this

hello!

How are you installing newrelic? From what I understand of your message, you are trying to run pip install newrelic from a shell on your instance.

You should instead update your requirements.txt file and add newrelic in it.

If my message doesn’t help, can you please provide:

  • a screenshot of the error
  • explain how do you package your application?

Thanks,

1 Like

Hello!
Thank you for your response I already have new relic in my requirements.txt and it works partially at the moment so basically there are 2 things you have to do

  1. Adding the new relic file and adding the build command in koyeb [I already did this successfully]
NEW_RELIC_CONFIG_FILE=newrelic. ini newrelic-admin run-program $YOUR_COMMAND_OPTIONS
  1. this is where I am stuck as it requires me to download the cli on the host but I do not know how to … I tried “Console” but no luck

still waiting :slight_smile: …

Hello,

I’m sorry it took me a while to answer.
I think the only option is to use a Dockerfile, instead of the buildpack builder.

Create a Dockerfile, install your application and the CLI, and it should work.

Do you need help to do so?

Yes I do since I do not know docker

Alright. Below is a small explanation how to start a fastapi application. You will have to adapt this to fit your use-case.

Let’s say you have a fastapi application stored in app.py:

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

Next to it, let’s say you have a requirements.txt file to install fastapi and uvicorn:

fastapi
uvicorn

All you have to do is create a file named Dockerfile with the following content:

FROM python

RUN curl http://download.newrelic.com/install/newrelic-cli/scripts/install.sh | bash

COPY requirements.txt /app/
WORKDIR /app

# Install dependencies
RUN pip install -r requirements.txt

COPY . /app

CMD ["uvicorn", "app:app", "--host", "0.0.0.0"]

Notice we install the newrelic CLI, using the link you gave in your previous answer.

From Koyeb, create a new service and select the Docker builder, and expose the port 8000.

1 Like

I’m struggling with this on the Docker level as well.

I’m deploying a Ruby on Rails app and, while the infrastructure and logs step is optional/not blocking, NewRelic doesn’t make it clear what information is lost without this. The lack of instructions for running this within a Docker container as opposed to on the host or via its own Dockercontain suggests this may not be a common or supported case.

I’ll pop my findings in here to see if either Koyeb can help or if this information is useful to the next person searching for this issue.

My Dockerfile is almost exactly the Rails 7.2 standard, copied at the bottom of this post. It contains a base step, a throw-away build step for a smaller final image, and the final step.

The NewRelic command is as follows, modified slightly to show the two parts.

curl -Ls https://download.newrelic.com/install/newrelic-cli/scripts/install.sh | bash && \

This first part curls an install script and pipes it to bash which grabs the latest from GitHub and unpacks it onto the system.

sudo NEW_RELIC_API_KEY=REDACTED NEW_RELIC_ACCOUNT_ID=REDACTED /usr/local/bin/newrelic install -n logs-integration

This second part runs the code from GitHub and I believe installs the additional libraries for runtime.

I imagine this would be ideally run in the build step of the Dockerfile with the installed binaries copied in the final step. I also imagine there’s something that might be needed here or in the docker-entrypoint to run these tools. I’ll annotate the Dockerfile below where these might fit in.

Hopefully this is enough information for us to find a solution or help the next person who is more familiar with NewRelic or Docker.

# syntax = docker/dockerfile:1

# This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand:
# docker build -t my-app .
# docker run -d -p 80:80 -p 443:443 --name my-app -e RAILS_MASTER_KEY=<value from config/master.key> my-app

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
ARG RUBY_VERSION=3.3.5
FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base

# Rails app lives here
WORKDIR /rails

# Install base packages
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y curl libvips postgresql-client && \
    rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Set production environment
ENV RAILS_ENV="production" \
    BUNDLE_DEPLOYMENT="1" \
    BUNDLE_PATH="/usr/local/bundle" \
    BUNDLE_WITHOUT="development"


# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build gems
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y build-essential git libpq-dev pkg-config && \
    rm -rf /var/lib/apt/lists /var/cache/apt/archives

# NEWRELIC INSTALL POINT
# MAYBE NEED TO ADD `sudo` TO THIS INSTALL OR THE ONE IN `base`
RUN curl -Ls https://download.newrelic.com/install/newrelic-cli/scripts/install.sh | bash && \
    sudo NEW_RELIC_API_KEY=REDACTED NEW_RELIC_ACCOUNT_ID=REDACTED /usr/local/bin/newrelic install -n logs-integration

# Install application gems - `.ruby-version` needed as it is used by Gemfile
COPY Gemfile Gemfile.lock .ruby-version ./
RUN bundle install && \
    rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
    bundle exec bootsnap precompile --gemfile

# Copy application code
COPY . .

# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/

# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
RUN HOST=1 SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile


# Final stage for app image
FROM base

# Copy built artifacts: gems, application
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
COPY --from=build /rails /rails

# NEWRELIC COPY POINT
COPY --from=build "${NEWRELIC_INSTALL_PATH}" "${NEWRELIC_INSTALL_PATH}"

# Run and own only the runtime files as a non-root user for security
RUN groupadd --system --gid 1000 rails && \
    useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
    chown -R rails:rails db log storage tmp
USER 1000:1000

# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "server"]