---
title: "Aggregate spans"
method: POST
path: "/api/v2/spans/analytics/aggregate"
tags: ["Spans"]
---

# Aggregate spans

`POST /api/v2/spans/analytics/aggregate`

## Request

- Base URL: `https://api.datadoghq.com`
- URL: `https://api.datadoghq.com/api/v2/spans/analytics/aggregate`
- Auth: one of:
  - API key in header `DD-API-KEY` and API key in header `DD-APPLICATION-KEY`
  - OAuth 2
    - Authorization code: `/oauth2/v1/authorize`
    - Token URL: `/oauth2/v1/token` scopes: `apm_read`

The API endpoint to aggregate spans into buckets and compute metrics and timeseries.
This endpoint is rate limited to `300` requests per hour.

## Request body

- SpansAggregateRequest: The object sent with the request to retrieve a list of aggregated spans from your organization.
  - `data` SpansAggregateData: The object containing the query content.
    - `attributes` SpansAggregateRequestAttributes: The object containing all the query parameters.
      - `compute` SpansCompute[]: The list of metrics or timeseries to compute for the retrieved buckets.
        - `aggregation` 'count' | 'cardinality' | 'pc75' | 'pc90' | 'pc95' | 'pc98' | 'pc99' | 'sum' | 'min' | 'max' | 'avg' | 'median', required: An aggregation function.
        - `interval` string: The time buckets' size (only used for type=timeseries) Defaults to a resolution of 150 points.
        - `metric` string: The metric to use.
        - `type` 'timeseries' | 'total': The type of compute.
      - `filter` SpansQueryFilter: The search and filter query settings.
        - `from` string: The minimum time for the requested spans, supports date-time ISO8601, date math, and regular timestamps (milliseconds).
        - `query` string: The search query - following the span search syntax.
        - `to` string: The maximum time for the requested spans, supports date-time ISO8601, date math, and regular timestamps (milliseconds).
      - `group_by` SpansGroupBy[]: The rules for the group by.
        - `facet` string, required: The name of the facet to use (required).
        - `histogram` SpansGroupByHistogram: Used to perform a histogram computation (only for measure facets). Note: At most 100 buckets are allowed, the number of buckets is (max - min)/interval.
          - `interval` number, double, required: The bin size of the histogram buckets.
          - `max` number, double, required: The maximum value for the measure used in the histogram (values greater than this one are filtered out).
          - `min` number, double, required: The minimum value for the measure used in the histogram (values smaller than this one are filtered out).
        - `limit` integer: The maximum buckets to return for this group by.
        - `missing` union: The value to use for spans that don't have the facet used to group by.
          - string: The missing value to use if there is string valued facet.
          - number, double: The missing value to use if there is a number valued facet.
        - `sort` SpansAggregateSort: A sort rule.
          - `aggregation` 'count' | 'cardinality' | 'pc75' | 'pc90' | 'pc95' | 'pc98' | 'pc99' | 'sum' | 'min' | 'max' | 'avg' | 'median': An aggregation function.
          - `metric` string: The metric to sort by (only used for `type=measure`).
          - `order` 'asc' | 'desc': The order to use, ascending or descending.
          - `type` 'alphabetical' | 'measure': The type of sorting algorithm.
        - `total` union: A resulting object to put the given computes in over all the matching records.
          - boolean: If set to true, creates an additional bucket labeled "$facet_total".
          - string: A string to use as the key value for the total bucket.
          - number, double: A number to use as the key value for the total bucket.
      - `options` SpansQueryOptions: Global query options that are used during the query. Note: You should only supply timezone or time offset but not both otherwise the query will fail.
        - `timeOffset` integer: The time offset (in seconds) to apply to the query.
        - `timezone` string: The timezone can be specified as GMT, UTC, an offset from UTC (like UTC+1), or as a Timezone Database identifier (like America/New_York).
    - `type` 'aggregate_request': The type of resource. The value should always be aggregate_request.

## Response `200`

OK

- SpansAggregateResponse: The response object for the spans aggregate API endpoint.
  - `data` SpansAggregateBucket[]: The list of matching buckets, one item per bucket.
    - `attributes` SpansAggregateBucketAttributes: A bucket values.
      - `by` object: The key, value pairs for each group by.
      - `compute` object: The compute data.
      - `computes` object: A map of the metric name -> value for regular compute or list of values for a timeseries.
    - `id` string: ID of the spans aggregate.
    - `type` 'bucket': The spans aggregate bucket type.
  - `meta` SpansAggregateResponseMetadata: The metadata associated with a request.
    - `elapsed` integer: The time elapsed in milliseconds.
    - `request_id` string: The identifier of the request.
    - `status` 'done' | 'timeout': The status of the response.
    - `warnings` SpansWarning[]: A list of warnings (non fatal errors) encountered, partial results might be returned if warnings are present in the response.
      - `code` string: A unique code for this type of warning.
      - `detail` string: A detailed explanation of this specific warning.
      - `title` string: A short human-readable summary of the warning.

## Other responses

- `400`: Bad Request
- `403`: Forbidden
- `429`: Too many requests

---

[API](https://skmtc.dev/datadog/apis/datadog-api-v2-collection.md) - [All operations](https://skmtc.dev/datadog/apis/datadog-api-v2-collection/llms.txt) - [OpenAPI document](https://skmtc.dev/datadog/apis/datadog-api-v2-collection/revisions/da68bf029e4c?raw)
