---
title: Aggregate spans
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: Docs > API Reference > Spans
---

# Aggregate spans{% #aggregate-spans %}

{% tab title="v2" %}

| Datadog site      | API endpoint                                                        |
| ----------------- | ------------------------------------------------------------------- |
| ap1.datadoghq.com | POST https://api.ap1.datadoghq.com/api/v2/spans/analytics/aggregate |
| ap2.datadoghq.com | POST https://api.ap2.datadoghq.com/api/v2/spans/analytics/aggregate |
| app.datadoghq.eu  | POST https://api.datadoghq.eu/api/v2/spans/analytics/aggregate      |
| app.ddog-gov.com  | POST https://api.ddog-gov.com/api/v2/spans/analytics/aggregate      |
| us2.ddog-gov.com  | POST https://api.us2.ddog-gov.com/api/v2/spans/analytics/aggregate  |
| app.datadoghq.com | POST https://api.datadoghq.com/api/v2/spans/analytics/aggregate     |
| us3.datadoghq.com | POST https://api.us3.datadoghq.com/api/v2/spans/analytics/aggregate |
| us5.datadoghq.com | POST https://api.us5.datadoghq.com/api/v2/spans/analytics/aggregate |

### Overview

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

OAuth apps require the `apm_read` authorization [scope](https://docs.datadoghq.com/api/latest/scopes.md#spans) to access this endpoint.



### Request

#### Body Data (required)



{% tab title="Model" %}

| Parent field | Field                         | Type          | Description                                                                                                                                              |
| ------------ | ----------------------------- | ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
|              | data                          | object        | The object containing the query content.                                                                                                                 |
| data         | attributes                    | object        | The object containing all the query parameters.                                                                                                          |
| attributes   | compute                       | [object]      | The list of metrics or timeseries to compute for the retrieved buckets.                                                                                  |
| compute      | aggregation [*required*] | enum          | An aggregation function. Allowed enum values: `count,cardinality,pc75,pc90,pc95,pc98,pc99,sum,min,max`                                                   |
| compute      | interval                      | string        | The time buckets' size (only used for type=timeseries) Defaults to a resolution of 150 points.                                                           |
| compute      | metric                        | string        | The metric to use.                                                                                                                                       |
| compute      | type                          | enum          | The type of compute. Allowed enum values: `timeseries,total`                                                                                             |
| attributes   | filter                        | object        | The search and filter query settings.                                                                                                                    |
| filter       | from                          | string        | The minimum time for the requested spans, supports date-time ISO8601, date math, and regular timestamps (milliseconds).                                  |
| filter       | query                         | string        | The search query - following the span search syntax.                                                                                                     |
| filter       | to                            | string        | The maximum time for the requested spans, supports date-time ISO8601, date math, and regular timestamps (milliseconds).                                  |
| attributes   | group_by                      | [object]      | The rules for the group by.                                                                                                                              |
| group_by     | facet [*required*]       | string        | The name of the facet to use (required).                                                                                                                 |
| group_by     | histogram                     | object        | 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. |
| histogram    | interval [*required*]    | double        | The bin size of the histogram buckets.                                                                                                                   |
| histogram    | max [*required*]         | double        | The maximum value for the measure used in the histogram (values greater than this one are filtered out).                                                 |
| histogram    | min [*required*]         | double        | The minimum value for the measure used in the histogram (values smaller than this one are filtered out).                                                 |
| group_by     | limit                         | int64         | The maximum buckets to return for this group by.                                                                                                         |
| group_by     | missing                       |  <oneOf> | The value to use for spans that don't have the facet used to group by.                                                                                   |
| missing      | Option 1                      | string        | The missing value to use if there is string valued facet.                                                                                                |
| missing      | Option 2                      | double        | The missing value to use if there is a number valued facet.                                                                                              |
| group_by     | sort                          | object        | A sort rule.                                                                                                                                             |
| sort         | aggregation                   | enum          | An aggregation function. Allowed enum values: `count,cardinality,pc75,pc90,pc95,pc98,pc99,sum,min,max`                                                   |
| sort         | metric                        | string        | The metric to sort by (only used for `type=measure`).                                                                                                    |
| sort         | order                         | enum          | The order to use, ascending or descending. Allowed enum values: `asc,desc`                                                                               |
| sort         | type                          | enum          | The type of sorting algorithm. Allowed enum values: `alphabetical,measure`                                                                               |
| group_by     | total                         |  <oneOf> | A resulting object to put the given computes in over all the matching records.                                                                           |
| total        | Option 1                      | boolean       | If set to true, creates an additional bucket labeled "$facet_total".                                                                                     |
| total        | Option 2                      | string        | A string to use as the key value for the total bucket.                                                                                                   |
| total        | Option 3                      | double        | A number to use as the key value for the total bucket.                                                                                                   |
| attributes   | options                       | object        | 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.    |
| options      | timeOffset                    | int64         | The time offset (in seconds) to apply to the query.                                                                                                      |
| options      | 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).                |
| data         | type                          | enum          | The type of resource. The value should always be aggregate_request. Allowed enum values: `aggregate_request`                                             |

