Skip to content

Commit

Permalink
Requesting /v3/apps instead of /v3/droplets
Browse files Browse the repository at this point in the history
  • Loading branch information
adamspd authored and gmllt committed Jun 18, 2024
1 parent 57cd85a commit 7ee99ca
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 72 deletions.
11 changes: 4 additions & 7 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", "detect_output", "buildpack_name", "version"},
[]string{"application_id", "application_name", "buildpack_name"},
)

applicationInstancesMetric := prometheus.NewGaugeVec(
Expand Down Expand Up @@ -259,16 +259,13 @@ func (c ApplicationsCollector) reportApp(application models.Application, objs *m
}

// 3. Use the droplet data for the buildpack metric
droplet, exists := objs.AppDroplets[application.GUID]
apps, exists := objs.Apps[application.GUID]
if exists {
for _, bp := range droplet.Buildpacks {
for _, bp := range apps.Lifecycle.Data.Buildpacks {
c.applicationBuildpackMetric.WithLabelValues(
application.GUID,
application.Name,
bp.Name,
bp.DetectOutput,
bp.BuildpackName,
bp.Version,
bp,
).Set(float64(1))
}
}
Expand Down
2 changes: 1 addition & 1 deletion fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (c *Fetcher) workInit() {
c.worker.PushIf("service_bindings", c.fetchServiceBindings, filters.ServiceBindings)
c.worker.PushIf("users", c.fetchUsers, filters.Events)
c.worker.PushIf("events", c.fetchEvents, filters.Events)
c.worker.PushIf("droplets", c.fetchDroplets, filters.Droplets)
c.worker.PushIf("droplets", c.fetchV3Apps, filters.Applications)
}

func (c *Fetcher) fetch() *models.CFObjects {
Expand Down
30 changes: 5 additions & 25 deletions fetcher/fetcher_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,32 +97,12 @@ func (c *Fetcher) fetchDomains(session *SessionExt, entry *models.CFObjects) err
return err
}

func (c *Fetcher) fetchAndFilterDroplets(session *SessionExt) (map[string]models.Droplet, error) {
droplets, err := session.ListDroplets()
if err != nil {
return nil, err
}

// Create a map to store the first droplet per app
appDropletMap := make(map[string]models.Droplet)
for _, droplet := range droplets {
appGUID := droplet.Relationships.App.Data.GUID
if _, exists := appDropletMap[appGUID]; !exists {
appDropletMap[appGUID] = droplet
}
}

return appDropletMap, nil
}

func (c *Fetcher) fetchDroplets(session *SessionExt, result *models.CFObjects) error {
appDropletMap, err := c.fetchAndFilterDroplets(session)
if err != nil {
log.WithError(err).Error("unable to fetch and filter droplets")
return err
func (c *Fetcher) fetchV3Apps(session *SessionExt, entry *models.CFObjects) error {
v3Apps, err := session.ListV3Apps()
if err == nil {
loadIndex(entry.Apps, v3Apps, func(r models.Application) string { return r.GUID })
}
result.AppDroplets = appDropletMap
return nil
return err
}

func (c *Fetcher) fetchProcesses(session *SessionExt, entry *models.CFObjects) error {
Expand Down
12 changes: 6 additions & 6 deletions fetcher/sessionext.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ func (s SessionExt) GetSpaceSummary(guid string) (*models.SpaceSummary, error) {
return &res, nil
}

func (s SessionExt) ListDroplets() ([]models.Droplet, error) {
func (s SessionExt) ListV3Apps() ([]models.Application, error) {
client := s.Raw()
url := fmt.Sprintf("%s/v3/droplets", s.V3().CloudControllerURL)
var droplets []models.Droplet
url := "/v3/apps"
var apps []models.Application

for {
req, err := client.NewRequest("GET", url, nil)
Expand All @@ -179,15 +179,15 @@ func (s SessionExt) ListDroplets() ([]models.Droplet, error) {
Href string `json:"href"`
} `json:"next"`
} `json:"pagination"`
Resources []models.Droplet `json:"resources"`
Resources []models.Application `json:"resources"`
}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&data)
if err != nil {
return nil, err
}

droplets = append(droplets, data.Resources...)
apps = append(apps, data.Resources...)

if data.Pagination.Next.Href == "" {
break
Expand All @@ -199,7 +199,7 @@ func (s SessionExt) ListDroplets() ([]models.Droplet, error) {
url = nextURL
}

return droplets, nil
return apps, nil
}

func getNextURL(currentURL, nextHref string) (string, error) {
Expand Down
33 changes: 0 additions & 33 deletions models/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type CFObjects struct {
AppProcesses map[string][]resources.Process `json:"app_processes"`
Events map[string]Event `json:"events"`
Users map[string]resources.User `json:"users"`
AppDroplets map[string]Droplet `json:"app_droplets"`
Took float64
Error error
}
Expand Down Expand Up @@ -95,37 +94,6 @@ type Application struct {
UpdatedAt string `json:"updated_at,omitempty"`
}

type Droplet struct {
GUID string `json:"guid"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
State string `json:"state"`
Error string `json:"error"`
Lifecycle Lifecycle `json:"lifecycle"`
Buildpacks []Buildpack `json:"buildpacks"`
Stack string `json:"stack"`
Relationships Relationships `json:"relationships"`
}

type Buildpack struct {
Name string `json:"name"`
DetectOutput string `json:"detect_output"`
BuildpackName string `json:"buildpack_name"`
Version string `json:"version"`
}

type Relationships struct {
App RelationshipData `json:"app"`
}

type RelationshipData struct {
Data GUID `json:"data"`
}

type GUID struct {
GUID string `json:"guid"`
}

type Task struct {
GUID string `json:"guid,omitempty"`
State constant.TaskState `json:"state,omitempty"`
Expand Down Expand Up @@ -208,7 +176,6 @@ func NewCFObjects() *CFObjects {
AppProcesses: map[string][]resources.Process{},
Users: map[string]resources.User{},
Events: map[string]Event{},
AppDroplets: map[string]Droplet{},
Took: 0,
Error: nil,
}
Expand Down

0 comments on commit 7ee99ca

Please sign in to comment.