【大數據運維監控】使用CortexAPI 實現Cortex的基本操作

在第一次接觸這個 Cortex 的時候,必須承認,筆者在網上能找到的資料甚少,逼着筆者一點一點的看官網,真的是受不了了。這裏,筆者重點講一下,官網API中的筆者遇到的一些坑,以及我們去使用這些API。 目前筆者在網上找到的資料基本都是介紹 Cortex 和 Prometheus 的多租戶,要麼是講原理,而且原理還是官網上的。所以這裏簡單的去介紹下Cortex 裏面幾個API。

官網上的 API 介紹

在官網上,提供了一下幾種 API Remote API / Alerts & Rules API / Configs API / Endpoints / Manage Rules . 這裏呢,我只說這裏的 RemoteAPI .

我們先看一下這個 Remote API :

12.png

寫的很詳細,目前是算比較詳細的,筆者在一開始看的時候就幾行,第一行:支持Prometheus 的 remote_read / remote_write , 第二行 , 對於Http post 裏面的請求體首先是 Protocol Buffers , 再來一個 snappy . 第三行是 遠程讀寫 api . 目前來看是又修改了下。而且我們也能看到,當前的這個 Cortex 的APIS 正在編寫中。

Remote API

這裏的 remote api 官網講了兩個,這兩個對應着的是 Prometheus 中的 remote_write / remote_read . 這裏對這兩個做個簡單的介紹,筆者這裏也主要用到了的 remote_write .

remote_write

剛剛說了,這裏的 remote_write 對應着的是 Prometheus 裏面的那個 remote_write .所以呢,我們現在需要到 Prometheus 裏面看看這個remote_write 的基本配置是什麼樣的。

我們進入到Prometheus 的官網,去看一下。 我這裏不貼全部的,全部的大家去官網查看就好:

# The URL of the endpoint to send samples to.
url: <string>

# Timeout for requests to the remote write endpoint.
[ remote_timeout: <duration> | default = 30s ]

# List of remote write relabel configurations.
write_relabel_configs:
  [ - <relabel_config> ... ]

# Sets the `Authorization` header on every remote write request with the
# configured username and password.
# password and password_file are mutually exclusive.
basic_auth:
  [ username: <string> ]
  [ password: <string> ]
  [ password_file: <string> ]

# Sets the `Authorization` header on every remote write request with
# the configured bearer token. It is mutually exclusive with `bearer_token_file`.
[ bearer_token: <string> ]

# Sets the `Authorization` header on every remote write request with the bearer token
# read from the configured file. It is mutually exclusive with `bearer_token`.
[ bearer_token_file: /path/to/bearer/token/file ]

# Configures the remote write request's TLS settings.
tls_config:
  [ <tls_config> ]

# Optional proxy URL.
[ proxy_url: <string> ]

這裏的配置,寫的很清楚,這裏筆者用到的主要是兩個配置,一個是 url , 一個是 basic_auth ,大家可以參考我之前的一個文章使用Cortex實現Prometheus的多租戶管理 . 很簡單的一個remote_write 。

隱藏的 API

如果你以爲,我這就完了,那你就錯了。我這裏講的是 Cortex 裏面的查詢的 APIs , 這裏的幾個API 目前官網我還沒看到,也可能是因爲我沒找到吧。

這裏呢,沒必要去跟大家介紹每一個API。大家看一下這個API接口就清楚了:

// API provides bindings for Prometheus's v1 API.
type API interface {
	// Alerts returns a list of all active alerts.
	Alerts(ctx context.Context) (AlertsResult, error)
	// AlertManagers returns an overview of the current state of the Prometheus alert manager discovery.
	AlertManagers(ctx context.Context) (AlertManagersResult, error)
	// CleanTombstones removes the deleted data from disk and cleans up the existing tombstones.
	CleanTombstones(ctx context.Context) error
	// Config returns the current Prometheus configuration.
	Config(ctx context.Context) (ConfigResult, error)
	// DeleteSeries deletes data for a selection of series in a time range.
	DeleteSeries(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) error
	// Flags returns the flag values that Prometheus was launched with.
	Flags(ctx context.Context) (FlagsResult, error)
	// LabelNames returns all the unique label names present in the block in sorted order.
	LabelNames(ctx context.Context) ([]string, api.Warnings, error)
	// LabelValues performs a query for the values of the given label.
	LabelValues(ctx context.Context, label string) (model.LabelValues, api.Warnings, error)
	// Query performs a query for the given time.
	Query(ctx context.Context, query string, ts time.Time) (model.Value, api.Warnings, error)
	// QueryRange performs a query for the given range.
	QueryRange(ctx context.Context, query string, r Range) (model.Value, api.Warnings, error)
	// Series finds series by label matchers.
	Series(ctx context.Context, matches []string, startTime time.Time, endTime time.Time) ([]model.LabelSet, api.Warnings, error)
	// Snapshot creates a snapshot of all current data into snapshots/<datetime>-<rand>
	// under the TSDB's data directory and returns the directory as response.
	Snapshot(ctx context.Context, skipHead bool) (SnapshotResult, error)
	// Rules returns a list of alerting and recording rules that are currently loaded.
	Rules(ctx context.Context) (RulesResult, error)
	// Targets returns an overview of the current state of the Prometheus target discovery.
	Targets(ctx context.Context) (TargetsResult, error)
	// TargetsMetadata returns metadata about metrics currently scraped by the target.
	TargetsMetadata(ctx context.Context, matchTarget string, metric string, limit string) ([]MetricMetadata, error)
}

看到這個是不是就很好理解了?

我們在使用的時候,首先需要先知道一個結構體:

type Client struct {
	alertmanagerClient promapi.Client
	distributorAddress string
	timeout            time.Duration
	httpClient         *http.Client
	querierClient      promv1.API
	orgID              string
}

可以看到,在這個 Client 裏面,有幾個client , alertmanagerClient / httpClient / querierClient . 先不要好奇爲什麼會有這麼多的 client . 這裏的每一個 client 對應着的都不一樣。這裏會涉及到Cortex 的微服務的架構問題,這個我們後期再說。我這裏先說怎麼使用 API 做查詢。 以及我們的 Push 。

Query

func NewClient(distributorAddress string, querierAddress string, alertmanagerAddress string, orgID string) (*Client, error) {
	// Create querier API client
	querierAPIClient, err := promapi.NewClient(promapi.Config{
		Address:      "http://" + querierAddress + "/api/prom",
		RoundTripper: &addOrgIDRoundTripper{orgID: orgID, next: http.DefaultTransport},
	})
	if err != nil {
		return nil, err
	}

	c := &Client{
		distributorAddress: distributorAddress,
		timeout:            5 * time.Second,
		httpClient:         &http.Client{},
		querierClient:      promv1.NewAPI(querierAPIClient),
		orgID:              orgID,
	}
	return c, nil
}

我們先得到一個 client , 這裏的 promv1 是包 “github.com/prometheus/client_golang/api/prometheus/v1” .

// Query runs a query
func (c *Client) Query(query string, ts time.Time) (model.Value, error) {
	value, _, err := c.querierClient.Query(context.Background(), query, ts)
	return value, err
}

這樣就好了。

Push

這裏的 Push ,就是發送數據到 Cortex 裏面的。Client 用我們之前拿到的那個 Client

// Push the input timeseries to the remote endpoint
func (c *Client) Push(timeseries []prompb.TimeSeries) (*http.Response, error) {
	// Create write request
	data, err := proto.Marshal(&prompb.WriteRequest{Timeseries: timeseries})
	if err != nil {
		return nil, err
	}

	// Create HTTP request
	compressed := snappy.Encode(nil, data)
	req, err := http.NewRequest("POST", fmt.Sprintf("http://%s/api/prom/push", c.distributorAddress), bytes.NewReader(compressed))
	if err != nil {
		return nil, err
	}

	req.Header.Add("Content-Encoding", "snappy")
	req.Header.Set("Content-Type", "application/x-protobuf")
	req.Header.Set("X-Prometheus-Remote-Write-Version", "0.1.0")
	req.Header.Set("X-Scope-OrgID", c.orgID)

	ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
	defer cancel()

	// Execute HTTP request
	res, err := c.httpClient.Do(req.WithContext(ctx))
	if err != nil {
		return nil, err
	}

	defer res.Body.Close()
	return res, nil
}

其實就是一個客戶端在往服務器裏面寫數據的過程。

總結

當然了,上面的程序都是有的。大家可以到 cortex 的源程序裏面找到 , github 的速度很慢,大家可以到 這個地方去看 cortex

大家可以克隆到自己本地:

git clone https://gitee.com/sun-iot/cortex.git

在裏面我們可以找到這個 Cortex 的幾個隱藏的 API。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章