{% /tab %}

{% tab title="Example" %}

```json
{
  "data": {
    "attributes": {
      "compute": [
        {
          "aggregation": "count",
          "interval": "5m",
          "type": "timeseries"
        }
      ],
      "filter": {
        "from": "now-15m",
        "query": "*",
        "to": "now"
      }
    },
    "type": "aggregate_request"
  }
}
```

{% /tab %}

### Response

{% tab title="200" %}
OK
{% tab title="Model" %}
The response object for the spans aggregate API endpoint.

| Parent field         | Field      | Type          | Description                                                                                                                   |
| -------------------- | ---------- | ------------- | ----------------------------------------------------------------------------------------------------------------------------- |
|                      | data       | [object]      | The list of matching buckets, one item per bucket.                                                                            |
| data                 | attributes | object        | A bucket values.                                                                                                              |
| attributes           | by         | object        | The key, value pairs for each group by.                                                                                       |
| additionalProperties | <any-key>  |               | The values for each group by.                                                                                                 |
| attributes           | compute    | object        | The compute data.                                                                                                             |
| attributes           | computes   | object        | A map of the metric name -> value for regular compute or list of values for a timeseries.                                     |
| additionalProperties | <any-key>  |  <oneOf> | A bucket value, can be either a timeseries or a single value.                                                                 |
| <any-key>            | Option 1   | string        | A single string value.                                                                                                        |
| <any-key>            | Option 2   | double        | A single number value.                                                                                                        |
| <any-key>            | Option 3   | [object]      | A timeseries array.                                                                                                           |
| Option 3             | time       | string        | The time value for this point.                                                                                                |
| Option 3             | value      | double        | The value for this point.                                                                                                     |
| data                 | id         | string        | ID of the spans aggregate.                                                                                                    |
| data                 | type       | enum          | The spans aggregate bucket type. Allowed enum values: `bucket`                                                                |
|                      | meta       | object        | The metadata associated with a request.                                                                                       |
| meta                 | elapsed    | int64         | The time elapsed in milliseconds.                                                                                             |
| meta                 | request_id | string        | The identifier of the request.                                                                                                |
| meta                 | status     | enum          | The status of the response. Allowed enum values: `done,timeout`                                                               |
| meta                 | warnings   | [object]      | A list of warnings (non fatal errors) encountered, partial results might be returned if warnings are present in the response. |
| warnings             | code       | string        | A unique code for this type of warning.                                                                                       |
| warnings             | detail     | string        | A detailed explanation of this specific warning.                                                                              |
| warnings             | title      | string        | A short human-readable summary of the warning.                                                                                |

{% /tab %}

{% tab title="Example" %}

