/betterstack:logs-queryfrom Better Stack
Provides the required SQL syntax and patterns for querying Better Stack raw events (logs, spans, web-events) via query and render_chart tools. Covers time filters, aggregation limits, and multi-window merging.
Ships in Better Stack. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
These queries read raw events, so they run ad hoc through query and render_chart; dashboard charts only read the metrics collection (see dashboard-builder for log and span volumes there). Write them as this skill shows, with {{source}}, {{time}}, {{start_time}} and {{end_time}}, and pass source_id, source_type (logs, spans or web_events), start_time and end_time (now-3h, now-1d, or an ISO 8601 datetime) and no table; the tool builds the source's collections for {{source}} and resolves {{time}} to a bucket that suits the range. One change from the examples: filter time with dt >= {{start_time}} AND dt < {{end_time}}, not {{time}} BETWEEN {{start_time}} AND {{end_time}}, the dashboards' form, which drops the rows of the range's first partial bucket and which query cannot split.
First call query_help with the source ID for the syntax.
Add a LIMIT. A template that lists the newest rows (ORDER BY dt DESC LIMIT n, no aggregate, GROUP BY, DISTINCT or subquery), or one that only aggregates (count(), countIf(), sum(), sumIf(), min() or max() beside its GROUP BY columns, no HAVING, DISTINCT or subquery) over at most 300,000,000 rows, then needs one query call however wide its range: query reads the range in time windows itself, merges the rows and says how far it read. A query of any other shape whose range is too wide for one call, in query or render_chart, is refused without running, with the time windows that each fit, newest first: run it once per window, each as start_time and end_time, and merge the results: rows concatenate, count() and sum() add up, min() and max() combine, and an average needs sum() and count() per window. quantile() and uniq() don't merge, so narrow the range or the filters until one call holds them.
| Need | Pattern |
|---|---|
| Source | FROM {{source}} |
| Time filter | WHERE {{time}} BETWEEN {{start_time}} AND {{end_time}} |
| Time bucket | {{time}} AS time |
| Service filter | <service expression> = 'service-name' |
| Group by service | <service expression> AS series |
| Extract field | JSONExtractCaseInsensitive(raw, 'field', 'Nullable(String)') |
| Nested field | JSONExtractCaseInsensitive(raw, 'parent', 'child', 'Nullable(String)') |
| Keyword search | positionCaseInsensitive(field, 'keyword') > 0 |
raw Column (JSON)-- Always use JSONExtractCaseInsensitive with Nullable types
JSONExtractCaseInsensitive(raw, 'level', 'Nullable(String)') AS log_level
JSONExtractCaseInsensitive(raw, 'metadata', 'user_id', 'Nullable(Int32)') AS user_id
-- Fields with dots in name: use single string (NOT separate args)
JSONExtractCaseInsensitive(raw, 'field.with.dots', 'Nullable(String)')
List of fields coming from source_fields (application_fields for an Errors application) lists fields as which are nested in raw - always access using JSONExtractCaseInsensitive(raw, 'nginx', 'agent', 'Nullable(String)'), not SELECT nginx.agent FROM {{source}}
{{source}} - table name (never hardcode){{time}} - time bucket column (NOT dt){{start_time}}, {{end_time}} - time range boundssource returns the source's service_fields: the fields that name a service, in order. Each name_key is a path whose dots separate keys, and \. is a dot inside a key. Build the service name expression from them and use it in SELECT / WHERE / GROUP BY: one JSONExtractCaseInsensitive(raw, ...) per name_key, each path key a separate argument, inside COALESCE(...) in that order, such as COALESCE(JSONExtractCaseInsensitive(raw, 'resource', 'service.name', 'Nullable(String)'), JSONExtractCaseInsensitive(raw, 'resource', 'k8s.container.name', 'Nullable(String)')) for resource.service\.name and resource.k8s\.container\.name. Never guess service field paths.
Use COUNT, AVG, SUM - NOT avgMerge, countMerge (those are for pre-aggregated metrics).
Column aliases become chart legend labels. Use descriptive names instead of generic value.
-- BAD: Legend shows "value"
SELECT {{time}} AS time, COUNT(*) AS value FROM ...
-- GOOD: Legend shows "Error count"
SELECT {{time}} AS time, COUNT(*) AS "Error count" FROM ...
raw LIKE PrefilterWhen filtering on one specific ID (session ID, trace_id, request ID, user ID), add a substring prefilter before the extraction:
AND raw LIKE '%YOUR_SESSION_ID%'
AND JSONExtractCaseInsensitive(raw, 'session_id', 'Nullable(String)') = 'YOUR_SESSION_ID'
The cheap LIKE scan skips JSON-parsing rows that can't match; keep the equality as the authoritative filter. LIKE is case-sensitive — use the ID verbatim.
Time-series (single):
SELECT {{time}} AS time, COUNT(*) AS "Log count"
FROM {{source}}
WHERE {{time}} BETWEEN {{start_time}} AND {{end_time}}
GROUP BY time
ORDER BY time
Time-series (multi-series by service) - series uses the service name expression from the source tool:
SELECT {{time}} AS time, JSONExtractCaseInsensitive(raw, 'service', 'Nullable(String)') AS series, COUNT(*) AS "Log count"
FROM {{source}}
WHERE {{time}} BETWEEN {{start_time}} AND {{end_time}}
GROUP BY time, series
ORDER BY time
Time-series (multi-series by level):
SELECT
{{time}} AS time,
JSONExtractCaseInsensitive(raw, 'level', 'Nullable(String)') AS series,
COUNT(*) AS "Log count"
FROM {{source}}
WHERE {{time}} BETWEEN {{start_time}} AND {{end_time}}
GROUP BY time, series
ORDER BY time
Error rate:
SELECT
{{time}} AS time,
countIf(JSONExtractCaseInsensitive(raw, 'level', 'Nullable(String)') = 'ERROR') / COUNT(*) AS "Error rate"
FROM {{source}}
WHERE {{time}} BETWEEN {{start_time}} AND {{end_time}}
GROUP BY time
ORDER BY time
Keyword search:
SELECT {{time}} AS time, JSONExtractCaseInsensitive(raw, 'message', 'Nullable(String)') AS message
FROM {{source}}
WHERE {{time}} BETWEEN {{start_time}} AND {{end_time}}
AND positionCaseInsensitive(JSONExtractCaseInsensitive(raw, 'message', 'Nullable(String)'), 'timeout') > 0
ORDER BY time DESC
LIMIT 100
Map (geographic by IP) — for map_chart: wrap the source's IP field (or any city/country/airport string) in location(), group by it, no time column — load the map-hierarchy-charts skill for the query shape and settings.
| Mistake | Fix |
|---|---|
{{_service}} | Removed - use the expression from the source tool |
| Guessed service field path | Copy the service name expression from the source tool |
WHERE dt BETWEEN | WHERE dt >= {{start_time}} AND dt < {{end_time}} |
raw.level or just level | JSONExtractCaseInsensitive(raw, 'level', ...) |
'String' type | 'Nullable(String)' |
'parent.child' for nested | 'parent', 'child' as separate arguments |
avgMerge(...) on logs | AVG(...) - logs are not pre-aggregated |
AS value for chart queries | Use descriptive alias: AS "Log count" |
| ID equality without a prefilter | Add raw LIKE '%<id>%' before the extraction (see rule 5) |
Web events are browser events from the Better Stack JS tag on Errors sources (Applications): page views/navigation, web vitals, and user/session context. They are stored and queried exactly like logs — raw JSON rows in the raw column with the same template variables.
source_type: web_events in query (and source_fields (application_fields for an Errors application) to discover fields — don't guess field paths).payload.meta.url (path at payload.meta.path): JSONExtractCaseInsensitive(raw, 'payload', 'meta', 'path', 'Nullable(String)').To check the volume of rows by type (log, span, web_event, exception) without any WHERE filtering, always use a metrics query instead of scanning raw events — load the metrics-query skill and use its per-row-type counts (sum(logs_count), sum(spans_count), sum(web_events_count), sum(exceptions_count)).
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub betterstackhq/claude-plugin --plugin betterstack17 skills in this plugin
/chart-alertsGuide for creating and managing Better Stack chart alerts. Load when asked to set up alerting, create alerts on charts, or configure monitoring thresholds./chart-generationComplete reference for chart types, units, and settings on Better Stack dashboards. Load right before creating charts - after the queries are built with the query skills./chart-stylingBetter Stack chart styling and advanced settings reference - series colors and order, number chart coloring thresholds, heatmap color categories, gauge bounds, y-axis tuning, custom units, and the per-key settings table with cross-setting gotchas. Load when configuring how a chart looks beyond defaults, or when unsure which settings key applies to a chart type.Other plugins' skills for the same job