> ## Documentation Index
> Fetch the complete documentation index at: https://otel.fyi/llms.txt
> Use this file to discover all available pages before exploring further.

# Count

> OpenTelemetry connector for Count

# Count Connector

![Status](https://img.shields.io/badge/status-alpha-red)

**Available in:** `contrib`, `k8s`

**Maintainers:** [@akats7](https://github.com/akats7)

**Source:** [opentelemetry-collector-contrib](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/connector/countconnector)

## Overview

The `count` connector can be used to count spans, span events, metrics, data points, and log records.

## Configuration

If you are not already familiar with connectors, you may find it helpful to first visit the [Connectors README].

### Default Configuration

The `count` connector may be used without any configuration settings. The following table describes the
default behavior of the connector.

| \[Exporter Pipeline Type] | Description                         | Default Metric Names                         |
| ------------------------- | ----------------------------------- | -------------------------------------------- |
| traces                    | Counts all spans and span events.   | `trace.span.count`, `trace.span.event.count` |
| metrics                   | Counts all metrics and data points. | `metric.count`, `metric.datapoint.count`     |
| logs                      | Counts all log records.             | `log.record.count`                           |

For example, in the following configuration the connector will count spans and span events from the `traces/in`
pipeline and emit metrics called `trace.span.count` and `trace.span.event.count` onto the `metrics/out` pipeline.

```yaml theme={null}
receivers:
  foo:
exporters:
  bar:
connectors:
  count:

service:
  pipelines:
    traces/in:
      receivers: [foo]
      exporters: [count]
    metrics/out:
      receivers: [count]
      exporters: [bar]
```

### Custom Counts

Optionally, emit custom counts by defining metrics under one or more of the following sections:

* `spans`
* `spanevents`
* `metrics`
* `datapoints`
* `logs`

Optionally, specify a description for the metric.

Note: If any custom metrics are defined for a data type, the default metric will not be emitted.

#### Conditions

Conditions may be specified for custom metrics. If specified, data that matches any one
of the conditions will be counted. i.e. Conditions are ORed together.

```yaml theme={null}
receivers:
  foo:
exporters:
  bar:
connectors:
  count:
    spanevents:
      my.prod.event.count:
        description: The number of span events from my prod environment.
        conditions:
          - 'spanevent.attributes["env"] == "prod"'
          - 'spanevent.name == "prodevent"'
```

#### Attributes

`spans`, `spanevents`, `datapoints`, and `logs` may be counted according to attributes. In such cases, attribute precedence follows this order:
span(logRecord, DataPoint, profile) attributes > scope attributes > resource attributes.

If attributes are specified for custom metrics, a separate count will be generated for each unique
set of attribute values. Each count will be emitted as a data point on the same metric.

Optionally, include a `default_value` for an attribute, to count data that does not contain the attribute. The `default_value` value can be of type string, integer, or float.

```yaml theme={null}
receivers:
  foo:
exporters:
  bar:
connectors:
  count:
    logs:
      my.log.count:
        description: The number of logs from each environment.
        attributes:
          - key: env
            default_value: unspecified_environment
```

### Example Usage

Count spans and span events, only exporting the count metrics.

```yaml theme={null}
receivers:
  foo:
exporters:
  bar:
connectors:
  count:
service:
  pipelines:
    traces:
      receivers: [foo]
      exporters: [count]
    metrics:
      receivers: [count]
      exporters: [bar]
```

Count spans and span events, exporting both the original traces and the count metrics.

```yaml theme={null}
receivers:
  foo:
exporters:
  bar/traces_backend:
  bar/metrics_backend:
connectors:
  count:
service:
  pipelines:
    traces:
      receivers: [foo]
      exporters: [bar/traces_backend, count]
    metrics:
      receivers: [count]
      exporters: [bar/metrics_backend]
```

Count spans, span events, metrics, data points, and log records, exporting count metrics to a separate backend.

```yaml theme={null}
receivers:
  foo/traces:
  foo/metrics:
  foo/logs:
exporters:
  bar/all_types:
  bar/counts_only:
connectors:
  count:
service:
  pipelines:
    traces:
      receivers: [foo/traces]
      exporters: [bar/all_types, count]
    metrics:
      receivers: [foo/metrics]
      exporters: [bar/all_types, count]
    logs:
      receivers: [foo/logs]
      exporters: [bar/all_types, count]
    metrics/counts:
      receivers: [count]
      exporters: [bar/counts_only]
```

Count logs with a severity of ERROR or higher.

```yaml theme={null}
receivers:
  foo:
exporters:
  bar:
connectors:
  count:
    logs:
      my.error.log.count:
        description: Error+ logs.
        conditions:
          - `severity_number >= SEVERITY_NUMBER_ERROR`
service:
  pipelines:
    logs:
      receivers: [foo]
      exporters: [count]
    metrics:
      receivers: [count]
      exporters: [bar]
```

Count logs with a severity of ERROR or higher. Maintain a separate count for each environment.

```yaml theme={null}
receivers:
  foo:
exporters:
  bar:
connectors:
  count:
    logs:
      my.error.log.count:
        description: Error+ logs.
        conditions:
          - `severity_number >= SEVERITY_NUMBER_ERROR`
        attributes:
          - key: env
service:
  pipelines:
    logs:
      receivers: [foo]
      exporters: [count]
    metrics:
      receivers: [count]
      exporters: [bar]
```

Count all spans and span events (default behavior). Count metrics and data points based on the `env` attribute.

```yaml theme={null}
receivers:
  foo/traces:
  foo/metrics:
  foo/logs:
exporters:
  bar/all_types:
  bar/counts_only:
connectors:
  count:
    metrics:
      my.prod.metric.count:
        conditions:
         - `attributes["env"] == "prod"
      my.test.metric.count:
        conditions:
         - `attributes["env"] == "test"
    datapoints:
      my.prod.datapoint.count:
        conditions:
         - `attributes["env"] == "prod"
      my.test.datapoint.count:
        conditions:
         - `attributes["env"] == "test"
service:
  pipelines:
    traces:
      receivers: [foo/traces]
      exporters: [bar/all_types, count]
    metrics:
      receivers: [foo/metrics]
      exporters: [bar/all_types, count]
    metrics/counts:
      receivers: [count]
      exporters: [bar/counts_only]
```

[Connectors README]: https://github.com/open-telemetry/opentelemetry-collector/blob/main/connector/README.md

## Configuration

### Example Configuration

```yaml theme={null}
count:
  count/custom_description:
    spans:
      trace.span.count:
        description: My description for default span count metric.
    spanevents:
      trace.span.event.count:
        description: My description for default span event count metric.
    metrics:
      metric.count:
        description: My description for default metric count metric.
    datapoints:
      metric.datapoint.count:
        description: My description for default datapoint count metric.
    logs:
      log.record.count:
        description: My description for default log count metric.
    profiles:
      profile.count:
        description: My description for default profile count metric.
  count/custom_metric:
    spans:
      my.span.count:
        description: My span count.
    spanevents:
      my.spanevent.count:
        description: My span event count.
    metrics:
      my.metric.count:
        description: My metric count.
    datapoints:
      my.datapoint.count:
        description: My data point count.
    logs:
      my.logrecord.count:
        description: My log record count.
    profiles:
      my.profiling.count:
        description: My profile count.
  count/condition:
    spans:
      my.span.count:
        description: My span count.
        conditions:
          - IsMatch(resource.attributes["host.name"], "pod-s")
    spanevents:
      my.spanevent.count:
        description: My span event count.
        conditions:
          - IsMatch(resource.attributes["host.name"], "pod-e")
    metrics:
      my.metric.count:
        description: My metric count.
        conditions:
          - IsMatch(resource.attributes["host.name"], "pod-m")
    datapoints:
      my.datapoint.count:
        description: My data point count.
        conditions:
          - IsMatch(resource.attributes["host.name"], "pod-d")
    logs:
      my.logrecord.count:
        description: My log record count.
        conditions:
          - IsMatch(resource.attributes["host.name"], "pod-l")
    profiles:
      my.profiling.count:
        description: My profile count.
        conditions:
          - IsMatch(resource.attributes["host.name"], "pod-l")
  count/multiple_condition:
    spans:
      my.span.count:
        description: My span count.
        conditions:
          - IsMatch(resource.attributes["host.name"], "pod-s")
          - IsMatch(resource.attributes["foo"], "bar-s")
    spanevents:
      my.spanevent.count:
        description: My span event count.
        conditions:
          - IsMatch(resource.attributes["host.name"], "pod-e")
          - IsMatch(resource.attributes["foo"], "bar-e")
    metrics:
      my.metric.count:
        description: My metric count.
        conditions:
          - IsMatch(resource.attributes["host.name"], "pod-m")
          - IsMatch(resource.attributes["foo"], "bar-m")
    datapoints:
      my.datapoint.count:
        description: My data point count.
        conditions:
          - IsMatch(resource.attributes["host.name"], "pod-d")
          - IsMatch(resource.attributes["foo"], "bar-d")
    logs:
      my.logrecord.count:
        description: My log record count.
        conditions:
          - IsMatch(resource.attributes["host.name"], "pod-l")
          - IsMatch(resource.attributes["foo"], "bar-l")
    profiles:
      my.profiling.count:
        description: My profile count.
        conditions:
          - IsMatch(resource.attributes["host.name"], "pod-l")
          - IsMatch(resource.attributes["foo"], "bar-l")
  count/attribute:
    spans:
      my.span.count:
        description: My span count by environment.
        attributes:
          - key: env
    spanevents:
      my.spanevent.count:
        description: My span event count by environment.
        attributes:
          - key: env
    metrics:
      my.metric.count:
        description: My metric count.
        # Metrics do not have attributes.
    datapoints:
      my.datapoint.count:
        description: My data point count by environment.
        attributes:
          - key: env
    logs:
      my.logrecord.count:
        description: My log record count by environment.
        attributes:
          - key: env
    profiles:
      my.profiling.count:
        description: My profile count by environment.
        attributes:
          - key: env
  count/multiple_metrics:
    spans:
      my.span.count:
        description: My span count.
      limited.span.count:
        description: Limited span count.
        conditions:
          - IsMatch(resource.attributes["host.name"], "pod-s")
        attributes:
          - key: env
          - key: component
            default_value: other
    spanevents:
      my.spanevent.count:
        description: My span event count.
      limited.spanevent.count:
        description: Limited span event count.
        conditions:
          - IsMatch(resource.attributes["host.name"], "pod-e")
        attributes:
          - key: env
          - key: component
            default_value: other
    metrics:
      my.metric.count:
        description: My metric count.
      limited.metric.count:
        description: Limited metric count.
        conditions:
          - IsMatch(resource.attributes["host.name"], "pod-m")
    datapoints:
      my.datapoint.count:
        description: My data point count.
      limited.datapoint.count:
        description: Limited data point count.
        conditions:
          - IsMatch(resource.attributes["host.name"], "pod-d")
        attributes:
          - key: env
          - key: component
            default_value: other
    logs:
      my.logrecord.count:
        description: My log record count.
      limited.logrecord.count:
        description: Limited log record count.
        conditions:
          - IsMatch(resource.attributes["host.name"], "pod-l")
        attributes:
          - key: env
          - key: component
            default_value: other
    profiles:
      my.profiling.count:
        description: My profile count.
      limited.profiling.count:
        description: Limited profile count.
        conditions:
          - IsMatch(resource.attributes["host.name"], "pod-l")
        attributes:
          - key: env
          - key: component
            default_value: other
  count/default_values:
    logs:
      my.logrecord.count:
        description: My log record count with default values.
      limited.logrecord.count:
        description: Limited log record count.
        attributes:
          - key: env
            default_value: "local"
          - key: http_code
            default_value: 200
          - key: request_success
            default_value: 0.85
          - key: cache_hit
            default_value: true
```

***

*Last generated: 2026-07-06*
