---
title: Instrument your app with the C SDK
source: https://docs.newrelic.com/docs/apm/agents/c-sdk/instrumentation/instrument-your-app-c-sdk
---

> #### ⚠️ EOL NOTICE
>
> From April 2022, we don't support the C SDK capability. For more details, see our [Support Forum post](https://support.newrelic.com/s/hubtopic/aAX8W0000008d0r/q1-bulk-eol-announcement-fy23).

In order to monitor any application on Linux using a language that can import C libraries, you must:

1.  Create a config using `newrelic_new_app_config()`, connect to the daemon using [`newrelic_init()`](https://newrelic.github.io/c-sdk/libnewrelic_8h.html#a03f07e64a51b5f0cd51caa4f28c8b6c1), and connect your application using [`newrelic_create_app()`](https://newrelic.github.io/c-sdk/libnewrelic_8h.html#a61dd90439ae3cc5060021f6ab4701132). For more information, see the [C SDK installation procedures](https://docs.newrelic.com/docs/agents/c-sdk/install-configure/install-c-sdk-compile-link-your-code).
2.  Manually instrument transactions using the C SDK, as described in this document.

New Relic defines a [web or non-web transaction](https://docs.newrelic.com/docs/apm/transactions/intro-transactions/transactions-new-relic-apm) as one logical unit of work in a software application. After you manually instrument transactions in your source code by adding New Relic functions, you can view the data on the [**Transactions** page](https://docs.newrelic.com/docs/apm/applications-menu/monitoring/transactions-page) in New Relic. You can also instrument segments of a transaction and errors.

## Instrument a transaction [#instrument-c-txn]

To instrument a transaction so you can monitor it, wrap the New Relic functions that start and stop instrumentation around the transaction. The function that you use depends on whether you want to instrument a [web or non-web transaction](https://docs.newrelic.com/docs/apm/transactions/intro-transactions/transactions-new-relic-apm#types).

In the following example, the app is created after a call to `newrelic_create_app()`. For more information, see the [C SDK installation procedures](https://docs.newrelic.com/docs/agents/c-sdk/install-configure/install-c-sdk-compile-link-your-code) as well as the [C SDK `libnewrelic.h` documentation on GitHub](https://newrelic.github.io/c-sdk/libnewrelic_8h.html).

1.  Add the following code immediately **before** the transaction that you want to monitor, supplying the required parameters.

    For web transactions:

    ```
    // Example code:
    <a href="https://newrelic.github.io/c-sdk/libnewrelic_8h.html#add4646ef16f4f0c9b75934cf96909655">newrelic_txn_t</a> *txn;
    /* ... */
    txn = <a href="https://newrelic.github.io/c-sdk/libnewrelic_8h.html#adbf7c1fa57482f6e0a7f291e0b5ec80f">newrelic_start_web_transaction</a>(app, "NAME_YOUR_TRANSACTION");
    ```

    For non-web transactions:

    ```
    // Example code:
    newrelic_txn_t *txn;
    /* ... */
    txn = <a href="https://newrelic.github.io/c-sdk/libnewrelic_8h.html#a88648cc287f8d7e371139dc3809b7afb">newrelic_start_non_web_transaction</a>(app, "NAME_YOUR_TRANSACTION");
    ```
2.  Add `newrelic_end_transaction()` immediately after the web or non-web transaction that you want to monitor, supplying a pointer the transaction, `&txn`, as a parameter.

## Instrument segments [#segments]

Once you instrument a transaction using the C SDK, you can instrument [segments](https://docs.newrelic.com/docs/apm/transactions/intro-transactions/transactions-new-relic-apm#segments) in it. By instrumenting segments, you can monitor the individual functions and calls inside a transaction.

**Segments example**

You have a transaction associated with a checkout process, which processes both shipping information and credit card information. You can instrument your application to break that transaction up into two segments: one segment for shipping and one segment for payment.

You can instrument segments to monitor the following kinds of calls:

-   [External services](#external-segments) using external segments
-   [Custom segments](#custom-segments) for arbitrary code
-   [Datastores](#datastore-segments) using datastore segments
-   [Slow query traces](#slow-query-datastore) (SQL databases only)

For more information, see the [C SDK installation procedures](https://docs.newrelic.com/docs/agents/c-sdk/install-configure/install-c-sdk-compile-link-your-code) as well as the [C SDK `libnewrelic.h` documentation on GitHub](https://newrelic.github.io/c-sdk/libnewrelic_8h.html).

**Instrument calls to external services**

To monitor calls to external services, instrument external segments that are within an instrumented transaction. External segments appear in the [**Transactions** page's **Breakdown** table](https://docs.newrelic.com/docs/apm/applications-menu/monitoring/transactions-page-find-specific-performance-problems) and the [**External services** page](https://docs.newrelic.com/docs/apm/applications-menu/monitoring/external-services-page).

To instrument an external segment, wrap the New Relic functions that start and stop instrumentation around the function you want to monitor:

1.  [Instrument a transaction](#instrument-c-txn).
2.  Create a [`newrelic_external_segment_params_t`](https://newrelic.github.io/c-sdk/structnewrelic__external__segment__params__t.html) that describes the external segment, supplying the required parameters.
3.  Add `newrelic_start_external_segment()` immediately before the function you want to monitor, supplying the required parameters.
4.  Add `newrelic_end_segment()` immediately after the function you want to monitor, supplying the required parameters.

    For more information, see the [C SDK installation procedures](https://docs.newrelic.com/docs/agents/c-sdk/install-configure/install-c-sdk-compile-link-your-code) as well as the [C SDK `libnewrelic.h` documentation on GitHub](https://newrelic.github.io/c-sdk/libnewrelic_8h.html).

**Instrument calls to arbitrary code (custom segments)**

To monitor calls to arbitrary code, instrument custom segments that are within an instrumented transaction. Custom segments appear in the **Breakdown** table on the [**Transactions** page](https://docs.newrelic.com/docs/apm/applications-menu/monitoring/transactions-page-find-specific-performance-problems).

To instrument a custom segment, wrap the New Relic functions that start and stop instrumentation around the function you want to monitor:

1.  [Instrument a transaction](#instrument-c-txn).
2.  Add `newrelic_start_segment()` immediately before the function you want to monitor, supplying the required parameters.
3.  Add `newrelic_end_segment()` immediately after the function you want to monitor, supplying the required parameters.

    For more information, see the [C SDK installation procedures](https://docs.newrelic.com/docs/agents/c-sdk/install-configure/install-c-sdk-compile-link-your-code) as well as the [C SDK `libnewrelic.h` documentation on GitHub](https://newrelic.github.io/c-sdk/libnewrelic_8h.html).

**Instrument calls to datastores**

To monitor calls to datastores, instrument the datastore segments within an instrumented transaction. Datastore segments appear in the **Breakdown** table and **Databases** tab on the [**Transactions** page](https://docs.newrelic.com/docs/apm/applications-menu/monitoring/transactions-page) in New Relic. You can also view datastore segments as a [`databaseDuration` attribute](/attribute-dictionary/?event=Transaction&attribute=databaseDuration) of [APM `Transaction` events](https://docs.newrelic.com/docs/insights/insights-data-sources/default-events-attributes/apm-default-event-attributes).

To instrument a datastore segment, wrap the New Relic functions that start and stop instrumentation around the function you want to monitor:

1.  [Instrument a transaction](#instrument-c-txn).
2.  Create a [`newrelic_datastore_segment_params_t`](https://newrelic.github.io/c-sdk/structnewrelic__datastore__segment__params__t.html) that describes the datastore segment.
3.  Add `newrelic_start_datastore_segment()` immediately before the function you want to monitor, supplying the required parameters.
4.  Add `newrelic_end_segment()` immediately after the function you want to monitor, supplying the required parameters.

    For more information, see the [C SDK installation procedures](https://docs.newrelic.com/docs/agents/c-sdk/install-configure/install-c-sdk-compile-link-your-code) as well as the [C SDK `libnewrelic.h` documentation on GitHub](https://newrelic.github.io/c-sdk/libnewrelic_8h.html).

    > #### 💡 TIP
    >
    > To configure how the database name and database instance are reported, use the [`newrelic_datastore_segment_config_t`](https://newrelic.github.io/c-sdk/structnewrelic__datastore__segment__config__t.html).

**Report slow query traces for datastore segments (SQL only)**

> #### ⚠️ IMPORTANT
>
> You can report slow query traces for SQL databases only.

To report slow query trace data for datastore segments that take longer than the time you specify, enable these settings in your [newrelic_app_config_t](https://newrelic.github.io/c-sdk/structnewrelic__app__config__t.html):

1.  Enable slow query tracing by setting [`transaction_tracer.datastore_reporting.enabled`](https://newrelic.github.io/c-sdk/structnewrelic__transaction__tracer__config__t.html) to `true`.
2.  To set the threshold, add a length of time in microseconds to [`transaction_tracer.datastore_reporting.threshold_us`](https://newrelic.github.io/c-sdk/structnewrelic__transaction__tracer__config__t.html).

    Then, if a datastore call takes longer than the threshold, the C SDK reports it as a slow query. To view slow query trace details, use the [**Databases**](https://docs.newrelic.com/docs/apm/applications-menu/monitoring/databases-page) and [**Slow queries**](https://docs.newrelic.com/docs/apm/applications-menu/monitoring/view-slow-query-details) pages in New Relic.

    For more information, see the [C SDK installation procedures](https://docs.newrelic.com/docs/agents/c-sdk/install-configure/install-c-sdk-compile-link-your-code) as well as the [C SDK `libnewrelic.h` documentation on GitHub](https://newrelic.github.io/c-sdk/libnewrelic_8h.html).

## Instrument errors [#errors]

In order to use the C SDK to monitor errors in transactions, you must manually instrument your source code by adding the [`newrelic_notice_error()`](https://newrelic.github.io/c-sdk/libnewrelic_8h.html#a02a9a959015a0ca68ce11c750f690475) function to it.

Transaction errors and error traces appear on the [**Error analytics** page](https://docs.newrelic.com/docs/apm/applications-menu/error-analytics/error-analytics-manage-error-traces) in New Relic. The C SDK reports the total number of errors and up to 100 error traces per minute. You can also view, query, and visualize transaction errors as [APM `TransactionError` events](https://docs.newrelic.com/docs/insights/insights-data-sources/default-events-attributes/apm-default-event-attributes).

> #### 💡 TIP
>
> To include function calls in error traces, use GNU's `-rdynamic` linker flag to [link your apps when compiling](https://docs.newrelic.com/docs/agents/c-sdk/install-configure/install-c-sdk-compile-link-your-code#compile). The `-rdynamic` linker flag gives you more meaningful error traces.

To instrument errors in transactions:

1.  [Start a transaction](#start-transaction).
2.  Record an error with [`newrelic_notice_error()`](https://newrelic.github.io/c-sdk/libnewrelic_8h.html#a02a9a959015a0ca68ce11c750f690475), supplying the required parameters.
3.  [End the transaction](#end-transaction), supplying the required parameters.

For more information, see the [C SDK installation procedures](https://docs.newrelic.com/docs/agents/c-sdk/install-configure/install-c-sdk-compile-link-your-code) as well as the [C SDK `libnewrelic.h` documentation on GitHub](https://newrelic.github.io/c-sdk/libnewrelic_8h.html).

## Avoid metric grouping issues [#mgi]

When an account or application sends many individual metrics that could be better managed in groups, New Relic uses the term **metric grouping issue** or **MGI** to describe this situation. If your application sends unnecessarily large amounts of data to New Relic, this reduces the effectiveness of charts, tables, and reports.

Metric grouping issues occur most commonly with web transactions, especially if the name is based on URLs. To help prevent this situation, see [Metric grouping issues](https://docs.newrelic.com/docs/agents/manage-apm-agents/troubleshooting/metric-grouping-issues).
