Query Examples

以下是使用示例数据集的示例查询的列表。我们将说明可能遇到的多种常见查询类型,以便您可以了解查询系统的工作方式。示例集中的每个时间序列仅存储一个数据点,并且 UID 已被截断为单个字节,以使其更易于读取。示例查询是来自 HTTP API 的所有* Metric *查询,仅显示m=组件。有关详情,请参见/api/query。如果使用的是 CLI 工具,查询格式将略有不同,因此请阅读特定命令的文档。

Sample Data

Time Series

TS# Metric Tags TSUID
1 sys.cpu.system dc=dal host=web01 0102040101
2 sys.cpu.system dc=dal host=web02 0102040102
3 sys.cpu.system dc=dal host=web03 0102040103
4 sys.cpu.system host=web01 010101
5 sys.cpu.system host=web01 owner=jdoe 0101010306
6 sys.cpu.system dc=lax host=web01 0102050101
7 sys.cpu.system dc=lax host=web02 0102050102
8 sys.cpu.user dc=dal host=web01 0202040101
9 sys.cpu.user dc=dal host=web02 0202040102

UIDs

Name UID
Metrics
cpu.system 01
cpu.user 02
Tagks
host 01
dc 02
owner 03
Tagvs
web01 01
web02 02
web03 03
dal 04
lax 05
jdoe 06

Warning

这不一定是设置 Metrics 和标记的最佳方法,而是用来说明查询系统的工作方式。特别是,TS#4 和 TS 5,尽管合法的时间序列,可能会使查询搞砸,除非您知道它们是如何工作的。通常,尝试为每个时间序列保持相同数量和类型的标记。

引擎盖下

您可能想在以下位置阅读有关 OpenTSDB 如何存储时间序列数据的信息:Storage。否则,请记住存储中的每一行都有一个唯一的键,其格式为:

<metricID><normalized_timestamp><tagkID1><tagvID1>[...<tagkIDN><tagvIDN>]

上面的数据表将存储为:

01<ts>0101
01<ts>01010306
01<ts>02040101
01<ts>02040102
01<ts>02040103
01<ts>02050101
01<ts>02050102
02<ts>02040101
02<ts>02040102

当您查询 OpenTSDB 时,这是实际情况。

查询 1-度量的所有时间序列

m=sum:cpu.system

这是最简单的查询。 TSDB 将设置一个扫描程序,以获取*<start> <end> *之间度量 UID 01的所有数据点。结果将是时间序列#1 至#7 相加的单个数据集。如果给定 Metrics 有成千上万的唯一标记组合,则它们将全部一起添加到一个系列中。

[
    {
        "metric": "cpu.system",
        "tags": {},
        "aggregated_tags": [
            "host"
        ],
        "tsuids": [
            "010101",
            "0101010306",
            "0102050101",
            "0102040101",
            "0102040102",
            "0102040103",
            "0102050102"
        ],
        "dps": {
            "1346846400": 130.29999923706055
        }
    }
]

查询 2-按标签过滤

通常,汇总 Metrics 的所有时间序列并不是特别有用。相反,我们可以通过过滤包含特定 tagk/tagv 对组合的时间序列来进行深入研究。只需将两对放在花括号中:

m=sum:cpu.system{host=web01}

这将返回时间序列#1,#4,#5 和#6 的汇总,因为它们是唯一包含host=web01的序列。

[
    {
        "metric": "cpu.system",
        "tags": {
            "host": "web01"
        },
        "aggregated_tags": [],
        "tsuids": [
            "010101",
            "0101010306",
            "0102040101",
            "0102050101"
        ],
        "dps": {
            "1346846400": 63.59999942779541
        }
    }
]

查询 3-特定时间序列

如果要特定的时间序列怎么办?您必须包括每个标签和对应的值。

m=sum:cpu.system{host=web01,dc=lax}

这将仅返回时间序列 6 中的数据。

[
    {
        "metric": "cpu.system",
        "tags": {
            "dc": "lax",
            "host": "web01"
        },
        "aggregated_tags": [],
        "tsuids": [
            "0102050101"
        ],
        "dps": {
            "1346846400": 15.199999809265137
        }
    }
]

Warning

这是标记方案将立足或落败的地方。假设您只想从时间序列#4 中获取数据。使用当前系统,您将无法进行。您可能会发送查询#2 m=sum:cpu.system{host=web01},以为它只会返回来自#4 的数据,但是正如我们所看到的,您将获得#1,#4,#5 和#6 的汇总结果。为防止这种情况发生,您需要在#4 中添加另一个标签,以使其与组中的其他时间序列区分开。或者,如果已经提交,则可以使用 TSUID 查询。

