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 all 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
12 changes: 8 additions & 4 deletions config/crds/v1/bases/logstash.k8s.elastic.co_logstashes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1944,7 +1944,8 @@ spec:
defined in spec.resourceClaims, that are used
by this container. \n This is an alpha field and
requires enabling the DynamicResourceAllocation
feature gate. \n This field is immutable."
feature gate. \n This field is immutable. It can
only be set for containers."
items:
description: ResourceClaim references one entry
in PodSpec.ResourceClaims.
Expand Down Expand Up @@ -3314,7 +3315,8 @@ spec:
defined in spec.resourceClaims, that are used
by this container. \n This is an alpha field and
requires enabling the DynamicResourceAllocation
feature gate. \n This field is immutable."
feature gate. \n This field is immutable. It can
only be set for containers."
items:
description: ResourceClaim references one entry
in PodSpec.ResourceClaims.
Expand Down Expand Up @@ -4717,7 +4719,8 @@ spec:
defined in spec.resourceClaims, that are used
by this container. \n This is an alpha field and
requires enabling the DynamicResourceAllocation
feature gate. \n This field is immutable."
feature gate. \n This field is immutable. It can
only be set for containers."
items:
description: ResourceClaim references one entry
in PodSpec.ResourceClaims.
Expand Down Expand Up @@ -6523,7 +6526,8 @@ spec:
that are used by this container. \n
This is an alpha field and requires
enabling the DynamicResourceAllocation
feature gate. \n This field is immutable."
feature gate. \n This field is immutable.
It can only be set for containers."
items:
description: ResourceClaim references
one entry in PodSpec.ResourceClaims.
Expand Down
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:
pipeline_count: 0
pipeline_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
42 changes: 40 additions & 2 deletions 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 @@ -40,8 +41,7 @@ const (
resourceCount = "resource_count"
podCount = "pod_count"
helmManagedResourceCount = "helm_resource_count"

timestampFieldName = "timestamp"
timestampFieldName = "timestamp"
)

type ECKTelemetry struct {
Expand Down Expand Up @@ -123,6 +123,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 +399,43 @@ func agentStats(k8sClient k8s.Client, managedNamespaces []string) (string, inter
return "agents", stats, nil
}

func logstashStats(k8sClient k8s.Client, managedNamespaces []string) (string, interface{}, error) {
const (
pipelineCount = "pipeline_count"
pipelineRefCount = "pipeline_ref_count"
serviceCount = "service_count"
stackMonitoringLogsCount = "stack_monitoring_logs_count"
stackMonitoringMetricsCount = "stack_monitoring_metrics_count"
)
stats := map[string]int32{resourceCount: 0, podCount: 0, stackMonitoringLogsCount: 0,
stackMonitoringMetricsCount: 0, serviceCount: 0, pipelineCount: 0, pipelineRefCount: 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[pipelineCount] += int32(len(ls.Spec.Pipelines))
if ls.Spec.PipelinesRef != nil {
stats[pipelineRefCount]++
}
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:
pipeline_count: 1
pipeline_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