当版本号是v2以及以上版本的,可以用go get带版本号在terminal里进行下载,它会提供另一个地址,这样就可以下载下来了。
比如:
// 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 (
"encoding/json"
"flag"
"github.com/Shopify/sarama"
"github.com/prometheus/prometheus/storage/remote"
"log"
"net/http"
"strconv"
"time"
)
func main() {
kafkaServerName := flag.String("kafka-server-name", "aiops01", "kafka的服务ip或者别称,默认为aiops01")
kafkaServerPort := flag.Int64("kafka-server-port", 6667, "Kafka的端口号,默认为6667")
receiveUrl := flag.String("receive_url", "/receive", "服务的接收路径,默认为/receive")
serverPort := flag.String("server-port", "1234", "服务的监听端口,默认为1234")
topic := flag.String("topic", "metrics", "topic的名称")
//参数转换
flag.Parse()
config := sarama.NewConfig() //实例化个sarama的Config
config.Producer.Return.Successes = true //是否开启消息发送成功后通知 successes channel
config.Producer.Partitioner = sarama.NewRandomPartitioner //随机分区器
client,err := sarama.NewClient([]string{*kafkaServerName + ":" + strconv.Itoa(int(*kafkaServerPort))}, config) //初始化客户端
if err != nil{
panic(err.Error())
}
defer func(client sarama.Client) {
err := client.Close()
if err != nil {
panic(err.Error())
}
}(client)
producer,err := sarama.NewSyncProducerFromClient(client)
if err != nil{
panic(err.Error())
}
http.HandleFunc(*receiveUrl, func(w http.ResponseWriter, r *http.Request) {
req, err := remote.DecodeWriteRequest(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
for _, ts := range req.Timeseries {
m := make(map[string]interface{}, len(ts.Labels))
for _, l := range ts.Labels {
m[l.Name] = l.Value
}
//log.Println(m)
for _, s := range ts.Samples {
m["value"] = s.Value
m["Timestamp"] = s.Timestamp
var timestampStr = time.Unix(s.Timestamp,0).Format("2006-01-02 15:04:05")
bytes, _ := json.Marshal(&m)
//log.Println(string(bytes))
if string(bytes) != ""{
//partition, offset , err := producer.SendMessage(&sarama.ProducerMessage{Topic: *topic, Key: sarama.StringEncoder(timestampStr), Value: sarama.StringEncoder(string(bytes))})
_, _ , err := producer.SendMessage(&sarama.ProducerMessage{Topic: *topic, Key: sarama.StringEncoder(timestampStr), Value: sarama.StringEncoder(string(bytes))})
if err != nil {
log.Fatalf("unable to produce message: %q", err)
}
//fmt.Println("partition",partition)
//fmt.Println("offset",offset)
}
}
//for _, e := range ts.Exemplars {
// m := make(model.Metric, len(e.Labels))
// for _, l := range e.Labels {
// m[model.LabelName(l.Name)] = model.LabelValue(l.Value)
// }
// fmt.Printf("\tExemplar: %+v %f %d\n", m, e.Value, e.Timestamp)
//}
}
})
log.Fatal(http.ListenAndServe(":" + *serverPort, nil))
}
github.com/prometheus/prometheus/storage/remote:
可以进行
go get github.com/prometheus/prometheus@v2.30.0
tlzs@wangtianlongdeMacBook-Pro KafkaAdapter % go get github.com/prometheus/prometheus@v2.30.0
go: downloading github.com/prometheus/prometheus v0.0.0-20210914090109-37468d88dce8
go get: added github.com/prometheus/prometheus v0.0.0-20210914090109-37468d88dce8
它会在go.mod里填入一个地址,这个地址是可以下载的。