Git Product home page Git Product logo

c-sdk-container-example's Introduction

C-SDK And Daemon Containers Example

This is an unsupported example. This project is provided AS-IS WITHOUT WARRANTY OR SUPPORT.

The C SDK is a software development kit that allows users to create instrumentation for applications written in C and C++. You can also use it in languages like Perl, which are implemented in C or C++, and in any language that has a foreign function interface with C. With the release of C-SDK version 1.2, the application and daemon no longer have to reside on the same host and can now communicate over an IPv4 or IPv6 TCP socket. This repository contains a simple application that demonstrates how an instrumented application and daemon can be set up on separate containers.

Data transmitted from an instrumented application to the daemon is not encrypted. We recommend only using a private network connection between the app and daemon.

Example Application

The application uses the following environment variables.

Var Name Description Required
NEW_RELIC_LICENSE_KEY The New Relic License Key. Required
NEW_RELIC_APP_NAME The New Relic app name that will appear in the UI. Required
NEW_RELIC_HOST New Relic collector host. If NULL will be set to the default of collector.newrelic.com Not required

Run the application by using the run_app script:

  • ./run_app

Instrumented Application Container

The app Dockerfile below shows how to configure an application container:

  • All the required packages to build and run are installed. See C-SDK Requirements for more information.
  • C-SDK is cloned into the container.
  • The files in the app/example directory are copied into the container example directory.
  • The C-SDK and example are both compiled.
  • Dockerize is used to overcome possible synchronization issues. If the app container is ready first, it will wait for the daemon container to be available on the daemon address.
  • Run using ./ex_container.out.

Visit C-SDK getting started for more details on ex_container.c.

app/Dockerfile:

FROM ubuntu:bionic

# Install dependencies
RUN apt-get update && apt-get install -y \
  build-essential \
  git \
  golang \
  libpcre3-dev \
  wget

# Clone C-SDK repo
RUN git clone https://github.com/newrelic/c-sdk.git
RUN mkdir example

# Copy app files
COPY ["/example/*", "/example/"]

# Build C-SDK and example app
WORKDIR /c-sdk
RUN make
WORKDIR /example/
RUN make 

# Use dockerize so the app container waits for the daemon container before connecting
ENV DOCKERIZE_VERSION v0.6.0
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
  && tar -C /usr/local/bin -xzvf dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
  && rm dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz

# Run example app
CMD dockerize -wait tcp://${NEW_RELIC_DAEMON_ADDRESS} \
  && ./ex_container.out

Daemon Container

There are two ways to configure the daemon container:

  • Pre-made newrelic/c-daemon Docker image.
  • Create your own daemon container.

Daemon Docker Image

To simplify the daemon container configuration, New Relic has published a newrelic/c-daemon docker image. To use it, pull the image with the docker pull newrelic/c-daemon command and run it with the docker run --name [daemon-name] newrelic-daemon. The daemon in this image listens on port 31339. For more information on this image, visit Docker Hub or our repo.

Daemon Dockerfile

If you want to manually set up the daemon container, the Dockerfile below demonstrates how this would be accomplished:

  • All the required packages to build and run are installed. See C-SDK Requirements for more information.
  • C-SDK is cloned into the container.
  • The daemon is compiled and started.
  • Run using the --address and --watchdog-foreground
    • --address argument is used to set a port the daemon is accepting connections on. --address and NEW_RELIC_DAEMON_ADDRESS (environment variable in the app section of the docker_compose.yml) must match.
    • --watchdog-foreground argument ensures that the daemon runs in the foreground.
    • --logfile is set to stdout and --loglevel is set to debug. See C-SDK daemon logs for more information.

daemon/Dockerfile:

FROM ubuntu:bionic

# Install dependencies
RUN apt-get update && apt-get install -y \
  git \
  golang

# Clone C-SDK repo
RUN git clone https://github.com/newrelic/c-sdk.git

# Build daemon
WORKDIR /c-sdk
RUN make daemon

# Run daemon
CMD ./newrelic-daemon --logfile stdout --loglevel debug --address=daemon:31339  --watchdog-foreground

Application Docker Compose

The docker_compose.yml file displays the container structure of the example application. It will look differently depending on how you configure the daemon container. Using the newrelic/c-daemon image simplifies daemon container setup, but limits the ability to use the non-default port. If you would like to use a different port for the agent <--> daemon connection, manual daemon container setup is recommended.

App

The app section is used to setup the app container. Environment variables used in app/Dockerfile are set here (using a valid NEW_RELIC_LICENSE_KEY, NEW_RELIC_APP_NAME and NEW_RELIC_HOST ).

Daemon

Daemon Docker Image

When using the pre-made newrelic/c-daemon image, image: newrelic/c-daemon is added to the daemon section. The default port for this image is 31339 and cannot be changed.

version: '2.4'

services:
  app:
    build:
      context: app 
    depends_on:
      - "daemon"
    environment:
      NEW_RELIC_LICENSE_KEY: ${NEW_RELIC_LICENSE_KEY}
      NEW_RELIC_APP_NAME: ${NEW_RELIC_APP_NAME}
      NEW_RELIC_HOST: ${NEW_RELIC_HOST}
      NEW_RELIC_DAEMON_ADDRESS: "daemon:31339"
  daemon:
    image: newrelic/c-daemon

Create Your Own Daemon Container

When manually setting up the daemon (downloading and installing the daemon in its container), expose is used to set the port the daemon is listening on. This must match the --address (daemon startup argument) and NEW_RELIC_DAEMON_ADDRESS (in the app environement section).

version: '2.4'

services:
  app:
    build:
      context: app 
    depends_on:
      - "daemon"
    environment:
      NEW_RELIC_LICENSE_KEY: ${NEW_RELIC_LICENSE_KEY}
      NEW_RELIC_APP_NAME: ${NEW_RELIC_APP_NAME}
      NEW_RELIC_HOST: ${NEW_RELIC_HOST}
      NEW_RELIC_DAEMON_ADDRESS: "daemon:31339"
  daemon:
    build:
      context: daemon
    expose:
      - "31339"

c-sdk-container-example's People

Contributors

fahmy-mohammed avatar melissaklein24 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.