This software is provided as-is with no support provided.

# Luvio Instrumentation Service

This package re-exports the instrumentation service interface types and implementations.

## Package Structure

The instrumentation service is divided into the following packages:

- [`@luvio/service-instrumentation-types`](../instrumentation-types): Type definitions
- [`@luvio/service-instrumentation-noop`](../instrumentation-noop): A no-op implementation
- [`@luvio/service-instrumentation-o11y`](../instrumentation-o11y): An implementation using the O11y library

This main package (`@luvio/service-instrumentation`) re-exports all the types and implementations to maintain backward compatibility.

## Installation

```bash
npm install @luvio/service-instrumentation
```

## Usage

```js
// Using the noop implementation
import { buildNoopInstrumentationServiceDescriptor } from '@luvio/service-instrumentation/v1';

const noopService = buildNoopInstrumentationServiceDescriptor();

// Using the o11y implementation
import { buildO11yInstrumentationServiceDescriptor } from '@luvio/service-instrumentation/v1';
import { logger } from '@luvio/logger';

const o11yService = buildO11yInstrumentationServiceDescriptor(logger);
```

# Instrumentation Service

We are about standards around here. OpenTelemetry is the standard. We are also about exposing what makes sense from their API for a library. We want to trace and record metrics. We want to log too, but that component is [under development](https://opentelemetry.io/docs/languages/js/) (as of March 2024), so for the sake of stability we will hold off on that.

## No-Op Implementation

We provide a no-op implementation OOTB, so things run smoothly when an actual implementation is not provided.

Use `buildNoopInstrumentationService` when building out your services.

```ts
import { buildNoopInstrumentationService } from '@luvio/service-instrumentation/v1';
```

_Aside: We originally tried using the no-op implementations provided by the OTel API, but it came with a cost of 7kb, which was a little rich for our taste, especially when this service exposes a relatively small subset of the API. I'm sure there is a way to scope it down, but for now we make due with our own_

## Adding to the interface

So you realized we **have** to have [`DiagAPI`](https://open-telemetry.github.io/opentelemetry-js/classes/_opentelemetry_api.DiagAPI.html) as part of the API? (We probably should add it at some point)

The process is pretty simple, albeit tedious (with the handrolling of a No-Op implementation and the unit-testing). What's important is that we are only pulling in the portion of the API that makes sense. For example, with the [TraceAPI](https://open-telemetry.github.io/opentelemetry-js/classes/_opentelemetry_api.TraceAPI.html), `setGlobalTracerProvider` is the responsibility of the runtime application, and we shouldn't have the power to set it ourselves, so we leave that out. With that in mind, there are only a few steps to take.

1. Create a type from the OTel API you'd like to include, use `Pick<>` to select what makes sense
2. Add it to the service interface
3. Create a No-Op implementation, along with unit tests to keep our code coverage happy
4. Update the `buildNoopInstrumentationService` function we export with the service

Checkout [this PR](https://github.com/salesforce-experience-platform-emu/luvio-next/pull/171) that adds the MetricsAPI to the service interface for guidance.