```json
{
  "data": [
    {
      "attributes": {
        "by": {
          "<any-key>": "undefined"
        },
        "compute": {},
        "computes": {
          "<any-key>": {
            "description": "undefined",
            "type": "undefined"
          }
        }
      },
      "id": "string",
      "type": "bucket"
    }
  ],
  "meta": {
    "elapsed": 132,
    "request_id": "MWlFUjVaWGZTTTZPYzM0VXp1OXU2d3xLSVpEMjZKQ0VKUTI0dEYtM3RSOFVR",
    "status": "done",
    "warnings": [
      {
        "code": "unknown_index",
        "detail": "indexes: foo, bar",
        "title": "One or several indexes are missing or invalid, results hold data from the other indexes"
      }
    ]
  }
}
```

{% /tab %}

{% /tab %}

{% tab title="400" %}
Bad Request
{% tab title="Model" %}
API error response.

| Field                    | Type     | Description       |
| ------------------------ | -------- | ----------------- |
| errors [*required*] | [string] | A list of errors. |

{% /tab %}

{% tab title="Example" %}

```json
{
  "errors": [
    "Bad Request"
  ]
}
```

{% /tab %}

{% /tab %}

{% tab title="403" %}
Forbidden
{% tab title="Model" %}
API error response.

| Field                    | Type     | Description       |
| ------------------------ | -------- | ----------------- |
| errors [*required*] | [string] | A list of errors. |

{% /tab %}

{% tab title="Example" %}

```json
{
  "errors": [
    "Bad Request"
  ]
}
```

{% /tab %}

{% /tab %}

{% tab title="429" %}
Too many requests
{% tab title="Model" %}
API error response.

| Field                    | Type     | Description       |
| ------------------------ | -------- | ----------------- |
| errors [*required*] | [string] | A list of errors. |

{% /tab %}

{% tab title="Example" %}

```json
{
  "errors": [
    "Bad Request"
  ]
}
```

{% /tab %}

{% /tab %}

### Code Example

##### 
                          \## default
# 
 \# Curl command curl -X POST "https://api.datadoghq.com/api/v2/spans/analytics/aggregate" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "DD-API-KEY: ${DD_API_KEY}" \
-H "DD-APPLICATION-KEY: ${DD_APP_KEY}" \
-d @- << EOF
{
  "data": {
    "attributes": {
      "compute": [
        {
          "aggregation": "pc90",
          "interval": "5m",
          "metric": "@duration",
          "type": "timeseries"
        }
      ],
      "filter": {
        "from": "now-15m",
        "query": "service:web* AND @http.status_code:[200 TO 299]",
        "to": "now"
      },
      "group_by": [
        {
          "facet": "host",
          "histogram": {
            "interval": 10,
            "max": 100,
            "min": 50
          },
          "sort": {
            "aggregation": "count",
            "order": "asc"
          }
        }
      ],
      "options": {
        "timezone": "GMT"
      }
    },
    "type": "aggregate_request"
  }
}
EOF 
                        
##### 

```go
// Aggregate spans returns "OK" response

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"os"

	"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
	"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
)

func main() {
	body := datadogV2.SpansAggregateRequest{
		Data: &datadogV2.SpansAggregateData{
			Attributes: &datadogV2.SpansAggregateRequestAttributes{
				Compute: []datadogV2.SpansCompute{
					{
						Aggregation: datadogV2.SPANSAGGREGATIONFUNCTION_COUNT,
						Interval:    datadog.PtrString("5m"),
						Type:        datadogV2.SPANSCOMPUTETYPE_TIMESERIES.Ptr(),
					},
				},
				Filter: &datadogV2.SpansQueryFilter{
					From:  datadog.PtrString("now-15m"),
					Query: datadog.PtrString("*"),
					To:    datadog.PtrString("now"),
				},
			},
			Type: datadogV2.SPANSAGGREGATEREQUESTTYPE_AGGREGATE_REQUEST.Ptr(),
		},
	}
	ctx := datadog.NewDefaultContext(context.Background())
	configuration := datadog.NewConfiguration()
	apiClient := datadog.NewAPIClient(configuration)
	api := datadogV2.NewSpansApi(apiClient)
	resp, r, err := api.AggregateSpans(ctx, body)

	if err != nil {
		fmt.Fprintf(os.Stderr, "Error when calling `SpansApi.AggregateSpans`: %v\n", err)
		fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
	}

	responseContent, _ := json.MarshalIndent(resp, "", "  ")
	fmt.Fprintf(os.Stdout, "Response from `SpansApi.AggregateSpans`:\n%s\n", responseContent)
}
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=go) and then save the example to `main.go` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" go run "main.go"
##### 

```java
// Aggregate spans returns "OK" response

