OpenObserve 指標(biāo)和鏈路追蹤方面的支持
前面我們講到了 OpenObserve 的基本使用,使用 Fluentd 將日志采集后輸出到了 OpenObserve,此外 OpenObserve 還支持指標(biāo)和鏈路追蹤。
指標(biāo)
OpenObserve 除了支持日志之外,也支持指標(biāo)數(shù)據(jù)的攝取,它支持 Prometheus 的遠(yuǎn)程寫(xiě)入?yún)f(xié)議,這樣我們就可以直接將 Prometheus 的數(shù)據(jù)遠(yuǎn)程寫(xiě)入到 OpenObserve 中了。
下面的資源清單就是一個(gè)簡(jiǎn)單的 Prometheus 示例,我們使用 node_exporter 來(lái)采集節(jié)點(diǎn)的指標(biāo)數(shù)據(jù),然后通過(guò) Prometheus 將其遠(yuǎn)程寫(xiě)入到 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: # 寫(xiě)入到遠(yuǎn)程 OO,url 是遠(yuǎn)程寫(xiě)入接口地址
- 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 抓取指標(biāo)很大,可以加調(diào)整 queue,但是會(huì)提高內(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: # 通過(guò) relabeling 從 __address__ 中提取 IP 信息,為了后面驗(yàn)證 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 配置項(xiàng)來(lái)將數(shù)據(jù)遠(yuǎn)程寫(xiě)入到 OpenObserve 中,其中 url 參數(shù)指定了遠(yuǎn)程寫(xiě)入接口地址,username 和 password 參數(shù)指定了管理員的郵箱和密碼。
直接應(yīng)用上面的資源清單文件即可:
$ 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 界面來(lái)查看指標(biāo)數(shù)據(jù):
Prometheus UI
正?,F(xiàn)在 Prometheus 的指標(biāo)數(shù)據(jù)就會(huì)被寫(xiě)入到 OpenObserve 中了,我們可以在 OpenObserve 的 UI 界面中查看該指標(biāo)流:
Prometheus stream
可以看到 OpenObserve 中是將每個(gè)指標(biāo)看成一個(gè)獨(dú)立的 stream 流來(lái)進(jìn)行管理的,這樣無(wú)疑大大增加了對(duì)指標(biāo)數(shù)據(jù)管理的靈活性,但要想針對(duì)某個(gè) job 進(jìn)行管理難度也就大大增加了?,F(xiàn)在我們就可以在 OpenObserve 的 UI 界面中查看指標(biāo)數(shù)據(jù)了,比如查詢 node_load5 指標(biāo):
load5
可以和 Prometheus 中的查詢結(jié)果進(jìn)行對(duì)比:
load5
從圖形中可以看到 OpenObserve 的查詢結(jié)果和 Prometheus 的查詢結(jié)果是一致的。但是目前 OpenObserve 的 UI 界面中支持的 promql 語(yǔ)法還比較有限,比如不支持向量運(yùn)算等操作(本周發(fā)布的版本即將支持了)。
不支持
此外我們也可以使用 SQL 語(yǔ)法來(lái)查詢指標(biāo)數(shù)據(jù),比如查詢 node_load5
指標(biāo):
sql
除了使用 Prometheus 的遠(yuǎn)程寫(xiě)入方式之外,OpenObserve 還支持通過(guò) OpenTelemetry Collector(后面會(huì)講解)來(lái)寫(xiě)入指標(biāo)數(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)
在指標(biāo)頁(yè)面查詢數(shù)據(jù)的時(shí)候我們還可以將查詢結(jié)果保存為 Dashboard:
Add Dashboard
在 Dashboard 里面還可以添加變量,比如我們這里添加一個(gè)變量 instance:
Add var
然后編輯 Panel,將查詢語(yǔ)句中的 instance 替換成 $instance:
編輯面板
然后就可以在 Dashboard 中選擇不同的 instance 來(lái)查看不同的指標(biāo)數(shù)據(jù)了:
篩選節(jié)點(diǎn)
鏈路追蹤
OpenObserve 除了支持日志和指標(biāo)之外,還支持鏈路追蹤,OpenObserve 遵循 OpenTelemetry 的追蹤標(biāo)準(zhǔn),我們可以使用通過(guò) OpenTelemetry SDK 檢測(cè)的代碼將 Trace 數(shù)據(jù)發(fā)送到 OpenObserve,或通過(guò)自動(dòng)檢測(cè)將跟蹤發(fā)送到 OpenObserve。
下面是一個(gè)集成 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 的地址和管理員的認(rèn)證信息即可。其中地址為 https://url:5080/api/<orgname>/traces,Authorization 為 Basic base64(userid:password)。然后運(yùn)行上面的代碼即可將 Trace 數(shù)據(jù)發(fā)送到 OpenObserve 中。
除此之外 OpenObserve 還支持通過(guò) OpenTelemetry Collector 來(lái)收集鏈路追蹤數(shù)據(jù),只需要在 OpenTelemetry Collector 的配置文件中配置 otlp
輸出即可,如下所示:
# ......
exporters: # 導(dǎo)出器,用于導(dǎo)出數(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) # 管理員的認(rèn)證信息
service: # 服務(wù)配置
pipelines: # 管道配置
traces: # 鏈路追蹤配置
receivers: [otlp] # 接收器
exporters: [otlphttp] # 導(dǎo)出器
processors: [batch] # 處理器
然后我們?cè)趹?yīng)用中只需要通過(guò) OpenTelemetry SDK 配置 OTLPTraceExporter 地址為 OpenTelemetry Collector 的地址即可,正常鏈路追蹤數(shù)據(jù)就可以被收集到 OpenObserve 中去了。
如下圖所示,我們可以在 OpenObserve 的 UI 界面中查看鏈路追蹤數(shù)據(jù):
traces
點(diǎn)擊某個(gè) Trace 可以查看詳細(xì)信息:
trace detail
關(guān)于如何使用 OpenTelemetry 來(lái)生成并收集 Traces 數(shù)據(jù)、OpenTelemetry Collector 的部署等知識(shí)點(diǎn)我們后面會(huì)詳細(xì)講解。
報(bào)警
OpenObserve 支持兩種警報(bào)方式,針對(duì)指定流進(jìn)行評(píng)估:
- 計(jì)劃報(bào)警
- 實(shí)時(shí)報(bào)警
要使用報(bào)警我們首先需要?jiǎng)?chuàng)建一個(gè)報(bào)警模板,當(dāng)發(fā)送警報(bào)通知時(shí)使用模板,模板構(gòu)建發(fā)送到目的地的請(qǐng)求正文,例如。對(duì)于 slack,可以創(chuàng)建如下模板:
{
"text": "For stream {stream_name} of organization {org_name} alert {alert_name} of type {alert_type} is active"
}
在報(bào)警頁(yè)面先添加一個(gè)報(bào)警模板,如下所示:
Add Template
發(fā)送通知時(shí),OpenObserve 會(huì)將 {stream_name}、{org_name} 等占位符替換為 stream、alert、organization 的實(shí)際值。可以在模板中使用的變量有:
變量 | 值 | 描述 |
stream_name | 流名稱 | 報(bào)警創(chuàng)建的流名稱 |
org_name | 組織名 | 組織名稱 |
alert_name | 報(bào)警名 | 報(bào)警名稱 |
alert_type | 報(bào)警類型 | 可以取的值 : real time 或者 scheduled |
然后接下來(lái)需要?jiǎng)?chuàng)建一個(gè) Destinations 目的地,用于發(fā)送警報(bào)通知,創(chuàng)建后可以在多個(gè)報(bào)警中使用。
比如我們這里創(chuàng)建一個(gè) Slack 目的地,用于發(fā)送警報(bào)通知,如下所示:
Add Destination
最后我們就可以創(chuàng)建一個(gè)報(bào)警了,如下所示:
Add Alert
這里我們添加了一個(gè)計(jì)劃報(bào)警,當(dāng) K8sLogs 日志流在 1 分鐘內(nèi)的日志數(shù)量大于 50 條時(shí)就會(huì)觸發(fā)報(bào)警,然后發(fā)送到 Slack 目的地中,需要注意的是這里查詢語(yǔ)句最終會(huì)加上時(shí)間范圍進(jìn)行過(guò)濾,比如我們這里的查詢語(yǔ)句在執(zhí)行的時(shí)候?qū)嶋H執(zhí)行的語(yǔ)句如下所示:
select count(*) as echocnt FROM 'K8sLogs' WHERE (_timestamp >= 1691488182902275 AND _timestamp < 1691488242902275) LIMIT 100
正常情況下我們可以在 Slack 中看到報(bào)警信息:
Slack
另外對(duì)于實(shí)時(shí)報(bào)警是根據(jù)指定的條件在攝取時(shí)進(jìn)行評(píng)估:
實(shí)時(shí)報(bào)警
除此之外 OpenObserve 還支持函數(shù)功能,使用的是 Vector Remap Language (vrl)(https://vector.dev/docs/reference/vrl/) 定義的,可以在數(shù)據(jù)攝取或查詢期間用于幫助實(shí)現(xiàn)高級(jí)功能,例如增強(qiáng)、遮蔽、日志減少、合規(guī)性等。還有內(nèi)置的查詢函數(shù),如 match_all 和 match_all_ignore_case 等,可用于根據(jù)用戶的流設(shè)置或默認(rèn)設(shè)置進(jìn)行全文搜索,這些高級(jí)功能可以關(guān)注后續(xù)文章。