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

Draft: Adds possibility to call BBS to retrieve stats for application's processes states. #129

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
72 changes: 46 additions & 26 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 @@ -224,6 +224,7 @@ func (c ApplicationsCollector) reportApp(application models.Application, objs *m
process = cProc
}
}

spaceRel, ok := application.Relationships[constant.RelationshipTypeSpace]
if !ok {
return fmt.Errorf("could not find space relation in application '%s'", application.GUID)
Expand All @@ -241,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 != "" {
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 @@ -276,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 @@ -290,6 +298,18 @@ func (c ApplicationsCollector) reportApp(application models.Application, objs *m
string(application.State),
).Set(float64(process.Instances.Value))

// Use bbs data if available
runningInstances := 0
if len(objs.ProcessActualLRPs) > 0 {
LRPs, ok := objs.ProcessActualLRPs[process.GUID]
if ok {
for _, lrp := range LRPs {
if lrp.State == "RUNNING" {
runningInstances++
}
}
}
}
c.applicationInstancesRunningMetric.WithLabelValues(
application.GUID,
application.Name,
Expand All @@ -298,7 +318,7 @@ func (c ApplicationsCollector) reportApp(application models.Application, objs *m
space.GUID,
space.Name,
string(application.State),
).Set(float64(appSum.RunningInstances))
).Set(float64(runningInstances))

c.applicationMemoryMbMetric.WithLabelValues(
application.GUID,
Expand Down
12 changes: 8 additions & 4 deletions collectors/collectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ type ObjectCollector interface {

type Collector struct {
workers int
config *fetcher.CFConfig
cfConfig *fetcher.CFConfig
bbsConfig *fetcher.BBSConfig
filter *filters.Filter
collectors []ObjectCollector
}
Expand All @@ -24,12 +25,14 @@ func NewCollector(
environment string,
deployment string,
workers int,
config *fetcher.CFConfig,
cfConfig *fetcher.CFConfig,
bbsConfig *fetcher.BBSConfig,
filter *filters.Filter,
) (*Collector, error) {
res := &Collector{
workers: workers,
config: config,
cfConfig: cfConfig,
bbsConfig: bbsConfig,
filter: filter,
collectors: []ObjectCollector{},
}
Expand Down Expand Up @@ -118,8 +121,9 @@ func NewCollector(
}

func (c *Collector) Collect(ch chan<- prometheus.Metric) {
fetcher := fetcher.NewFetcher(c.workers, c.config, c.filter)
fetcher := fetcher.NewFetcher(c.workers, c.cfConfig, c.bbsConfig, c.filter)
objs := fetcher.GetObjects()

for _, collector := range c.collectors {
collector.Collect(objs, ch)
}
Expand Down
62 changes: 62 additions & 0 deletions fetcher/bbs_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package fetcher

import (
"strings"
"time"

"code.cloudfoundry.org/bbs"
"code.cloudfoundry.org/bbs/models"
"code.cloudfoundry.org/bbs/trace"
"code.cloudfoundry.org/lager/v3"
)

const (
clientSessionCacheSize int = -1
maxIdleConnsPerHost int = -1
)

type BBSClient struct {
client bbs.Client
config *BBSConfig
logger lager.Logger
}

type BBSConfig struct {
URL string `yaml:"url"`
Timeout int `yaml:"timeout"`
CAFile string `yaml:"ca_file"`
CertFile string `yaml:"cert_file"`
KeyFile string `yaml:"key_file"`
SkipCertVerify bool `yaml:"skip_cert_verify"`
}

func NewBBSClient(config *BBSConfig) (*BBSClient, error) {
var err error
bbsClient := BBSClient{
config: config,
logger: lager.NewLogger("bbs-client"),
}
bbsClientConfig := bbs.ClientConfig{
URL: config.URL,
Retries: 1,
RequestTimeout: time.Duration(config.Timeout) * time.Second,
}
if strings.HasPrefix(config.URL, "https://") {
bbsClientConfig.IsTLS = true
bbsClientConfig.InsecureSkipVerify = config.SkipCertVerify
bbsClientConfig.CAFile = config.CAFile
bbsClientConfig.CertFile = config.CertFile
bbsClientConfig.KeyFile = config.KeyFile
bbsClientConfig.ClientSessionCacheSize = clientSessionCacheSize
bbsClientConfig.MaxIdleConnsPerHost = maxIdleConnsPerHost
}
bbsClient.client, err = bbs.NewClientWithConfig(bbsClientConfig)
return &bbsClient, err
}

func (b *BBSClient) GetActualLRPs() ([]*models.ActualLRP, error) {
traceID := trace.GenerateTraceID()
actualLRPs, err := b.client.ActualLRPs(b.logger, traceID, models.ActualLRPFilter{})

return actualLRPs, err
}
24 changes: 17 additions & 7 deletions fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ type CFConfig struct {

type Fetcher struct {
sync.Mutex
config *CFConfig
worker *Worker
cfConfig *CFConfig
bbsConfig *BBSConfig
worker *Worker
}

func NewFetcher(threads int, config *CFConfig, filter *filters.Filter) *Fetcher {
func NewFetcher(threads int, config *CFConfig, bbsConfig *BBSConfig, filter *filters.Filter) *Fetcher {
return &Fetcher{
config: config,
worker: NewWorker(threads, filter),
cfConfig: config,
bbsConfig: bbsConfig,
worker: NewWorker(threads, filter),
}
}

Expand All @@ -65,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 All @@ -82,20 +85,27 @@ func (c *Fetcher) workInit() {
c.worker.PushIf("service_route_bindings", c.fetchServiceRouteBindings, filters.ServiceRouteBindings)
c.worker.PushIf("users", c.fetchUsers, filters.Events)
c.worker.PushIf("events", c.fetchEvents, filters.Events)
c.worker.PushIf("actual_lrps", c.fetchActualLRPs)
}

func (c *Fetcher) fetch() *models.CFObjects {
result := models.NewCFObjects()

session, err := NewSessionExt(c.config)
session, err := NewSessionExt(c.cfConfig)
if err != nil {
log.WithError(err).Error("unable to initialize cloud foundry clients")
result.Error = err
return result
}
bbs, err := NewBBSClient(c.bbsConfig)
if err != nil {
log.WithError(err).Error("unable to initialize bbs client")
result.Error = err
return result
}

c.workInit()

result.Error = c.worker.Do(session, result)
result.Error = c.worker.Do(session, bbs, result)
return result
}
Loading