import com.datadog.api.client.ApiClient;
import com.datadog.api.client.ApiException;
import com.datadog.api.client.v2.api.SpansApi;
import com.datadog.api.client.v2.model.SpansAggregateData;
import com.datadog.api.client.v2.model.SpansAggregateRequest;
import com.datadog.api.client.v2.model.SpansAggregateRequestAttributes;
import com.datadog.api.client.v2.model.SpansAggregateRequestType;
import com.datadog.api.client.v2.model.SpansAggregateResponse;
import com.datadog.api.client.v2.model.SpansAggregationFunction;
import com.datadog.api.client.v2.model.SpansCompute;
import com.datadog.api.client.v2.model.SpansComputeType;
import com.datadog.api.client.v2.model.SpansQueryFilter;
import java.util.Collections;

public class Example {
  public static void main(String[] args) {
    ApiClient defaultClient = ApiClient.getDefaultApiClient();
    SpansApi apiInstance = new SpansApi(defaultClient);

    SpansAggregateRequest body =
        new SpansAggregateRequest()
            .data(
                new SpansAggregateData()
                    .attributes(
                        new SpansAggregateRequestAttributes()
                            .compute(
                                Collections.singletonList(
                                    new SpansCompute()
                                        .aggregation(SpansAggregationFunction.COUNT)
                                        .interval("5m")
                                        .type(SpansComputeType.TIMESERIES)))
                            .filter(new SpansQueryFilter().from("now-15m").query("*").to("now")))
                    .type(SpansAggregateRequestType.AGGREGATE_REQUEST));

    try {
      SpansAggregateResponse result = apiInstance.aggregateSpans(body);
      System.out.println(result);
    } catch (ApiException e) {
      System.err.println("Exception when calling SpansApi#aggregateSpans");
      System.err.println("Status code: " + e.getCode());
      System.err.println("Reason: " + e.getResponseBody());
      System.err.println("Response headers: " + e.getResponseHeaders());
      e.printStackTrace();
    }
  }
}
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=java) and then save the example to `Example.java` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" java "Example.java"
##### 

```python
"""
Aggregate spans returns "OK" response
"""

from datadog_api_client import ApiClient, Configuration
from datadog_api_client.v2.api.spans_api import SpansApi
from datadog_api_client.v2.model.spans_aggregate_data import SpansAggregateData
from datadog_api_client.v2.model.spans_aggregate_request import SpansAggregateRequest
from datadog_api_client.v2.model.spans_aggregate_request_attributes import SpansAggregateRequestAttributes
from datadog_api_client.v2.model.spans_aggregate_request_type import SpansAggregateRequestType
from datadog_api_client.v2.model.spans_aggregation_function import SpansAggregationFunction
from datadog_api_client.v2.model.spans_compute import SpansCompute
from datadog_api_client.v2.model.spans_compute_type import SpansComputeType
from datadog_api_client.v2.model.spans_query_filter import SpansQueryFilter

body = SpansAggregateRequest(
    data=SpansAggregateData(
        attributes=SpansAggregateRequestAttributes(
            compute=[
                SpansCompute(
                    aggregation=SpansAggregationFunction.COUNT,
                    interval="5m",
                    type=SpansComputeType.TIMESERIES,
                ),
            ],
            filter=SpansQueryFilter(
                _from="now-15m",
                query="*",
                to="now",
            ),
        ),
        type=SpansAggregateRequestType.AGGREGATE_REQUEST,
    ),
)

configuration = Configuration()
with ApiClient(configuration) as api_client:
    api_instance = SpansApi(api_client)
    response = api_instance.aggregate_spans(body=body)

    print(response)
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=python) and then save the example to `example.py` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" python3 "example.py"
##### 

```ruby
# Aggregate spans returns "OK" response

