diff --git a/pkg/telemetry/telemetry.go b/pkg/telemetry/telemetry.go index 329212a57d..2bc3cf6886 100644 --- a/pkg/telemetry/telemetry.go +++ b/pkg/telemetry/telemetry.go @@ -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 { @@ -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( @@ -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 @@ -250,12 +252,19 @@ 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++ } @@ -263,12 +272,6 @@ func esStats(k8sClient k8s.Client, managedNamespaces []string) (string, interfac resourcesWithDownwardLabels++ distinctNodeLabels.MergeWith(set.Make(es.DownwardNodeLabels()...)) } - if monitoring.IsLogsDefined(&es) { - stats.StackMonitoringLogsCount++ - } - if monitoring.IsMetricsDefined(&es) { - stats.StackMonitoringMetricsCount++ - } } } if resourcesWithDownwardLabels > 0 { @@ -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 { @@ -307,11 +314,19 @@ 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++ } } } @@ -319,7 +334,10 @@ func kbStats(k8sClient k8s.Client, managedNamespaces []string) (string, interfac } 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 { @@ -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 @@ -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 { @@ -369,19 +399,21 @@ 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 { @@ -389,16 +421,19 @@ func agentStats(k8sClient k8s.Client, managedNamespaces []string) (string, inter } 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++ } } } @@ -406,8 +441,14 @@ func agentStats(k8sClient k8s.Client, managedNamespaces []string) (string, inter } 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 { @@ -417,18 +458,21 @@ 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++ } } } @@ -436,7 +480,7 @@ func logstashStats(k8sClient k8s.Client, managedNamespaces []string) (string, in } 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 { @@ -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 diff --git a/pkg/telemetry/telemetry_test.go b/pkg/telemetry/telemetry_test.go index 2d1cb56f5b..9927d69cee 100644 --- a/pkg/telemetry/telemetry_test.go +++ b/pkg/telemetry/telemetry_test.go @@ -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: @@ -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