Install the C SDK: Compile and link your code

To monitor your application with New Relic's C SDK, instrument the features you want to use; for example:

  • Web transactions, transaction events, non-web transactions
  • Segments (for additional levels of timing details)
  • Attributes
  • Errors

Then compile and link your app against the C SDK static library.

Add the C SDK to your code

To install New Relic C SDK into your application's code library, follow this procedure.

1. Verify requirements.
  1. Make sure your application meets New Relic's compatibility and requirements for the C SDK.
  2. Make sure you have a New Relic license key.
2. Include the provided header file.
#include "libnewrelic.h"
3. Configure logging.

Follow the procedures to configure logging for both the C SDK and the daemon. For example:

if (!newrelic_configure_log("./c_sdk.log", NEWRELIC_LOG_INFO)) {
    printf("Error configuring logging.\n");
    return -1;
  }
  
4. Be ready to provide a meaningful app name.

Be prepared to provide a meaningful app name in your initial application configuration; for example:

newrelic_app_config_t* config;
/* ... */
config = newrelic_create_app_config("Your Application Name", "LICENSE_KEY_HERE");

You may give your application up to three different names, separated by ;. Giving your application multiple names allows you aggregate metrics for multiple agents across an entire app or service; for example:

config = newrelic_create_app_config("YOUR_APP_NAME;APP_GROUP_1;ALL_APPS", "LICENSE_KEY_HERE");

With the application configured, you can create a new application to connect to the daemon.

newrelic_app_t* app;

/* ... */

if (!newrelic_init(NULL, 0)) {
  printf("Error connecting to daemon.\n");
  return -1;
}

/* Wait up to 10 seconds for the SDK to connect to the daemon */
app = newrelic_create_app(config, 10000);
newrelic_destroy_app_config(&config);
5. Instrument your code.

To instrument your code, refer to the example programs in the New Relic C SDK Examples documentation on GitHub. For more information about source code and features, see the New Relic C SDK's source documentation for libnewrelic.h on GitHub.

6. Compile and link your app.

The C SDK's libnewrelic.a is a static library that is already linked with the libpcre and libpthread libraries. To avoid symbol collisions in this linking step, be sure to link against each of these libraries.

In addition, to take full advantage of error traces in New Relic APM's Error analytics page, link your application using GNU's -rdynamic linker flag. This will allow more meaningful information to appear in the stack trace for the error recording on a transaction using the C SDK's newrelic_notice_error API call.

For example:

gcc -o test_app test_app.c -L. -lnewrelic -lpcre -lm -pthread -rdynamic
7. Start the daemon and check logs.
  1. Start the C SDK's daemon. For example: ./newrelic-daemon -f -logfile newrelic-daemon.log -loglevel debug.
  2. Check the output in the c_sdk.log and newrelic-daemon.log files.

The C SDK's architecture requires that the daemon must be invoked first before your instrumented application is invoked.

To see all of the available options for the C daemon: At the command line, type:

./newrelic-daemon --help

For more information, see the C SDK GUIDE.md.

View app performance in New Relic

To view your app's performance with New Relic APM:

  1. Generate some traffic for your app, then wait a few minutes for your app to send data to New Relic.
  2. Explore your app's data in the New Relic APM UI.

If no data appears within a few minutes, check your c_sdk.log and newrelic-daemon.log files for errors. If you still have problems, follow the troubleshooting tips.

For more help

Recommendations for learning more: