Skip to content

Commit

Permalink
Fix merge conflicts
Browse files Browse the repository at this point in the history
This PR adds support for Exporter telemetry data; decouples NIC internal data from imported Exporter data
  • Loading branch information
jjngx committed Mar 11, 2024
1 parent 61154df commit 23ab904
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 234 deletions.
5 changes: 3 additions & 2 deletions internal/configs/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1479,8 +1479,9 @@ func (cnf *Configurator) GetIngressCounts() map[string]int {

// GetVirtualServerCounts returns the total count of
// VirtualServer and VirtualServerRoute resources that are handled by the Ingress Controller
func (cnf *Configurator) GetVirtualServerCounts() (vsCount int, vsrCount int) {
vsCount = len(cnf.virtualServers)
func (cnf *Configurator) GetVirtualServerCounts() (int, int) {
vsCount := len(cnf.virtualServers)
vsrCount := 0
for _, vs := range cnf.virtualServers {
vsrCount += len(vs.VirtualServerRoutes)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/telemetry/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (

// NodeCount returns the total number of nodes in the cluster.
// It returns an error if the underlying k8s API client errors.
func (c *Collector) NodeCount(ctx context.Context) (int64, error) {
func (c *Collector) NodeCount(ctx context.Context) (int, error) {
nodes, err := c.Config.K8sClientReader.CoreV1().Nodes().List(ctx, metaV1.ListOptions{})
if err != nil {
return 0, err
}
return int64(len(nodes.Items)), nil
return len(nodes.Items), nil
}

// ClusterID returns the UID of the kube-system namespace representing cluster id.
Expand Down
4 changes: 2 additions & 2 deletions internal/telemetry/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestNodeCountInAClusterWithThreeNodes(t *testing.T) {
if err != nil {
t.Fatal(err)
}
var want int64 = 3
want := 3
if want != got {
t.Errorf("want %v, got %v", want, got)
}
Expand All @@ -33,7 +33,7 @@ func TestNodeCountInAClusterWithOneNode(t *testing.T) {
if err != nil {
t.Fatal(err)
}
var want int64 = 1
want := 1
if want != got {
t.Errorf("want %v, got %v", want, got)
}
Expand Down
86 changes: 64 additions & 22 deletions internal/telemetry/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"runtime"
"time"

telemetry "github.com/nginxinc/telemetry-exporter/pkg/telemetry"
tel "github.com/nginxinc/telemetry-exporter/pkg/telemetry"

"github.com/nginxinc/kubernetes-ingress/internal/configs"

Expand Down Expand Up @@ -85,50 +85,92 @@ func (c *Collector) Start(ctx context.Context) {
// It exports data using provided exporter.
func (c *Collector) Collect(ctx context.Context) {
glog.V(3).Info("Collecting telemetry data")
data, err := c.BuildReport(ctx)
report, err := c.BuildReport(ctx)
if err != nil {
glog.Errorf("Error collecting telemetry data: %v", err)
}
err = c.Exporter.Export(ctx, data)
if err != nil {
glog.Errorf("Error exporting telemetry data: %v", err)
}
glog.V(3).Infof("Exported telemetry data: %+v", data)
}

// BuildReport takes context and builds report from gathered telemetry data.
func (c *Collector) BuildReport(ctx context.Context) (telemetry.Exportable, error) {
d := Data{
Data: telemetry.Data{
ProjectName: "NIC",
nicData := Data{
tel.Data{
ProjectName: report.Name,
ProjectVersion: c.Config.Version,
ProjectArchitecture: runtime.GOARCH,
ClusterID: report.ClusterID,
ClusterVersion: report.ClusterVersion,
ClusterPlatform: report.ClusterPlatform,
ClusterNodeCount: int64(report.ClusterNodeCount),
},
NICResourceCounts{
VirtualServers: int64(report.VirtualServers),
VirtualServerRoutes: int64(report.VirtualServerRoutes),
TransportServers: int64(report.TransportServers),
},
}

err = c.Exporter.Export(ctx, &nicData)
if err != nil {
glog.Errorf("Error exporting telemetry data: %v", err)
}
glog.V(3).Infof("Exported telemetry data: %+v", nicData)
}

// Report holds collected NIC telemetry data. It is the package internal
// data structure used for decoupling types between the NIC `telemetry`
// package and the imported `telemetry` exporter.
type Report struct {
Name string
Version string
Architecture string
ClusterID string
ClusterVersion string
ClusterPlatform string
ClusterNodeCount int
VirtualServers int
VirtualServerRoutes int
TransportServers int
}

var err error
// BuildReport takes context, collects telemetry data and builds the report.
func (c *Collector) BuildReport(ctx context.Context) (Report, error) {
vsCount := 0
vsrCount := 0
tsCount := 0

if c.Config.Configurator != nil {
vsCount, vsrCount := c.Config.Configurator.GetVirtualServerCounts()
d.VirtualServers, d.VirtualServerRoutes = int64(vsCount), int64(vsrCount)
d.TransportServers = int64(c.Config.Configurator.GetTransportServerCounts())
vsCount, vsrCount = c.Config.Configurator.GetVirtualServerCounts()
tsCount = c.Config.Configurator.GetTransportServerCounts()
}

if d.ClusterID, err = c.ClusterID(ctx); err != nil {
clusterID, err := c.ClusterID(ctx)
if err != nil {
glog.Errorf("Error collecting telemetry data: ClusterID: %v", err)
}

if d.ClusterNodeCount, err = c.NodeCount(ctx); err != nil {
nodes, err := c.NodeCount(ctx)
if err != nil {
glog.Errorf("Error collecting telemetry data: Nodes: %v", err)
}

if d.ClusterVersion, err = c.ClusterVersion(); err != nil {
version, err := c.ClusterVersion()
if err != nil {
glog.Errorf("Error collecting telemetry data: K8s Version: %v", err)
}

if d.Platform, err = c.Platform(ctx); err != nil {
platform, err := c.Platform(ctx)
if err != nil {
glog.Errorf("Error collecting telemetry data: Platform: %v", err)
}

return &d, err
return Report{
Name: "NIC",
Version: c.Config.Version,
Architecture: runtime.GOARCH,
ClusterID: clusterID,
ClusterVersion: version,
ClusterPlatform: platform,
ClusterNodeCount: nodes,
VirtualServers: vsCount,
VirtualServerRoutes: vsrCount,
TransportServers: tsCount,
}, err
}
Loading

0 comments on commit 23ab904

Please sign in to comment.