# @dineroregnskab/datadog-angular

**Datadog integration for Angular projects.**

Has built-in unwrapping of `HttpErrorResponse` and `UncaughtPromiseError`.

### Enrichers

Included enrichers for `Logs` and `RUM`:

- `@ngrx/store`

Enrichers for `Logs` can be imported from: `@dineroregnskab/datadog-angular/enrichers/logs`  
Enrichers for `RUM` can be imported from: `@dineroregnskab/datadog-angular/enrichers/rum`

### Filters

Included filters for `Logs` and `RUM`:

- Discard HTTP errors with status code `0`
- Discard HTTP errors with `0 Unknown Error` message

Filters for `Logs` can be imported from: `@dineroregnskab/datadog-angular/filters/logs`  
Filters for `RUM` can be imported from: `@dineroregnskab/datadog-angular/filters/rum`

## Templates:

### Enrichers:

#### Logs:

```typescript
export const enricher = (log: LogsEvent, injector: Injector | null): void => {
    // Append data to log object.
};
```

#### RUM:

```typescript
export const enricher = (
    event: RumEvent,
    context: RumEventDomainContext,
    injector: Injector | null,
): void => {
    // Append data to the event context here.
};
```

- For `Logs`, `event` is the captured event object. New log information can be appended to this object. See https://docs.datadoghq.com/logs/log_collection/javascript/#advanced-usage for more information.
- For `RUM`, see https://docs.datadoghq.com/real_user_monitoring/guide/enrich-and-control-rum-data/?tab=event for information about the `event` and `context` properties.
- The `injector` parameter is the Angular DI injector. This can be used to extract information from Angular components. **The injector will be `null` if the log happens before `setNgInjector` is called.**

### Filters:

#### Logs:

```typescript
export const filter = (
    event: RumEvent,
    context: RumEventDomainContext,
    injector: Injector | null,
): boolean => {
    // Return true to discard the log.
};
```

#### RUM:

```typescript
export const filter = (log: LogsEvent, injector: Injector | null): boolean => {
    // Return true to discard log.
};
```

- For `Logs`, `event` is the captured event object. New log information can be appended to this object. See https://docs.datadoghq.com/logs/log_collection/javascript/#advanced-usage for more information.
- For `RUM`, see https://docs.datadoghq.com/real_user_monitoring/guide/enrich-and-control-rum-data/?tab=event for information about the `event` and `context` properties.
- The `injector` parameter is the Angular DI injector. This can be used to extract information from Angular components. **The injector will be `null` if the log happens before `setNgInjector` is called.**

## Initialization

Initializer will instrument both Logs and RUM.

`main.ts`

```typescript
import { setNgInjector } from '@dineroregnskab/datadog-angular';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app';

platformBrowserDynamic()
    .bootstrapModule(AppModule)
    .then((appRef) => {
        // Save a reference to the Angular DI injector for use in logging.
        setNgInjector(appRef.injector);
    })
    .catch((err) => console.error(err));
```

## Error handler

```typescript
import { ErrorHandler, Injectable } from '@angular/core';
import { DatadogErrorHandler } from '@dineroregnskab/datadog-angular';

@Injectable()
export class CustomErrorHandler
    extends DatadogErrorHandler
    implements ErrorHandler
{
    public constructor() {
        super();
    }

    public handleError(error: any): void {
        this.datadogLogger.error(error);
    }
}
```
