OpenObserve 指標和鏈路追蹤方面的支持

前面我們講到了 OpenObserve 的基本使用,使用 Fluentd 將日志采集后輸出到了 OpenObserve,此外 OpenObserve 還支持指標和鏈路追蹤。
指標
OpenObserve 除了支持日志之外,也支持指標數(shù)據(jù)的攝取,它支持 Prometheus 的遠程寫入?yún)f(xié)議,這樣我們就可以直接將 Prometheus 的數(shù)據(jù)遠程寫入到 OpenObserve 中了。
下面的資源清單就是一個簡單的 Prometheus 示例,我們使用 node_exporter 來采集節(jié)點的指標數(shù)據(jù),然后通過 Prometheus 將其遠程寫入到 OpenObserve 中:
# prometheus.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-config
namespace: openobserve
data:
prometheus.yaml: |
global:
scrape_interval: 15s
scrape_timeout: 15s
remote_write: # 寫入到遠程 OO,url 是遠程寫入接口地址
- url: http://openobserve.openobserve.svc.cluster.local:5080/api/default/prometheus/api/v1/write
basic_auth:
username: root@example.com
password: root321
# queue_config: # 如果 Prometheus 抓取指標很大,可以加調(diào)整 queue,但是會提高內(nèi)存占用
# max_samples_per_send: 10000 # 每次發(fā)送的最大樣本數(shù)
# capacity: 20000
# max_shards: 30 # 最大分片數(shù),即并發(fā)量。
scrape_configs:
- job_name: "nodes"
static_configs:
- targets: ['10.206.16.6:9100', '10.206.16.5:9100', '10.206.16.10:9100']
relabel_configs: # 通過 relabeling 從 __address__ 中提取 IP 信息,為了后面驗證 VM 是否兼容 relabeling
- source_labels: [__address__]
regex: "(.*):(.*)"
replacement: "${1}"
target_label: 'ip'
action: replace
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus
namespace: openobserve
spec:
selector:
matchLabels:
app: prometheus
template:
metadata:
labels:
app: prometheus
spec:
containers:
- args:
- --config.file=/etc/prometheus/prometheus.yaml
- --storage.tsdb.path=/prometheus
- --storage.tsdb.retention.time=4h
- --web.enable-lifecycle
image: prom/prometheus:v2.44.0
imagePullPolicy: IfNotPresent
name: prometheus
ports:
- containerPort: 9090
name: http
protocol: TCP
securityContext:
runAsUser: 0
volumeMounts:
- mountPath: /etc/prometheus
name: config-volume
- mountPath: /prometheus
name: data
volumes:
- name: data
emptyDir: {}
- configMap:
defaultMode: 420
name: prometheus-config
name: config-volume
---
apiVersion: v1
kind: Service
metadata:
name: prometheus
namespace: openobserve
spec:
ports:
- name: http
port: 9090
targetPort: 9090
selector:
app: prometheus
type: NodePort上面的資源清單文件中,我們使用了 Prometheus 的 remote_write 配置項來將數(shù)據(jù)遠程寫入到 OpenObserve 中,其中 url 參數(shù)指定了遠程寫入接口地址,username 和 password 參數(shù)指定了管理員的郵箱和密碼。
直接應用上面的資源清單文件即可:
$ kubectl apply -f prometheus.yaml
$ kubectl get pods -n openobserve
NAME READY STATUS RESTARTS AGE
openobserve-0 1/1 Running 0 2d18h
prometheus-756c8c78f5-kvvbl 1/1 Running 0 20s
$ kubectl get svc -n openobserve
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
openobserve ClusterIP None <none> 5080/TCP 2d18h
prometheus NodePort 10.107.32.131 <none> 9090:31019/TCP 37s部署后我們依然可以使用 Prometheus 的 UI 界面來查看指標數(shù)據(jù):

