---
# System prepended metadata

title: SUSE Observability 自定義圖示
tags: [work]

---

# SUSE Observability 自定義圖示


## 實作

* 先在 CLI 安裝 sts 指令，在 o11y UI 左下角點選 CLI 複製以下安裝 sts 指令

![image](https://hackmd.io/_uploads/SymugIU01g.png)
```
$ curl -o- https://dl.stackstate.com/stackstate-cli/install.sh | STS_URL="http://obs1.example.com" STS_API_TOKEN="HPoktLn_wz0J4Y7ffD0Eh7GN2dDOyc5V" bash
```

### 透過 PromQL 表達式來查詢 namespace 的 cpu、memory usage 指標
* 產生查詢指定 suse-observability namespace 的 cpu useage

```
$ nano cpu.yaml
nodes:
- _type: MetricBinding
  chartType: line
  enabled: true
  tags: {}
  unit: short
  name: namespace cpu useage
  priority: MEDIUM
  identifier: urn:custom:metric-binding:my-cpu-test
  queries:
    - expression: sum(rate(container_cpu_usage{namespace="suse-observability"}[5m]))
      alias: cpu useage
  scope: label = "name:suse-observability" and type = "namespace"

$ sts settings apply -f cpu.yaml
```

* 產生查詢指定 suse-observability namespace 的 memory useage 
```
$ nano memory.yaml
nodes:
- _type: MetricBinding
  chartType: line
  enabled: true
  tags: {}
  unit: short
  name: namespace memory useage
  priority: MEDIUM
  identifier: urn:custom:metric-binding:my-memory-test
  queries:
    - expression: sum(container_memory_usage{namespace="suse-observability"})
      alias: memory useage
  scope: label = "name:suse-observability" and type = "namespace"
  
$ sts settings apply -f memory.yaml
```

* 進入 o11y UI -> 指定點選 suse-observability Namepace -> Metrics -> Other 就可以看到我們自己創建的指標

![image](https://hackmd.io/_uploads/HJRSELURye.png)


## 刪除 MetricBinding
* 列出所有 MetricBinding
```
$ sts settings list --type MetricBinding
......
MetricBinding | 257964042792977 | urn:custom:metric-binding:my-cpu-test   | namespace cpu useage                    |                                         | Fri Apr 11 16:34:25 2025 CST
MetricBinding | 168948220646633 | urn:custom:metric-binding:my-memory-tes | namespace memory useage                 |                                         | Fri Apr 11 16:34:42 2025 CST
```

* 刪除 MetricBinding
```
$ sts settings delete --ids 257964042792977

$ sts settings delete --ids 168948220646633
```

### 使用變數置換
* 透過 `${name}` 這個變數可以替換成所有的 namespace 名稱
```
$ nano cpu2.yaml
nodes:
- _type: MetricBinding
  chartType: line
  enabled: true
  tags: {}
  unit: short
  name: namespace cpu useage
  priority: MEDIUM
  identifier: urn:custom:metric-binding:my-cpu-test
  queries:
    - expression: sum(rate(container_cpu_usage{namespace="${name}"}[5m]))
      alias: cpu useage
  scope: label = "stackpack:kubernetes" and type = "namespace"

$ sts settings apply -f cpu2.yaml
```

```
$ nano memory2.yaml
nodes:
- _type: MetricBinding
  chartType: line
  enabled: true
  tags: {}
  unit: short
  name: namespace memory useage
  priority: MEDIUM
  identifier: urn:custom:metric-binding:my-memory-test
  queries:
    - expression: sum(container_memory_usage{namespace="${name}"})
      alias: memory useage
  scope: label = "stackpack:kubernetes" and type = "namespace"
  
$ sts settings apply -f memory2.yaml
```
* 現在在每個 namespace 下都可以看到我們新添加的指標了

![image](https://hackmd.io/_uploads/S1IM6IU0Jl.png)
![image](https://hackmd.io/_uploads/ryjy0LL0ke.png)

## 參考

https://docs.stackstate.com/metrics/custom-charts/k8s-writing-promql-for-charts


https://github.com/ravan/suse-observability-guides/blob/main/guides/suse/virtualization/metrics/README.md