From 2ccfe8aafc60734c897cdfd9ea89b9e42324f144 Mon Sep 17 00:00:00 2001 From: Gilles Miraillet Date: Fri, 4 Aug 2023 10:44:10 +0200 Subject: [PATCH 1/5] fix wrongly formatted code using gofmt --- collectors/tasks.go | 14 +++++++------- fetcher/fetcher_test.go | 4 ++-- fetcher/sessionext_test.go | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/collectors/tasks.go b/collectors/tasks.go index 95e55539..4849a7c1 100644 --- a/collectors/tasks.go +++ b/collectors/tasks.go @@ -3,14 +3,14 @@ package collectors import ( "time" - "github.com/prometheus/client_golang/prometheus" "github.com/bosh-prometheus/cf_exporter/models" + "github.com/prometheus/client_golang/prometheus" ) type TasksCollector struct { - namespace string - environment string - deployment string + namespace string + environment string + deployment string taskInfoMetric *prometheus.GaugeVec tasksCountMetric *prometheus.GaugeVec tasksMemoryMbSumMetric *prometheus.GaugeVec @@ -134,9 +134,9 @@ func NewTasksCollector( ) return &TasksCollector{ - namespace: namespace, - environment: environment, - deployment: deployment, + namespace: namespace, + environment: environment, + deployment: deployment, taskInfoMetric: taskInfoMetric, tasksCountMetric: tasksCountMetric, tasksMemoryMbSumMetric: tasksMemoryMbSumMetric, diff --git a/fetcher/fetcher_test.go b/fetcher/fetcher_test.go index 57d8ddb8..da871aaa 100644 --- a/fetcher/fetcher_test.go +++ b/fetcher/fetcher_test.go @@ -123,8 +123,8 @@ var _ = Describe("Fetcher", func() { When("tasks filter is set", func() { BeforeEach(func() { - active = []string{ filters.Tasks } - expected = []string{ "info", "tasks" } + active = []string{filters.Tasks} + expected = []string{"info", "tasks"} }) It("plans only specific jobs", func() { Ω(jobs).Should(ConsistOf(expected)) diff --git a/fetcher/sessionext_test.go b/fetcher/sessionext_test.go index 4dbf43d7..548fc558 100644 --- a/fetcher/sessionext_test.go +++ b/fetcher/sessionext_test.go @@ -121,11 +121,11 @@ var _ = Describe("Extensions", func() { ghttp.VerifyRequest("GET", "/v3/tasks", "per_page=5000&states=PENDING,RUNNING,CANCELING"), ghttp.RespondWith(http.StatusOK, serializeList( models.Task{ - GUID: "guid1", + GUID: "guid1", State: constant.TaskPending, }, models.Task{ - GUID: "guid2", + GUID: "guid2", State: constant.TaskCanceling, }, )), From 47b3eefd6fd9883cc009d25c0b2889f08c165be5 Mon Sep 17 00:00:00 2001 From: Gilles Miraillet Date: Fri, 4 Aug 2023 10:45:35 +0200 Subject: [PATCH 2/5] removes unecessary returned result from (TasksCollector).resportTasksMetrics method --- collectors/tasks.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/collectors/tasks.go b/collectors/tasks.go index 4849a7c1..5ddfe473 100644 --- a/collectors/tasks.go +++ b/collectors/tasks.go @@ -186,7 +186,7 @@ func (c TasksCollector) Describe(ch chan<- *prometheus.Desc) { c.lastTasksScrapeDurationSecondsMetric.Describe(ch) } -func (c TasksCollector) reportTasksMetrics(objs *models.CFObjects, ch chan<- prometheus.Metric) error { +func (c TasksCollector) reportTasksMetrics(objs *models.CFObjects, ch chan<- prometheus.Metric) { c.taskInfoMetric.Reset() c.tasksCountMetric.Reset() c.tasksMemoryMbSumMetric.Reset() @@ -258,6 +258,4 @@ func (c TasksCollector) reportTasksMetrics(objs *models.CFObjects, ch chan<- pro c.tasksMemoryMbSumMetric.Collect(ch) c.tasksDiskQuotaMbSumMetric.Collect(ch) c.tasksOldestCreatedAtMetric.Collect(ch) - - return nil } From cb49e23d0914729f9c1871d4034295a7fceb70ad Mon Sep 17 00:00:00 2001 From: Gilles Miraillet Date: Fri, 4 Aug 2023 10:46:37 +0200 Subject: [PATCH 3/5] removes unecessary type convertion --- collectors/tasks.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/collectors/tasks.go b/collectors/tasks.go index 5ddfe473..2a9b1ef6 100644 --- a/collectors/tasks.go +++ b/collectors/tasks.go @@ -225,7 +225,7 @@ func (c TasksCollector) reportTasksMetrics(objs *models.CFObjects, ch chan<- pro memorySum := int64(0) for _, task := range tasks { - memorySum += int64(task.MemoryInMb) + memorySum += task.MemoryInMb } c.tasksMemoryMbSumMetric.WithLabelValues( key.application_id, @@ -234,7 +234,7 @@ func (c TasksCollector) reportTasksMetrics(objs *models.CFObjects, ch chan<- pro diskSum := int64(0) for _, task := range tasks { - diskSum += int64(task.DiskInMb) + diskSum += task.DiskInMb } c.tasksDiskQuotaMbSumMetric.WithLabelValues( key.application_id, From a67eddce91aeba3ecdc0ee392c15b87fcf86cd2b Mon Sep 17 00:00:00 2001 From: Gilles Miraillet Date: Fri, 4 Aug 2023 10:49:16 +0200 Subject: [PATCH 4/5] removes unecessary reference usage in a loop --- collectors/tasks.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/collectors/tasks.go b/collectors/tasks.go index 2a9b1ef6..53849b92 100644 --- a/collectors/tasks.go +++ b/collectors/tasks.go @@ -197,7 +197,7 @@ func (c TasksCollector) reportTasksMetrics(objs *models.CFObjects, ch chan<- pro application_id string state string } - groupedTasks := map[keyType][]*models.Task{} + groupedTasks := map[keyType][]models.Task{} for _, task := range objs.Tasks { application_id := "unavailable" if app, ok := task.Relationships["app"]; ok && app.GUID != "" { @@ -207,9 +207,9 @@ func (c TasksCollector) reportTasksMetrics(objs *models.CFObjects, ch chan<- pro existingValue, ok := groupedTasks[key] if !ok { - existingValue = []*models.Task{} + existingValue = []models.Task{} } - groupedTasks[key] = append(existingValue, &task) + groupedTasks[key] = append(existingValue, task) } for key, tasks := range groupedTasks { From d54326b66dadf46299496063cb6d1bbea5642535 Mon Sep 17 00:00:00 2001 From: Gilles Miraillet Date: Fri, 4 Aug 2023 10:50:54 +0200 Subject: [PATCH 5/5] renames some variables that was usint underscores in their name --- collectors/applications.go | 10 +++++----- collectors/service_bindings.go | 2 +- collectors/tasks.go | 30 +++++++++++++++--------------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/collectors/applications.go b/collectors/applications.go index 18165860..c60c78dc 100644 --- a/collectors/applications.go +++ b/collectors/applications.go @@ -39,7 +39,7 @@ func NewApplicationsCollector( Help: "Labeled Cloud Foundry Application information with a constant '1' value.", ConstLabels: prometheus.Labels{"environment": environment, "deployment": deployment}, }, - []string{"application_id", "application_name", "detected_buildpack", "buildpack", "organization_id", "organization_name", "space_id", "space_name", "stack_id", "state"}, + []string{"applicationID", "application_name", "detected_buildpack", "buildpack", "organization_id", "organization_name", "space_id", "space_name", "stack_id", "state"}, ) applicationInstancesMetric := prometheus.NewGaugeVec( @@ -50,7 +50,7 @@ func NewApplicationsCollector( Help: "Number of desired Cloud Foundry Application Instances.", ConstLabels: prometheus.Labels{"environment": environment, "deployment": deployment}, }, - []string{"application_id", "application_name", "organization_id", "organization_name", "space_id", "space_name", "state"}, + []string{"applicationID", "application_name", "organization_id", "organization_name", "space_id", "space_name", "state"}, ) applicationInstancesRunningMetric := prometheus.NewGaugeVec( @@ -61,7 +61,7 @@ func NewApplicationsCollector( Help: "Number of running Cloud Foundry Application Instances.", ConstLabels: prometheus.Labels{"environment": environment, "deployment": deployment}, }, - []string{"application_id", "application_name", "organization_id", "organization_name", "space_id", "space_name", "state"}, + []string{"applicationID", "application_name", "organization_id", "organization_name", "space_id", "space_name", "state"}, ) applicationMemoryMbMetric := prometheus.NewGaugeVec( @@ -72,7 +72,7 @@ func NewApplicationsCollector( Help: "Cloud Foundry Application Memory (Mb).", ConstLabels: prometheus.Labels{"environment": environment, "deployment": deployment}, }, - []string{"application_id", "application_name", "organization_id", "organization_name", "space_id", "space_name"}, + []string{"applicationID", "application_name", "organization_id", "organization_name", "space_id", "space_name"}, ) applicationDiskQuotaMbMetric := prometheus.NewGaugeVec( @@ -83,7 +83,7 @@ func NewApplicationsCollector( Help: "Cloud Foundry Application Disk Quota (Mb).", ConstLabels: prometheus.Labels{"environment": environment, "deployment": deployment}, }, - []string{"application_id", "application_name", "organization_id", "organization_name", "space_id", "space_name"}, + []string{"applicationID", "application_name", "organization_id", "organization_name", "space_id", "space_name"}, ) applicationsScrapesTotalMetric := prometheus.NewCounter( diff --git a/collectors/service_bindings.go b/collectors/service_bindings.go index 7cff5076..8935a8c5 100644 --- a/collectors/service_bindings.go +++ b/collectors/service_bindings.go @@ -32,7 +32,7 @@ func NewServiceBindingsCollector( Help: "Labeled Cloud Foundry Service Binding information with a constant '1' value.", ConstLabels: prometheus.Labels{"environment": environment, "deployment": deployment}, }, - []string{"service_binding_id", "application_id", "service_instance_id"}, + []string{"service_binding_id", "applicationID", "service_instance_id"}, ) serviceBindingsScrapesTotalMetric := prometheus.NewCounter( diff --git a/collectors/tasks.go b/collectors/tasks.go index 53849b92..84072492 100644 --- a/collectors/tasks.go +++ b/collectors/tasks.go @@ -36,7 +36,7 @@ func NewTasksCollector( Help: "Labeled Cloud Foundry Task information with a constant '1' value.", ConstLabels: prometheus.Labels{"environment": environment, "deployment": deployment}, }, - []string{"application_id", "state"}, + []string{"applicationID", "state"}, ) tasksCountMetric := prometheus.NewGaugeVec( @@ -47,7 +47,7 @@ func NewTasksCollector( Help: "Number of Cloud Foundry Tasks.", ConstLabels: prometheus.Labels{"environment": environment, "deployment": deployment}, }, - []string{"application_id", "state"}, + []string{"applicationID", "state"}, ) tasksMemoryMbSumMetric := prometheus.NewGaugeVec( @@ -58,7 +58,7 @@ func NewTasksCollector( Help: "Sum of Cloud Foundry Tasks Memory (Mb).", ConstLabels: prometheus.Labels{"environment": environment, "deployment": deployment}, }, - []string{"application_id", "state"}, + []string{"applicationID", "state"}, ) tasksDiskQuotaMbSumMetric := prometheus.NewGaugeVec( @@ -69,7 +69,7 @@ func NewTasksCollector( Help: "Sum of Cloud Foundry Tasks Disk Quota (Mb).", ConstLabels: prometheus.Labels{"environment": environment, "deployment": deployment}, }, - []string{"application_id", "state"}, + []string{"applicationID", "state"}, ) tasksOldestCreatedAtMetric := prometheus.NewGaugeVec( @@ -80,7 +80,7 @@ func NewTasksCollector( Help: "Number of seconds since 1970 of creation time of oldest Cloud Foundry task.", ConstLabels: prometheus.Labels{"environment": environment, "deployment": deployment}, }, - []string{"application_id", "state"}, + []string{"applicationID", "state"}, ) tasksScrapesTotalMetric := prometheus.NewCounter( @@ -194,16 +194,16 @@ func (c TasksCollector) reportTasksMetrics(objs *models.CFObjects, ch chan<- pro c.tasksOldestCreatedAtMetric.Reset() type keyType struct { - application_id string - state string + applicationID string + state string } groupedTasks := map[keyType][]models.Task{} for _, task := range objs.Tasks { - application_id := "unavailable" + applicationID := "unavailable" if app, ok := task.Relationships["app"]; ok && app.GUID != "" { - application_id = app.GUID + applicationID = app.GUID } - key := keyType{application_id, string(task.State)} + key := keyType{applicationID, string(task.State)} existingValue, ok := groupedTasks[key] if !ok { @@ -214,12 +214,12 @@ func (c TasksCollector) reportTasksMetrics(objs *models.CFObjects, ch chan<- pro for key, tasks := range groupedTasks { c.taskInfoMetric.WithLabelValues( - key.application_id, + key.applicationID, key.state, ).Set(float64(1)) c.tasksCountMetric.WithLabelValues( - key.application_id, + key.applicationID, key.state, ).Set(float64(len(tasks))) @@ -228,7 +228,7 @@ func (c TasksCollector) reportTasksMetrics(objs *models.CFObjects, ch chan<- pro memorySum += task.MemoryInMb } c.tasksMemoryMbSumMetric.WithLabelValues( - key.application_id, + key.applicationID, key.state, ).Set(float64(memorySum)) @@ -237,7 +237,7 @@ func (c TasksCollector) reportTasksMetrics(objs *models.CFObjects, ch chan<- pro diskSum += task.DiskInMb } c.tasksDiskQuotaMbSumMetric.WithLabelValues( - key.application_id, + key.applicationID, key.state, ).Set(float64(diskSum)) @@ -248,7 +248,7 @@ func (c TasksCollector) reportTasksMetrics(objs *models.CFObjects, ch chan<- pro } } c.tasksOldestCreatedAtMetric.WithLabelValues( - key.application_id, + key.applicationID, key.state, ).Set(float64(createdAtOldest.Unix())) }