Prometheus UI
正常現(xiàn)在 Prometheus 的指標數(shù)據(jù)就會被寫入到 OpenObserve 中了,我們可以在 OpenObserve 的 UI 界面中查看該指標流:

Prometheus stream
可以看到 OpenObserve 中是將每個指標看成一個獨立的 stream 流來進行管理的,這樣無疑大大增加了對指標數(shù)據(jù)管理的靈活性,但要想針對某個 job 進行管理難度也就大大增加了?,F(xiàn)在我們就可以在 OpenObserve 的 UI 界面中查看指標數(shù)據(jù)了,比如查詢 node_load5 指標:

load5
可以和 Prometheus 中的查詢結(jié)果進行對比:

load5
從圖形中可以看到 OpenObserve 的查詢結(jié)果和 Prometheus 的查詢結(jié)果是一致的。但是目前 OpenObserve 的 UI 界面中支持的 promql 語法還比較有限,比如不支持向量運算等操作(本周發(fā)布的版本即將支持了)。

不支持
此外我們也可以使用 SQL 語法來查詢指標數(shù)據(jù),比如查詢 node_load5 指標:

sql
除了使用 Prometheus 的遠程寫入方式之外,OpenObserve 還支持通過 OpenTelemetry Collector(后面會講解)來寫入指標數(shù)據(jù),只需要在 exporters 中配置 prometheusremotewrite 即可,如下所示配置:
exporters:
prometheusremotewrite:
endpoint: "http://<oo-url>/api/org_name/prometheus/api/v1/write"
headers:
Authorization: Basic base64_encoded_data_of(userid:password)在指標頁面查詢數(shù)據(jù)的時候我們還可以將查詢結(jié)果保存為 Dashboard:

Add Dashboard
在 Dashboard 里面還可以添加變量,比如我們這里添加一個變量 instance:

Add var
然后編輯 Panel,將查詢語句中的 instance 替換成 $instance:

編輯面板
然后就可以在 Dashboard 中選擇不同的 instance 來查看不同的指標數(shù)據(jù)了:

篩選節(jié)點
鏈路追蹤
OpenObserve 除了支持日志和指標之外,還支持鏈路追蹤,OpenObserve 遵循 OpenTelemetry 的追蹤標準,我們可以使用通過 OpenTelemetry SDK 檢測的代碼將 Trace 數(shù)據(jù)發(fā)送到 OpenObserve,或通過自動檢測將跟蹤發(fā)送到 OpenObserve。
下面是一個集成 OpenTelemetry SDK 的 Python 示例,代碼位于 git clone https://github.com/openobserve/sample-tracing-python:
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
# Service name is required for most backends
resource = Resource(attributes={
SERVICE_NAME: "python-service"
})
# create a tracer provider
tracer_provider = TracerProvider(resource=resource)
# create an OTLP trace exporter
url = 'HTTP_Endpoint'
headers = {"Authorization": "Authorization"}
exporter = OTLPSpanExporter(endpoint=url, headers=headers)
# create a span processor to send spans to the exporter
span_processor = BatchSpanProcessor(exporter)
# add the span processor to the tracer provider
tracer_provider.add_span_processor(span_processor)
# set the tracer provider as the global provider
trace.set_tracer_provider(tracer_provider)我們只需要將 HTTP_Endpoint 和 Authorization 替換成 OpenObserve 的地址和管理員的認證信息即可。其中地址為 https://url:5080/api/<orgname>/traces,Authorization 為 Basic base64(userid:password)。然后運行上面的代碼即可將 Trace 數(shù)據(jù)發(fā)送到 OpenObserve 中。
除此之外 OpenObserve 還支持通過 OpenTelemetry Collector 來收集鏈路追蹤數(shù)據(jù),只需要在 OpenTelemetry Collector 的配置文件中配置 otlp 輸出即可,如下所示:
# ......
exporters: # 導出器,用于導出數(shù)據(jù)
jaeger:
endpoint: "jaeger:14250" # 使用 jaeger 容器名稱
tls:
insecure: true # 不使用 TLS
# Data sources: traces, metrics
otlphttp:
traces_endpoint: http://url:5080/api/<orgname>/traces # OpenObserve 的接口地址,這里要用 traces_endpoint
headers:
Authorization: Basic base64(userid:password) # 管理員的認證信息
service: # 服務配置
pipelines: # 管道配置
traces: # 鏈路追蹤配置
receivers: [otlp] # 接收器
exporters: [otlphttp] # 導出器
processors: [batch] # 處理器然后我們在應用中只需要通過 OpenTelemetry SDK 配置 OTLPTraceExporter 地址為 OpenTelemetry Collector 的地址即可,正常鏈路追蹤數(shù)據(jù)就可以被收集到 OpenObserve 中去了。
如下圖所示,我們可以在 OpenObserve 的 UI 界面中查看鏈路追蹤數(shù)據(jù):