查询 4-TSUID 查询

如果您知道要检索的时间序列的确切 TSUID,则可以像这样传递它:

tsuids=sum:0102040102

结果将是您请求的数据点。

[
    {
        "metric": "cpu.system",
        "tags": {
            "dc": "lax",
            "host": "web01"
        },
        "aggregated_tags": [],
        "tsuids": [
            "0102050101"
        ],
        "dps": {
            "1346846400": 15.199999809265137
        }
    }
]

查询 5-多 TSUID 查询

您还可以在同一查询中聚合多个 TSUID,前提是它们共享相同的度量。如果您尝试汇总多个 Metrics,则 API 将发出错误。

tsuids=sum:0102040101,0102050101
[
    {
        "metric": "cpu.system",
        "tags": {
            "host": "web01"
        },
        "aggregated_tags": [
            "dc"
        ],
        "tsuids": [
            "0102040101",
            "0102050101"
        ],
        "dps": {
            "1346846400": 33.19999980926514
        }
    }
]

查询 6-分组

m=sum:cpu.system{host=*}

*(星号)是一种分组运算符,它将为给定标签名称的每个唯一值返回一个数据集。包含给定 Metrics 和给定标签名称的每个时间序列,无论其他标签或值如何,都将包含在结果中。将各个时间序列结果分组后,将它们汇总并返回。

在此示例中,我们将返回 3 个组:

Group 包括时间序列
web01 #1, #4, #5, #6
web02 #2, #7
web03 #3

TSDB 找到了总共 7 个包含“主机”标签的时间序列。该标记有 3 个唯一值(web01,web02 和 web03)。

[
    {
        "metric": "cpu.system",
        "tags": {
            "host": "web01"
        },
        "aggregated_tags": [],
        "tsuids": [
            "010101",
            "0101010306",
            "0102040101",
            "0102050101"
        ],
        "dps": {
            "1346846400": 63.59999942779541
        }
    },
    {
        "metric": "cpu.system",
        "tags": {
            "host": "web02"
        },
        "aggregated_tags": [
            "dc"
        ],
        "tsuids": [
            "0102040102",
            "0102050102"
        ],
        "dps": {
            "1346846400": 24.199999809265137
        }
    },
    {
        "metric": "cpu.system",
        "tags": {
            "dc": "dal",
            "host": "web03"
        },
        "aggregated_tags": [],
        "tsuids": [
            "0102040103"
        ],
        "dps": {
            "1346846400": 42.5
        }
    }
]

查询 7-分组和过滤

请注意,在示例#2 中,web01组包括奇数球时间序列#4 和#5.我们可以通过指定第二个标签 ala 来过滤掉它们:

m=sum:cpu.nice{host=*,dc=dal}

现在,我们只会得到#1-#3 的结果,但会丢失dc=lax值。

[
    {
        "metric": "cpu.system",
        "tags": {
            "dc": "dal",
            "host": "web01"
        },
        "aggregated_tags": [],
        "tsuids": [
            "0102040101"
        ],
        "dps": {
            "1346846400": 18
        }
    },
    {
        "metric": "cpu.system",
        "tags": {
            "dc": "dal",
            "host": "web02"
        },
        "aggregated_tags": [],
        "tsuids": [
            "0102040102"
        ],
        "dps": {
            "1346846400": 9
        }
    },
    {
        "metric": "cpu.system",
        "tags": {
            "dc": "dal",
            "host": "web03"
        },
        "aggregated_tags": [],
        "tsuids": [
            "0102040103"
        ],
        "dps": {
            "1346846400": 42.5
        }
    }
]

查询 8-或分组

*运算符是贪婪的,它将返回分配给标签名称的所有*值。如果只需要一些标记值,则可以使用|(管道)运算符代替。

m=sum:cpu.nice{host=web01|web02}

这将找到所有包含“ web01”或“ web02”的“主机”值的时间序列,然后按值将它们分组,类似于*运算符。这次,我们的小组将如下所示:

Group 包括时间序列
web01 #1, #4, #5, #6
web02 #2, #7
[
    {
        "metric": "cpu.system",
        "tags": {
            "host": "web01"
        },
        "aggregated_tags": [],
        "tsuids": [
            "010101",
            "0101010306",
            "0102040101",
            "0102050101"
        ],
        "dps": {
            "1346846400": 63.59999942779541
        }
    },
    {
        "metric": "cpu.system",
        "tags": {
            "host": "web02"
        },
        "aggregated_tags": [
            "dc"
        ],
        "tsuids": [
            "0102040102",
            "0102050102"
        ],
        "dps": {
            "1346846400": 24.199999809265137
        }
    }
]
首页