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 6 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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ usage: cf_exporter --cf.api_url=CF.API_URL --cf.deployment-name=CF.DEPLOYMENT-NA

Flags:
-h, --help Show context-sensitive help (also try --help-long and --help-man).
--bbs.api_url=BBS.API_URL BBS API URL ($CF_EXPORTER_BBS_API_URL)
--bbs.ca_file=BBS.CA_FILE BBS CA File ($CF_EXPORTER_BBS_CA_FILE)
--bbs.cert_file=BBS.CERT_FILE
BBS Cert File ($CF_EXPORTER_BBS_CERT_FILE)
--bbs.key_file=BBS.KEY_FILE
BBS Key File ($CF_EXPORTER_BBS_KEY_FILE)
--bbs.skip_ssl_verify Disable SSL Verify for BBS ($CF_EXPORTER_BBS_SKIP_SSL_VERIFY)
--bbs.timeout=5 BBS API Timeout ($CF_EXPORTER_BBS_TIMEOUT)
--cf.api_url=CF.API_URL Cloud Foundry API URL ($CF_EXPORTER_CF_API_URL)
--cf.username=CF.USERNAME Cloud Foundry Username ($CF_EXPORTER_CF_USERNAME)
--cf.password=CF.PASSWORD Cloud Foundry Password ($CF_EXPORTER_CF_PASSWORD)
Expand Down
83 changes: 56 additions & 27 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,31 +242,14 @@ 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
}

// 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))
stackGUID := ""
for _, stack := range objs.Stacks {
if stack.Name == application.Lifecycle.Data.Stack {
stackGUID = stack.GUID
break
}
}
detectedBuildpack, buildpack := c.collectAppBuildpacks(application, objs)

c.applicationInfoMetric.WithLabelValues(
application.GUID,
Expand All @@ -276,7 +260,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 +274,22 @@ 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++
}
}
}
} else if len(objs.AppSummaries) > 0 {
if appSummary, ok := objs.AppSummaries[application.GUID]; ok {
runningInstances = appSummary.RunningInstances
}
}
c.applicationInstancesRunningMetric.WithLabelValues(
application.GUID,
application.Name,
Expand All @@ -298,7 +298,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 All @@ -320,6 +320,35 @@ func (c ApplicationsCollector) reportApp(application models.Application, objs *m
return nil
}

func (c ApplicationsCollector) collectAppBuildpacks(application models.Application, objs *models.CFObjects) (detectedBuildpack string, buildpack string) {
detectedBuildpack = ""
buildpack = ""
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))
}
}
}
return detectedBuildpack, buildpack
}

// reportApplicationsMetrics
// 1. continue processing application list upon error
func (c ApplicationsCollector) reportApplicationsMetrics(objs *models.CFObjects, ch chan<- prometheus.Metric) error {
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
77 changes: 77 additions & 0 deletions fetcher/bbs_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package fetcher

import (
"fmt"
"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)
if err != nil {
return nil, err
}
if bbsClient.client.Ping(bbsClient.logger, trace.GenerateTraceID()) {
return &bbsClient, nil
}
return nil, fmt.Errorf("failed to ping BBS")
}

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

return actualLRPs, err
}

func (b *BBSClient) TestConnection() error {
traceID := trace.GenerateTraceID()
if b.client.Ping(b.logger, traceID) {
return nil
}
return fmt.Errorf("failed to ping BBS")
}
29 changes: 22 additions & 7 deletions fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ type CFConfig struct {

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

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,
filters: filter,
worker: NewWorker(threads, filter),
}
}

Expand All @@ -65,6 +69,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 +87,30 @@ 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, filters.ActualLRPs)
}

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
}

var bbs *BBSClient
if c.bbsConfig.URL != "" {
bbs, err = NewBBSClient(c.bbsConfig)
if err != nil {
log.WithError(err).Error("unable to initialize bbs client")
c.filters.Disable([]string{filters.ActualLRPs})
}
}

c.workInit()

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