Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Logstash Telemetry #6562

Merged
merged 9 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/telemetry/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ const expectedTelemetryTemplate = `eck:
helm_resource_count: 0
pod_count: 0
resource_count: 1
logstashes:
pipelines_count: 0
pipelines_ref_count: 0
pod_count: 0
resource_count: 0
service_count: 0
stack_monitoring_logs_count: 0
stack_monitoring_metrics_count: 0
maps:
pod_count: 0
resource_count: 0
Expand Down
39 changes: 38 additions & 1 deletion pkg/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
esv1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/elasticsearch/v1"
entv1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/enterprisesearch/v1"
kbv1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/kibana/v1"
logstashv1alpha1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/logstash/v1alpha1"
mapsv1alpha1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/maps/v1alpha1"
policyv1alpha1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/stackconfigpolicy/v1alpha1"
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/common/reconciler"
Expand All @@ -41,7 +42,12 @@ const (
podCount = "pod_count"
helmManagedResourceCount = "helm_resource_count"

timestampFieldName = "timestamp"
timestampFieldName = "timestamp"
stackMonitoringLogsCount = "stack_monitoring_logs_count"
stackMonitoringMetricsCount = "stack_monitoring_metrics_count"
serviceCount = "service_count"
pipelinesCount = "pipelines_count"
pipelinesRefCount = "pipelines_ref_count"
thbkrkr marked this conversation as resolved.
Show resolved Hide resolved
)

type ECKTelemetry struct {
Expand Down Expand Up @@ -123,6 +129,7 @@ func (r *Reporter) getResourceStats(ctx context.Context) (map[string]interface{}
agentStats,
mapsStats,
scpStats,
logstashStats,
} {
key, statsPart, err := f(r.client, r.managedNamespaces)
if err != nil {
Expand Down Expand Up @@ -398,6 +405,36 @@ func agentStats(k8sClient k8s.Client, managedNamespaces []string) (string, inter
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}

var logstashList logstashv1alpha1.LogstashList
for _, ns := range managedNamespaces {
if err := k8sClient.List(context.Background(), &logstashList, client.InNamespace(ns)); err != nil {
return "", nil, err
}

for _, ls := range logstashList.Items {
ls := ls
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one was caught by the linter, and is related to loop variable semantics in go - there is a great summary of the issue (and proposal for "fixing" it in future versions of golang) here. I don't actually think that that behaviour will bite us here, given what we are doing, but I'm still happy keeping this "odd" looking assignment in here and keep the linter satisfied

stats[resourceCount]++
stats[serviceCount] += int32(len(ls.Spec.Services))
stats[podCount] += ls.Status.AvailableNodes
stats[pipelinesCount] += int32(len(ls.Spec.Pipelines))
if ls.Spec.PipelinesRef != nil {
stats[pipelinesRefCount] ++
thbkrkr marked this conversation as resolved.
Show resolved Hide resolved
}
if monitoring.IsLogsDefined(&ls) {
stats[stackMonitoringLogsCount]++
}
if monitoring.IsMetricsDefined(&ls) {
stats[stackMonitoringMetricsCount]++
}
}
}
return "logstashes", stats, nil
}

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

Expand Down
72 changes: 72 additions & 0 deletions pkg/telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
esv1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/elasticsearch/v1"
entv1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/enterprisesearch/v1"
kbv1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/kibana/v1"
logstashv1alpha1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/logstash/v1alpha1"
mapsv1alpha1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/maps/v1alpha1"
policyv1alpha1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/stackconfigpolicy/v1alpha1"
"github.com/elastic/cloud-on-k8s/v2/pkg/controller/kibana"
Expand Down Expand Up @@ -227,6 +228,69 @@ func TestNewReporter(t *testing.T) {
},
},
},
&logstashv1alpha1.Logstash{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns1",
},
Spec: logstashv1alpha1.LogstashSpec{
Count: 3,
Monitoring: commonv1.Monitoring{
Logs: commonv1.LogsMonitoring{ElasticsearchRefs: []commonv1.ObjectSelector{{Name: "monitoring"}}},
Metrics: commonv1.MetricsMonitoring{ElasticsearchRefs: []commonv1.ObjectSelector{{Name: "monitoring"}}},
},
Pipelines: []commonv1.Config{
{Data: map[string]interface{}{"pipeline.id": "main"}},
},
Services: []logstashv1alpha1.LogstashService{
{
Name: "test1",
Service: commonv1.ServiceTemplate{
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{Port: 9200},
},
},
},
},
{
Name: "test2",
Service: commonv1.ServiceTemplate{
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{Port: 9201},
},
},
},
},
},
},
Status: logstashv1alpha1.LogstashStatus{
AvailableNodes: 3,
},
},
&logstashv1alpha1.Logstash{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns2",
},
Spec: logstashv1alpha1.LogstashSpec{
Count: 1,
Services: []logstashv1alpha1.LogstashService{
{
Name: "test1",
Service: commonv1.ServiceTemplate{
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{Port: 9200},
},
},
},
},
},
},
Status: logstashv1alpha1.LogstashStatus{
AvailableNodes: 1,
},
},
&beatv1beta1.Beat{
ObjectMeta: metav1.ObjectMeta{
Name: "beat1",
Expand Down Expand Up @@ -417,6 +481,14 @@ func TestNewReporter(t *testing.T) {
helm_resource_count: 1
pod_count: 0
resource_count: 3
logstashes:
pipelines_count: 1
pipelines_ref_count: 0
pod_count: 4
resource_count: 2
service_count: 3
stack_monitoring_logs_count: 1
stack_monitoring_metrics_count: 1
maps:
pod_count: 1
resource_count: 1
Expand Down