Skip to content

Extract first array element after split for grouping #978

@patsevanton

Description

@patsevanton

Is your question related to a specific component?

LogsQL

Describe the question in detail

Hello,

I have the following LogsQL query that splits logs from the nginx-log-generator container by "?" in the _msg field:

kubernetes.container_name: "nginx-log-generator" | split "?" from _msg

This produces logs like:

2025-12-30 09:43:30.254522801 ["/api/v1/users","RequestId=1ab2c345-d6e7-4890-b1c2-d3e4f5a6b7c8"]
2025-12-30 09:43:29.255310492 ["/api/v1/users","RequestId=123e4567-e89b-12d3-a456-426614174000"]
2025-12-30 09:43:28.255173279 ["/api/v1/users","RequestId=1ab2c345-d6e7-4890-b1c2-d3e4f5a6b7c8"]

The split creates an array in a field (likely _msg[0] or similar), where the first element is the path like "/api/v1/users".

Question: How can I extract just the first element of this array (e.g. "/api/v1/users") into a new field, so I can then group by it with stats by (path) or similar?

For example, something like:

... | split "?" from _msg | extract_first_array_element as path | stats by (path) count()

I've checked the docs for split, unroll, unpack_json, extract, but couldn't find a direct way to pick the first array item after split. Is there a pipe or function for this?

Key concepts docs

Metadata

Metadata

Assignees

No one assigned

    Labels

    logsqlIssues related to LogsQLquestionAsking about a problem, or not sure whether the issue is an enhancement or a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions