跳转至

SQL 探索器

借助 Logfire,您可以使用“探索”页面对跟踪和指标数据运行任意 SQL 查询,以分析和调查您的系统。

Logfire explore screen

查询跟踪

您将查询的主要表是记录表,其中包含来自您跟踪的请求的所有跨度/日志。

要查询记录,只需使用 SELECT ...FROM 记录并添加 WHERE 子句以过滤所需的跨度。

例如,下面是一个查询,该查询返回具有异常的所有跨度的消息、start_timestamp、持续时间和属性:

SELECT
  message,
  start_timestamp,
  EXTRACT(EPOCH FROM (end_timestamp - start_timestamp)) * 1000 AS duration_ms,
  attributes
FROM records
WHERE is_exception

您还可以使用子查询、CTE、联接、聚合、自定义表达式和任何其他标准 SQL 来运行更复杂的查询。

记录架构

记录表的架构为:

CREATE TABLE records AS (
    start_timestamp timestamp with time zone,
    created_at timestamp with time zone,
    trace_id text,
    span_id text,
    parent_span_id text,
    kind span_kind,
    end_timestamp timestamp with time zone,
    level smallint,
    span_name text,
    message text,
    attributes_json_schema text,
    attributes jsonb,
    tags text[],
    otel_links jsonb,
    otel_events jsonb,
    is_exception boolean,
    otel_status_code status_code,
    otel_status_message text,
    otel_scope_name text,
    otel_scope_version text,
    otel_scope_attributes jsonb,
    service_namespace text,
    service_name text,
    service_version text,
    service_instance_id text,
    process_pid integer
)

与实时取景交叉链接

运行查询后,您可以获取任何trace_id和/或span_id,并使用它来查找在实时视图中显示为跟踪的数据。

只需转到实时视图并输入如下查询:

trace_id = '7bda3ddf6e6d4a0c8386093209eb0bfc' -- 替换为您自己的真实trace_id

这将显示具有该特定跟踪 ID 的所有跨度。

指标架构

除了跟踪之外,您还可以使用指标表查询指标数据。

metrics 表的架构为:

CREATE TABLE metrics AS (
    recorded_timestamp timestamp with time zone,
    metric_name text,
    metric_type text,
    unit text,
    start_timestamp timestamp with time zone,
    aggregation_temporality public.aggregation_temporality,
    is_monotonic boolean,
    metric_description text,
    scalar_value double precision,
    histogram_min double precision,
    histogram_max double precision,
    histogram_count integer,
    histogram_sum double precision,
    exp_histogram_scale integer,
    exp_histogram_zero_count integer,
    exp_histogram_zero_threshold double precision,
    exp_histogram_positive_bucket_counts integer[],
    exp_histogram_positive_bucket_counts_offset integer,
    exp_histogram_negative_bucket_counts integer[],
    exp_histogram_negative_bucket_counts_offset integer,
    histogram_bucket_counts integer[],
    histogram_explicit_bounds double precision[],
    attributes jsonb,
    tags text[],
    otel_scope_name text,
    otel_scope_version text,
    otel_scope_attributes jsonb,
    service_namespace text,
    service_name text,
    service_version text,
    service_instance_id text,
    process_pid integer
)

您可以使用标准 SQL 查询指标,就像跟踪一样。例如:

SELECT *
FROM metrics
WHERE metric_name = 'system.cpu.time'
  AND recorded_timestamp > now() - interval '1 hour'

执行查询

要执行查询,请将其键入或粘贴到查询编辑器中,然后单击“运行查询”按钮。

Logfire explore screen

您可以使用按钮旁边的下拉列表修改查询的时间范围。还有一个“限制”下拉列表,用于控制返回的最大结果行数。

“浏览”页面提供了一个灵活的界面,可以使用标准 SQL 查询跟踪和指标。

祝您查询愉快!🚀


本文总阅读量