Telemetry.ExtractedMetric
A rule that extracts a metric, or a label, from a Source’s logs:
sqlExpression is evaluated for each log line (fields are in the raw
JSON column, read with JSONExtract(raw, …, 'Nullable(Type)')), and the
aggregations are precomputed so dashboards chart it cheaply. An empty
(or omitted) aggregations makes it a label to filter and group by.
The API has no endpoint to get one rule: it is read through the source’s
list. buildType (whether a change also applies to logs already stored)
is never returned, so it is compared against the previous deploy.
sourceId replaces the rule when changed; everything else updates in
place.
Extracting metrics from logs
Section titled “Extracting metrics from logs”A duration metric and a label
const app = yield* BetterStack.Telemetry.Source("app", { platform: "http" });yield* BetterStack.Telemetry.ExtractedMetric("duration", { sourceId: app.id, name: "duration_ms", sqlExpression: "JSONExtract(raw, 'message_json', 'duration_ms', 'Nullable(Float64)')", aggregations: ["avg", "max", "min"],});yield* BetterStack.Telemetry.ExtractedMetric("log-type", { sourceId: app.id, name: "log_type", sqlExpression: "JSONExtract(raw, 'message_json', 'log_type', 'Nullable(String)')",});name string required
Metric or label name.
sourceId string required
sqlExpression string required
SQL expression evaluated for each log. Log fields are in the raw JSON field, so read them with JSONExtract(raw, ..., 'Nullable(Type)').
aggregations string[]
Aggregations to precompute, avg, count, uniq, max, min, sum or histogram. An empty array makes this a label rather than a metric.
buildType "new_data" | "historical_logs" | string
Whether this change also applies to logs already stored.
teamId string
Team that owns the resource, such as t123. Defaults to the credentials' team (BETTERSTACK_TEAM_ID, or the profile's team). Changing it replaces the resource.
type "string" | "boolean" | "string_low_cardinality" | "int64_delta" | "int64" | "uint64_delta" | "uint64" | "float64_delta" | "datetime64_delta" | string
ClickHouse type the value is stored as.
Attributes
Section titled “Attributes”id string always set
The extracted_metric's id.
sourceId string always set
The source whose logs the metric is extracted from.
aggregations string[]
Aggregations to precompute, avg, count, uniq, max, min, sum or histogram. An empty array makes this a label rather than a metric.
name string
Metric or label name.
sqlExpression string
SQL expression evaluated for each log. Log fields are in the raw JSON field, so read them with JSONExtract(raw, ..., 'Nullable(Type)').
teamId string
Team that owns the metric's source, such as t123.
type "string" | "boolean" | "string_low_cardinality" | "int64_delta" | "int64" | "uint64_delta" | "uint64" | "float64_delta" | "datetime64_delta"
ClickHouse type the value is stored as.
Lookup
Section titled “Lookup”ExtractedMetric.lookup(props) reads an object this stack does not manage. It is never created, updated or deleted.
sourceId string required
The source it belongs to.
teamId string
Team that owns the source, such as t123. Defaults to the credentials' team.