require "datadog_api_client"
api_instance = DatadogAPIClient::V2::SpansAPI.new

body = DatadogAPIClient::V2::SpansAggregateRequest.new({
  data: DatadogAPIClient::V2::SpansAggregateData.new({
    attributes: DatadogAPIClient::V2::SpansAggregateRequestAttributes.new({
      compute: [
        DatadogAPIClient::V2::SpansCompute.new({
          aggregation: DatadogAPIClient::V2::SpansAggregationFunction::COUNT,
          interval: "5m",
          type: DatadogAPIClient::V2::SpansComputeType::TIMESERIES,
        }),
      ],
      filter: DatadogAPIClient::V2::SpansQueryFilter.new({
        from: "now-15m",
        query: "*",
        to: "now",
      }),
    }),
    type: DatadogAPIClient::V2::SpansAggregateRequestType::AGGREGATE_REQUEST,
  }),
})
p api_instance.aggregate_spans(body)
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=ruby) and then save the example to `example.rb` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" rb "example.rb"
##### 

```rust
// Aggregate spans returns "OK" response
use datadog_api_client::datadog;
use datadog_api_client::datadogV2::api_spans::SpansAPI;
use datadog_api_client::datadogV2::model::SpansAggregateData;
use datadog_api_client::datadogV2::model::SpansAggregateRequest;
use datadog_api_client::datadogV2::model::SpansAggregateRequestAttributes;
use datadog_api_client::datadogV2::model::SpansAggregateRequestType;
use datadog_api_client::datadogV2::model::SpansAggregationFunction;
use datadog_api_client::datadogV2::model::SpansCompute;
use datadog_api_client::datadogV2::model::SpansComputeType;
use datadog_api_client::datadogV2::model::SpansQueryFilter;

#[tokio::main]
async fn main() {
    let body = SpansAggregateRequest::new().data(
        SpansAggregateData::new()
            .attributes(
                SpansAggregateRequestAttributes::new()
                    .compute(vec![SpansCompute::new(SpansAggregationFunction::COUNT)
                        .interval("5m".to_string())
                        .type_(SpansComputeType::TIMESERIES)])
                    .filter(
                        SpansQueryFilter::new()
                            .from("now-15m".to_string())
                            .query("*".to_string())
                            .to("now".to_string()),
                    ),
            )
            .type_(SpansAggregateRequestType::AGGREGATE_REQUEST),
    );
    let configuration = datadog::Configuration::new();
    let api = SpansAPI::with_config(configuration);
    let resp = api.aggregate_spans(body).await;
    if let Ok(value) = resp {
        println!("{:#?}", value);
    } else {
        println!("{:#?}", resp.unwrap_err());
    }
}
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=rust) and then save the example to `src/main.rs` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" cargo run
##### 

```typescript
/**
 * Aggregate spans returns "OK" response
 */

import { client, v2 } from "@datadog/datadog-api-client";

const configuration = client.createConfiguration();
const apiInstance = new v2.SpansApi(configuration);

const params: v2.SpansApiAggregateSpansRequest = {
  body: {
    data: {
      attributes: {
        compute: [
          {
            aggregation: "count",
            interval: "5m",
            type: "timeseries",
          },
        ],
        filter: {
          from: "now-15m",
          query: "*",
          to: "now",
        },
      },
      type: "aggregate_request",
    },
  },
};

apiInstance
  .aggregateSpans(params)
  .then((data: v2.SpansAggregateResponse) => {
    console.log(
      "API called successfully. Returned data: " + JSON.stringify(data)
    );
  })
  .catch((error: any) => console.error(error));
```

#### Instructions

First [install the library and its dependencies](https://docs.datadoghq.com/api/latest.md?code-lang=typescript) and then save the example to `example.ts` and run following commands:
    DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" DD_APP_KEY="<DD_APP_KEY>" tsc "example.ts"
{% /tab %}
