Skip to content

Commit

Permalink
rename prometheus->metrics, enhance qsfs nic metrics collection
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarElawady committed Oct 11, 2021
1 parent e3ade32 commit 42436ec
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 70 deletions.
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1066,10 +1066,6 @@ github.com/threefoldtech/go-rmb v0.1.4-0.20211001084350-a0f5b8f508f5 h1:4t0Lf/DZ
github.com/threefoldtech/go-rmb v0.1.4-0.20211001084350-a0f5b8f508f5/go.mod h1:L0Lg63BUqalwynE9l037UHSBY9+q00nySO/QZRZPVIM=
github.com/threefoldtech/substrate-client v0.0.0-20210930125201-9bd88e80762d h1:dc2KuqZkCIEOxmatevfYb3vVHB8jqqfPMxW3/u1IWCA=
github.com/threefoldtech/substrate-client v0.0.0-20210930125201-9bd88e80762d/go.mod h1:t/85gAaltVbJmUnCVwu5lpNKn0HLn9Z9vu/NFJlJcrk=
github.com/threefoldtech/substrate-client v0.0.0-20211007130322-ece81d9a8d18 h1:gubNcFGsKwC5oxMHrvMUfqCxgS0jyk8xov04BCOgonE=
github.com/threefoldtech/substrate-client v0.0.0-20211007130322-ece81d9a8d18/go.mod h1:t/85gAaltVbJmUnCVwu5lpNKn0HLn9Z9vu/NFJlJcrk=
github.com/threefoldtech/substrate-client v0.0.0-20211007133201-97a478b0363a h1:m/JrjdDySfNO1Muok3A8ofBx7Cm2qgca0tvDHWZ6j8g=
github.com/threefoldtech/substrate-client v0.0.0-20211007133201-97a478b0363a/go.mod h1:t/85gAaltVbJmUnCVwu5lpNKn0HLn9Z9vu/NFJlJcrk=
github.com/threefoldtech/substrate-client v0.0.0-20211007134519-74137b8f68ec h1:dGqu3OQmNJS/ikcGIMPGUBrizO0YeSsXxzNEjjx3Hhg=
github.com/threefoldtech/substrate-client v0.0.0-20211007134519-74137b8f68ec/go.mod h1:t/85gAaltVbJmUnCVwu5lpNKn0HLn9Z9vu/NFJlJcrk=
github.com/threefoldtech/zbus v0.1.5 h1:S9kXbjejoRRnJw1yKHEXFGF2vqL+Drae2n4vpj0pGHo=
Expand Down
6 changes: 3 additions & 3 deletions pkg/gridtypes/zos/qsfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (q QuantumSafeFS) Capacity() (gridtypes.Capacity, error) {
}

type QuatumSafeFSResult struct {
Path string `json:"path"`
PrometheusEndpoint string `json:"prometheus_endpoint"`
PrometheusPort int `json:"prometheus_port"`
Path string `json:"path"`
MetricsEndpoint string `json:"metrics_endpoint"`
MetricsPort int `json:"metrics_port"`
}
4 changes: 2 additions & 2 deletions pkg/primitives/qsfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func (p *Primitives) qsfsProvision(ctx context.Context, wl *gridtypes.WorkloadWi
return nil, errors.Wrap(err, "failed to setup create qsfs mount")
}
result.Path = info.Path
result.PrometheusEndpoint = info.PrometheusEndpoint
result.PrometheusPort = info.PrometheusPort
result.MetricsEndpoint = info.MetricsEndpoint
result.MetricsPort = info.MetricsPort
return result, nil
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/qsfsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ type QSFSMetrics struct {
}

type QSFSInfo struct {
Path string
PrometheusEndpoint string
PrometheusPort int
Path string
MetricsEndpoint string
MetricsPort int
}

