Skip to content

Commit

Permalink
- 基础指标添加node_ip node_name作为宿主机标识标签
Browse files Browse the repository at this point in the history
- ksm指标没有nid的默认上报到服务节点server_side_nid ,例如kube_node_status_allocatable_cpu_cores这种共享指标
- import package fmt
  • Loading branch information
ning1875 committed Jan 20, 2021
1 parent 5bedead commit 07fb3c0
Show file tree
Hide file tree
Showing 18 changed files with 119 additions and 86 deletions.
1 change: 1 addition & 0 deletions collect/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package collect

import (
"github.com/go-kit/kit/log"

"github.com/n9e/k8s-mon/config"
)

Expand Down
6 changes: 3 additions & 3 deletions collect/cadvisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package collect
import (
"encoding/json"
"fmt"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/spf13/viper"
"net"
"net/http"
"path"
Expand All @@ -15,8 +12,11 @@ import (
"time"

"github.com/docker/docker/api/types"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/google/cadvisor/client"
v1 "github.com/google/cadvisor/info/v1"
"github.com/spf13/viper"
"github.com/toolkits/pkg/file"
"github.com/toolkits/pkg/logger"

Expand Down
59 changes: 54 additions & 5 deletions collect/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,67 @@ import (
"context"
"crypto/md5"
"fmt"
"github.com/didi/nightingale/src/common/dataobj"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/n9e/k8s-mon/config"
config_util "github.com/prometheus/common/config"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"time"

"github.com/didi/nightingale/src/common/dataobj"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
config_util "github.com/prometheus/common/config"

"github.com/n9e/k8s-mon/config"
)

func GetHostName(logger log.Logger) string {
name, err := os.Hostname()
if err != nil {
level.Error(logger).Log("msg", "GetHostNameError", "err", err)
}
return name

}

func GetPortListenAddr(port int64) (portListenAddr string, err error) {
addrs, err := net.InterfaceAddrs()

if err != nil {
return "", err
}
for _, address := range addrs {

ipnet, ok := address.(*net.IPNet)
if !ok {
continue
}

addr := ipnet.IP.To4()
if addr == nil {
continue
}
// 检查ip地址判断是否回环地址
if ipnet.IP.IsLoopback() {
continue
}
adds := addr.String()
addrAndPort := fmt.Sprintf("%s:%d", adds, port)
conn, err := net.DialTimeout("tcp", addrAndPort, time.Second*1)

if err != nil {
continue
}
conn.Close()
portListenAddr = adds
break

}
return
}

func CommonCollectTickerForWithDataM(cg *config.Config, ctx context.Context, logger log.Logger, dataMap *HistoryMap, collectFunc func(*config.Config, log.Logger, *HistoryMap, string), funcName string) error {
ticker := time.NewTicker(time.Second * (time.Duration(cg.Step)))
level.Info(logger).Log("msg", "CommonCollectTickerForWithDataM start....", "funcName", funcName)
Expand Down
3 changes: 0 additions & 3 deletions collect/const.go

This file was deleted.

1 change: 1 addition & 0 deletions collect/coredns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package collect

import (
"github.com/go-kit/kit/log"

"github.com/n9e/k8s-mon/config"
)

Expand Down
6 changes: 4 additions & 2 deletions collect/get_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package collect

import (
"context"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
kconfig "github.com/n9e/k8s-mon/config"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"time"

kconfig "github.com/n9e/k8s-mon/config"
)

func GetServerAddrByGetNode(logger log.Logger, dataMap *HistoryMap) {
Expand Down
6 changes: 4 additions & 2 deletions collect/get_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package collect

import (
"context"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
kconfig "github.com/n9e/k8s-mon/config"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"time"

kconfig "github.com/n9e/k8s-mon/config"
)

func GetServerAddrAll(logger log.Logger, dataMap *HistoryMap) {
Expand Down
1 change: 1 addition & 0 deletions collect/kube_controller_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package collect

import (
"github.com/go-kit/kit/log"

"github.com/n9e/k8s-mon/config"
)

Expand Down
4 changes: 3 additions & 1 deletion collect/kube_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package collect

import (
"fmt"
"time"

"github.com/didi/nightingale/src/common/dataobj"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"

"github.com/n9e/k8s-mon/config"
"time"
)

func DoKubeProxyOnNodeCollect(cg *config.Config, logger log.Logger, dataMap *HistoryMap, funcName string) {
Expand Down
1 change: 1 addition & 0 deletions collect/kube_scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package collect

import (
"github.com/go-kit/kit/log"

"github.com/n9e/k8s-mon/config"
)

Expand Down
10 changes: 8 additions & 2 deletions collect/kube_state_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package collect

import (
"fmt"
"strings"
"time"

"github.com/didi/nightingale/src/common/dataobj"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"

"github.com/n9e/k8s-mon/config"
"strings"
"time"
)

func DoKubeStatsMetricsCollect(cg *config.Config, logger log.Logger, funcName string) {
Expand Down Expand Up @@ -160,6 +162,10 @@ func DoKubeStatsMetricsCollect(cg *config.Config, logger log.Logger, funcName st
if nid, loaded := nidM[kk]; loaded {
metric.Nid = nid
}
if metric.Nid == "" {
metric.Nid = cg.ServerSideNid
}

if _, loaded := tagWhiteM[k]; !loaded {
delete(metric.TagsMap, k)
}
Expand Down
61 changes: 11 additions & 50 deletions collect/kubelet_cadvisor.go
Original file line number Diff line number Diff line change
@@ -1,66 +1,20 @@
package collect

import (
"fmt"
"strings"
"time"

"github.com/didi/nightingale/src/common/dataobj"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"

"github.com/n9e/k8s-mon/config"
"net"
"strings"
"time"
)

func GetPortListenAddr(port int64) (portListenAddr string, err error) {
addrs, err := net.InterfaceAddrs()

if err != nil {
return "", err
}
for _, address := range addrs {

ipnet, ok := address.(*net.IPNet)
if !ok {
continue
}

addr := ipnet.IP.To4()
if addr == nil {
continue
}
// 检查ip地址判断是否回环地址
if ipnet.IP.IsLoopback() {
continue
}
adds := addr.String()
addrAndPort := fmt.Sprintf("%s:%d", adds, port)
conn, err := net.DialTimeout("tcp", addrAndPort, time.Second*1)

if err != nil {
continue
}
conn.Close()
portListenAddr = adds
break

}
return
}

func DoKubeletCollect(cg *config.Config, logger log.Logger, dataMap *HistoryMap, funcName string) {
// 通过kubelet prometheus 接口拿到数据后做ETL
// 根据docker inspect 接口拿到所有容器的数据,根据podName一致找到pause 容器的label 给对应的pod数据
start := time.Now()
//kubeletAddr, err := GetPortListenAddr(cg.KubeletC.Port)
//
//if kubeletAddr == "" {
// level.Warn(logger).Log("msg", "getPortListenAddrEmptyKubeletAddr", "err:", err, "port", cg.KubeletC.Port)
//
//} else {
//
// cg.KubeletC.Addr = fmt.Sprintf("%s://%s:%d/%s", cg.KubeletC.Scheme, kubeletAddr, cg.KubeletC.Port, cg.KubeletC.MetricsPath)
// level.Info(logger).Log("msg", "getPortListenAddrForKubeletAddr", "port", cg.KubeletC.Port, "ipaddr", kubeletAddr, "kubeletPath", cg.KubeletC.Addr)
//}
if cg.KubeletC.Addr == "" && len(cg.KubeletC.UserSpecifyAddrs) > 0 {
cg.KubeletC.Addr = cg.KubeletC.UserSpecifyAddrs[0]
}
Expand Down Expand Up @@ -207,6 +161,13 @@ func DoKubeletCollect(cg *config.Config, logger log.Logger, dataMap *HistoryMap,
delete(metric.TagsMap, k)
}
}
// 添加个node_ip label
if cg.KubeletC.HostIp != "" {
metric.TagsMap["node_ip"] = cg.KubeletC.HostIp
}
if cg.KubeletC.HostName != "" {
metric.TagsMap["node_name"] = cg.KubeletC.HostName
}

// tags string
metric.Tags = makeAppendTags(metric.TagsMap, cg.AppendTags)
Expand Down
4 changes: 3 additions & 1 deletion collect/kubelet_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package collect

import (
"fmt"
"time"

"github.com/didi/nightingale/src/common/dataobj"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"

"github.com/n9e/k8s-mon/config"
"time"
)

func DoKubeletNodeOnNodeCollect(cg *config.Config, logger log.Logger, dataMap *HistoryMap, funcName string) {
Expand Down
3 changes: 2 additions & 1 deletion collect/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package collect

import (
"encoding/json"
"github.com/prometheus/common/promlog"
"io/ioutil"
"log"
"os"
"testing"

"github.com/prometheus/common/promlog"
)

func TestParseInfValue(t *testing.T) {
Expand Down
5 changes: 2 additions & 3 deletions collect/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ import (
"bytes"
"errors"
"fmt"
"github.com/go-kit/kit/log"
"math"
"strconv"
"strings"
"time"

"github.com/didi/nightingale/src/common/dataobj"
"github.com/go-kit/kit/log"
fmodel "github.com/open-falcon/falcon-plus/common/model"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"

fmodel "github.com/open-falcon/falcon-plus/common/model"
)

func valueConv(valueUntyped interface{}) (error, float64) {
Expand Down
7 changes: 4 additions & 3 deletions collect/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package collect
import (
"bytes"
"encoding/json"
"github.com/didi/nightingale/src/common/dataobj"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"io/ioutil"
"net/http"
"time"

"github.com/didi/nightingale/src/common/dataobj"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
)

func PushWork(url string, tt int64, metricList []dataobj.MetricValue, logger log.Logger, funcName string) {
Expand Down
7 changes: 5 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package config
import (
"errors"
"fmt"
"io/ioutil"
"net/url"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
config_util "github.com/prometheus/common/config"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
"io/ioutil"
"net/url"
)

type Config struct {
Expand All @@ -34,6 +35,8 @@ type Config struct {
}

type CommonApiServerConfig struct {
HostName string `yaml:"-"`
HostIp string `yaml:"-"`
HashModNum uint64 `yaml:"hash_mod_num"`
HashModShard uint64 `yaml:"hash_mod_shard"`
ConcurrencyLimit int64 `yaml:"concurrency_limit"`
Expand Down
Loading

0 comments on commit 07fb3c0

Please sign in to comment.