前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >02 Jul 2020 使用curl命令给prometheus remote write endpoint发送数据

02 Jul 2020 使用curl命令给prometheus remote write endpoint发送数据

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

remote write adapter

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

代码语言:javascript
复制
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))
}

启动remote write adapter

代码语言:javascript
复制
$ go run remote-write-adapter.go
start remote write http server ...

使用curl命令发送数据

代码语言:javascript
复制
$ cat compressed.data | curl -i --data-binary @- -H "Content-Encoding: compress" http://localhost:1234/receive
HTTP/1.1 200 OK
Date: Thu, 02 Jul 2020 06:49:48 GMT
Content-Length: 0

compressed.data是经过压缩后的指标数据,具体参考:https://prometheus.io/docs/prometheus/latest/storage/#overview

remote write adapter收到数据

代码语言:javascript
复制
$ grn remote-write-adapter.go
start remote write http server ...
time series data = <go_info{cluster="ssli-test", instance="127.0.0.1:9090", job="prometheus", replica="0", version="go1.13.8"}> samples.Value = <1.000000> samples.Timestamp = <1583412460> seconds

LEo at 12:12

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • remote write adapter
  • 启动remote write adapter
  • 使用curl命令发送数据
  • remote write adapter收到数据
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档