func (q *QSFSMetrics) Nu(wlID string) (result uint64) {
Expand Down
66 changes: 13 additions & 53 deletions pkg/qsfsd/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"

"github.com/containernetworking/plugins/pkg/ns"
Expand All @@ -17,6 +12,7 @@ import (
"github.com/threefoldtech/zos/pkg"
"github.com/threefoldtech/zos/pkg/network/namespace"
"github.com/threefoldtech/zos/pkg/stubs"
"github.com/vishvananda/netlink"
)

// Metrics gets running qsfs network metrics
Expand All @@ -41,25 +37,7 @@ func (m *QSFS) Metrics() (pkg.QSFSMetrics, error) {
defer netNs.Close()
metrics := pkg.NetMetric{}
err = netNs.Do(func(_ ns.NetNS) error {
dir, err := ioutil.TempDir("/tmp", "qsfs-sysfs")
if err != nil {
return errors.Wrap(err, "coudln't create temp dir")
}
defer func() {
if err := os.RemoveAll(dir); err != nil {
log.Error().Err(err).Msg(fmt.Sprintf("qsfs metrics: couldn't remove: %s", dir))
}
}()
if err := syscall.Mount("newns", dir, "sysfs", 0, ""); err != nil {
return errors.Wrap(err, "couldn't mount sysfs")
}
defer func() {
if err := syscall.Unmount(dir, syscall.MNT_DETACH); err != nil {
log.Error().Err(err).Msg("qsfs metrics: couldn't detach sysfs: %s")
}
}()

metrics, err = metricsForNics(dir, []string{"public", "ygg0"})
metrics, err = metricsForNics([]string{"public", "ygg0"})
return err
})
if err != nil {
Expand All @@ -72,37 +50,19 @@ func (m *QSFS) Metrics() (pkg.QSFSMetrics, error) {
return pkg.QSFSMetrics{Consumption: result}, nil
}

func readFileUint64(p string) (uint64, error) {
bytes, err := ioutil.ReadFile(p)
if err != nil {
// we do skip but may be this is not crre
return 0, err
}

return strconv.ParseUint(strings.TrimSpace(string(bytes)), 10, 64)
}

func metricsForNics(sysfsPath string, nics []string) (m pkg.NetMetric, err error) {
template := filepath.Join(sysfsPath, "class/net/%s/statistics/")
dict := map[string]*uint64{
"rx_bytes": &m.NetRxBytes,
"rx_packets": &m.NetRxPackets,
"tx_bytes": &m.NetTxBytes,
"tx_packets": &m.NetTxPackets,
}
func metricsForNics(nics []string) (pkg.NetMetric, error) {
var m pkg.NetMetric
for _, nic := range nics {
base := fmt.Sprintf(template, nic)
for metric, ptr := range dict {
path := filepath.Join(base, metric)
value, err := readFileUint64(path)
if err != nil {
log.Error().Err(err).Str("path", path).Msg("failed to read statistics")
continue
}

*ptr += value
link, err := netlink.LinkByName(nic)
if err != nil {
return m, errors.Wrapf(err, "couldn't get nic %s info", nic)
}
stats := link.Attrs().Statistics
m.NetRxBytes += stats.RxBytes
m.NetRxPackets += stats.RxPackets
m.NetTxBytes += stats.TxBytes
m.NetTxPackets += stats.TxPackets
}

return
return m, nil
}
10 changes: 5 additions & 5 deletions pkg/qsfsd/qsfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
qsfsRootFsPropagation = pkg.RootFSPropagationSlave
zstorSocket = "/var/run/zstor.sock"
zstorZDBFSMountPoint = "/mnt" // hardcoded in the container
zstorPrometheusPort = 9100
zstorMetricsPort = 9100
zstorZDBDataDirPath = "/data"
)

Expand All @@ -40,7 +40,7 @@ type zstorConfig struct {
zos.QuantumSafeFSConfig
ZDBDataDirPath string `toml:"zdb_data_dir_path"`
Socket string `toml:"socket"`
PrometheusPort uint32 `toml:"prometheus_port"`
MetricsPort uint32 `toml:"prometheus_port"`
ZDBFSMountpoint string `toml:"zdbfs_mountpoint"`
Root string `toml:"root"`
}
Expand All @@ -63,7 +63,7 @@ func setQSFSDefaults(cfg *zos.QuantumSafeFS) zstorConfig {
return zstorConfig{
QuantumSafeFSConfig: cfg.Config,
Socket: zstorSocket,
PrometheusPort: zstorPrometheusPort,
MetricsPort: zstorMetricsPort,
ZDBFSMountpoint: zstorZDBFSMountPoint,
ZDBDataDirPath: zstorZDBDataDirPath,
Root: zstorZDBFSMountPoint,
Expand Down Expand Up @@ -135,8 +135,8 @@ func (q *QSFS) Mount(wlID string, cfg zos.QuantumSafeFS) (info pkg.QSFSInfo, err
return
}
info.Path = mountPath
info.PrometheusPort = 9100
info.PrometheusEndpoint, err = q.waitYggIPs(ctx, networkd, netns)
info.MetricsPort = 9100
info.MetricsEndpoint, err = q.waitYggIPs(ctx, networkd, netns)

return
}
Expand Down

0 comments on commit 42436ec

Please sign in to comment.