Skip to content

Commit

Permalink
remove usage of /v2/spaces/:space_guid/summary
Browse files Browse the repository at this point in the history
  • Loading branch information
gmllt committed Jan 8, 2025
1 parent 81350cd commit aea97bd
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 69 deletions.
66 changes: 36 additions & 30 deletions collectors/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewApplicationsCollector(
Help: "Buildpack used by an Application.",
ConstLabels: prometheus.Labels{"environment": environment, "deployment": deployment},
},
[]string{"application_id", "application_name", "buildpack_name"},
[]string{"application_id", "application_name", "buildpack_name", "detected_buildpack"},
)

applicationInstancesMetric := prometheus.NewGaugeVec(
Expand Down Expand Up @@ -242,30 +242,37 @@ func (c ApplicationsCollector) reportApp(application models.Application, objs *m
return fmt.Errorf("could not find org with guid '%s'", orgRel.GUID)
}

appSum, ok := objs.AppSummaries[application.GUID]
if !ok {
return fmt.Errorf("could not find app summary with guid '%s'", application.GUID)
}

// 1.
detectedBuildpack := appSum.DetectedBuildpack
if len(detectedBuildpack) == 0 {
detectedBuildpack = appSum.Buildpack
}

// 2.
buildpack := appSum.Buildpack
if len(buildpack) == 0 {
buildpack = appSum.DetectedBuildpack
detectedBuildpack := ""
buildpack := ""
stackGUID := ""
for _, stack := range objs.Stacks {
if stack.Name == application.Lifecycle.Data.Stack {
stackGUID = stack.GUID
break
}
}

// 3. Use the droplet data for the buildpack metric
for _, bp := range application.Lifecycle.Data.Buildpacks {
c.applicationBuildpackMetric.WithLabelValues(
application.GUID,
application.Name,
bp,
).Set(float64(1))
if dropletGUID := application.Relationships[constant.RelationshipTypeCurrentDroplet].GUID; dropletGUID != "" {

Check failure on line 254 in collectors/applications.go

View workflow job for this annotation

GitHub Actions / linter

undefined: constant.RelationshipTypeCurrentDroplet (typecheck)

Check failure on line 254 in collectors/applications.go

View workflow job for this annotation

GitHub Actions / linter

undefined: constant.RelationshipTypeCurrentDroplet) (typecheck)

Check failure on line 254 in collectors/applications.go

View workflow job for this annotation

GitHub Actions / tests

undefined: constant.RelationshipTypeCurrentDroplet
if droplet, ok := objs.Droplets[dropletGUID]; ok {
// 1.
detectedBuildpack = droplet.Buildpacks[0].DetectOutput
// 2.
buildpack = droplet.Buildpacks[0].BuildpackName
if len(detectedBuildpack) == 0 {
detectedBuildpack = buildpack
}
if len(buildpack) == 0 {
buildpack = detectedBuildpack
}
// 3.Use the droplet data for the buildpack metric
for _, bp := range droplet.Buildpacks {
c.applicationBuildpackMetric.WithLabelValues(
application.GUID,
application.Name,
bp.BuildpackName,
bp.DetectOutput,
).Set(float64(1))
}
}
}

c.applicationInfoMetric.WithLabelValues(
Expand All @@ -277,7 +284,7 @@ func (c ApplicationsCollector) reportApp(application models.Application, objs *m
organization.Name,
space.GUID,
space.Name,
appSum.StackID,
stackGUID,
string(application.State),
).Set(float64(1))

Expand All @@ -291,15 +298,14 @@ func (c ApplicationsCollector) reportApp(application models.Application, objs *m
string(application.State),
).Set(float64(process.Instances.Value))

runningInstances := appSum.RunningInstances
// Use bbs data if available
runningInstances := 0
if len(objs.ProcessActualLRPs) > 0 {
runningsInstances := 0
lrps, ok := objs.ProcessActualLRPs[process.GUID]
LRPs, ok := objs.ProcessActualLRPs[process.GUID]
if ok {
for _, lrp := range lrps {
for _, lrp := range LRPs {
if lrp.State == "RUNNING" {
runningsInstances++
runningInstances++
}
}
}
Expand Down
1 change: 1 addition & 0 deletions fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (c *Fetcher) workInit() {
c.worker.PushIf("spaces", c.fetchSpaces, filters.Applications, filters.Spaces)
c.worker.PushIf("space_quotas", c.fetchSpaceQuotas, filters.Spaces)
c.worker.PushIf("applications", c.fetchApplications, filters.Applications)
c.worker.PushIf("droplets", c.fetchDroplets, filters.Droplets)
c.worker.PushIf("domains", c.fetchDomains, filters.Domains)
c.worker.PushIf("process", c.fetchProcesses, filters.Applications)
c.worker.PushIf("routes", c.fetchRoutes, filters.Routes)
Expand Down
39 changes: 11 additions & 28 deletions fetcher/fetcher_handlers.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package fetcher

import (
"fmt"
"regexp"
"time"

models2 "code.cloudfoundry.org/bbs/models"

"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
"code.cloudfoundry.org/cli/resources"
"github.com/cloudfoundry/cf_exporter/filters"
"github.com/cloudfoundry/cf_exporter/models"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -74,33 +72,10 @@ func (c *Fetcher) fetchOrgQuotas(session *SessionExt, _ *BBSClient, entry *model
// summary fetching attempt. See cloudfoundry/cf_exporter#85
func (c *Fetcher) fetchSpaces(session *SessionExt, _ *BBSClient, entry *models.CFObjects) error {
spaces, _, _, err := session.V3().GetSpaces(LargeQuery)
if err != nil {
return err
}

loadIndex(entry.Spaces, spaces, func(r resources.Space) string { return r.GUID })
total := len(spaces)
for idx := 0; idx < total; idx++ {
space := spaces[idx]
name := fmt.Sprintf("space_summaries %04d/%04d (%s)", idx, total, space.GUID)
c.worker.PushIf(name, func(session *SessionExt, bbs *BBSClient, entry *models.CFObjects) error {
spaceSum, err := session.GetSpaceSummary(space.GUID)
if err == nil {
c.Lock()
entry.SpaceSummaries[spaceSum.GUID] = *spaceSum
for _, app := range spaceSum.Apps {
entry.AppSummaries[app.GUID] = app
}
c.Unlock()
} else {
log.WithError(err).Warnf("could not fetch space '%s' summary", space.GUID)
}
// 1
return nil
}, filters.Applications)
if err == nil {
loadIndex(entry.Spaces, spaces, func(r resources.Space) string { return r.GUID })
}

return nil
return err
}

func (c *Fetcher) fetchSpaceQuotas(session *SessionExt, _ *BBSClient, entry *models.CFObjects) error {
Expand Down Expand Up @@ -169,6 +144,14 @@ func (c *Fetcher) fetchSecurityGroups(session *SessionExt, _ *BBSClient, entry *
return err
}

func (c *Fetcher) fetchDroplets(session *SessionExt, _ *BBSClient, entry *models.CFObjects) error {
droplets, _, err := session.V3().GetDroplets(LargeQuery)
if err == nil {
loadIndex(entry.Droplets, droplets, func(r resources.Droplet) string { return r.GUID })
}
return err
}

func (c *Fetcher) fetchStacks(session *SessionExt, _ *BBSClient, entry *models.CFObjects) error {
stacks, _, err := session.V3().GetStacks(LargeQuery)
if err == nil {
Expand Down
7 changes: 4 additions & 3 deletions filters/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

const (
Applications = "applications"
Droplets = "droplets"
Buildpacks = "buildpacks"
Domains = "domains"
Events = "events"
Expand All @@ -22,12 +23,12 @@ const (
Spaces = "spaces"
Stacks = "stacks"
Tasks = "tasks"
InstancesRunning = "instances_running"
)

var (
All = []string{
Applications,
Droplets,
Buildpacks,
Domains,
Events,
Expand All @@ -54,6 +55,7 @@ func NewFilter(active ...string) (*Filter, error) {
filter := &Filter{
activated: map[string]bool{
Applications: true,
Droplets: true,
Buildpacks: true,
Domains: true,
IsolationSegments: true,
Expand All @@ -69,7 +71,6 @@ func NewFilter(active ...string) (*Filter, error) {
Stacks: true,
Tasks: false,
Events: false,
InstancesRunning: false,
},
}

Expand All @@ -86,6 +87,7 @@ func (f *Filter) setActive(active []string) error {
// override default states with all disabled
f.activated = map[string]bool{
Applications: false,
Droplets: false,
Buildpacks: false,
Domains: false,
IsolationSegments: false,
Expand All @@ -101,7 +103,6 @@ func (f *Filter) setActive(active []string) error {
Stacks: false,
Tasks: false,
Events: false,
InstancesRunning: false,
}

// enable only given filters
Expand Down
5 changes: 1 addition & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
log "github.com/sirupsen/logrus"
kingpin "gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/alecthomas/kingpin.v2"
)

var (
Expand Down Expand Up @@ -195,9 +195,6 @@ func main() {
SkipCertVerify: *bbsSkipSSLValidation,
}

log.Infof("cfConfig: %+v", cfConfig)
log.Infof("bbsConfig: %+v", bbsConfig)

active := []string{}
if len(*filterCollectors) != 0 {
active = strings.Split(*filterCollectors, ",")
Expand Down
7 changes: 3 additions & 4 deletions models/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type CFObjects struct {
Spaces map[string]resources.Space `json:"spaces"`
SpaceQuotas map[string]Quota `json:"space_quotas"`
Apps map[string]Application `json:"apps"`
Droplets map[string]resources.Droplet `json:"droplets"`
Processes map[string]resources.Process `json:"process"`
Tasks map[string]Task `json:"tasks"`
Routes map[string]resources.Route `json:"routes"`
Expand All @@ -26,13 +27,12 @@ type CFObjects struct {
SecurityGroups map[string]resources.SecurityGroup `json:"security_groups"`
Stacks map[string]resources.Stack `json:"stacks"`
Buildpacks map[string]resources.Buildpack `json:"buildpacks"`
BuildpacksByName map[string]resources.Buildpack `json:"builpacks_by_name"`
Domains map[string]resources.Domain `json:"domains"`
ServiceBrokers map[string]resources.ServiceBroker `json:"service_brokers"`
ServiceOfferings map[string]resources.ServiceOffering `json:"service_offerings"`
ServicePlans map[string]resources.ServicePlan `json:"service_plans"`
ServiceBindings map[string]resources.ServiceCredentialBinding `json:"service_bindings"`
SpaceSummaries map[string]SpaceSummary `json:"space_summaries"`
AppSummaries map[string]AppSummary `json:"app_summaries"`
AppProcesses map[string][]resources.Process `json:"app_processes"`
ProcessActualLRPs map[string][]*models.ActualLRP `json:"process_actual_lrps"`
Events map[string]Event `json:"events"`
Expand Down Expand Up @@ -159,6 +159,7 @@ func NewCFObjects() *CFObjects {
Spaces: map[string]resources.Space{},
SpaceQuotas: map[string]Quota{},
Apps: map[string]Application{},
Droplets: map[string]resources.Droplet{},
Processes: map[string]resources.Process{},
Tasks: map[string]Task{},
Routes: map[string]resources.Route{},
Expand All @@ -173,8 +174,6 @@ func NewCFObjects() *CFObjects {
ServiceOfferings: map[string]resources.ServiceOffering{},
ServicePlans: map[string]resources.ServicePlan{},
ServiceBindings: map[string]resources.ServiceCredentialBinding{},
SpaceSummaries: map[string]SpaceSummary{},
AppSummaries: map[string]AppSummary{},
AppProcesses: map[string][]resources.Process{},
ProcessActualLRPs: map[string][]*models.ActualLRP{},
Users: map[string]resources.User{},
Expand Down

0 comments on commit aea97bd

Please sign in to comment.