> ## Documentation Index
> Fetch the complete documentation index at: https://launchdarkly-preview.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Recording metrics

> This topic explains how to record metrics through the SDK observability plugin.

> For the complete documentation index, see [llms.txt](/llms.txt). For the full documentation snapshot, see [llms-full.txt](/llms-full.txt).

## Overview

The observability plugin provides different functions depending on what kind of data you want to record.

The recorded data is available as an `$ld:telemetry:metric` event. To learn more, read [Metrics autogenerated from observability events](/home/metrics/autogen/observability).

You can view all metrics sent to LaunchDarkly under **Metrics** in the LaunchDarkly user interface. To learn more, read [Metrics](/home/metrics).

Details about each SDK's configuration are available in the SDK-specific sections below:

* [Client-side SDKs](#client-side-sdks)
* [Server-side SDKs](#server-side-sdks)

## Client-side SDKs

This feature is available in the observability plugin for the following client-side SDKs:

* [iOS](#ios)
* [Android](#android)
* [JavaScript](#javascript)
* [React Native](#react-native)
* [React Web](#react-web)
* [Vue](#vue)

### iOS

<Accordion title="Expand iOS code sample">
  When you record a metric with the observability plugin, it must include a `name` and `value`. Optionally, it can also include `attributes` and a `timestamp`. To construct the attributes, use `Attributes` from the [`@opentelemetry/api`](https://opentelemetry.io/docs/specs/semconv/general/attributes/).

  Here are the options for recording metrics:

  <CodeGroup>
    ```swift title="Record point-in-time" theme={null}
            LDObserve.shared.recordMetric(metric: .init(name: "elapsedTimeMs", value: 2200))
    ```

    ```swift title="Record cumulative count" theme={null}
            LDObserve.shared.recordCount(metric: .init(name: "example-event-name", value: 42))
    ```

    ```swift title="Record cumulative increment" theme={null}
            LDObserve.shared.recordIncr(metric: .init(name: "example-counter", value: 15))
    ```

    ```swift title="Record point-in-time, aggregate to histogram" theme={null}
            LDObserve.shared.recordHistogram(metric: .init(name: "work.difficulty", value: 7.8))
    ```

    ```swift title="Record counter" theme={null}
            LDObserve.shared.recordUpDownCounter(metric: .init(name: "users", value: 2))
    ```
  </CodeGroup>

  To learn more, read [`Observe`](https://github.com/launchdarkly/swift-launchdarkly-observability/blob/main/Sources/LaunchDarklyObservability/API/Observe.swift).
</Accordion>

### Android

<Accordion title="Expand Android code sample">
  When you record a metric with the observability plugin, it must include a `name` and `value`. Optionally, it can also include `attributes` and a `timestamp`. To construct the attributes, use `Attributes` from the [`@opentelemetry/api`](https://opentelemetry.io/docs/specs/semconv/general/attributes/).

  Here are the options for recording metrics:

  <CodeGroup>
    ```java title="Record point-in-time" theme={null}
            LDObserve.recordMetric(Metric("elapsedTimeMs", 2200))
    ```

    ```java title="Record cumulative count" theme={null}
            LDObserve.recordCount(Metric("example-event-name", 42))
    ```

    ```java title="Record cumulative increment" theme={null}
            LDObserve.recordIncr(Metric("example-counter", 15))
    ```

    ```java title="Record point-in-time, aggregate to histogram" theme={null}
            LDObserve.recordHistogram(Metric("work.difficulty", 7.8))
    ```

    ```java title="Record counter" theme={null}
            LDObserve.recordUpDownCounter(Metric("users", 2))
    ```
  </CodeGroup>

  To learn more, read [`LDObserve`](https://launchdarkly.github.io/observability-sdk/sdk/@launchdarkly/observability-android/com/launchdarkly/observability/sdk/LDObserve.html).
</Accordion>

### Java

Script

<Accordion title="Expand JavaScript code sample">
  Here are the options for recording metrics:

  <CodeGroup>
    ```js title="Record point-in-time" theme={null}
          const onClick = () => {
            const start = Date.now();
            doInterestingWork();
            const elapsed = Date.now() - start;
            LDObserve.recordGauge({name: "elapsedTimeMs", value: elapsed});
          };
    ```

    ```js title="Record cumulative count" theme={null}
          const onClick = () => {
            LDObserve.recordCount({name: "example-event-name", value: 42});
          };
    ```

    ```js title="Record cumulative increment" theme={null}
          const onClick = () => {
            doInterestingWork();
            LDObserve.recordIncr({name: "example-counter"});
          };
    ```

    ```js title="Record point-in-time, aggregate to histogram" theme={null}
          const onClick = () => {
            doInterestingWork();
            // measure some value about the code path
            const difficulty = 1.23;
            LDObserve.recordHistogram({name: "work.difficulty", value: difficulty});
          };
    ```

    ```js title="Record counter" theme={null}
          // a counter metric may be incremented or decremented
          const onClick = () => {
            const isActive = checkUserActivity();
            const value = isActive ? 1 : -1;
            LDObserve.recordUpDownCounter({name: "users", value: value});
          };
    ```
  </CodeGroup>

  To learn more, read [`Observe`](https://launchdarkly.github.io/observability-sdk/packages/@launchdarkly/observability/interfaces/api_observe.Observe.html).
</Accordion>

### React Native

<Accordion title="Expand React Native code sample">
  When you record a metric with the observability plugin, it must include a `name` and `value`. Optionally, it can also include `attributes` and a `timestamp`. To construct the attributes, use `Attributes` from the [`@opentelemetry/api`](https://opentelemetry.io/docs/specs/semconv/general/attributes/).

  Here are the options for recording metrics:

  <CodeGroup>
    ```js title="Record point-in-time" theme={null}
            LDObserve.recordMetric({name: "elapsedTimeMs", value: 2200});
    ```

    ```js title="Record cumulative count" theme={null}
            LDObserve.recordCount({name: "example-event-name", value: 42});
    ```

    ```js title="Record cumulative increment" theme={null}
            LDObserve.recordIncr({name: "example-counter", value: 15});
    ```

    ```js title="Record point-in-time, aggregate to histogram" theme={null}
            LDObserve.recordHistogram({name: "work.difficulty", value: 7.8});
    ```

    ```js title="Record counter" theme={null}
            LDObserve.recordUpDownCounter({name: "users", value: 2});
    ```
  </CodeGroup>

  To learn more, read [`Observe`](https://launchdarkly.github.io/observability-sdk/sdk/@launchdarkly/observability-react-native/interfaces/Observe.html).
</Accordion>

### React Web

To record metrics with the React Web SDK, follow the example for [JavaScript](#javascript).

### Vue

To record metrics with the Vue SDK, follow the example for [JavaScript](#javascript).

## Server-side SDKs

This feature is available in the observability plugin for the following server-side SDKs:

* [.NET (server-side)](#net-server-side)
* [Go](#go)
* [Node.js (server-side)](#nodejs-server-side)
* [Python](#python)

### .NET (server-side)

<Accordion title="Expand .NET (server-side) code sample">
  Here are the options for recording metrics:

  <CodeGroup>
    ```csharp title="Record point-in-time" theme={null}
            Observe.RecordMetric(name, value, attributes);
    ```

    ```csharp title="Record cumulative count" theme={null}
          Observe.RecordCount(name, value, attributes);
    ```

    ```csharp title="Record cumulative increment" theme={null}
            Observe.RecordIncr(name, attributes);
    ```

    ```csharp title="Record point-in-time, aggregate to histogram" theme={null}
            Observe.RecordHistogram(name, value, attributes);
    ```

    ```csharp title="Record counter" theme={null}
            // a counter metric may be incremented or decremented
            Observe.RecordUpDownCounter(name, delta, attributes);
    ```
  </CodeGroup>

  To learn more, read [`Observe`](https://launchdarkly.github.io/observability-sdk/sdk/@launchdarkly/observability-dotnet/api/LaunchDarkly.Observability.Observe.html).
</Accordion>

### Go

<Accordion title="Expand Go code sample">
  To record a metric, pass the Go `context.Context`, as well as the name, value, and optional attributes of the metric to the appropriate `Record*` function. For example, you might want to record the value for a point-in-time measurement, such as the current CPU utilization percentage, or for a counter, such as the number of cache hits. The optional attributes may include any [attributes from the OpenTelemetry specification](https://pkg.go.dev/go.opentelemetry.io/otel/attribute#KeyValue).

  Metrics with the same name and attributes are aggregated using the OpenTelemetry SDK. To learn more, read the OTel documentation on the [Metrics Data Model](https://opentelemetry.io/docs/specs/otel/metrics/data-model/).

  Here are the options for recording metrics:

  <CodeGroup>
    ```go title="Record point-in-time" theme={null}
            ldobserve.RecordMetric(ctx, "example-metric-name", 3.7)
    ```

    ```go title="Record cumulative count" theme={null}
            ldobserve.RecordCount(ctx, "example-metric-name", 42)
    ```

    ```go title="Record point-in-time, aggregate to histogram" theme={null}
            ldobserve.RecordHistogram(ctx, "example-metric-name", 3.7)
    ```
  </CodeGroup>

  To learn more, read [`RecordMetric`](https://pkg.go.dev/github.com/launchdarkly/observability-sdk/go#RecordMetric), [`RecordCount`](https://pkg.go.dev/github.com/launchdarkly/observability-sdk/go#RecordCount), and [`RecordHistogram`](https://pkg.go.dev/github.com/launchdarkly/observability-sdk/go#RecordHistogram).
</Accordion>

### Node.js (server-side)

<Accordion title="Expand Node.js (server-side) code sample">
  To record a metric, first create the metric within your application. The `Metric` interface includes a `name`, `value`, and optional `tags`. For example, you might create a metric for a point-in-time measurement, such as the current CPU utilization percentage, or for a counter, such as the number of cache hits.

  Values with the same metric name and attributes are aggregated using the OpenTelemetry SDK. To learn more, read the OTel documentation on the [Metrics Data Model](https://opentelemetry.io/docs/specs/otel/metrics/data-model/).

  <CodeGroup>
    ```js title="Create OTel metric" theme={null}
          const metric = {
            name: "exampleMetric",
            value: 42
          }
    ```
  </CodeGroup>

  Here are the options for recording metrics:

  <CodeGroup>
    ```js title="Record point-in-time" theme={null}
            LDObserve.recordMetric(metric);
    ```

    ```js title="Record cumulative count" theme={null}
            LDObserve.recordCount(metric);
    ```

    ```js title="Record cumulative increment" theme={null}
            LDObserve.recordIncr(metric);
    ```

    ```js title="Record point-in-time, aggregate to histogram" theme={null}
            LDObserve.recordHistogram(metric);
    ```

    ```js title="Record counter" theme={null}
            // a counter metric may be incremented or decremented
            LDObserve.recordUpDownCounter(metric);
    ```
  </CodeGroup>

  To learn more, read [`Observe`](https://launchdarkly.github.io/observability-sdk/sdk/@launchdarkly/observability-node/interfaces/Observe.html).
</Accordion>

### Python

<Accordion title="Expand Python code sample">
  To record a metric, pass the name, value, and optional attributes to the appropriate `record_*` function. For example, you might want to record the value for a point-in-time measurement, such as the current CPU utilization percentage, or for a counter, such as the number of cache hits. The optional attributes may include any [attributes from the OpenTelemetry specification](https://opentelemetry.io/docs/specs/semconv/general/attributes/).

  Metrics with the same name and attributes are aggregated using the OpenTelemetry SDK. To learn more, read the OTel documentation on the [Metrics Data Model](https://opentelemetry.io/docs/specs/otel/metrics/data-model/).

  Here are the options for recording metrics:

  <CodeGroup>
    ```python title="Record point-in-time" theme={null}
            observe.record_metric("example-metric-name", 3.7)
    ```

    ```python title="Record cumulative count" theme={null}
            observe.record_count("example-metric-name", 42)
    ```

    ```python title="Record cumulative increment" theme={null}
            observe.record_incr("example-metric-name")
    ```

    ```python title="Record point-in-time, aggregate to histogram" theme={null}
            observe.record_histogram("example-metric-name", 3.7)
    ```

    ```python title="Record counter" theme={null}
            # a counter metric may be incremented or decremented
            observe.record_up_down_counter("example-metric-name", 42);
    ```
  </CodeGroup>

  To learn more, read [`ldobserve`](https://launchdarkly.github.io/observability-sdk/sdk/@launchdarkly/observability-python/ldobserve.html).
</Accordion>
