Skip to main content

Count Connector

Status Available in: contrib, k8s Maintainers: @akats7 Source: opentelemetry-collector-contrib

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]DescriptionDefault Metric Names
tracesCounts all spans and span events.trace.span.count, trace.span.event.count
metricsCounts all metrics and data points.metric.count, metric.datapoint.count
logsCounts 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.
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.
receivers:
  foo:
exporters:
  bar:
connectors:
  count:
    spanevents:
      my.prod.event.count:
        description: The number of span events from my prod environment.
        conditions:
          - 'attributes["env"] == "prod"'
          - '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.
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.
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.
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.
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.
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.
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.
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]

Configuration

Example Configuration

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-04-13