npm package: @lahaus-nuxt/datadog-trace
Description: Install and configure Datadog trace to receive traces of your instrumented application. By default
Author: Alver Alexander Grisales Ortega
Latest version: 0.7.0

# Welcome to @lahaus-nuxt/datadog-trace 👋

[![Version](https://img.shields.io/npm/v/@lahaus-nuxt/datadog-trace.svg)](https://www.npmjs.com/package/@lahaus-nuxt/datadog-trace)
[![Documentation](https://img.shields.io/badge/documentation-yes-brightgreen.svg)](https://github.com/la-haus/nuxt-datadog-trace#readme)
[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/la-haus/nuxt-datadog-trace/graphs/commit-activity)

Nuxt module to add Datadog APM tracing to your application by capturing the requests passing through the http module in node.js.

### 🏠 [Homepage](https://github.com/la-haus/nuxt-datadog-trace#readme)

## Installation

1. Install npm package

```sh
yarn add @lahaus-nuxt/datadog-trace # or npm i @lahaus-nuxt/datadog-trace
```

2. Install dd-trace

```sh
yarn add dd-trace # or  npm i dd-trace
```

3. Edit your `nuxt.config.js` file to add module

```js
{
  modules: ['@lahaus-nuxt/datadog-trace'];
}
```

4. Change the options using the `datadogTrace` key or by adding a second parameter in the module configuration. Refer to the Usage section for details.

# Usage

1. By default, `@lahaus-nuxt/datadog-trace` exposes some basic options for common needs. Internally, these options are used to create a basic [dd-trace instance](https://datadoghq.dev/dd-trace-js/) and wire up middleware.

   The default values are:

   ```js
   // ...
   {
     // activate the module
     enabled: true,
     // Set an application’s environment e.g. prod, pre-prod, stage.
     env: process.env.NODE_ENV,
     // The service name to be used for this program
     service: `host app's [package.json].name property`
     // Whether to enable trace ID injection in log records to be able to correlate traces with logs.
     logInjection: true,
     // The version number of the application
     version: hostAppPkg.version,
     ...yourOverrides,
     // This plugin automatically instruments the http module.
     httpOptions: {
       // activate the module
       enabled: true,
       // List of URLs that should not be instrumented
       blocklist: ['/api/healthz', '/_nuxt/?'],
       // An array of headers to include in the span metadata
       headers: [
         'x-request-id',
         'x-application-id',
         'x-session-id',
         'Host',
         'User-Agent',
         'Referer',
         'Accept',
         'Content-Type',
         'Origin',
         'x-client-id',
         'x-context',
       ]
     }
   }
   // ...
   ```

With options module

```js
{
  modules: ['@lahaus-nuxt/datadog-trace', { enabled: true, httpOptions: {} }];
}
```

With `datadogTrace` key

```js
{
  modules: ['@lahaus-nuxt/datadog-trace'],
  datadogTrace: {
    enabled: true, httpOptions: {}
  }
}
```

2. To access the dd-trace instance from within Nuxt lifecycle areas, use the `$ddTrace` key from the Nuxt context object. Note: This is only available for server-side executions. For example, because asyncData is an isomorphic function in Nuxt, you will need to guard `$ddTrace` access with something like if (process.server) { ... }

Example `nuxtServerInit` in your apps `~/store/index.js:`

```js
// ...
export const actions = {
  async nuxtServerInit({ store, commit }, { req, $ddTrace }) {
    $ddTrace.use('<plugin-name>');
  },
};
// ...
```

Example `asyncData` in your apps `~/pages/somepage.vue`:

```js
    // ...
    asyncData(context) {
      if (process.server) {
        context.$ddTrace.use('<plugin-name>')
      }
    }
    // ...
```

For more information on dd-trace options, see the package [documentation](https://datadoghq.dev/dd-trace-js/interfaces/traceroptions.html).

# Using `process.ddTrace` in a Nuxt Module

Because modules are executed sequentially, any additional [Nuxt modules](https://nuxtjs.org/docs/2.x/directory-structure/modules#write-your-own-module) should be loaded _after the `@lahaus-nuxt/datadog-trace` module_. You can then access the dd-trace instance via `process.ddTrace` as needed.

## Author

👤 **Alver Alexander Grisales Ortega <alvergrisales@lahaus.com>**

---

_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_