Grafana Cloud

Adaptive Logs HTTP API

The following topic describes the Adaptive Logs HTTP API.

Before you begin

To interact with the API, gather the following information:

  • URL: In the form https://<your-grafana-cloud-loki-url>.grafana.net. To find your URL value, go to your Grafana Cloud account and check the Details page of your hosted Loki endpoint.
  • TENANT: The numeric instance ID where you want to use Adaptive Logs. To find your TENANT value, go to your Grafana Cloud account and check the Details page of your hosted Loki endpoint for Username / Instance ID.
  • TOKEN: A token from a Grafana Cloud Access Policy that has the adaptive-logs:admin scope for your stack. More details about available permissions and scopes can be found in RBAC for Adaptive Logs.

Note

You can create Grafana Cloud Access Policies from the Grafana portal, with the API, or in your Grafana Cloud Stack.

Patterns and recommendations

Adaptive Logs groups your log lines into patterns and generates recommendations for each pattern based on query usage over a rolling 15-day window. Each pattern has a drop rate that controls what percentage of matching log lines are dropped upon ingestion.

The endpoint returns a JSON array of recommendations:

JSON
[
  {
    "tokens": ["level=info ts=", "<*>", " caller=", "<*>", " msg=\"request completed\" status=200 duration=", "<*>"],
    "locked": false,
    "configured_drop_rate": 0,
    "volume": 1288490188,
    "ingested_lines": 4500000,
    "queried_lines": 1200,
    "recommended_drop_rate": 80,
    "superseded": false,
    "levels": ["info"],
    "segments": {}
  }
]

In the preceding example:

  • tokens contains the detected pattern. Concatenate the values in order to rebuild the pattern text.
  • volume is the estimated ingested volume in bytes over the lookback window.
  • ingested_lines is the estimated number of ingested lines that match the pattern.
  • queried_lines is the number of matching lines returned by queries.
  • configured_drop_rate is the percentage of matching lines currently dropped.
  • recommended_drop_rate is the percentage that Adaptive Logs recommends dropping.

The API doesn’t return query-frequency labels or projected savings. Derive query frequency from queried_lines and ingested_lines.

List recommendations

Download recommendations for patterns and drop rates using the following command. TOKEN and TENANT are variables defined in the requirements section.

Bash
curl -u "$TENANT:$TOKEN" "$URL/adaptive-logs/recommendations"

TOKEN must belong to an access policy with the adaptive-logs:admin scope.

Note

Recommendations are generated asynchronously. The API call returns the most recently generated set of recommendations. Adaptive Logs recalculates recommendations every 24 hours.

Exemptions

Exemptions allow you to prevent certain logs from being dropped, even if Adaptive Logs recommends dropping them. Exemptions are defined using LogQL stream selectors. You can also create temporary exemptions that expire after a set duration.

The exemption format looks like this:

Field nameTypeDescription
ididentifierUnique identifier of the exemption (set by the server).
stream_selectorstringA valid LogQL stream selector. Logs matching this selector are never dropped.
reason (optional)stringBusiness context for why this exemption exists.
active_interval (optional)durationHow long a temporary exemption remains active, for example, 1h or 30m.
expires_attimestampWhen the exemption expires (set by the server).
created_attimestampWhen the exemption was created (set by the server).
updated_attimestampWhen the exemption was last updated (set by the server).

Warning

expires_at is read-only. Sending it in a request doesn’t create a temporary exemption. Use active_interval and the expiring-exemptions endpoint instead.

Below are examples of exemptions:

  1. Exempt all logs from the login service from being dropped.

    JSON
    {
      "stream_selector": "{service_name=\"login\"}"
    }
  2. Exempt all logs from the payments namespace with a reason.

    JSON
    {
      "stream_selector": "{namespace=\"payments\"}",
      "reason": "Required for PCI compliance audit trail"
    }
  3. Create a temporary exemption that expires after one hour.

    JSON
    {
      "stream_selector": "{service_name=\"api-gateway\"}",
      "reason": "Investigating latency spike",
      "active_interval": "1h"
    }

Create a permanent exemption

Bash
curl -u "$TENANT:$TOKEN" --request POST --data @exemption.json "$URL/adaptive-logs/exemptions"

TOKEN must belong to an access policy with the adaptive-logs:admin scope.

If successful, the server returns a JSON object including the id of the newly created exemption.

Create a temporary exemption

Set active_interval in exemption.json, then use the temporary exemption endpoint:

Bash
curl -u "$TENANT:$TOKEN" --request POST --data @exemption.json "$URL/adaptive-logs/expiring-exemptions"

TOKEN must belong to an access policy with the adaptive-logs:admin scope.

List exemptions

Bash
curl -u "$TENANT:$TOKEN" "$URL/adaptive-logs/exemptions"

TOKEN must belong to an access policy with the adaptive-logs:admin scope.

Get an exemption

Bash
curl -u "$TENANT:$TOKEN" "$URL/adaptive-logs/exemptions/<exemption_id>"

TOKEN must belong to an access policy with the adaptive-logs:admin scope.

The exemption_id is the identifier of the exemption to fetch, as returned by the server when creating the exemption.

Update an exemption

Bash
curl -u "$TENANT:$TOKEN" --request PUT --data @exemption.json "$URL/adaptive-logs/exemptions/<exemption_id>"

TOKEN must belong to an access policy with the adaptive-logs:admin scope.

The exemption_id is the identifier of the exemption to update, as returned by the server when creating the exemption.

Delete an exemption

Bash
curl -u "$TENANT:$TOKEN" --request DELETE "$URL/adaptive-logs/exemptions/<exemption_id>"