traces
點擊某個 Trace 可以查看詳細信息:

trace detail
關(guān)于如何使用 OpenTelemetry 來生成并收集 Traces 數(shù)據(jù)、OpenTelemetry Collector 的部署等知識點我們后面會詳細講解。
報警
OpenObserve 支持兩種警報方式,針對指定流進行評估:
- 計劃報警
- 實時報警
要使用報警我們首先需要創(chuàng)建一個報警模板,當發(fā)送警報通知時使用模板,模板構(gòu)建發(fā)送到目的地的請求正文,例如。對于 slack,可以創(chuàng)建如下模板:
{
"text": "For stream {stream_name} of organization {org_name} alert {alert_name} of type {alert_type} is active"
}在報警頁面先添加一個報警模板,如下所示:

Add Template
發(fā)送通知時,OpenObserve 會將 {stream_name}、{org_name} 等占位符替換為 stream、alert、organization 的實際值??梢栽谀0逯惺褂玫淖兞坑校?/p>
變量 | 值 | 描述 |
stream_name | 流名稱 | 報警創(chuàng)建的流名稱 |
org_name | 組織名 | 組織名稱 |
alert_name | 報警名 | 報警名稱 |
alert_type | 報警類型 | 可以取的值 : real time 或者 scheduled |
然后接下來需要創(chuàng)建一個 Destinations 目的地,用于發(fā)送警報通知,創(chuàng)建后可以在多個報警中使用。
比如我們這里創(chuàng)建一個 Slack 目的地,用于發(fā)送警報通知,如下所示:

Add Destination
最后我們就可以創(chuàng)建一個報警了,如下所示:

Add Alert
這里我們添加了一個計劃報警,當 K8sLogs 日志流在 1 分鐘內(nèi)的日志數(shù)量大于 50 條時就會觸發(fā)報警,然后發(fā)送到 Slack 目的地中,需要注意的是這里查詢語句最終會加上時間范圍進行過濾,比如我們這里的查詢語句在執(zhí)行的時候?qū)嶋H執(zhí)行的語句如下所示:
select count(*) as echocnt FROM 'K8sLogs' WHERE (_timestamp >= 1691488182902275 AND _timestamp < 1691488242902275) LIMIT 100正常情況下我們可以在 Slack 中看到報警信息:

Slack
另外對于實時報警是根據(jù)指定的條件在攝取時進行評估:

實時報警
除此之外 OpenObserve 還支持函數(shù)功能,使用的是 Vector Remap Language (vrl)(https://vector.dev/docs/reference/vrl/) 定義的,可以在數(shù)據(jù)攝取或查詢期間用于幫助實現(xiàn)高級功能,例如增強、遮蔽、日志減少、合規(guī)性等。還有內(nèi)置的查詢函數(shù),如 match_all 和 match_all_ignore_case 等,可用于根據(jù)用戶的流設置或默認設置進行全文搜索,這些高級功能可以關(guān)注后續(xù)文章。



































