|
@@ -227,161 +227,162 @@ func main() {
|
|
|
//profiling.Start()
|
|
//profiling.Start()
|
|
|
//defer profiling.Stop()
|
|
//defer profiling.Stop()
|
|
|
// 创建一个/metrics路由处理函数
|
|
// 创建一个/metrics路由处理函数
|
|
|
- sendNetDataFunc := func() {
|
|
|
|
|
- // 从注册表中获取指标数据
|
|
|
|
|
- metrics, err := registry.Gather()
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- // 错误处理
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 创建正则表达式对象
|
|
|
|
|
- // regex, err := regexp.Compile(`^process_.+_queries_total$`)
|
|
|
|
|
- // if err != nil {
|
|
|
|
|
- // return
|
|
|
|
|
- // }
|
|
|
|
|
-
|
|
|
|
|
- var postData PostData
|
|
|
|
|
- postData.AccountID = strconv.Itoa(nodeInfo.AccountID)
|
|
|
|
|
- postData.IP = nodeInfo.HostIp
|
|
|
|
|
- postData.HostID = nodeInfo.HostID
|
|
|
|
|
- postData.TimeStamp = uint64(time.Now().UnixNano())
|
|
|
|
|
- postData.ServiceType = 30002
|
|
|
|
|
- postData.HostName = nodeInfo.Hostname
|
|
|
|
|
- for _, metric := range metrics {
|
|
|
|
|
- if metric.GetName() != "process_net_tcp_successful_connects_total" &&
|
|
|
|
|
- metric.GetName() != "process_net_tcp_failed_connects_total" &&
|
|
|
|
|
- metric.GetName() != "process_net_tcp_retransmits_total" &&
|
|
|
|
|
- metric.GetName() != "process_net_tcp_listen_info" &&
|
|
|
|
|
- metric.GetName() != "process_http_requests_total" &&
|
|
|
|
|
- metric.GetName() != "process_http_requests_duration_seconds_total" &&
|
|
|
|
|
- metric.GetName() != "process_http_requests_duration_seconds_total_count" &&
|
|
|
|
|
- metric.GetName() != "process_mysql_queries_total" &&
|
|
|
|
|
- metric.GetName() != "process_mysql_queries_duration_seconds_total" &&
|
|
|
|
|
- // metric.GetName() != "process_mysql_queries_duration_seconds_total_count" &&
|
|
|
|
|
- metric.GetName() != "process_redis_queries_total" &&
|
|
|
|
|
- metric.GetName() != "process_redis_queries_duration_seconds_total" &&
|
|
|
|
|
- // metric.GetName() != "process_redis_queries_duration_seconds_total_count" &&
|
|
|
|
|
- metric.GetName() != "process_postgres_queries_total" &&
|
|
|
|
|
- metric.GetName() != "process_postgres_queries_duration_seconds_total" &&
|
|
|
|
|
- // metric.GetName() != "process_postgres_queries_duration_seconds_total_count" &&
|
|
|
|
|
- // regex.MatchString(metric.GetName()) == false &&
|
|
|
|
|
- metric.GetName() != "process_dm_queries_total" &&
|
|
|
|
|
- metric.GetName() != "process_dm_queries_duration_seconds_total" &&
|
|
|
|
|
- metric.GetName() != "process_application_type" &&
|
|
|
|
|
- metric.GetName() != "process_net_tcp_bytes_received_per" &&
|
|
|
|
|
- metric.GetName() != "process_net_tcp_bytes_sent_per" &&
|
|
|
|
|
- metric.GetName() != "process_net_tcp_bytes_received_total" &&
|
|
|
|
|
- metric.GetName() != "process_net_tcp_bytes_sent_total" &&
|
|
|
|
|
- metric.GetName() != "process_net_tcp_data_latency_time" &&
|
|
|
|
|
- metric.GetName() != "process_net_tcp_flow_duration_time" &&
|
|
|
|
|
- metric.GetName() != "process_net_tcp_connection_establish_time" {
|
|
|
|
|
- continue
|
|
|
|
|
|
|
+ sendNetDataDone := make(chan struct{})
|
|
|
|
|
+ if *flags.SendNetData {
|
|
|
|
|
+ sendNetDataFunc := func() {
|
|
|
|
|
+ // 从注册表中获取指标数据
|
|
|
|
|
+ metrics, err := registry.Gather()
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ // 错误处理
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- var item MetricData
|
|
|
|
|
- var itemOther MetricData
|
|
|
|
|
- item.MetricKey = metric.GetName()
|
|
|
|
|
-
|
|
|
|
|
- for _, m := range metric.GetMetric() {
|
|
|
|
|
- metricItem := MetricItemData{}
|
|
|
|
|
- label := make(map[string]string)
|
|
|
|
|
- for _, l := range m.GetLabel() {
|
|
|
|
|
- label[l.GetName()] = l.GetValue()
|
|
|
|
|
|
|
+ // 创建正则表达式对象
|
|
|
|
|
+ // regex, err := regexp.Compile(`^process_.+_queries_total$`)
|
|
|
|
|
+ // if err != nil {
|
|
|
|
|
+ // return
|
|
|
|
|
+ // }
|
|
|
|
|
+
|
|
|
|
|
+ var postData PostData
|
|
|
|
|
+ postData.AccountID = strconv.Itoa(nodeInfo.AccountID)
|
|
|
|
|
+ postData.IP = nodeInfo.HostIp
|
|
|
|
|
+ postData.HostID = nodeInfo.HostID
|
|
|
|
|
+ postData.TimeStamp = uint64(time.Now().UnixNano())
|
|
|
|
|
+ postData.ServiceType = 30002
|
|
|
|
|
+ postData.HostName = nodeInfo.Hostname
|
|
|
|
|
+ for _, metric := range metrics {
|
|
|
|
|
+ if metric.GetName() != "process_net_tcp_successful_connects_total" &&
|
|
|
|
|
+ metric.GetName() != "process_net_tcp_failed_connects_total" &&
|
|
|
|
|
+ metric.GetName() != "process_net_tcp_retransmits_total" &&
|
|
|
|
|
+ metric.GetName() != "process_net_tcp_listen_info" &&
|
|
|
|
|
+ metric.GetName() != "process_http_requests_total" &&
|
|
|
|
|
+ metric.GetName() != "process_http_requests_duration_seconds_total" &&
|
|
|
|
|
+ metric.GetName() != "process_http_requests_duration_seconds_total_count" &&
|
|
|
|
|
+ metric.GetName() != "process_mysql_queries_total" &&
|
|
|
|
|
+ metric.GetName() != "process_mysql_queries_duration_seconds_total" &&
|
|
|
|
|
+ // metric.GetName() != "process_mysql_queries_duration_seconds_total_count" &&
|
|
|
|
|
+ metric.GetName() != "process_redis_queries_total" &&
|
|
|
|
|
+ metric.GetName() != "process_redis_queries_duration_seconds_total" &&
|
|
|
|
|
+ // metric.GetName() != "process_redis_queries_duration_seconds_total_count" &&
|
|
|
|
|
+ metric.GetName() != "process_postgres_queries_total" &&
|
|
|
|
|
+ metric.GetName() != "process_postgres_queries_duration_seconds_total" &&
|
|
|
|
|
+ // metric.GetName() != "process_postgres_queries_duration_seconds_total_count" &&
|
|
|
|
|
+ // regex.MatchString(metric.GetName()) == false &&
|
|
|
|
|
+ metric.GetName() != "process_dm_queries_total" &&
|
|
|
|
|
+ metric.GetName() != "process_dm_queries_duration_seconds_total" &&
|
|
|
|
|
+ metric.GetName() != "process_application_type" &&
|
|
|
|
|
+ metric.GetName() != "process_net_tcp_bytes_received_per" &&
|
|
|
|
|
+ metric.GetName() != "process_net_tcp_bytes_sent_per" &&
|
|
|
|
|
+ metric.GetName() != "process_net_tcp_bytes_received_total" &&
|
|
|
|
|
+ metric.GetName() != "process_net_tcp_bytes_sent_total" &&
|
|
|
|
|
+ metric.GetName() != "process_net_tcp_data_latency_time" &&
|
|
|
|
|
+ metric.GetName() != "process_net_tcp_flow_duration_time" &&
|
|
|
|
|
+ metric.GetName() != "process_net_tcp_connection_establish_time" {
|
|
|
|
|
+ continue
|
|
|
}
|
|
}
|
|
|
- metricItem.Label = label
|
|
|
|
|
- switch metric.GetType() {
|
|
|
|
|
- case dto.MetricType_COUNTER:
|
|
|
|
|
- metricItem.Value = m.GetCounter().GetValue()
|
|
|
|
|
- item.Metric = append(item.Metric, metricItem)
|
|
|
|
|
- case dto.MetricType_GAUGE:
|
|
|
|
|
- metricItem.Value = m.GetGauge().GetValue()
|
|
|
|
|
- item.Metric = append(item.Metric, metricItem)
|
|
|
|
|
- case dto.MetricType_HISTOGRAM:
|
|
|
|
|
- item.MetricKey = metric.GetName() + "_sum"
|
|
|
|
|
- metricItem.Value = m.GetHistogram().GetSampleSum()
|
|
|
|
|
- item.Metric = append(item.Metric, metricItem)
|
|
|
|
|
- metricItemOther := MetricItemData{}
|
|
|
|
|
- metricItemOther.Label = label
|
|
|
|
|
|
|
|
|
|
- itemOther.MetricKey = metric.GetName() + "_count"
|
|
|
|
|
- metricItemOther.Value = m.GetHistogram().GetSampleCount()
|
|
|
|
|
- itemOther.Metric = append(itemOther.Metric, metricItemOther)
|
|
|
|
|
- default:
|
|
|
|
|
- continue
|
|
|
|
|
|
|
+ var item MetricData
|
|
|
|
|
+ var itemOther MetricData
|
|
|
|
|
+ item.MetricKey = metric.GetName()
|
|
|
|
|
+
|
|
|
|
|
+ for _, m := range metric.GetMetric() {
|
|
|
|
|
+ metricItem := MetricItemData{}
|
|
|
|
|
+ label := make(map[string]string)
|
|
|
|
|
+ for _, l := range m.GetLabel() {
|
|
|
|
|
+ label[l.GetName()] = l.GetValue()
|
|
|
|
|
+ }
|
|
|
|
|
+ metricItem.Label = label
|
|
|
|
|
+ switch metric.GetType() {
|
|
|
|
|
+ case dto.MetricType_COUNTER:
|
|
|
|
|
+ metricItem.Value = m.GetCounter().GetValue()
|
|
|
|
|
+ item.Metric = append(item.Metric, metricItem)
|
|
|
|
|
+ case dto.MetricType_GAUGE:
|
|
|
|
|
+ metricItem.Value = m.GetGauge().GetValue()
|
|
|
|
|
+ item.Metric = append(item.Metric, metricItem)
|
|
|
|
|
+ case dto.MetricType_HISTOGRAM:
|
|
|
|
|
+ item.MetricKey = metric.GetName() + "_sum"
|
|
|
|
|
+ metricItem.Value = m.GetHistogram().GetSampleSum()
|
|
|
|
|
+ item.Metric = append(item.Metric, metricItem)
|
|
|
|
|
+ metricItemOther := MetricItemData{}
|
|
|
|
|
+ metricItemOther.Label = label
|
|
|
|
|
+
|
|
|
|
|
+ itemOther.MetricKey = metric.GetName() + "_count"
|
|
|
|
|
+ metricItemOther.Value = m.GetHistogram().GetSampleCount()
|
|
|
|
|
+ itemOther.Metric = append(itemOther.Metric, metricItemOther)
|
|
|
|
|
+ default:
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ postData.Data = append(postData.Data, item)
|
|
|
|
|
+ if metric.GetType() == dto.MetricType_HISTOGRAM {
|
|
|
|
|
+ postData.Data = append(postData.Data, itemOther)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- postData.Data = append(postData.Data, item)
|
|
|
|
|
- if metric.GetType() == dto.MetricType_HISTOGRAM {
|
|
|
|
|
- postData.Data = append(postData.Data, itemOther)
|
|
|
|
|
|
|
+ // 将指标数据转换为JSON格式
|
|
|
|
|
+ jsonData, err := json.Marshal(postData)
|
|
|
|
|
+ //jsonData, err := json.Marshal(metrics)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ // log.Debugln("netdata is:", string(jsonData))
|
|
|
|
|
+ // 创建请求
|
|
|
|
|
+ urlRoute := "/api/v2/ebpf/receive"
|
|
|
|
|
+
|
|
|
|
|
+ log.Infoln("send url is ", *flags.DataServer+*flags.ServerPrefix+urlRoute)
|
|
|
|
|
+ // req, err := http.NewRequest("POST", "http://10.0.7.115:18080/api/v2/ebpf/receive", bytes.NewBuffer(jsonData))
|
|
|
|
|
+ req, err := http.NewRequest("POST", *flags.DataServer+*flags.ServerPrefix+urlRoute, bytes.NewBuffer(jsonData))
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ log.Errorf(err.Error())
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
- // 将指标数据转换为JSON格式
|
|
|
|
|
- jsonData, err := json.Marshal(postData)
|
|
|
|
|
- //jsonData, err := json.Marshal(metrics)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
- // log.Debugln("netdata is:", string(jsonData))
|
|
|
|
|
- // 创建请求
|
|
|
|
|
- urlRoute := "/api/v2/ebpf/receive"
|
|
|
|
|
|
|
|
|
|
- log.Infoln("send url is ", *flags.DataServer+*flags.ServerPrefix+urlRoute)
|
|
|
|
|
- // req, err := http.NewRequest("POST", "http://10.0.7.115:18080/api/v2/ebpf/receive", bytes.NewBuffer(jsonData))
|
|
|
|
|
- req, err := http.NewRequest("POST", *flags.DataServer+*flags.ServerPrefix+urlRoute, bytes.NewBuffer(jsonData))
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- log.Errorf(err.Error())
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // 添加 Content-Type header
|
|
|
|
|
+ req.Header.Add("Content-Type", "application/json")
|
|
|
|
|
|
|
|
- // 添加 Content-Type header
|
|
|
|
|
- req.Header.Add("Content-Type", "application/json")
|
|
|
|
|
|
|
+ // 添加一个自定义 header
|
|
|
|
|
+ req.Header.Add("DataCount", strconv.Itoa(len(postData.Data)))
|
|
|
|
|
+ req.Header.Add("Account-Id", strconv.Itoa(nodeInfo.AccountID))
|
|
|
|
|
+ req.Header.Add("ip", nodeInfo.HostIp)
|
|
|
|
|
|
|
|
- // 添加一个自定义 header
|
|
|
|
|
- req.Header.Add("DataCount", strconv.Itoa(len(postData.Data)))
|
|
|
|
|
- req.Header.Add("Account-Id", strconv.Itoa(nodeInfo.AccountID))
|
|
|
|
|
- req.Header.Add("ip", nodeInfo.HostIp)
|
|
|
|
|
|
|
+ // 创建 HTTP 客户端
|
|
|
|
|
+ client := &http.Client{}
|
|
|
|
|
|
|
|
- // 创建 HTTP 客户端
|
|
|
|
|
- client := &http.Client{}
|
|
|
|
|
|
|
+ // 发送 HTTP POST 请求
|
|
|
|
|
+ response, err := client.Do(req)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ log.Errorf(err.Error())
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ defer response.Body.Close()
|
|
|
|
|
|
|
|
- // 发送 HTTP POST 请求
|
|
|
|
|
- response, err := client.Do(req)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- log.Errorf(err.Error())
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
- defer response.Body.Close()
|
|
|
|
|
|
|
+ // 读取响应内容
|
|
|
|
|
+ responseData, err := io.ReadAll(response.Body)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ log.Infoln("Error:", err)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // 读取响应内容
|
|
|
|
|
- responseData, err := io.ReadAll(response.Body)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- log.Infoln("Error:", err)
|
|
|
|
|
- return
|
|
|
|
|
|
|
+ // 输出响应状态码和响应正文
|
|
|
|
|
+ log.Infoln("Status Code:", response.StatusCode)
|
|
|
|
|
+ log.Infoln("Response Body:", string(responseData))
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- // 输出响应状态码和响应正文
|
|
|
|
|
- log.Infoln("Status Code:", response.StatusCode)
|
|
|
|
|
- log.Infoln("Response Body:", string(responseData))
|
|
|
|
|
- }
|
|
|
|
|
- sendNetDataDone := make(chan struct{})
|
|
|
|
|
-
|
|
|
|
|
- try.Go(func() {
|
|
|
|
|
- sendNetDataTicker := time.NewTicker(sendNetDataInterval)
|
|
|
|
|
- defer sendNetDataTicker.Stop()
|
|
|
|
|
- for {
|
|
|
|
|
- select {
|
|
|
|
|
- case <-sendNetDataDone:
|
|
|
|
|
- return
|
|
|
|
|
- case _ = <-sendNetDataTicker.C:
|
|
|
|
|
- log.Infoln("before enter sendNetDataFunc")
|
|
|
|
|
- if !cr.IsFusing() {
|
|
|
|
|
- log.Infoln("after enter sendNetDataFunc")
|
|
|
|
|
- sendNetDataFunc()
|
|
|
|
|
|
|
+ try.Go(func() {
|
|
|
|
|
+ sendNetDataTicker := time.NewTicker(sendNetDataInterval)
|
|
|
|
|
+ defer sendNetDataTicker.Stop()
|
|
|
|
|
+ for {
|
|
|
|
|
+ select {
|
|
|
|
|
+ case <-sendNetDataDone:
|
|
|
|
|
+ return
|
|
|
|
|
+ case _ = <-sendNetDataTicker.C:
|
|
|
|
|
+ log.Infoln("before enter sendNetDataFunc")
|
|
|
|
|
+ if !cr.IsFusing() {
|
|
|
|
|
+ log.Infoln("after enter sendNetDataFunc")
|
|
|
|
|
+ sendNetDataFunc()
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
- }, utils.CatchFn)
|
|
|
|
|
|
|
+ }, utils.CatchFn)
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
/* metricsHandler := func(w http.ResponseWriter, r *http.Request) {
|
|
/* metricsHandler := func(w http.ResponseWriter, r *http.Request) {
|
|
|
// 从注册表中获取指标数据
|
|
// 从注册表中获取指标数据
|