TOKEN must belong to an access policy with the adaptive-logs:admin scope.

The exemption_id is the identifier of the exemption to delete, as returned by the server when creating the exemption.

Drop rules

Drop rules let you define custom rules that drop a percentage of log lines matching specific criteria. Unlike recommendations, which are system-generated, drop rules are user-defined and give you fine-grained control over which logs are dropped.

Each drop rule has the following format:

Field nameTypeDescription
ididentifierUnique identifier of the drop rule (set by the server).
segment_idstringThe segment this rule belongs to. Use __global__ for tenant-wide rules.
namestringA human-readable name for the drop rule.
versionintegerThe version of the drop rule, used for optimistic concurrency control.
disabledbooleanWhether the drop rule is currently disabled.
expires_at (optional)timestampWhen this drop rule expires. If not set, the drop rule is permanent.
created_attimestampWhen the drop rule was created (set by the server).
updated_attimestampWhen the drop rule was last updated (set by the server).
bodyobjectThe rule definition, containing the fields described in the following table.

The body object has the following fields:

Field nameTypeDescription
stream_selectorstringA LogQL stream selector that defines which log streams the rule applies to.
drop_ratenumberThe percentage of matching log lines to drop (0 to 100).
levels (optional)string[]Log levels to match (for example, ["info", "debug"]). If empty, all levels match.
log_line_contains (optional)string[]Strings that must appear in the log line for the rule to apply.

Below is an example of a drop rule:

JSON
{
  "segment_id": "__global__",
  "name": "Drop noisy health checks",
  "version": 1,
  "disabled": false,
  "body": {
    "stream_selector": "{service_name=\"api-gateway\"}",
    "drop_rate": 90,
    "levels": ["info", "debug"],
    "log_line_contains": ["healthcheck"]
  }
}

Create a drop rule

Bash
curl -u "$TENANT:$TOKEN" --request POST --data @drop-rule.json "$URL/adaptive-logs/drop-rules"

TOKEN must belong to an access policy with the adaptive-logs:admin scope.

If successful, the server returns a JSON object including the id of the newly created drop rule.

List drop rules

Bash
curl -u "$TENANT:$TOKEN" "$URL/adaptive-logs/drop-rules"

TOKEN must belong to an access policy with the adaptive-logs:admin scope.

You can filter results with the following query parameters:

ParameterDescription
segment_idFilter by segment. Use __global__ for tenant-wide rules.
expiration_filterFilter by expiration status: all, active, or expired.

Get a drop rule

Bash
curl -u "$TENANT:$TOKEN" "$URL/adaptive-logs/drop-rules/<drop_rule_id>"

TOKEN must belong to an access policy with the adaptive-logs:admin scope.

The drop_rule_id is the identifier of the drop rule to fetch, as returned by the server when creating the drop rule.

Update a drop rule

Bash
curl -u "$TENANT:$TOKEN" --request PUT --data @drop-rule.json "$URL/adaptive-logs/drop-rules/<drop_rule_id>"

TOKEN must belong to an access policy with the adaptive-logs:admin scope.

The drop_rule_id is the identifier of the drop rule to update, as returned by the server when creating the drop rule.

Delete a drop rule

Bash
curl -u "$TENANT:$TOKEN" --request DELETE "$URL/adaptive-logs/drop-rules/<drop_rule_id>"

TOKEN must belong to an access policy with the adaptive-logs:admin scope.

The drop_rule_id is the identifier of the drop rule to delete, as returned by the server when creating the drop rule.

Segments

Segments let you organize log streams into groups based on a shared label, so you can manage Adaptive Logs recommendations on a per-team or per-service basis.

Each segment has the following format:

Field nameTypeDescription
ididentifierUnique identifier of the segment (set by the server).
namestringA human-readable name for the segment.
selectorstringA LogQL stream selector that defines which log streams belong to this segment.

Note

All segments must reference the same label name. Only equality matchers (for example, {team="billing"}) or multi-literal regular expression matchers (for example, {team=~"(alerting|alerting-dev)"}) are allowed.

Below is an example of a segment:

JSON
{
  "name": "Billing team",
  "selector": "{team=\"billing\"}"
}

Create a segment

Bash
curl -u "$TENANT:$TOKEN" --request POST --data @segment.json "$URL/adaptive-logs/segment"

TOKEN must belong to an access policy with the adaptive-logs:admin scope.

If successful, the server returns a JSON object including the id of the newly created segment.

List segments

Bash
curl -u "$TENANT:$TOKEN" "$URL/adaptive-logs/segments"

TOKEN must belong to an access policy with the adaptive-logs:admin scope.

Get a segment

Bash
curl -u "$TENANT:$TOKEN" "$URL/adaptive-logs/segment?segment=<segment_id>"

TOKEN must belong to an access policy with the adaptive-logs:admin scope.

The segment_id is the identifier of the segment to fetch, as returned by the server when creating the segment.

Update a segment

Bash
curl -u "$TENANT:$TOKEN" --request PUT --data @segment.json "$URL/adaptive-logs/segment?segment=<segment_id>"

TOKEN must belong to an access policy with the adaptive-logs:admin scope.

The segment_id is the identifier of the segment to update, as returned by the server when creating the segment.

Delete a segment

Bash
curl -u "$TENANT:$TOKEN" --request DELETE "$URL/adaptive-logs/segment?segment=<segment_id>"

TOKEN must belong to an access policy with the adaptive-logs:admin scope.

The segment_id is the identifier of the segment to delete, as returned by the server when creating the segment.