Observability - ApostropheCMS, OpenTelemetry, and New Relic
Tags: javascript, tutorial, webdev, tooling
Published: 2022-11-16 16:47:37

In this article, we will look at how to connect your site to New Relic using the OpenTelemetry observability framework.


##What is OpenTelemetry?
OpenTelemetry, an open-source project, offers a set of vendor-neutral APIs that allow us to collect and transfer data to a location. This enables you to understand what your site is doing without having to worry about the tools or programming language being used. For more information, check out [Open Telemetry’s website] (https://opentelemetry.io/).

##What is New Relic?
[New Relic] (https://newrelic.com/) is a monitoring tool that can import data emitted by OpenTelemetry to help analyze and resolve application or website issues. It provides real-time data and shows the analytical performance of your site on the New Relic dashboard. It is designed to help discover unexpected outcomes and help you improve performance based on key metrics.

##What is Observability?
Observability has become more important for sites in recent years. Most people tend to confuse Observability and Monitoring, but the main difference is that Monitoring tells you when something goes wrong, and Observability tells you why it went wrong. Monitoring has to happen before Observability takes place. Observability is one important concept that needs to be considered in complex systems, because if it doesn’t exist, then you can never know why things go wrong. This, in turn, will affect business performance and customer experience.

[Profiling ApostropheCMS with OpenTelemetry] (https://v3.docs.apostrophecms.org/cookbook/opentelemetry.html#_1-install-the-required-dependencies) already provides instructions for using another tool, [Jaeger] (https://www.jaegertracing.io/). The reason we are using New Relic in this article is that it provides more features out-of-the-box besides distributed tracing or monitoring. This article will be a quick guide on configuring this tool, which will extract telemetry data and export them to New Relic for monitoring.

Before moving on with this guide, we recommend you update Apostrophe to version 3.18.0, or preferably the latest [3.x] (https://apostrophecms.com/blog/apostrophe-3-release) version available.

##Setup OpenTelemetry and NewRelic
Before we begin, there are a few things to note; we will be using an SDK for OpenTelemetry, which basically is used for measurement, and those measurements(data) will be exported to New Relic using an [OTLP/PROTO](https://www.npmjs.com/package/@opentelemetry/exporter-trace-otlp-proto) Trace Exporter. The reason for using OTLP/PROTO is that New Relic expects its data in Proto format unlike the more familiar JSON format.

####Step 1: Install the required dependencies for OpenTelemetry
Install the required dependencies in your project:

`$ npm install @opentelemetry/sdk-node@0.27.0 @opentelemetry/auto-instrumentations-node@0.27.4 @opentelemetry/semantic-conventions@1.0.1 @opentelemetry/resources @opentelemetry/exporter-trace-otlp-proto uuid`

####Step 2: Login to New Relic
Before working with New Relic, you will need to have an account. So, go ahead and create one if you don't have one.

While on the dashboard, head over to the [API Keys page](https://login.newrelic.com/login?return_to=https%3A%2F%2Fone.newrelic.com%2Flauncher%2Fapi-keys-ui.launcher) and copy the INGEST-License key. This is what will provide permission to get data into New Relic.


![Observability OpenTelemetry](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a1rdgurui4yspzphfptl.png)



With that complete, there are two ways we can export our data to New Relic; exporting directly to New Relic from our app or exporting to New Relic from a collector. But in this guide, we will be exporting directly from our Apostrophe app.

####Step 3: Configure OpenTelemetry
At this point, we are about to do the real work where we have to configure OpenTelemetry and export telemetry data to New Relic. Exporting this kind of data relies on a specific protocol; the [OpenTelemetry Protocol](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/otlp.md) or OTLP.

In your project root directory, create a file telemetry.js:

```
const { NodeSDK } = require('@opentelemetry/sdk-node');
    const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
    const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
    const { Resource } = require('@opentelemetry/resources');
    const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-proto');
    const { v4: uuidv4 } = require('uuid');
    const pkg = require('./package-lock.json');

    // Resources attributes
    const resource = new Resource({
      [SemanticResourceAttributes.SERVICE_NAME]: pkg.name,
      [SemanticResourceAttributes.SERVICE_VERSION]: pkg.version,
      [SemanticResourceAttributes.INSTANCE]: uuidv4(),
    });

    const instrumentations = [getNodeAutoInstrumentations()];

    const traceExporter = new OTLPTraceExporter({
      url: '<https://otlp.nr-data.net:4317/v1/traces>',
      headers: {
        'api-key': '<your-api-key>', // Your license key from NewRelic account
      },
    });

    const sdk = new NodeSDK({
      resource,
      instrumentations,
      traceExporter,
    });

    const shutdown = () => {
      sdk
        .shutdown()
        .then(() => {
          console.log('Shutdown complete');
        })
        .catch((err) => {
          console.error('Shutdown failed', err);
        });
    };

    module.exports = {
      sdk,
      shutdown,
    };
```

This code imports the necessary packages we had installed earlier and provides the resources attributes, which is more information about the service. It also enables [auto-instrumentation](https://opentelemetry.io/docs/concepts/instrumenting/#:~:text=The) (OpenTelemetry project facilitates the instrumentation or non-core components) before configuring the OTLP/PROTO trace exporter.

Finally, this code exports both the configured SDK function we will use and a shutdown function.

With that set, we have to refractor our app.js to make use of the opentelemetry setup:

```
const apostrophe = require('apostrophe');
    const process = require('process');
    const { sdk, shutdown } = require('./telemetry');

    const config = {
      shortName: 'apos-relic',
      modules: {},
    };

    if (process.env.APOS_OPENTELEMETRY) {
      sdk
        .start()
        .then(() => console.log('Started OpenTelemetry'))
        .then(() => apostrophe(config))
        .catch((err) => console.error('Failed to start OpenTelemetry', err));
    } else {
      apostrophe(config);
    }

    process.on('SIGTERM', () => {
      shutdown()
        .then(() => console.log('Tracing terminated'))
        .catch((error) => console.log('Error terminating tracing', error))
        .finally(() => process.exit(0));
    });
```

The important things to note in the above code is that we start Apostrophe after OpenTelemetry has started, and we shut down the sdk on process exit.

####Step 4: Start your application
After everything is set and configured, we can then start our application.

`$ APOS_OPENTELEMETRY=1 node app`

Once our app is started, open http://localhost:3000 and head over to the New Relic UI to view your data. If you are logged into your account and navigate to the monitoring [home page](https://one.newrelic.com/), you should be on the “Explorer” page. This page allows you to select any services that you have running. You site will be under “Services- OpenTelemetry”. Selecting it will bring up a dashboard showing various data about your site, like response time and error rate.


![composite relic dash](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sr1u5u40bwimbm78fgjx.png)



Three important observations you will need to look at on your New Relic UI are the [traces](https://one.newrelic.com/launcher/nr1-core.explorer?overlay=eyJuZXJkbGV0SWQiOiJkYXRhLWV4cGxvcmF0aW9uLnF1ZXJ5LWJ1aWxkZXIiLCJpbml0aWFsQWN0aXZlSW50ZXJmYWNlIjoibnJxbEVkaXRvciIsImluaXRpYWxOcnFsVmFsdWUiOiIiLCJpbml0aWFsUXVlcmllcyI6W3sibnJxbCI6IkZST00gU3BhbiBTRUxFQ1QgY291bnQoKikgd2hlcmUgbmV3cmVsaWMuc291cmNlPSclb3RscCUnIFRJTUVTRVJJRVMifV0sImluaXRpYWxDaGFydFNldHRpbmdzIjp7ImNoYXJ0VHlwZSI6IkNIQVJUX0xJTkUiLCJsaW1pdCI6NzU0MiwibGlua2VkRW50aXR5R3VpZCI6bnVsbCwibGlua2VkRGFzaGJvYXJkSWQiOm51bGwsInlTY2FsZSI6eyJzdGF0aWMiOmZhbHNlLCJkb21haW4iOltudWxsLG51bGxdfSwieVplcm8iOnRydWV9fQo=), [logs](https://one.newrelic.com/launcher/nr1-core.explorer?overlay=eyJuZXJkbGV0SWQiOiJkYXRhLWV4cGxvcmF0aW9uLnF1ZXJ5LWJ1aWxkZXIiLCJpbml0aWFsQWN0aXZlSW50ZXJmYWNlIjoibnJxbEVkaXRvciIsImluaXRpYWxOcnFsVmFsdWUiOiIiLCJpbml0aWFsUXVlcmllcyI6W3sibnJxbCI6IkZST00gTG9nIFNFTEVDVCBjb3VudCgqKSB3aGVyZSBuZXdyZWxpYy5zb3VyY2U9JyVvdGxwJScgVElNRVNFUklFUyJ9XSwiaW5pdGlhbENoYXJ0U2V0dGluZ3MiOnsiY2hhcnRUeXBlIjoiQ0hBUlRfTElORSIsImxpbWl0Ijo3NTQyLCJsaW5rZWRFbnRpdHlHdWlkIjpudWxsLCJsaW5rZWREYXNoYm9hcmRJZCI6bnVsbCwieVNjYWxlIjp7InN0YXRpYyI6ZmFsc2UsImRvbWFpbiI6W251bGwsbnVsbF19LCJ5WmVybyI6dHJ1ZX19Cg==) and [metrics](https://one.newrelic.com/launcher/nr1-core.explorer?overlay=eyJuZXJkbGV0SWQiOiJkYXRhLWV4cGxvcmF0aW9uLnF1ZXJ5LWJ1aWxkZXIiLCJpbml0aWFsQWN0aXZlSW50ZXJmYWNlIjoibnJxbEVkaXRvciIsImluaXRpYWxOcnFsVmFsdWUiOiIiLCJpbml0aWFsUXVlcmllcyI6W3sibnJxbCI6IkZST00gTWV0cmljIFNFTEVDVCBjb3VudCgqKSB3aGVyZSBuZXdyZWxpYy5zb3VyY2UgTElLRSAnJW90bHAlJyBUSU1FU0VSSUVTIn1dLCJpbml0aWFsQ2hhcnRTZXR0aW5ncyI6eyJjaGFydFR5cGUiOiJDSEFSVF9MSU5FIiwibGltaXQiOjc1NDIsImxpbmtlZEVudGl0eUd1aWQiOm51bGwsImxpbmtlZERhc2hib2FyZElkIjpudWxsLCJ5U2NhbGUiOnsic3RhdGljIjpmYWxzZSwiZG9tYWluIjpbbnVsbCxudWxsXX0sInlaZXJvIjp0cnVlfX0K). Traces, otherwise known as distributed tracing, are basically application requests which are tagged with a unique identifier. Logs, on the other hand, are vital information and trigger alerts as they happen in your application. These may need some setup before any data is logged. Metrics are the core information or better still statistics; viewership statistics, engagement statistics, etc.

You may decide to trace command line tasks as well.

`$ APOS_OPENTELEMETRY=1 node app @moduleName:taskName`



![request image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wzfowes0hqiqubolu8mx.png)


Observing your application is critical to noticing which part is slow or needs attention. This guide should give you the power to ensure that your site is in perfect health. Good luck!

*This post was originally contributed by an Apostrophe Community member, [Victor Jonah](https://twitter.com/vectormike_). If you would like to contribute an article to the community, [submit your article here](https://apostrophecms.com/contact-us).*

####We'd love to hear your feedback on this!
* Join our team and other Apostrophe developers from around the world on [Discord](http://chat.apostrophecms.org/)
* Follow product updates and more on [Github Discussions](https://github.com/apostrophecms/apostrophe/discussions)