Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
thbkrkr committed Apr 19, 2023
1 parent 1e0e39d commit 07b62c0
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 68 deletions.
170 changes: 108 additions & 62 deletions pkg/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,7 @@ import (
)

const (
resourceCount = "resource_count"
podCount = "pod_count"
helmManagedResourceCount = "helm_resource_count"

timestampFieldName = "timestamp"
stackMonitoringLogsCount = "stack_monitoring_logs_count"
stackMonitoringMetricsCount = "stack_monitoring_metrics_count"
serviceCount = "service_count"
pipelinesCount = "pipelines_count"
pipelinesRefCount = "pipelines_ref_count"
timestampFieldName = "timestamp"
)

type ECKTelemetry struct {
Expand All @@ -60,6 +51,19 @@ type ECK struct {
License map[string]string `json:"license"`
}

type baseStats struct {
ResourceCount int32 `json:"resource_count"`
PodCount int32 `json:"pod_count"`
}

type helmStats struct {
HelmManagedResourceCount int32 `json:"helm_resource_count"`
}
type stackMonStats struct {
StackMonitoringLogsCount int32 `json:"stack_monitoring_logs_count"`
StackMonitoringMetricsCount int32 `json:"stack_monitoring_metrics_count"`
}

type getStatsFn func(k8s.Client, []string) (string, interface{}, error)

func NewReporter(
Expand Down Expand Up @@ -232,13 +236,11 @@ type downwardNodeLabelsStats struct {

func esStats(k8sClient k8s.Client, managedNamespaces []string) (string, interface{}, error) {
stats := struct {
ResourceCount int32 `json:"resource_count"`
HelmManagedResourceCount int32 `json:"helm_resource_count"`
PodCount int32 `json:"pod_count"`
AutoscaledResourceCount int32 `json:"autoscaled_resource_count"`
StackMonitoringLogsCount int32 `json:"stack_monitoring_logs_count"`
StackMonitoringMetricsCount int32 `json:"stack_monitoring_metrics_count"`
DownwardNodeLabels *downwardNodeLabelsStats `json:"downward_node_labels,omitempty"`
baseStats
helmStats
stackMonStats
AutoscaledResourceCount int32 `json:"autoscaled_resource_count"`
DownwardNodeLabels *downwardNodeLabelsStats `json:"downward_node_labels,omitempty"`
}{}
distinctNodeLabels := set.Make()
var resourcesWithDownwardLabels int32
Expand All @@ -250,25 +252,26 @@ func esStats(k8sClient k8s.Client, managedNamespaces []string) (string, interfac

for _, es := range esList.Items {
es := es

stats.ResourceCount++
stats.PodCount += es.Status.AvailableNodes

if isManagedByHelm(es.Labels) {
stats.HelmManagedResourceCount++
}
if monitoring.IsLogsDefined(&es) {
stats.StackMonitoringLogsCount++
}
if monitoring.IsMetricsDefined(&es) {
stats.StackMonitoringMetricsCount++
}
if es.IsAutoscalingAnnotationSet() {
stats.AutoscaledResourceCount++
}
if es.HasDownwardNodeLabels() {
resourcesWithDownwardLabels++
distinctNodeLabels.MergeWith(set.Make(es.DownwardNodeLabels()...))
}
if monitoring.IsLogsDefined(&es) {
stats.StackMonitoringLogsCount++
}
if monitoring.IsMetricsDefined(&es) {
stats.StackMonitoringMetricsCount++
}
}
}
if resourcesWithDownwardLabels > 0 {
Expand Down Expand Up @@ -298,7 +301,11 @@ func isManagedByHelm(labels map[string]string) bool {
}

func kbStats(k8sClient k8s.Client, managedNamespaces []string) (string, interface{}, error) {
stats := map[string]int32{resourceCount: 0, podCount: 0, helmManagedResourceCount: 0}
stats := struct {
baseStats
helmStats
stackMonStats
}{}

var kbList kbv1.KibanaList
for _, ns := range managedNamespaces {
Expand All @@ -307,19 +314,30 @@ func kbStats(k8sClient k8s.Client, managedNamespaces []string) (string, interfac
}

for _, kb := range kbList.Items {
stats[resourceCount]++
stats[podCount] += kb.Status.AvailableNodes
kb := kb

stats.ResourceCount++
stats.PodCount += kb.Status.AvailableNodes

if isManagedByHelm(kb.Labels) {
stats[helmManagedResourceCount]++
stats.HelmManagedResourceCount++
}
if monitoring.IsLogsDefined(&kb) {
stats.StackMonitoringLogsCount++
}
if monitoring.IsMetricsDefined(&kb) {
stats.StackMonitoringMetricsCount++
}
}
}
return "kibanas", stats, nil
}

func apmStats(k8sClient k8s.Client, managedNamespaces []string) (string, interface{}, error) {
stats := map[string]int32{resourceCount: 0, podCount: 0}
stats := struct {
baseStats
//helmStats
}{}

var apmList apmv1.ApmServerList
for _, ns := range managedNamespaces {
Expand All @@ -328,19 +346,29 @@ func apmStats(k8sClient k8s.Client, managedNamespaces []string) (string, interfa
}

for _, apm := range apmList.Items {
stats[resourceCount]++
stats[podCount] += apm.Status.AvailableNodes
stats.ResourceCount++
stats.PodCount += apm.Status.AvailableNodes

/*if isManagedByHelm(apm.Labels) {
stats.HelmManagedResourceCount++
}*/
}
}
return "apms", stats, nil
}

func beatStats(k8sClient k8s.Client, managedNamespaces []string) (string, interface{}, error) {
typeToName := func(typ string) string { return fmt.Sprintf("%s_count", typ) }

stats := map[string]int32{resourceCount: 0, podCount: 0}
stats := struct {
baseStats
//helmStats
PerType map[string]int32 `json:"per_type"`
}{}
stats.PerType = map[string]int32{}

typeToName := func(typ string) string { return fmt.Sprintf("%s_count", typ) }
for typ := range beatv1beta1.KnownTypes {
stats[typeToName(typ)] = 0
stats.PerType[typeToName(typ)] = 0
}

var beatList beatv1beta1.BeatList
Expand All @@ -350,17 +378,19 @@ func beatStats(k8sClient k8s.Client, managedNamespaces []string) (string, interf
}

for _, beat := range beatList.Items {
stats[resourceCount]++
stats[typeToName(beat.Spec.Type)]++
stats[podCount] += beat.Status.AvailableNodes
beat := beat

stats.ResourceCount++
stats.PodCount += beat.Status.AvailableNodes

stats.PerType[typeToName(beat.Spec.Type)]++
}
}

return "beats", stats, nil
}

func entStats(k8sClient k8s.Client, managedNamespaces []string) (string, interface{}, error) {
stats := map[string]int32{resourceCount: 0, podCount: 0}
stats := baseStats{}

var entList entv1.EnterpriseSearchList
for _, ns := range managedNamespaces {
Expand All @@ -369,45 +399,56 @@ func entStats(k8sClient k8s.Client, managedNamespaces []string) (string, interfa
}

for _, ent := range entList.Items {
stats[resourceCount]++
stats[podCount] += ent.Status.AvailableNodes
stats.ResourceCount++
stats.PodCount += ent.Status.AvailableNodes
}
}
return "enterprisesearches", stats, nil
}

func agentStats(k8sClient k8s.Client, managedNamespaces []string) (string, interface{}, error) {
multipleRefsKey := "multiple_refs"
fleetModeKey := "fleet_mode"
fleetServerKey := "fleet_server"
stats := map[string]int32{resourceCount: 0, podCount: 0, multipleRefsKey: 0}

stats := struct {
baseStats
//helmStats
MultipleRefsKey int32 `json:"multiple_refs"`
FleetModeKey int32 `json:"fleet_mode"`
FleetServerKey int32 `json:"fleet_server"`
}{}
var agentList agentv1alpha1.AgentList
for _, ns := range managedNamespaces {
if err := k8sClient.List(context.Background(), &agentList, client.InNamespace(ns)); err != nil {
return "", nil, err
}

for _, agent := range agentList.Items {
stats[resourceCount]++
stats[podCount] += agent.Status.AvailableNodes
agent := agent

stats.ResourceCount++
stats.PodCount += agent.Status.AvailableNodes

if len(agent.Spec.ElasticsearchRefs) > 1 {
stats[multipleRefsKey]++
stats.MultipleRefsKey++
}
if agent.Spec.FleetModeEnabled() {
stats[fleetModeKey]++
stats.FleetModeKey++
}
if agent.Spec.FleetServerEnabled {
stats[fleetServerKey]++
stats.FleetServerKey++
}
}
}
return "agents", stats, nil
}

func logstashStats(k8sClient k8s.Client, managedNamespaces []string) (string, interface{}, error) {
stats := map[string]int32{resourceCount: 0, podCount: 0, stackMonitoringLogsCount: 0,
stackMonitoringMetricsCount: 0, serviceCount: 0, pipelinesCount: 0, pipelinesRefCount: 0}
stats := struct {
baseStats
//helmStats
stackMonStats
ServiceCount int32 `json:"service_count"`
PipelinesCount int32 `json:"pipelines_count"`
PipelinesRefCount int32 `json:"pipelines_ref_count"`
}{}

var logstashList logstashv1alpha1.LogstashList
for _, ns := range managedNamespaces {
Expand All @@ -417,26 +458,29 @@ func logstashStats(k8sClient k8s.Client, managedNamespaces []string) (string, in

for _, ls := range logstashList.Items {
ls := ls
stats[resourceCount]++
stats[serviceCount] += int32(len(ls.Spec.Services))
stats[podCount] += ls.Status.AvailableNodes
stats[pipelinesCount] += int32(len(ls.Spec.Pipelines))

stats.ResourceCount++
stats.PodCount += ls.Status.AvailableNodes

stats.ServiceCount += int32(len(ls.Spec.Services))
stats.PipelinesCount += int32(len(ls.Spec.Pipelines))

if ls.Spec.PipelinesRef != nil {
stats[pipelinesRefCount] ++
stats.PipelinesRefCount++
}
if monitoring.IsLogsDefined(&ls) {
stats[stackMonitoringLogsCount]++
stats.StackMonitoringLogsCount++
}
if monitoring.IsMetricsDefined(&ls) {
stats[stackMonitoringMetricsCount]++
stats.StackMonitoringMetricsCount++
}
}
}
return "logstashes", stats, nil
}

func mapsStats(k8sClient k8s.Client, managedNamespaces []string) (string, interface{}, error) {
stats := map[string]int32{resourceCount: 0, podCount: 0}
stats := baseStats{}

var mapsList mapsv1alpha1.ElasticMapsServerList
for _, ns := range managedNamespaces {
Expand All @@ -445,8 +489,10 @@ func mapsStats(k8sClient k8s.Client, managedNamespaces []string) (string, interf
}

for _, maps := range mapsList.Items {
stats[resourceCount]++
stats[podCount] += maps.Status.AvailableNodes
maps := maps

stats.ResourceCount++
stats.PodCount += maps.Status.AvailableNodes
}
}
return "maps", stats, nil
Expand Down
15 changes: 9 additions & 6 deletions pkg/telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,13 @@ func TestNewReporter(t *testing.T) {
pod_count: 2
resource_count: 1
beats:
auditbeat_count: 0
filebeat_count: 1
heartbeat_count: 0
journalbeat_count: 0
metricbeat_count: 1
packetbeat_count: 0
per_type:
auditbeat_count: 0
filebeat_count: 1
heartbeat_count: 0
journalbeat_count: 0
metricbeat_count: 1
packetbeat_count: 0
pod_count: 8
resource_count: 2
elasticsearches:
Expand All @@ -481,6 +482,8 @@ func TestNewReporter(t *testing.T) {
helm_resource_count: 1
pod_count: 0
resource_count: 3
stack_monitoring_logs_count: 0
stack_monitoring_metrics_count: 0
logstashes:
pipelines_count: 1
pipelines_ref_count: 0
Expand Down

0 comments on commit 07b62c0

Please sign in to comment.