前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >29 Jun 2020 prometheus remote write filter

29 Jun 2020 prometheus remote write filter

作者头像
俊采
发布2023-10-17 10:39:55
2210
发布2023-10-17 10:39:55
举报
文章被收录于专栏:LEo的网络日志

remote write adapter

以下代码是我参考prometheus官方给出的remote write adapter修改的:

代码语言:javascript
复制
// Copyright 2016 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"

	"github.com/gogo/protobuf/proto"
	"github.com/golang/snappy"
	"github.com/prometheus/common/model"

	"github.com/prometheus/prometheus/prompb"
)

func main() {
	fmt.Println("start remote write http server ...")
	http.HandleFunc("/receive", func(w http.ResponseWriter, r *http.Request) {
		compressed, err := ioutil.ReadAll(r.Body)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		reqBuf, err := snappy.Decode(nil, compressed)
		if err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}

		var req prompb.WriteRequest
		if err := proto.Unmarshal(reqBuf, &req); err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}

		for _, ts := range req.Timeseries {
			m := make(model.Metric, len(ts.Labels))
			for _, l := range ts.Labels {
				m[model.LabelName(l.Name)] = model.LabelValue(l.Value)
			}
			fmt.Printf("time series data = <%v> ", m)

			for _, s := range ts.Samples {
				fmt.Printf("samples.Value = <%f> samples.Timestamp = <%d> seconds\n", s.Value, s.Timestamp/1000)
			}
		}
	})

	log.Fatal(http.ListenAndServe(":1234", nil))
}

enable remote write for prometheus

在prometheus的配置文件prometheus.yml中添加以下配置,启用remote write功能。

代码语言:javascript
复制
remote_write:
  - url: "http://192.168.1.105:1234/receive"

这里仅保留go_info和go_goroutines指标数据,完整的prometheus.yml如下:

代码语言:javascript
复制
global:
  scrape_interval:     60s
  evaluation_interval: 15s

remote_write:
  - url: "http://192.168.1.105:1234/receive"
    write_relabel_configs:
        - action: keep
          source_labels:
          - __name__
          regex: go_info|go_goroutines

scrape_configs:
  - job_name: 'ssli-prometheus'
    scrape_interval: 5s
    static_configs:
    - targets: ['192.168.1.105:9090']

启动prometheus

启动prometheus和remote write adapter,查看remote write adapter收到的监控数据。

1 启动prometheus

代码语言:javascript
复制
$ docker run -d --rm -p 9090:9090 -v /Users/ssli/share/git/k8s_practice/prometheus/remote_write/prometheus-remote-write-filter.yml:/etc/prometheus/prometheus.yml prom/prometheus
b4bbb85ae846f533c533c72f16b684e74416e627003c78e0637f4b651ccf1298
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
b4bbb85ae846        prom/prometheus     "/bin/prometheus --c…"   4 seconds ago       Up 3 seconds        0.0.0.0:9090->9090/tcp   brave_beaver

输入 http://localhost:9090/graph 可以看到prometheus的ui界面。

2 启动remote write adapter

remote write adapter会接收到prometheus发送的监控数据,可以看到每隔5s收到2条指标数据,分别是go_info和go_goroutines。其他指标数据都没有被发送到remote write adapter,实现了发送指定指标数据到remote write adapter的功能。

代码语言:javascript
复制
$ go run remote-write-adapter.go
start remote write http server ...
time series data = <go_goroutines{instance="192.168.1.105:9090", job="ssli-prometheus"}> samples.Value = <47.000000> samples.Timestamp = <1593428516> seconds
time series data = <go_info{instance="192.168.1.105:9090", job="ssli-prometheus", version="go1.14.4"}> samples.Value = <1.000000> samples.Timestamp = <1593428516> seconds
time series data = <go_goroutines{instance="192.168.1.105:9090", job="ssli-prometheus"}> samples.Value = <45.000000> samples.Timestamp = <1593428521> seconds
time series data = <go_info{instance="192.168.1.105:9090", job="ssli-prometheus", version="go1.14.4"}> samples.Value = <1.000000> samples.Timestamp = <1593428521> seconds
time series data = <go_goroutines{instance="192.168.1.105:9090", job="ssli-prometheus"}> samples.Value = <47.000000> samples.Timestamp = <1593428526> seconds
time series data = <go_info{instance="192.168.1.105:9090", job="ssli-prometheus", version="go1.14.4"}> samples.Value = <1.000000> samples.Timestamp = <1593428526> seconds

...

如果你配置多个job,可以通过一下方式实现只发送指定job的指定指标数据到remote write adapter:

代码语言:javascript
复制
global:
  scrape_interval:     60s
  evaluation_interval: 15s

remote_write:
  - url: "http://192.168.1.105:1234/receive"
    write_relabel_configs:
        - action: keep
          source_labels:
          - __name__
          regex: go_info|go_goroutines
        - action: keep
          source_labels:
          - job
          regex: ssli-prometheus

scrape_configs:
  - job_name: 'ssli-prometheus'
    scrape_interval: 5s
    static_configs:
    - targets: ['192.168.1.105:9090']

  - job_name: 'new-job'
    scrape_interval: 5s
    static_configs:
    - targets: ['192.168.1.105:9090']

使用以上配置,prometheus只会将ssli-prometheus的go_info和go_goroutines指标数据发送到remote write adapter。而忽略new-job采集的指标数据。

若需要往发送的数据上添加一些自定义的label,可以在write_relabel_configs配置相应的replacement,例如以下配置会在发送的数据中添加owner=”ssli”标签数据:

代码语言:javascript
复制
global:
  scrape_interval:     60s
  evaluation_interval: 15s

remote_write:
  - url: "http://192.168.1.105:1234/receive"
    write_relabel_configs:
        - replacement: ssli
          source_labels:
          - __name__
          target_label: owner
        - action: keep
          source_labels:
          - __name__
          regex: go_info

scrape_configs:
  - job_name: 'ssli-prometheus'
    scrape_interval: 5s
    static_configs:
    - targets: ['192.168.1.105:9090']

remote write adapter收到的数据如下:

代码语言:javascript
复制
time series data = <go_info{instance="192.168.1.105:9090", job="ssli-prometheus", owner="ssli", version="go1.14.4"}> samples.Value = <1.000000> samples.Timestamp = <1593429888> seconds
time series data = <go_info{instance="192.168.1.105:9090", job="ssli-prometheus", owner="ssli", version="go1.14.4"}> samples.Value = <1.000000> samples.Timestamp = <1593429893> seconds
time series data = <go_info{instance="192.168.1.105:9090", job="ssli-prometheus", owner="ssli", version="go1.14.4"}> samples.Value = <1.000000> samples.Timestamp = <1593429898> seconds

ref

  • https://prometheus.io/docs/prometheus/latest/configuration/configuration/#remote_write
  • https://raw.githubusercontent.com/prometheus/prometheus/master/documentation/examples/remote_storage/example_write_adapter/server.go

LEo at 12:12

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • remote write adapter
  • enable remote write for prometheus
  • 启动prometheus
  • ref
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档