Skip to content

Commit

Permalink
更新笔记。
Browse files Browse the repository at this point in the history
  • Loading branch information
Suomea committed Dec 27, 2024
1 parent afb0407 commit eb09ec4
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 72 deletions.
58 changes: 28 additions & 30 deletions .obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@
"state": {
"type": "markdown",
"state": {
"file": "docs/Java/日期时间操作.md",
"file": "docs/Prometheus/探索 PromQL.md",
"mode": "source",
"source": false
},
"icon": "lucide-file",
"title": "日期时间操作"
"title": "探索 PromQL"
}
}
],
Expand Down Expand Up @@ -116,8 +116,7 @@
"title": "书签"
}
}
],
"currentTab": 1
]
}
],
"direction": "horizontal",
Expand Down Expand Up @@ -208,36 +207,35 @@
"vscode-editor:新建代码文件": false
}
},
"active": "c3e1cd9fa3752c91",
"active": "70e320b58f86b1d6",
"lastOpenFiles": [
"docs/Java/Tips.md",
"docs/Java/日期时间操作.md",
"docs/Linux/未命名.md",
"docs/Redis/Redis 数据结构.md",
"docs/Redis/Redis 安装.md",
"docs/Redis/Redis 分布式锁.md",
"docs/MySQL/MySQL 备份.md",
"docs/MySQL/MySQL 安装.md",
"docs/Kafka/Kafka 快速开始.md",
"docs/Kafka/Kafka 常用命令.md",
"docs/Kafka/Kafka 集群搭建.md",
"docs/Kafka/Kafka Tips.md",
"docs/MySQL/MySQL 配置主从.md",
"docs/Linux/运维监控.md",
"docs/MySQL/MySQL 系统和状态变量.md",
"mkdocs.yml",
"docs/Prometheus/探索 PromQL.md",
"docs/Prometheus",
"docs/DM/达梦 Tips.md",
"docs/DM/达梦备份.md",
"docs/DM/达梦安装.md",
"docs/Arts/满江红-岳飞.md",
"docs/Arts/大观楼长联-孙髯.md",
"docs/Arts/侠客行-李白.md",
"docs/Algorithms/求解算数表达式的值.md",
"docs/Algorithms/数据结构:图.md",
"docs/Algorithms/字符串匹配算法.md",
"docs/Algorithms/Base64 算法.md",
"docs/Linux/ls.md",
"未命名",
"docs/Linux/Linux 文件系统.md",
"docs/Linux/Linux Tips.md",
"docs/Linux/Nginx 编译安装.md",
"docs/Linux/Nginx 使用自签名证书.md",
"docs/Linux/Nginx Tips.md",
"docs/Linux/PT.md",
"docs/Linux/磁盘分区.md",
"template/Doris.md",
"未命名.md",
"docs/Linux/未命名.md",
"docs/Linux/Debian 软件安装.md",
"docs/Linux/Git.md",
"docs/Linux/curl.md",
"docs/Linux/ArcLinux 安装.md",
"mkdocs.yml",
"docs/Linux/运维监控.md",
"docs/index.md",
"docs/Linux/Linux 文件系统.md",
"docs/Java/Java 并发.md",
"docs/Kafka/Kafka 配置.md",
"docs/Kafka/Kafka 常用命令.md",
"docs/Kafka/Kafka 集群搭建.md",
"overrides/partials/comments.html",
"overrides/partials/footer.html",
"docs/LocalFile/Picture/ReentrantReadWriteLock类图.svg",
Expand Down
39 changes: 13 additions & 26 deletions docs/Kafka/Kafka 常用命令.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,23 @@ tags:
---
## Topic

创建 topic
topic 操作选项
```
bin/kafka-topics.sh --bootstrap-server localhost:9092 --topic <topic_name> --create
--topic 指定要操作的 topic 名称
--create 创建 topic
--delete 删除 topic
--describe 查看 topic 的详细描述
--alter 修改主题
--partitions <num> 修改主题的分区数,只能增加,不能减少
--replication-factor <num> 设置分区副本数(如果设置为 3,那么就是 1 个 Leader 副本,3 个 Follower 副本)
--config <key=val> 更新系统默认设置
```

查看所有的 topic
```
bin/kafka-topics.sh --bootstrap-server localhost:9092 --list
```

查看 topic 的信息
```
bin/kafka-topics.sh --bootstrap-server localhost:9092 --topic <topic_name> --describe
```

查看 topic 每个分区的 offset
```
bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --topic <topic_name>
```

修改 topic 的分区数量
```
bin/kafka
创建 Topic。
```shell
bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic quickstart-events [--partitions num] [--replication-factor num] --command-config admin-jaas
```

## Consumer

控制台消费者
Expand All @@ -51,12 +44,6 @@ bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic <topic_n
```

## ACL
### Topic

创建 Topic。
```shell
bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic quickstart-events [--partitions num] [--replication-factor num] --command-config admin-jaas
```

### 生产

Expand Down
10 changes: 10 additions & 0 deletions docs/Kafka/Kafka 配置.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ Partition 区,一个主题可以有多个分区,每个分区包含主题中
Consumer Group 是消费者组,消费者属于消费者组。一个消费者组会完整的消费一个主题的数据,具体由消费者消费分区的数据实现。同一个消费者组的每个消费者可以消费零到多个分区的数据,但是不能多个消费者同时消费一个分区的数据。

![[Kafka-ConsumerGroup.png]]
## 生产者配置
Kafka 生产者有两个线程(假设 Main 线程和 Sender 线程),一个内存池缓存发送的数据。

假设 send(msg) 方法在 Main 线程调用,消息会经历拦截器、序列化器、分区器,然后消息会存储在内存池中。Sender 线程负责读取缓存的数据发送到 Kafka。

buffer.memory 控制缓冲区的容量,默认 32MB。如果缓冲区满了,max.blocks.ms 控制 Main 线程写入到缓冲区的阻塞时间。
缓冲区里面是多个队列,每个 topic 的分区都对应一个队列。队列中的多个消息可以组成一个批次,batch.size 控制批次的大小。如果消息批次达到 batch.size 则整批次的消息会发送到 Kafka。如果一批次的消息大小未达到 batch.size,可能会因为超时 linger.ms 而提前发送,linger.ms 默认为 0,消息立即发送。

buffer-available-bytes 用来监控缓冲区的情况。
compression.type=gzip 可以启用消息压缩,减少缓冲区的占用。

## 消费者配置
**auto.offset.reset**
Expand Down
2 changes: 0 additions & 2 deletions docs/Linux/ls.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#Linux

## 文件的时间:

- mtime,modification time 当文件的内容变更时,就会更新这个时间。
Expand Down
Empty file removed docs/Linux/未命名.md
Empty file.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#Linux

## 列出所有块设备及其挂载点
`lsblk` 命令默认输出字段解释:
Expand Down
213 changes: 213 additions & 0 deletions docs/Prometheus/探索 PromQL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
Prometheus 会将所有采集到的样本数据以时间序列的方式存储在内存数据库中,并定时保存在硬盘上。每条时间序列通过指标名称和一组标签集合命名。
样本 sample
时间序列 time-series
指标名称 metrics name
标签集合 labelset
```
^
│ . . . . . . . . . . . . . . . . . . . node_cpu{cpu="cpu0",mode="idle"}
│ . . . . . . . . . . . . . . . . . . . node_cpu{cpu="cpu0",mode="system"}
│ . . . . . . . . . . . . . . . . . . . node_load1{}
│ . . . . . . . . . . . . . . . . . . .
v
<------------------ 时间 ---------------->
```

`__` 开头命名的标签是系统保留标签,智能在系统内部使用。
下面两种方式均可以表示同一条时间序列
```
api_total{method="POST", path="/message"}
{__name__="api_total", method="POST", path="/message"}
```


从存储上来说,所有的监控指标都是相同的,但是在业务 Prometheus 定义了 4 中不同的指标类型:Counter 计数器、Gauge 仪表盘、Histogram 直方图、Summary 摘要。

Counter 只增加不减少的计数器,比如统计接口请求数量的指标 http_request_total。一般在定义 Counter 类型的指标时建议使用 `_total` 作为后缀。

Gauge 可增可减的仪表盘,常见指标如:node_memory_MemFree(主机当前空闲的内容大小)、node_memory_MemAvailable(可用内存大小)都是Gauge类型的监控指标。

除了 Counter 和 Gauge 类型的监控指标以外,Prometheus 还定义了 Histogram 和 Summary 的指标类型。Histogram 和 Summary 主用用于统计和分析样本的分布情况。

例如,指标 prometheus_tsdb_wal_fsync_duration_seconds 的指标类型为Summary。 它记录了 Prometheus Server 中 wal_fsync 处理的处理时间,通过访问 Prometheus Server的 /metrics地址,可以获取到以下监控样本数据:
```
# HELP prometheus_tsdb_wal_fsync_duration_seconds Duration of WAL fsync.
# TYPE prometheus_tsdb_wal_fsync_duration_seconds summary
prometheus_tsdb_wal_fsync_duration_seconds{quantile="0.5"} 0.012352463
prometheus_tsdb_wal_fsync_duration_seconds{quantile="0.9"} 0.014458005
prometheus_tsdb_wal_fsync_duration_seconds{quantile="0.99"} 0.017316173
prometheus_tsdb_wal_fsync_duration_seconds_sum 2.888716127000002
prometheus_tsdb_wal_fsync_duration_seconds_count 216
```

从上面的样本中可以得知当前 Prometheus Server 进行 wal_fsync 操作的总次数为 216 次,耗时 2.888716127000002s。其中中位数(quantile=0.5)的耗时为 0.012352463,9 分位数(quantile=0.9)的耗时为 0.014458005s。

在Prometheus Server自身返回的样本数据中,我们还能找到类型为 Histogram 的监控指标 prometheus_tsdb_compaction_chunk_range_bucket。
```
# HELP prometheus_tsdb_compaction_chunk_range Final time range of chunks on their first compaction
# TYPE prometheus_tsdb_compaction_chunk_range histogram
prometheus_tsdb_compaction_chunk_range_bucket{le="100"} 0
prometheus_tsdb_compaction_chunk_range_bucket{le="400"} 0
prometheus_tsdb_compaction_chunk_range_bucket{le="1600"} 0
prometheus_tsdb_compaction_chunk_range_bucket{le="6400"} 0
prometheus_tsdb_compaction_chunk_range_bucket{le="25600"} 0
prometheus_tsdb_compaction_chunk_range_bucket{le="102400"} 0
prometheus_tsdb_compaction_chunk_range_bucket{le="409600"} 0
prometheus_tsdb_compaction_chunk_range_bucket{le="1.6384e+06"} 260
prometheus_tsdb_compaction_chunk_range_bucket{le="6.5536e+06"} 780
prometheus_tsdb_compaction_chunk_range_bucket{le="2.62144e+07"} 780
prometheus_tsdb_compaction_chunk_range_bucket{le="+Inf"} 780
prometheus_tsdb_compaction_chunk_range_sum 1.1540798e+09
prometheus_tsdb_compaction_chunk_range_count 780
```
与Summary类型的指标相似之处在于Histogram类型的样本同样会反应当前指标的记录的总数(以_count作为后缀)以及其值的总量(以_sum作为后缀)。不同在于Histogram指标直接反应了在不同区间内样本的个数,区间通过标签len进行定义。

查询时间序列
```
http_requests_total
```

等同于
```
http_requests_total{}
```

该表达式会返回指标名称为http_requests_total的所有时间序列:
```
http_requests_total{code="200",handler="alerts",instance="localhost:9090",job="prometheus",method="get"}=([email protected])
http_requests_total{code="200",handler="graph",instance="localhost:9090",job="prometheus",method="get"}=([email protected])
```

PromQL 还支持指标的标签过滤,完全匹配:= 和 !=,正则匹配:=~ 和 !~
```
http_requests_total{environment=~"staging|testing|development",method!="GET"}
```

上述查询默认返回指标的最新一个样本,这样的返回结构称为瞬时向量,相应的表达式称为瞬时向量表达式。

如果想要获取一段时间范围内的样本数据,则需要使用区间向量表达式,区间向量表达式需要定义事件选择范围。
```
http_requests_total{}[5m]
```
返回结果
```
http_requests_total{code="200",handler="alerts",instance="localhost:9090",job="prometheus",method="get"}=[
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
]
http_requests_total{code="200",handler="graph",instance="localhost:9090",job="prometheus",method="get"}=[
4 @1518096812.326
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
]
```

还支持其它时间单位:y、w、d、h、m、s。


在瞬时向量表达式或者区间向量表达式中,都是以当前时间为基准:
```
http_request_total{} # 瞬时向量表达式,选择当前最新的数据
http_request_total{}[5m] # 区间向量表达式,选择以当前时间为基准,5分钟内的数据
```

而如果我们想查询,5分钟前的瞬时样本数据,或昨天一天的区间内的样本数据呢? 这个时候我们就可以使用位移操作,位移操作的关键字为**offset**
可以使用offset时间位移操作:
```
http_request_total{} offset 5m
http_request_total{}[1d] offset 1d
```

标量 scalar

标量只是一个数字,没有时序。

可以通过内置函数 scalar 将单个瞬时向量转换为标量。


数学运算

```
node_memory_free_bytes_total / (1024 * 1024)
```
当瞬时向量与标量之间进行数学运算时,数学运算会依次作用与瞬时向量中的每一个样本值,从而的到一组新的时间序列。

如果是瞬时向量与瞬时向量之间进行数学运算时,过程会相对复杂一点。 例如,如果我们想根据node_disk_bytes_written和node_disk_bytes_read获取主机磁盘IO的总量,可以使用如下表达式:
```
node_disk_bytes_written + node_disk_bytes_read
```

那这个表达式是如何工作的呢?依次找到与左边向量元素匹配(标签完全一致)的右边向量元素进行运算,如果没找到匹配元素,则直接丢弃。同时新的时间序列将不会包含指标名称。 该表达式返回结果的示例如下所示:
```
{device="sda",instance="localhost:9100",job="node_exporter"}=>[email protected] + [email protected]
{device="sdb",instance="localhost:9100",job="node_exporter"}=>[email protected] + [email protected]
```

PromQL支持的所有数学运算符如下所示:
- `+` (加法)
- `-` (减法)
- `*` (乘法)
- `/` (除法)
- `%` (求余)
- `^` (幂运算)

Prometheus还提供了下列内置的聚合操作符,这些操作符作用域瞬时向量。可以将瞬时表达式返回的样本数据进行聚合,形成一个新的时间序列。
- `sum` (求和)
- `min` (最小值)
- `max` (最大值)
- `avg` (平均值)
- `stddev` (标准差)
- `stdvar` (标准方差)
- `count` (计数)
- `count_values` (对value进行计数)
- `bottomk` (后n条时序)
- `topk` (前n条时序)
- `quantile` (分位数)

使用聚合操作的语法如下:
```
<aggr-op>([parameter,] <vector expression>) [without|by (<label list>)]
```
其中只有`count_values`, `quantile`, `topk`, `bottomk`支持参数(parameter)。

by 是通过指定的标签进行分组,without 是剔除指定的标签,其余标签作为分组依据。
假设有以下指标:
```
http_requests_total{instance="server1", job="web", method="GET"} 100
http_requests_total{instance="server2", job="web", method="GET"} 200
http_requests_total{instance="server1", job="web", method="POST"} 50
http_requests_total{instance="server2", job="web", method="POST"} 150
```


使用 by 聚合
```
sum(http_requests_total) by (method)
```

结果
```
{method="GET"} 300
{method="POST"} 200
```


使用 without 聚合
```
sum(http_requests_total) without (instance)
```

结果
```
{job="web", method="GET"} 300
{job="web", method="POST"} 200
```

内置函数
Loading

0 comments on commit eb09ec4

Please sign in to comment.